aboutsummaryrefslogtreecommitdiff
path: root/helix-term/src/ui
diff options
context:
space:
mode:
authorGokul Soumya2022-06-27 16:05:07 +0000
committerGitHub2022-06-27 16:05:07 +0000
commit3108a11d350ab89f827d30664b9c00ca751b6abc (patch)
treeb1716f8ac73a690b2de042580f7527f782f4bbce /helix-term/src/ui
parent23ce5f1837d34649d1c973d3cd55b7a1a49885b2 (diff)
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.
Diffstat (limited to 'helix-term/src/ui')
-rw-r--r--helix-term/src/ui/editor.rs107
1 files changed, 38 insertions, 69 deletions
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index f71f716e..32f70c8b 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -1051,22 +1051,33 @@ impl EditorView {
cxt: &mut commands::Context,
) -> EventResult {
let config = cxt.editor.config();
- match event {
- MouseEvent {
- kind: MouseEventKind::Down(MouseButton::Left),
- row,
- column,
- modifiers,
- ..
- } => {
- let editor = &mut cxt.editor;
+ let MouseEvent {
+ kind,
+ row,
+ column,
+ 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))
+ })
+ };
- let result = editor.tree.views().find_map(|(view, _focus)| {
- view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
- .map(|pos| (pos, view.id))
- });
+ match kind {
+ MouseEventKind::Down(MouseButton::Left) => {
+ let editor = &mut cxt.editor;
- if let Some((pos, view_id)) = result {
+ if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
if modifiers == crossterm::event::KeyModifiers::ALT {
@@ -1081,12 +1092,7 @@ impl EditorView {
return EventResult::Consumed(None);
}
- let result = 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 {
+ if let Some((coords, view_id)) = gutter_coords_and_view(editor, row, column) {
editor.tree.focus = view_id;
let view = editor.tree.get(view_id);
@@ -1107,12 +1113,7 @@ impl EditorView {
EventResult::Ignored(None)
}
- MouseEvent {
- kind: MouseEventKind::Drag(MouseButton::Left),
- row,
- column,
- ..
- } => {
+ MouseEventKind::Drag(MouseButton::Left) => {
let (view, doc) = current!(cxt.editor);
let pos = match view.pos_at_screen_coords(doc, row, column) {
@@ -1124,15 +1125,11 @@ impl EditorView {
let primary = selection.primary_mut();
*primary = primary.put_cursor(doc.text().slice(..), pos, true);
doc.set_selection(view.id, selection);
+
EventResult::Consumed(None)
}
- MouseEvent {
- kind: MouseEventKind::ScrollUp | MouseEventKind::ScrollDown,
- row,
- column,
- ..
- } => {
+ MouseEventKind::ScrollUp | MouseEventKind::ScrollDown => {
let current_view = cxt.editor.tree.focus;
let direction = match event.kind {
@@ -1141,13 +1138,8 @@ impl EditorView {
_ => unreachable!(),
};
- let result = cxt.editor.tree.views().find_map(|(view, _focus)| {
- view.pos_at_screen_coords(&cxt.editor.documents[&view.doc], row, column)
- .map(|_| view.id)
- });
-
- match result {
- Some(view_id) => cxt.editor.tree.focus = view_id,
+ match pos_and_view(cxt.editor, row, column) {
+ Some((_, view_id)) => cxt.editor.tree.focus = view_id,
None => return EventResult::Ignored(None),
}
@@ -1159,10 +1151,7 @@ impl EditorView {
EventResult::Consumed(None)
}
- MouseEvent {
- kind: MouseEventKind::Up(MouseButton::Left),
- ..
- } => {
+ MouseEventKind::Up(MouseButton::Left) => {
if !config.middle_click_paste {
return EventResult::Ignored(None);
}
@@ -1184,19 +1173,8 @@ impl EditorView {
EventResult::Consumed(None)
}
- MouseEvent {
- kind: MouseEventKind::Up(MouseButton::Right),
- 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 {
+ MouseEventKind::Up(MouseButton::Right) => {
+ if let Some((coords, view_id)) = gutter_coords_and_view(cxt.editor, row, column) {
cxt.editor.tree.focus = view_id;
let view = cxt.editor.tree.get(view_id);
@@ -1213,16 +1191,11 @@ impl EditorView {
return EventResult::Consumed(None);
}
}
+
EventResult::Ignored(None)
}
- MouseEvent {
- kind: MouseEventKind::Up(MouseButton::Middle),
- row,
- column,
- modifiers,
- ..
- } => {
+ MouseEventKind::Up(MouseButton::Middle) => {
let editor = &mut cxt.editor;
if !config.middle_click_paste {
return EventResult::Ignored(None);
@@ -1235,16 +1208,12 @@ impl EditorView {
return EventResult::Consumed(None);
}
- let result = editor.tree.views().find_map(|(view, _focus)| {
- view.pos_at_screen_coords(&editor.documents[&view.doc], row, column)
- .map(|pos| (pos, view.id))
- });
-
- if let Some((pos, view_id)) = result {
+ if let Some((pos, view_id)) = pos_and_view(editor, row, column) {
let doc = editor.document_mut(editor.tree.get(view_id).doc).unwrap();
doc.set_selection(view_id, Selection::point(pos));
editor.tree.focus = view_id;
commands::MappableCommand::paste_primary_clipboard_before.execute(cxt);
+
return EventResult::Consumed(None);
}