Skip to content

Commit 2cd10bf

Browse files
committed
add click and drag
1 parent 6292c5c commit 2cd10bf

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

tui/src/floating_text.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{float::FloatContent, hint::Shortcut};
99
use linutil_core::Command;
1010

1111
use ratatui::{
12-
crossterm::event::{KeyCode, KeyEvent, MouseEvent, MouseEventKind},
12+
crossterm::event::{KeyCode, KeyEvent, MouseButton, MouseEvent, MouseEventKind},
1313
layout::Rect,
1414
style::{Style, Stylize},
1515
text::Line,
@@ -33,6 +33,8 @@ pub struct FloatingText {
3333
mode_title: String,
3434
wrap_words: bool,
3535
frame_height: usize,
36+
drag_start_y: Option<u16>,
37+
drag_start_scroll: Option<usize>,
3638
}
3739

3840
macro_rules! style {
@@ -141,6 +143,8 @@ impl FloatingText {
141143
h_scroll: 0,
142144
wrap_words,
143145
frame_height: 0,
146+
drag_start_y: None,
147+
drag_start_scroll: None,
144148
}
145149
}
146150

@@ -165,6 +169,8 @@ impl FloatingText {
165169
v_scroll: 0,
166170
wrap_words: false,
167171
frame_height: 0,
172+
drag_start_y: None,
173+
drag_start_scroll: None,
168174
})
169175
}
170176

@@ -206,6 +212,19 @@ impl FloatingText {
206212
};
207213
}
208214
}
215+
216+
fn handle_drag(&mut self, current_y: u16) {
217+
if let (Some(start_y), Some(start_scroll)) = (self.drag_start_y, self.drag_start_scroll) {
218+
let delta = start_y as i32 - current_y as i32;
219+
let new_scroll = start_scroll as i32 + delta;
220+
221+
let max_scroll = self
222+
.wrapped_lines
223+
.len()
224+
.saturating_sub(self.frame_height.saturating_sub(2));
225+
self.v_scroll = new_scroll.clamp(0, max_scroll as i32) as usize;
226+
}
227+
}
209228
}
210229

211230
impl FloatContent for FloatingText {
@@ -285,6 +304,17 @@ impl FloatContent for FloatingText {
285304

286305
fn handle_mouse_event(&mut self, event: &MouseEvent) -> bool {
287306
match event.kind {
307+
MouseEventKind::Down(MouseButton::Left) => {
308+
self.drag_start_y = Some(event.row);
309+
self.drag_start_scroll = Some(self.v_scroll);
310+
}
311+
MouseEventKind::Up(MouseButton::Left) => {
312+
self.drag_start_y = None;
313+
self.drag_start_scroll = None;
314+
}
315+
MouseEventKind::Drag(MouseButton::Left) => {
316+
self.handle_drag(event.row);
317+
}
288318
MouseEventKind::ScrollDown => self.scroll_down(),
289319
MouseEventKind::ScrollUp => self.scroll_up(),
290320
MouseEventKind::ScrollLeft => self.scroll_left(),

0 commit comments

Comments
 (0)