Skip to content

Visual Studio Code, rebuilt in Rust

Rust
7.8 KB
Raw Blame
//! mouseHandler.ts `_onMouseDownThenMove` past the text (`_getPositionOutsideEditor`) and dragScrolling.ts: a
//! selecting drag above, below, left or right of the text scrolls the editor every frame, faster the farther out the
//! pointer is, and selects up to the line at the edge.

use std::time::Instant;

use gpui::{Context, Modifiers, Pixels, Point};

use super::Workbench;
use crate::editor::{HORIZONTAL_SCROLLBAR_HEIGHT, Position, VERTICAL_SCROLLBAR_WIDTH, column_x};

#[derive(Clone, Copy, PartialEq, Debug)]
pub(super) enum Outside {
    Above,
    Below,
    Left,
    Right,
}

pub(super) struct DragScrolling {
    outside: Outside,
    distance: f32,
    /// The position a left or right drag selects to (`_position.position`).
    position: Position,
    /// `mouseColumn` as a visible column of the view line.
    visible: f32,
    modifiers: Modifiers,
    last_frame: Instant,
}

impl Workbench {
    /// A move of a selecting drag with the text area (gutter included) at `origin`, `height` tall, its text from
    /// `content_left` to `content_right` (the minimap's left, else the vertical scrollbar's): past the text a drag
    /// scrolling starts or goes on, below the last line the drag selects to its end; over a line the line's own
    /// listener takes it.
    pub(super) fn drag_select_moved(&mut self, origin: Point<Pixels>, height: f32, (content_left, content_right): (f32, f32), pointer: Point<Pixels>, modifiers: Modifiers, cx: &mut Context<Self>) {
        if self.selecting_text.is_none() || self.text_drag.is_some() {
            return;
        }
        let viewport = self.editors.viewport;
        let editor = self.editors.active().expect("a selecting drag runs on an open editor");
        let (x, y) = (f32::from(pointer.x - origin.x) - content_left, f32::from(pointer.y - origin.y));
        let visible = (x + editor.scroll.left) / viewport.char_width;
        let last = editor.view_line_count() - 1;
        let view_line = editor.view_line_at((editor.scroll.top + y).max(0.), viewport);
        let segment = editor.segment(view_line.min(last));
        let (outside, distance, position) = match () {
            _ if y < 0. => (Outside::Above, -y, Position::new(segment.line, segment.start)),
            _ if y > height => (Outside::Below, y - height, Position::new(segment.line, segment.end)),
            _ if x <= 0. => (Outside::Left, -x, Position::new(segment.line, segment.start)),
            _ if x >= content_right - content_left => (Outside::Right, x - content_right + content_left, Position::new(segment.line, segment.end)),
            _ => {
                self.drag_scrolling = None;
                // MouseTargetType.CONTENT_EMPTY below the last line: the end of the last line.
                if view_line > last {
                    self.drag_select_at(Position::new(segment.line, segment.end), visible, modifiers, true, cx);
                }
                return;
            }
        };
        let vertical = |outside| matches!(outside, Outside::Above | Outside::Below);
        // `start` keeps a running operation of the same axis and its clock; the other axis's stops.
        let last_frame = match &self.drag_scrolling {
            Some(running) if vertical(running.outside) == vertical(outside) => running.last_frame,
            _ => Instant::now(),
        };
        self.drag_scrolling = Some(DragScrolling { outside, distance, position, visible, modifiers, last_frame });
        cx.notify();
    }

    /// One `_execute` of the running drag scrolling: scrolls by the time since the last frame, then selects to the
    /// edge. False once the drag ended.
    pub(super) fn drag_scroll_frame(&mut self, cx: &mut Context<Self>) -> bool {
        if self.selecting_text.is_none() {
            self.drag_scrolling = None;
        }
        let viewport = self.editors.viewport;
        let (Some(scrolling), Some(editor)) = (self.drag_scrolling.as_mut(), self.editors.active_mut()) else { return false };
        let now = Instant::now();
        let elapsed = now.duration_since(scrolling.last_frame).as_secs_f32();
        scrolling.last_frame = now;
        let (outside, position, visible, modifiers) = (scrolling.outside, scrolling.position, scrolling.visible, scrolling.modifiers);
        let (top, left) = match outside {
            Outside::Above | Outside::Below => {
                let pixels = scroll_speed(viewport.height / viewport.line_height, scrolling.distance / viewport.line_height) * elapsed * viewport.line_height;
                (editor.scroll.top + if outside == Outside::Above { -pixels } else { pixels }, editor.scroll.left)
            }
            Outside::Left | Outside::Right => {
                // typicalFullwidthCharacterWidth: two half-width columns (`columns_for_full_width_char`).
                let char_width = viewport.char_width * 2.;
                let content_width = viewport.width - VERTICAL_SCROLLBAR_WIDTH;
                let pixels = scroll_speed(content_width / char_width, scrolling.distance / char_width) * elapsed * char_width * 0.5;
                (editor.scroll.top, editor.scroll.left + if outside == Outside::Left { -pixels } else { pixels })
            }
        };
        if editor.set_scroll(top, left, viewport) {
            self.editor_scrolled(cx);
        }
        let (position, visible) = match outside {
            Outside::Left => (position, 0.),
            Outside::Right => (position, self.line_end_visible(position)),
            Outside::Above | Outside::Below => self.vertical_drag_edge(outside == Outside::Above, visible),
        };
        self.drag_select_at(position, visible, modifiers, false, cx);
        true
    }

    /// TopBottomDragScrollingOperation's target: the pointer's column on the bottom line when that line is the edge
    /// one (`viewportData.endLineNumber`) and more can scroll, else the start of the first visible line above or the
    /// end of the last below.
    pub(super) fn vertical_drag_edge(&self, above: bool, visible: f32) -> (Position, f32) {
        let viewport = self.editors.viewport;
        let editor = self.editors.active().expect("a drag scrolling runs on an open editor");
        let lines = editor.visible_lines(viewport);
        let last = editor.view_line_count() - 1;
        let edge = if above { lines.start } else { lines.end - 1 };
        let cannot_scroll = if above { lines.start == 0 } else { lines.end - 1 == last };
        // The reference hit tests 0.1px above the horizontal scrollbar for both directions.
        let hit = editor.view_line_at(editor.scroll.top + viewport.height - HORIZONTAL_SCROLLBAR_HEIGHT - 0.1, viewport).min(last);
        let segment = editor.segment(edge);
        if hit != edge || cannot_scroll {
            let position = if above { Position::new(segment.line, segment.start) } else { Position::new(segment.line, segment.end) };
            return (position, visible);
        }
        let layout = editor.line_layout(segment.line);
        let (start, end) = (segment.start_x(&layout), if segment.last { f32::MAX } else { segment.end_x(&layout) });
        let model_visible = layout.model_visible((visible + start - segment.indent as f32).clamp(start, end));
        (self.text_position(segment.line, model_visible), visible)
    }

    fn line_end_visible(&self, position: Position) -> f32 {
        let editor = self.editors.active().expect("a drag scrolling runs on an open editor");
        let document = editor.document();
        column_x(&document.lines[position.line], position.column, document.indentation.tab_size)
    }
}

/// dragScrolling.ts `_getScrollSpeed`: lines (or full-width chars) per second.
fn scroll_speed(viewport: f32, outside: f32) -> f32 {
    if outside <= 1.5 {
        return 30_f32.max(viewport * (1. + outside));
    }
    if outside <= 3. {
        return 60_f32.max(viewport * (2. + outside));
    }
    200_f32.max(viewport * (7. + outside))
}