Visual Studio Code, rebuilt in Rust
Rust
//! mouseHandler.ts presses on the glyph margin and line numbers (`startedOnLineNumbers`): viewController.ts
//! `dispatchMouse` selects whole lines (`lineSelect`, `lineSelectDrag`), or with the multi-cursor modifier adds a
//! cursor on the line (`createCursor` with `wholeLine`) or selects the line with the last one (`lastCursorLineSelect`).
use gpui::{Context, Div, InteractiveElement, MouseButton, MouseDownEvent, MouseMoveEvent};
use super::Workbench;
use super::editor_view::MouseDown;
use super::model_events::Cursor;
use crate::editor::{Position, Reveal, Selection};
impl Workbench {
/// The gutter's mouse listeners for the row of `line`.
pub(super) fn gutter_mouse(&self, gutter: Div, line: usize, cx: &mut Context<Self>) -> Div {
gutter
.on_mouse_down(MouseButton::Left, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| {
window.focus(&workbench.editor_focus, cx);
workbench.selecting_text = Some(MouseDown { count: event.click_count, on_gutter: true });
workbench.gutter_line_select(line, event.modifiers, event.modifiers.shift, true, cx);
}))
.on_mouse_down(MouseButton::Right, cx.listener(move |workbench, event: &MouseDownEvent, window, cx| workbench.show_line_number_context_menu(line, event.position, true, window, cx)))
.on_mouse_move(cx.listener(move |workbench, event: &MouseMoveEvent, _, cx| {
if event.pressed_button == Some(MouseButton::Left) && workbench.drag_scrolling.is_none() {
workbench.drag_select(line, 0., event.modifiers, cx);
}
}))
}
/// `dispatchMouse` with `startedOnLineNumbers`, `in_selection_mode` being Shift on the press or a drag.
pub(super) fn gutter_line_select(&mut self, line: usize, modifiers: gpui::Modifiers, in_selection_mode: bool, reveal: bool, cx: &mut Context<Self>) {
let multi_cursor = match self.multi_cursor_modifier_is_cmd() {
true => modifiers.platform,
false => modifiers.alt,
};
let editor = self.editors.active_mut().expect("the gutter is clickable only with an editor open");
let before = editor.selections.clone();
let position = Position::new(line, 0);
let shown = match (multi_cursor, in_selection_mode) {
(true, true) => {
editor.last_cursor_line_select(position, false);
Reveal::Nothing
}
(true, false) => {
let end = if line + 1 < editor.document().lines.len() { Position::new(line + 1, 0) } else { crate::editor::cursor::line_end(&editor.document().lines, position) };
let mut selections = editor.selections.clone();
selections.push(Selection { anchor: position, head: end });
editor.set_selections(selections);
Reveal::All
}
(false, in_selection_mode) => {
editor.line_select(position, in_selection_mode);
Reveal::All
}
};
let moved = editor.selections != before;
self.cursor_position_changed(Cursor { moved, source: "mouse", reveal: if reveal { shown } else { Reveal::Nothing } }, cx);
}
}