mirror of
https://github.com/helix-editor/helix.git
synced 2024-11-22 17:36:19 +04:00
Refactor handling of mouse events (#2893)
- Simplified match statements by destructuring MouseEvent struct at the top and then matching on event.kind. - Extracted out closures for calculating (1) position and view of mouse click and (2) gutter coordinates and view of mouse click.
This commit is contained in:
parent
23ce5f1837
commit
3108a11d35
@ -1051,22 +1051,33 @@ fn handle_mouse_event(
|
|||||||
cxt: &mut commands::Context,
|
cxt: &mut commands::Context,
|
||||||
) -> EventResult {
|
) -> EventResult {
|
||||||
let config = cxt.editor.config();
|
let config = cxt.editor.config();
|
||||||
match event {
|
let MouseEvent {
|
||||||
MouseEvent {
|
kind,
|
||||||
kind: MouseEventKind::Down(MouseButton::Left),
|
row,
|
||||||
row,
|
column,
|
||||||
column,
|
modifiers,
|
||||||
modifiers,
|
..
|
||||||
..
|
} = event;
|
||||||
} => {
|
|
||||||
|
let pos_and_view = |editor: &Editor, row, column| {
|
||||||
|
editor.tree.views().find_map(|(view, _focus)| {
|
||||||
|
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
|
||||||
|
.map(|pos| (pos, view.id))
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
let gutter_coords_and_view = |editor: &Editor, row, column| {
|
||||||
|
editor.tree.views().find_map(|(view, _focus)| {
|
||||||
|
view.gutter_coords_at_screen_coords(row, column)
|
||||||
|
.map(|coords| (coords, view.id))
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
match kind {
|
||||||
|
MouseEventKind::Down(MouseButton::Left) => {
|
||||||
let editor = &mut cxt.editor;
|
let editor = &mut cxt.editor;
|
||||||
|
|
||||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
|
||||||
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
|
|
||||||
.map(|pos| (pos, view.id))
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some((pos, view_id)) = result {
|
|
||||||
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
||||||
|
|
||||||
if modifiers == crossterm::event::KeyModifiers::ALT {
|
if modifiers == crossterm::event::KeyModifiers::ALT {
|
||||||
@ -1081,12 +1092,7 @@ fn handle_mouse_event(
|
|||||||
return EventResult::Consumed(None);
|
return EventResult::Consumed(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
|
||||||
view.gutter_coords_at_screen_coords(row, column)
|
|
||||||
.map(|coords| (coords, view.id))
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some((coords, view_id)) = result {
|
|
||||||
editor.tree.focus = view_id;
|
editor.tree.focus = view_id;
|
||||||
|
|
||||||
let view = editor.tree.get(view_id);
|
let view = editor.tree.get(view_id);
|
||||||
@ -1107,12 +1113,7 @@ fn handle_mouse_event(
|
|||||||
EventResult::Ignored(None)
|
EventResult::Ignored(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEvent {
|
MouseEventKind::Drag(MouseButton::Left) => {
|
||||||
kind: MouseEventKind::Drag(MouseButton::Left),
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let (view, doc) = current!(cxt.editor);
|
let (view, doc) = current!(cxt.editor);
|
||||||
|
|
||||||
let pos = match view.pos_at_screen_coords(doc, row, column) {
|
let pos = match view.pos_at_screen_coords(doc, row, column) {
|
||||||
@ -1124,15 +1125,11 @@ fn handle_mouse_event(
|
|||||||
let primary = selection.primary_mut();
|
let primary = selection.primary_mut();
|
||||||
*primary = primary.put_cursor(doc.text().slice(..), pos, true);
|
*primary = primary.put_cursor(doc.text().slice(..), pos, true);
|
||||||
doc.set_selection(view.id, selection);
|
doc.set_selection(view.id, selection);
|
||||||
|
|
||||||
EventResult::Consumed(None)
|
EventResult::Consumed(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEvent {
|
MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
|
||||||
kind: MouseEventKind::ScrollUp | MouseEventKind::ScrollDown,
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let current_view = cxt.editor.tree.focus;
|
let current_view = cxt.editor.tree.focus;
|
||||||
|
|
||||||
let direction = match event.kind {
|
let direction = match event.kind {
|
||||||
@ -1141,13 +1138,8 @@ fn handle_mouse_event(
|
|||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
|
match pos_and_view(cxt.editor, row, column) {
|
||||||
view.pos_at_screen_coords(&cxt.editor.documents[&view.doc], row, column)
|
Some((_, view_id)) => cxt.editor.tree.focus = view_id,
|
||||||
.map(|_| view.id)
|
|
||||||
});
|
|
||||||
|
|
||||||
match result {
|
|
||||||
Some(view_id) => cxt.editor.tree.focus = view_id,
|
|
||||||
None => return EventResult::Ignored(None),
|
None => return EventResult::Ignored(None),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1159,10 +1151,7 @@ fn handle_mouse_event(
|
|||||||
EventResult::Consumed(None)
|
EventResult::Consumed(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEvent {
|
MouseEventKind::Up(MouseButton::Left) => {
|
||||||
kind: MouseEventKind::Up(MouseButton::Left),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
if !config.middle_click_paste {
|
if !config.middle_click_paste {
|
||||||
return EventResult::Ignored(None);
|
return EventResult::Ignored(None);
|
||||||
}
|
}
|
||||||
@ -1184,19 +1173,8 @@ fn handle_mouse_event(
|
|||||||
EventResult::Consumed(None)
|
EventResult::Consumed(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEvent {
|
MouseEventKind::Up(MouseButton::Right) => {
|
||||||
kind: MouseEventKind::Up(MouseButton::Right),
|
if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
|
||||||
row,
|
|
||||||
column,
|
|
||||||
modifiers,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
|
|
||||||
view.gutter_coords_at_screen_coords(row, column)
|
|
||||||
.map(|coords| (coords, view.id))
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some((coords, view_id)) = result {
|
|
||||||
cxt.editor.tree.focus = view_id;
|
cxt.editor.tree.focus = view_id;
|
||||||
|
|
||||||
let view = cxt.editor.tree.get(view_id);
|
let view = cxt.editor.tree.get(view_id);
|
||||||
@ -1213,16 +1191,11 @@ fn handle_mouse_event(
|
|||||||
return EventResult::Consumed(None);
|
return EventResult::Consumed(None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EventResult::Ignored(None)
|
EventResult::Ignored(None)
|
||||||
}
|
}
|
||||||
|
|
||||||
MouseEvent {
|
MouseEventKind::Up(MouseButton::Middle) => {
|
||||||
kind: MouseEventKind::Up(MouseButton::Middle),
|
|
||||||
row,
|
|
||||||
column,
|
|
||||||
modifiers,
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
let editor = &mut cxt.editor;
|
let editor = &mut cxt.editor;
|
||||||
if !config.middle_click_paste {
|
if !config.middle_click_paste {
|
||||||
return EventResult::Ignored(None);
|
return EventResult::Ignored(None);
|
||||||
@ -1235,16 +1208,12 @@ fn handle_mouse_event(
|
|||||||
return EventResult::Consumed(None);
|
return EventResult::Consumed(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = editor.tree.views().find_map(|(view, _focus)| {
|
if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
|
||||||
view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
|
|
||||||
.map(|pos| (pos, view.id))
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some((pos, view_id)) = result {
|
|
||||||
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
|
||||||
doc.set_selection(view_id, Selection::point(pos));
|
doc.set_selection(view_id, Selection::point(pos));
|
||||||
editor.tree.focus = view_id;
|
editor.tree.focus = view_id;
|
||||||
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
|
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
|
||||||
|
|
||||||
return EventResult::Consumed(None);
|
return EventResult::Consumed(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user