drag with mouse to select bytes

This commit is contained in:
alice pellerin
2026-04-30 12:23:28 -05:00
parent e60e63cb16
commit 7eb8c84c6e
3 changed files with 124 additions and 53 deletions
+101 -43
View File
@@ -19,6 +19,8 @@ pub struct App {
pub should_quit: bool,
pub is_dragging_mouse: bool,
pub logs: Vec<String>,
}
@@ -111,19 +113,23 @@ impl App {
should_quit: false,
is_dragging_mouse: false,
logs: Vec::new(),
}
}
pub fn handle_events(&mut self, terminal: &mut DefaultTerminal) {
self.handle_event(terminal);
pub fn handle_events(&mut self, terminal: &mut DefaultTerminal) -> bool {
let mut should_redraw = self.handle_event(terminal);
while event::poll(Duration::ZERO).unwrap() {
self.handle_event(terminal);
should_redraw |= self.handle_event(terminal);
}
should_redraw
}
pub fn handle_event(&mut self, terminal: &mut DefaultTerminal) {
pub fn handle_event(&mut self, terminal: &mut DefaultTerminal) -> bool {
let event = event::read()
.inspect_err(|error| {
#[cfg(target_os = "macos")] {
@@ -140,20 +146,24 @@ impl App {
})
.unwrap();
// self.logs.push(format!("{event:?}"));
match event {
Event::Resize(_, height) => {
self.window_size.rows = height as usize;
self.buffers[self.current_buffer_index]
.clamp_screen_to_primary_cursor(self.window_size);
true
}
Event::Key(key_event) => self.handle_key(key_event, terminal),
Event::Mouse(mouse_event) => self.handle_mouse(mouse_event),
_ => {}
_ => false
}
}
fn handle_key(&mut self, key_event: KeyEvent, terminal: &mut DefaultTerminal) {
fn handle_key(&mut self, key_event: KeyEvent, terminal: &mut DefaultTerminal) -> bool {
if key_event.modifiers == KeyModifiers::CONTROL &&
key_event.code == KeyCode::Char('c')
{
@@ -163,7 +173,7 @@ impl App {
exit(130);
}
let maybe_app_action = self.buffers[self.current_buffer_index].handle_key(
let (maybe_app_action, should_redraw) = self.buffers[self.current_buffer_index].handle_key(
key_event,
&self.config,
&self.primary_cursor_register,
@@ -182,62 +192,110 @@ impl App {
AppAction::Yank => self.yank(),
}
}
should_redraw || maybe_app_action.is_some()
}
fn handle_mouse(&mut self, mouse_event: MouseEvent) {
let tab_bar_rows = usize::from(self.buffers.len() > 1);
fn handle_mouse(&mut self, mouse_event: MouseEvent) -> bool {
let position = self.mouse_event_position(mouse_event);
let current_buffer = &mut self.buffers[self.current_buffer_index];
match mouse_event.kind {
MouseEventKind::Down(_) => {
let byte_column = match mouse_event.column {
10..=11 => Some(0),
13..=14 => Some(1),
16..=17 => Some(2),
19..=20 => Some(3),
23..=24 => Some(4),
26..=27 => Some(5),
29..=30 => Some(6),
32..=33 => Some(7),
36..=37 => Some(8),
39..=40 => Some(9),
42..=43 => Some(10),
45..=46 => Some(11),
49..=50 => Some(12),
52..=53 => Some(13),
55..=56 => Some(14),
58..=59 => Some(15),
_ => None,
};
if let Some(byte_column) = byte_column &&
mouse_event.row as usize - tab_bar_rows < self.window_size.hex_rows()
{
current_buffer.primary_cursor = Cursor::at(
current_buffer.scroll_position +
(mouse_event.row as usize - tab_bar_rows) * BYTES_PER_LINE +
byte_column
);
if let Some(position) = position {
current_buffer.primary_cursor = Cursor::at(position);
current_buffer.cursors.clear();
current_buffer.clamp_screen_to_primary_cursor(self.window_size);
self.is_dragging_mouse = true;
}
true
},
MouseEventKind::Drag(_) if self.is_dragging_mouse => {
if let Some(position) = position {
current_buffer.primary_cursor.head = position;
current_buffer.clamp_screen_to_primary_cursor(self.window_size);
}
true
},
MouseEventKind::Up(_) if self.is_dragging_mouse => {
if let Some(position) = position {
current_buffer.primary_cursor.head = position;
current_buffer.clamp_screen_to_primary_cursor(self.window_size);
}
self.is_dragging_mouse = false;
true
},
MouseEventKind::ScrollDown => {
for _ in 0..3 {
current_buffer.scroll_down(self.window_size);
}
true
},
MouseEventKind::ScrollUp => {
for _ in 0..3 {
current_buffer.scroll_up(self.window_size);
}
true
},
_ => (),
_ => false,
}
}
fn mouse_event_position(&self, mouse_event: MouseEvent) -> Option<usize> {
let tab_bar_rows = usize::from(self.buffers.len() > 1);
if usize::from(mouse_event.row) - tab_bar_rows >= self.window_size.hex_rows() {
return None;
}
let current_buffer = &self.buffers[self.current_buffer_index];
let byte_column = match mouse_event.column {
10..=11 => Some(0),
13..=14 => Some(1),
16..=17 => Some(2),
19..=20 => Some(3),
23..=24 => Some(4),
26..=27 => Some(5),
29..=30 => Some(6),
32..=33 => Some(7),
36..=37 => Some(8),
39..=40 => Some(9),
42..=43 => Some(10),
45..=46 => Some(11),
49..=50 => Some(12),
52..=53 => Some(13),
55..=56 => Some(14),
58..=59 => Some(15),
_ => None,
};
byte_column.map(|byte_column| {
current_buffer.scroll_position +
(mouse_event.row as usize - tab_bar_rows) * BYTES_PER_LINE +
byte_column
})
// if let Some(byte_column) = byte_column &&
// mouse_event.row as usize - tab_bar_rows < self.window_size.hex_rows()
// {
// Some(
// current_buffer.scroll_position +
// (mouse_event.row as usize - tab_bar_rows) * BYTES_PER_LINE +
// byte_column
// )
// } else {
// None
// }
}
}