diff: Fix tree auto-hide behavior
a0e0175f6b18679f5938b808e01d8d29a5ad29ea
1 parent
39fa18a7
crates/diff/src/viewer.rs
+4 -5
| 682 | 682 | }; |
|
| 683 | 683 | } |
|
| 684 | 684 | ||
| 685 | 685 | let tree_cols = self.file_tree.cols(area.cols); |
|
| 686 | 686 | if self.file_tree_visibility == FileTreeVisibility::Auto |
|
| 687 | - | && (area.cols <= tree::MIN_VIEW_COLS |
|
| 688 | - | || area |
|
| 689 | - | .cols |
|
| 690 | - | .saturating_sub(tree_cols.saturating_add(tree::GAP_COLS)) |
|
| 691 | - | < tree::MIN_DIFF_COLS) |
|
| 687 | + | && area |
|
| 688 | + | .cols |
|
| 689 | + | .saturating_sub(tree_cols.saturating_add(tree::GAP_COLS)) |
|
| 690 | + | < tree::MIN_DIFF_COLS |
|
| 692 | 691 | { |
|
| 693 | 692 | return DiffLayout { |
|
| 694 | 693 | diff: area, |
|
| 695 | 694 | tree: None, |
|
| 696 | 695 | }; |
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
crates/diff/tests/golden/sticky_header_before_next_file_64x5.txt
+0 -0
src/a.rs
+0 -0
src/b.rs
+0 -0
README.md
+0 -0
src/app/a.rs
+0 -0
src/lib/b.rs
+0 -0
README.md
+0 -0
a.rs
+0 -0
b.rs
+0 -0
assets/css/style.css
+0 -0
assets/css/style.css
+0 -0
a.rs
+0 -0
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
crates/diff/src/viewer/tree.rs
+6 -8
| 11 | 11 | }; |
|
| 12 | 12 | ||
| 13 | 13 | use super::{DiffPalette, FILE_KIND_BADGE_FG, MIN_RENDER_COLS}; |
|
| 14 | 14 | use crate::{DiffDocument, FileId, FileKind, Stats}; |
|
| 15 | 15 | ||
| 16 | - | /// Minimum columns reserved for the file tree when it is visible. |
|
| 17 | - | const DEFAULT_COLS: usize = 28; |
|
| 18 | 16 | /// Blank rows above the file tree list. |
|
| 19 | 17 | const TOP_PADDING_ROWS: usize = 1; |
|
| 20 | 18 | /// Rows reserved for the summary box below the file tree. |
|
| 21 | 19 | const INFO_BOX_ROWS: usize = 4; |
|
| 20 | + | /// Minimum columns needed to render the summary box without truncating labels. |
|
| 21 | + | const INFO_BOX_COLS: usize = 28; |
|
| 22 | 22 | /// Blank rows between the file tree and the summary box. |
|
| 23 | 23 | const INFO_BOX_GAP_ROWS: usize = 1; |
|
| 24 | 24 | /// Minimum tree rows kept visible when the summary box is shown. |
|
| 25 | 25 | const MIN_TREE_ROWS_WITH_INFO: usize = 3; |
|
| 26 | 26 | /// Background color for the selected or active file row. |
| 41 | 41 | /// Style for a collapsed folder containing the active file. |
|
| 42 | 42 | const SELECTED_FOLDER_STYLE: Style = Style::new(SELECTED_FOLDER_FG).bg(SELECTED_FOLDER_BG); |
|
| 43 | 43 | ||
| 44 | 44 | /// Blank columns between the file tree and the diff body. |
|
| 45 | 45 | pub(super) const GAP_COLS: usize = 1; |
|
| 46 | - | /// Minimum viewport width that can show the file tree beside the diff. |
|
| 47 | - | pub(super) const MIN_VIEW_COLS: usize = 80; |
|
| 48 | 46 | /// Minimum main diff columns preserved when the file tree is visible. |
|
| 49 | - | pub(super) const MIN_DIFF_COLS: usize = MIN_VIEW_COLS - DEFAULT_COLS - GAP_COLS; |
|
| 47 | + | pub(super) const MIN_DIFF_COLS: usize = 80; |
|
| 50 | 48 | ||
| 51 | 49 | /// Selectable target in the file tree. |
|
| 52 | 50 | #[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] |
|
| 53 | 51 | pub enum FileTreeTarget { |
|
| 54 | 52 | /// Root row for the current working directory. |
| 163 | 161 | /// Returns the file tree width for a viewport. |
|
| 164 | 162 | pub fn cols(&self, view_cols: usize) -> usize { |
|
| 165 | 163 | let available = view_cols.saturating_sub(GAP_COLS + MIN_RENDER_COLS); |
|
| 166 | 164 | ||
| 167 | 165 | Tree::new(&self.model, &self.state) |
|
| 168 | - | .content_cols(DEFAULT_COLS) |
|
| 166 | + | .content_cols(0) |
|
| 169 | 167 | .min(available) |
|
| 170 | 168 | } |
|
| 171 | 169 | ||
| 172 | 170 | /// Renders the file tree into `area` with an explicit active file and palette. |
|
| 173 | 171 | /// |
| 339 | 337 | ); |
|
| 340 | 338 | if body.is_empty() { |
|
| 341 | 339 | return None; |
|
| 342 | 340 | } |
|
| 343 | 341 | let info_rows = INFO_BOX_ROWS + INFO_BOX_GAP_ROWS + MIN_TREE_ROWS_WITH_INFO; |
|
| 344 | - | let info = (body.rows >= info_rows).then(|| { |
|
| 342 | + | let info = (body.rows >= info_rows && body.cols >= INFO_BOX_COLS).then(|| { |
|
| 345 | 343 | Rect::new( |
|
| 346 | 344 | body.x, |
|
| 347 | 345 | body.bottom().saturating_sub(INFO_BOX_ROWS), |
|
| 348 | 346 | body.cols, |
|
| 349 | 347 | INFO_BOX_ROWS, |
| 360 | 358 | } |
|
| 361 | 359 | ||
| 362 | 360 | impl Widget for FileTree { |
|
| 363 | 361 | fn size(&self, parent: Constraint) -> Size { |
|
| 364 | 362 | let tree = Tree::new(&self.model, &self.state); |
|
| 365 | - | let cols = tree.content_cols(DEFAULT_COLS); |
|
| 363 | + | let cols = tree.content_cols(0); |
|
| 366 | 364 | let rows = tree.visible_rows().len().saturating_add(TOP_PADDING_ROWS); |
|
| 367 | 365 | ||
| 368 | 366 | Size::new(cols, rows).constrain(parent) |
|
| 369 | 367 | } |
|
| 370 | 368 |
a.rs
+0 -0
b.rs
+0 -0
crates/diff/tests/golden.rs
+3 -3
| 191 | 191 | } |
|
| 192 | 192 | ||
| 193 | 193 | #[test] |
|
| 194 | 194 | fn wide_file_tree_matches_golden_frame() { |
|
| 195 | 195 | assert_golden( |
|
| 196 | - | "wide_file_tree_100x12.txt", |
|
| 197 | - | include_str!("golden/wide_file_tree_100x12.txt"), |
|
| 196 | + | "wide_file_tree_120x12.txt", |
|
| 197 | + | include_str!("golden/wide_file_tree_120x12.txt"), |
|
| 198 | 198 | two_file_tree_viewer(), |
|
| 199 | - | 100, |
|
| 199 | + | 120, |
|
| 200 | 200 | 12, |
|
| 201 | 201 | ); |
|
| 202 | 202 | } |
|
| 203 | 203 | ||
| 204 | 204 | #[test] |
a.rs
+0 -0
crates/diff/tests/golden/wide_file_tree_100x12.txt
deleted
+0 -12
| 1 | - | ╭────────────────────────────────────────────────────────────────────╮▐ |
|
| 2 | - | ▿ rustik/ │ ▿ a.rs +2 -1 │▐ |
|
| 3 | - | ⊙ a.rs ├────────────────────────────────────────────────────────────────────┤▐ |
|
| 4 | - | ⊙ b.rs │ 1 1 context │▐ |
|
| 5 | - | │ 2 - old │▐ |
|
| 6 | - | │ 2 + new │▐ |
|
| 7 | - | │ 3 + extra │▐ |
|
| 8 | - | │ 3 4 tail │▐ |
|
| 9 | - | ╭──────────────────────────╮ ╰────────────────────────────────────────────────────────────────────╯▐ |
|
| 10 | - | │ lines changed +3 -2 │ ╭────────────────────────────────────────────────────────────────────╮🮘 |
|
| 11 | - | │ files changed 2 │ │ ▿ b.rs +1 -1 │🮘 |
|
| 12 | - | ╰──────────────────────────╯ ├────────────────────────────────────────────────────────────────────┤🮘 |
crates/diff/tests/golden/wide_file_tree_120x12.txt
added
+12 -0
| 1 | + | ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────╮▐ |
|
| 2 | + | ▿ rustik/ │ ▿ a.rs +2 -1 │▐ |
|
| 3 | + | ⊙ a.rs ├───────────────────────────────────────────────────────────────────────────────────────────────────────────┤▐ |
|
| 4 | + | ⊙ b.rs │ 1 1 context │▐ |
|
| 5 | + | │ 2 - old │▐ |
|
| 6 | + | │ 2 + new │▐ |
|
| 7 | + | │ 3 + extra │▐ |
|
| 8 | + | │ 3 4 tail │▐ |
|
| 9 | + | ╰───────────────────────────────────────────────────────────────────────────────────────────────────────────╯▐ |
|
| 10 | + | ╭───────────────────────────────────────────────────────────────────────────────────────────────────────────╮🮘 |
|
| 11 | + | │ ▿ b.rs +1 -1 │🮘 |
|
| 12 | + | ├───────────────────────────────────────────────────────────────────────────────────────────────────────────┤🮘 |