add click and drag

This commit is contained in:
nnyyxxxx 2024-11-13 00:11:01 -05:00
parent 6292c5cd4e
commit 2cd10bf4ce
No known key found for this signature in database
GPG Key ID: 6038FFD6589902CB

View File

@ -9,7 +9,7 @@ use crate::{float::FloatContent, hint::Shortcut};
use linutil_core::Command; use linutil_core::Command;
use ratatui::{ use ratatui::{
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind}, crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind},
layout::Rect, layout::Rect,
style::{Style, Stylize}, style::{Style, Stylize},
text::Line, text::Line,
@ -33,6 +33,8 @@ pub struct FloatingText {
mode_title: String, mode_title: String,
wrap_words: bool, wrap_words: bool,
frame_height: usize, frame_height: usize,
drag_start_y: Option<u16>,
drag_start_scroll: Option<usize>,
} }
macro_rules! style { macro_rules! style {
@ -141,6 +143,8 @@ impl FloatingText {
h_scroll: 0, h_scroll: 0,
wrap_words, wrap_words,
frame_height: 0, frame_height: 0,
drag_start_y: None,
drag_start_scroll: None,
} }
} }
@ -165,6 +169,8 @@ impl FloatingText {
v_scroll: 0, v_scroll: 0,
wrap_words: false, wrap_words: false,
frame_height: 0, frame_height: 0,
drag_start_y: None,
drag_start_scroll: None,
}) })
} }
@ -206,6 +212,19 @@ impl FloatingText {
}; };
} }
} }
fn handle_drag(&mut self, current_y: u16) {
if let (Some(start_y), Some(start_scroll)) = (self.drag_start_y, self.drag_start_scroll) {
let delta = start_y as i32 - current_y as i32;
let new_scroll = start_scroll as i32 + delta;
let max_scroll = self
.wrapped_lines
.len()
.saturating_sub(self.frame_height.saturating_sub(2));
self.v_scroll = new_scroll.clamp(0, max_scroll as i32) as usize;
}
}
} }
impl FloatContent for FloatingText { impl FloatContent for FloatingText {
@ -285,6 +304,17 @@ impl FloatContent for FloatingText {
fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool { fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
match event.kind { match event.kind {
MouseEventKind::Down(MouseButton::Left) => {
self.drag_start_y = Some(event.row);
self.drag_start_scroll = Some(self.v_scroll);
}
MouseEventKind::Up(MouseButton::Left) => {
self.drag_start_y = None;
self.drag_start_scroll = None;
}
MouseEventKind::Drag(MouseButton::Left) => {
self.handle_drag(event.row);
}
MouseEventKind::ScrollDown => self.scroll_down(), MouseEventKind::ScrollDown => self.scroll_down(),
MouseEventKind::ScrollUp => self.scroll_up(), MouseEventKind::ScrollUp => self.scroll_up(),
MouseEventKind::ScrollLeft => self.scroll_left(), MouseEventKind::ScrollLeft => self.scroll_left(),