diff: Handle file tree mouse clicks

de199e36f430adc4d26adaa50ba26021e10bc799
Clicking file-tree rows should reuse the existing keyboard actions
instead of adding a separate selection path. This maps ordinary clicks
to Enter and control-clicks to Space after hit-testing against the
rendered tree rows.
Alexis Sellier committed ago 1 parent b723340d
crates/diff/src/renderer.rs +35 -0
6 6
use std::collections::{BTreeMap, BTreeSet};
7 7
use std::path::PathBuf;
8 8
9 9
use rustik_highlight as syntax;
10 10
use rustik_tui as tui;
11 +
use tui::event::Modifiers;
11 12
use tui::{
12 13
    Buffer, Cell, Color, Constraint, Control, Event, HStack, Key, Label, Line, Mouse, MouseButton,
13 14
    Placement, Rect, Scroll, ScrollView, ScrollbarStyle, Size, Style, Table, TableOptions, Text,
14 15
    VStack, View, Viewport, Widget, colors,
15 16
};
538 539
            }) => DiffAction::Scroll(Scroll::Lines(LINE_SCROLL_ROWS)),
539 540
            Event::Mouse(Mouse::Press {
540 541
                button: MouseButton::WheelUp,
541 542
                ..
542 543
            }) => DiffAction::Scroll(Scroll::Lines(-LINE_SCROLL_ROWS)),
544 +
            Event::Mouse(Mouse::Press {
545 +
                button: MouseButton::Left,
546 +
                at,
547 +
                modifiers,
548 +
            }) => return self.handle_click(at, modifiers, area),
543 549
            _ => match self.state.pane {
544 550
                DiffPane::FileTree => return self.handle_file_tree_key(event, area),
545 551
                DiffPane::Main => return self.handle_main_key(event, area),
546 552
            },
547 553
        };
548 554
        self.apply(action, area);
549 555
550 556
        Control::Redraw
551 557
    }
552 558
559 +
    /// Applies a left-click event.
560 +
    fn handle_click(&mut self, at: tui::Point, modifiers: Modifiers, area: Rect) -> Control {
561 +
        let Some(tree) = self.layout(area).tree else {
562 +
            return Control::Continue;
563 +
        };
564 +
        if !self.file_tree.select_at(tree, at) {
565 +
            return Control::Continue;
566 +
        }
567 +
        self.state.pane = DiffPane::FileTree;
568 +
569 +
        let action = if modifiers.contains(Modifiers::CONTROL) {
570 +
            DiffAction::ToggleFileTreeTarget
571 +
        } else {
572 +
            DiffAction::ActivateFileTreeTarget
573 +
        };
574 +
        self.apply(action, area);
575 +
576 +
        Control::Redraw
577 +
    }
578 +
553 579
    /// Applies a key event while the main diff owns input.
554 580
    fn handle_main_key(&mut self, event: Event<()>, area: Rect) -> Control {
555 581
        let action = match event {
556 582
            Event::Key(Key::Char('h')) => DiffAction::EnterFileTree,
557 583
            Event::Key(Key::Esc) => {
1463 1489
1464 1490
    use crate::Decode;
1465 1491
    use crate::DiffDocument;
1466 1492
    use rustik_tui::Point;
1467 1493
1494 +
    /// Returns a left-click event for tests.
1495 +
    fn left_click(x: usize, y: usize, modifiers: Modifiers) -> Event<()> {
1496 +
        Event::Mouse(Mouse::Press {
1497 +
            button: MouseButton::Left,
1498 +
            at: Point::new(x, y),
1499 +
            modifiers,
1500 +
        })
1501 +
    }
1502 +
1468 1503
    fn sample_viewer() -> DiffViewer {
1469 1504
        let document = DiffDocument::decode_str(
1470 1505
            "\
1471 1506
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
a.rs +0 -0
crates/diff/src/renderer/tree.rs +33 -12
3 3
use std::collections::BTreeSet;
4 4
use std::path::PathBuf;
5 5
6 6
use rustik_tui as tui;
7 7
use tui::{
8 -
    Buffer, Color, Label, Placement, Rect, Style, Tree, TreeModel, TreeNodeKind, TreeRow,
8 +
    Buffer, Color, Label, Placement, Point, Rect, Style, Tree, TreeModel, TreeNodeKind, TreeRow,
9 9
    TreeState, TreeStyle, View,
10 10
};
11 11
12 12
use super::{DiffFocus, DiffPalette, DiffState, MIN_RENDER_COLS};
13 13
use crate::{DiffDocument, FileId, FileKind};
145 145
        palette: DiffPalette,
146 146
        active: bool,
147 147
        area: Rect,
148 148
        buf: &mut Buffer,
149 149
    ) {
150 -
        if area.is_empty() {
150 +
        let Some(area) = Self::body_area(area) else {
151 151
            return;
152 -
        }
153 -
        let area = Rect::new(
154 -
            area.x,
155 -
            area.y.saturating_add(TOP_PADDING_ROWS),
156 -
            area.cols,
157 -
            area.rows.saturating_sub(TOP_PADDING_ROWS),
158 -
        );
159 -
        if area.is_empty() {
160 -
            return;
161 -
        }
152 +
        };
162 153
        let collapsed = self
163 154
            .model
164 155
            .branch_ids()
165 156
            .filter(|id| !self.state.is_expanded(*id))
166 157
            .collect();
196 187
    /// Moves the file-tree cursor by `delta` visible rows.
197 188
    pub(super) fn select(&mut self, delta: isize) {
198 189
        self.state.select_next(&self.model, delta);
199 190
    }
200 191
192 +
    /// Selects the file-tree row at `point` inside `area`.
193 +
    pub(super) fn select_at(&mut self, area: Rect, point: Point) -> bool {
194 +
        let Some(area) = Self::body_area(area) else {
195 +
            return false;
196 +
        };
197 +
        let tree = Tree::new(&self.model, &self.state);
198 +
        let rows = tree.visible_rows();
199 +
        let row = tree.selected_row(&rows).unwrap_or_default();
200 +
        let view = View::new(0, rows.len(), area.rows).with_row_visible(row, Placement::Minimal);
201 +
        let Some(row) = view.row_at(area, point) else {
202 +
            return false;
203 +
        };
204 +
        let Some(row) = rows.get(row) else {
205 +
            return false;
206 +
        };
207 +
        self.state.select(row.id);
208 +
        true
209 +
    }
210 +
201 211
    /// Extends file selection from the current anchor by `delta` rows.
202 212
    pub(super) fn extend_selection(&mut self, delta: isize) -> FileSelection {
203 213
        let range = self.state.extend_selection(&self.model, delta);
204 214
        let mut files = BTreeSet::new();
205 215
313 323
            .visible_rows()
314 324
            .into_iter()
315 325
            .next()
316 326
            .map(|row| row.id)
317 327
    }
328 +
329 +
    /// Returns the drawable row area for file-tree contents.
330 +
    fn body_area(area: Rect) -> Option<Rect> {
331 +
        let area = Rect::new(
332 +
            area.x,
333 +
            area.y.saturating_add(TOP_PADDING_ROWS),
334 +
            area.cols,
335 +
            area.rows.saturating_sub(TOP_PADDING_ROWS),
336 +
        );
337 +
        (!area.is_empty()).then_some(area)
338 +
    }
318 339
}
319 340
320 341
/// Path tree built once from a diff document. Immutable after construction.
321 342
#[derive(Clone, Debug, Eq, PartialEq)]
322 343
pub(super) struct FileTreeModel {
a.rs +0 -0
b.rs +0 -0