tui-widgets: Add tree hit testing
457197c07467dfc975f6de35df0506d3b317e352
Expose tree row hit-testing so callers can map pointer input through the tree's own visible row geometry instead of duplicating marker and indentation math. Assisted-by: Codex:gpt-5.5
1 parent
90c19c26
crates/diff/src/viewer/tree.rs
+7 -10
| 4 | 4 | use std::path::PathBuf; |
|
| 5 | 5 | ||
| 6 | 6 | use rustik_tui as tui; |
|
| 7 | 7 | use tui::{ |
|
| 8 | 8 | Buffer, Color, Label, Line, Placement, Point, Rect, Style, Table, TableOptions, Tree, |
|
| 9 | - | TreeModel, TreeNodeKind, TreeRow, TreeState, TreeStyle, View, Widget, colors, |
|
| 9 | + | TreeHitZone, TreeModel, TreeNodeKind, TreeRow, TreeState, TreeStyle, View, Widget, colors, |
|
| 10 | 10 | }; |
|
| 11 | 11 | ||
| 12 | 12 | use super::{DiffPalette, FILE_KIND_BADGE_FG, MIN_RENDER_COLS}; |
|
| 13 | 13 | use crate::{DiffDocument, FileId, FileKind, Stats}; |
|
| 14 | 14 |
| 20 | 20 | const INFO_BOX_ROWS: usize = 4; |
|
| 21 | 21 | /// Blank rows between the file tree and the summary box. |
|
| 22 | 22 | const INFO_BOX_GAP_ROWS: usize = 1; |
|
| 23 | 23 | /// Minimum tree rows kept visible when the summary box is shown. |
|
| 24 | 24 | const MIN_TREE_ROWS_WITH_INFO: usize = 3; |
|
| 25 | - | /// Columns added by the tree widget for each nesting level. |
|
| 26 | - | const TREE_INDENT_COLS: usize = 2; |
|
| 27 | 25 | /// Background color for the selected or active file row. |
|
| 28 | 26 | const SELECTED_FILE_BG: Color = Color::Fixed(235); |
|
| 29 | 27 | /// Background color for the selected or active folder row. |
|
| 30 | 28 | const SELECTED_FOLDER_BG: Color = Color::Fixed(234); |
|
| 31 | 29 | /// Foreground color for active file-tree row labels. |
| 225 | 223 | let tree = Tree::new(&self.model, &self.state); |
|
| 226 | 224 | let rows = tree.visible_rows(); |
|
| 227 | 225 | let row = tree.selected_row(&rows).unwrap_or_default(); |
|
| 228 | 226 | let view = |
|
| 229 | 227 | View::new(0, rows.len(), tree_area.rows).with_row_visible(row, Placement::Minimal); |
|
| 230 | - | let row = view.row_at(tree_area, point)?; |
|
| 231 | - | let row = rows.get(row)?; |
|
| 232 | - | if self.model.kind(row.id) == TreeNodeKind::Branch |
|
| 233 | - | && point.x.saturating_sub(tree_area.x) == row.depth.saturating_mul(TREE_INDENT_COLS) |
|
| 234 | - | { |
|
| 235 | - | self.state.toggle(&self.model, row.id); |
|
| 228 | + | let hit = tree.hit_test(&rows, view, tree_area, point)?; |
|
| 229 | + | let id = hit.row.id; |
|
| 230 | + | ||
| 231 | + | if self.model.kind(id) == TreeNodeKind::Branch && hit.zone == TreeHitZone::Marker { |
|
| 232 | + | self.state.toggle(&self.model, id); |
|
| 236 | 233 | Some(FileTreeClick::Disclosure) |
|
| 237 | 234 | } else { |
|
| 238 | - | self.state.select(row.id); |
|
| 235 | + | self.state.select(id); |
|
| 239 | 236 | Some(FileTreeClick::Row) |
|
| 240 | 237 | } |
|
| 241 | 238 | } |
|
| 242 | 239 | ||
| 243 | 240 | /// Activates the current file-tree cursor. |
a.rs
+0 -0
b.rs
+0 -0
crates/tui-widgets/src/lib.rs
+2 -1
| 32 | 32 | pub use list::{List, ListState, Selection}; |
|
| 33 | 33 | pub use overlay::{Overlay, overlay_area}; |
|
| 34 | 34 | pub use scrollbar::{Scrollbar, ScrollbarStyle}; |
|
| 35 | 35 | pub use scrollview::{ScrollLayout, ScrollMetrics, ScrollView, VirtualScrollView, scroll_layout}; |
|
| 36 | 36 | pub use tree::{ |
|
| 37 | - | Tree, TreeModel, TreeNodeKind, TreeNodeState, TreeRow, TreeState, TreeStyle, TreeVisibleRow, |
|
| 37 | + | Tree, TreeHit, TreeHitZone, TreeModel, TreeNodeKind, TreeNodeState, TreeRow, TreeState, |
|
| 38 | + | TreeStyle, TreeVisibleRow, |
|
| 38 | 39 | }; |
|
| 39 | 40 | pub use viewport::{Placement, Scroll, View, Viewport}; |
crates/tui-widgets/src/tree.rs
+68 -1
| 6 | 6 | //! into the small model trait. |
|
| 7 | 7 | ||
| 8 | 8 | use std::collections::BTreeSet; |
|
| 9 | 9 | use std::fmt; |
|
| 10 | 10 | ||
| 11 | - | use rustik_tui_core::{Buffer, Color, Constraint, Label, Line, Rect, Size, Style, Widget}; |
|
| 11 | + | use crate::View; |
|
| 12 | + | use rustik_tui_core::{Buffer, Color, Constraint, Label, Line, Point, Rect, Size, Style, Widget}; |
|
| 12 | 13 | ||
| 13 | 14 | /// Columns added for each tree depth. |
|
| 14 | 15 | const INDENT_COLS: usize = 2; |
|
| 15 | 16 | /// Columns reserved for the branch marker and following space. |
|
| 16 | 17 | const MARKER_COLS: usize = 2; |
| 344 | 345 | pub depth: usize, |
|
| 345 | 346 | /// Visible expansion state. |
|
| 346 | 347 | pub state: TreeNodeState, |
|
| 347 | 348 | } |
|
| 348 | 349 | ||
| 350 | + | /// Region hit inside a rendered tree row. |
|
| 351 | + | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
|
| 352 | + | pub enum TreeHitZone { |
|
| 353 | + | /// Branch or leaf marker column. |
|
| 354 | + | Marker, |
|
| 355 | + | /// Row content outside the marker column. |
|
| 356 | + | Content, |
|
| 357 | + | } |
|
| 358 | + | ||
| 359 | + | /// Tree row hit by a screen point. |
|
| 360 | + | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
|
| 361 | + | pub struct TreeHit<Id> { |
|
| 362 | + | /// Visible row hit by the point. |
|
| 363 | + | pub row: TreeVisibleRow<Id>, |
|
| 364 | + | /// Region hit inside the visible row. |
|
| 365 | + | pub zone: TreeHitZone, |
|
| 366 | + | } |
|
| 367 | + | ||
| 349 | 368 | /// Generic tree widget over caller-owned data. |
|
| 350 | 369 | #[derive(Clone)] |
|
| 351 | 370 | pub struct Tree<'a, M> |
|
| 352 | 371 | where |
|
| 353 | 372 | M: TreeModel, |
| 420 | 439 | let selected = self.state.selected?; |
|
| 421 | 440 | ||
| 422 | 441 | rows.iter().position(|row| row.id == selected) |
|
| 423 | 442 | } |
|
| 424 | 443 | ||
| 444 | + | /// Returns the visible row and row region under `point`. |
|
| 445 | + | pub fn hit_test( |
|
| 446 | + | &self, |
|
| 447 | + | rows: &[TreeVisibleRow<M::Id>], |
|
| 448 | + | view: View, |
|
| 449 | + | area: Rect, |
|
| 450 | + | point: Point, |
|
| 451 | + | ) -> Option<TreeHit<M::Id>> { |
|
| 452 | + | let index = view.row_at(area, point)?; |
|
| 453 | + | let row = rows.get(index).copied()?; |
|
| 454 | + | let x = point.x.saturating_sub(area.x); |
|
| 455 | + | let zone = if x == row.depth.saturating_mul(INDENT_COLS) { |
|
| 456 | + | TreeHitZone::Marker |
|
| 457 | + | } else { |
|
| 458 | + | TreeHitZone::Content |
|
| 459 | + | }; |
|
| 460 | + | ||
| 461 | + | Some(TreeHit { row, zone }) |
|
| 462 | + | } |
|
| 463 | + | ||
| 425 | 464 | /// Walks tree rows in render order. |
|
| 426 | 465 | fn walk(&self, mut visit: impl FnMut(TreeVisibleRow<M::Id>) -> bool) { |
|
| 427 | 466 | let roots = self.model.roots(); |
|
| 428 | 467 | let mut stack = Vec::with_capacity(roots.len()); |
|
| 429 | 468 | stack.extend(roots.iter().rev().map(|&id| (id, 0))); |
| 869 | 908 | "scrolled tree rows", |
|
| 870 | 909 | include_str!("../tests/golden/tree_scroll_22x2.txt"), |
|
| 871 | 910 | buffer.lines(), |
|
| 872 | 911 | ); |
|
| 873 | 912 | } |
|
| 913 | + | ||
| 914 | + | #[test] |
|
| 915 | + | fn hit_test_maps_points_to_visible_rows_and_regions() { |
|
| 916 | + | let model = Model::sample(); |
|
| 917 | + | let mut state = TreeState::new(); |
|
| 918 | + | ||
| 919 | + | state.expand(SRC); |
|
| 920 | + | let tree = Tree::new(&model, &state); |
|
| 921 | + | let rows = tree.visible_rows(); |
|
| 922 | + | let view = View::new(1, rows.len(), 2); |
|
| 923 | + | let area = Rect::new(4, 7, 20, 2); |
|
| 924 | + | ||
| 925 | + | assert_eq!( |
|
| 926 | + | tree.hit_test(&rows, view, area, Point::new(6, 7)), |
|
| 927 | + | Some(TreeHit { |
|
| 928 | + | row: rows[1], |
|
| 929 | + | zone: TreeHitZone::Marker, |
|
| 930 | + | }) |
|
| 931 | + | ); |
|
| 932 | + | assert_eq!( |
|
| 933 | + | tree.hit_test(&rows, view, area, Point::new(8, 8)), |
|
| 934 | + | Some(TreeHit { |
|
| 935 | + | row: rows[2], |
|
| 936 | + | zone: TreeHitZone::Content, |
|
| 937 | + | }) |
|
| 938 | + | ); |
|
| 939 | + | assert_eq!(tree.hit_test(&rows, view, area, Point::new(3, 7)), None); |
|
| 940 | + | } |
|
| 874 | 941 | } |