diff: Fix auto-hide tree bug

7ff8b84d983fcc61db69ab288a0aa53bc8b3ce47
Alexis Sellier committed ago 1 parent ae4230ed
crates/diff/src/viewer.rs +28 -5
329 329
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
330 330
enum FileTreeVisibility {
331 331
    /// Follow the configured file-tree rendering policy.
332 332
    #[default]
333 333
    Auto,
334 +
    /// Show the file tree even when automatic layout would hide it.
335 +
    Visible,
334 336
    /// Hide the file tree until it is toggled back on.
335 337
    Hidden,
336 338
}
337 339
338 340
impl Default for DiffOptions {
631 633
632 634
    /// Returns the viewport layout for `area`.
633 635
    fn layout(&self, area: Rect) -> DiffLayout {
634 636
        if self.options.file_tree != FileTreeMode::Auto
635 637
            || self.file_tree_visibility == FileTreeVisibility::Hidden
636 -
            || area.cols <= tree::MIN_VIEW_COLS
637 638
            || self.document.files.is_empty()
638 639
        {
639 640
            return DiffLayout {
640 641
                diff: area,
641 642
                tree: None,
642 643
            };
643 644
        }
644 645
646 +
        let tree_cols = self.file_tree.cols(area.cols);
647 +
        if self.file_tree_visibility == FileTreeVisibility::Auto
648 +
            && (area.cols <= tree::MIN_VIEW_COLS
649 +
                || area
650 +
                    .cols
651 +
                    .saturating_sub(tree_cols.saturating_add(tree::GAP_COLS))
652 +
                    < tree::MIN_DIFF_COLS)
653 +
        {
654 +
            return DiffLayout {
655 +
                diff: area,
656 +
                tree: None,
657 +
            };
658 +
        }
659 +
645 660
        let [tree, _, diff] = tui::layout::Layout::new(tui::Direction::Horizontal)
646 -
            .child(tui::Length::Fixed(self.file_tree.cols(area.cols)))
661 +
            .child(tui::Length::Fixed(tree_cols))
647 662
            .child(tui::Length::Fixed(tree::GAP_COLS))
648 663
            .child(tui::Length::Fill(1))
649 664
            .areas(area);
650 665
651 666
        DiffLayout {
763 778
                    self.sync_file_tree();
764 779
                }
765 780
            }
766 781
            DiffAction::ToggleFileTree => match self.file_tree_visibility {
767 782
                FileTreeVisibility::Hidden => {
768 -
                    self.file_tree_visibility = FileTreeVisibility::Auto;
783 +
                    self.file_tree_visibility = FileTreeVisibility::Visible;
784 +
                    if self.layout(area).tree.is_some() {
785 +
                        self.sync_file_tree();
786 +
                    }
787 +
                }
788 +
                FileTreeVisibility::Auto | FileTreeVisibility::Visible if layout.tree.is_some() => {
789 +
                    self.file_tree_visibility = FileTreeVisibility::Hidden;
790 +
                }
791 +
                FileTreeVisibility::Auto => {
792 +
                    self.file_tree_visibility = FileTreeVisibility::Visible;
769 793
                    if self.layout(area).tree.is_some() {
770 794
                        self.sync_file_tree();
771 795
                    }
772 796
                }
773 -
                FileTreeVisibility::Auto if layout.tree.is_some() => {
797 +
                FileTreeVisibility::Visible => {
774 798
                    self.file_tree_visibility = FileTreeVisibility::Hidden;
775 799
                }
776 -
                FileTreeVisibility::Auto => {}
777 800
            },
778 801
            DiffAction::MoveFileTree(delta) => {
779 802
                if self.file_tree.state().selected().is_none() {
780 803
                    self.file_tree.enter(self.visible_file());
781 804
                }
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 +2 -0
42 42
43 43
/// Blank columns between the file tree and the diff body.
44 44
pub(super) const GAP_COLS: usize = 1;
45 45
/// Minimum viewport width that can show the file tree beside the diff.
46 46
pub(super) const MIN_VIEW_COLS: usize = 80;
47 +
/// Minimum main diff columns preserved when the file tree is visible.
48 +
pub(super) const MIN_DIFF_COLS: usize = MIN_VIEW_COLS - DEFAULT_COLS - GAP_COLS;
47 49
48 50
/// Selectable target in the file tree.
49 51
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
50 52
pub enum FileTreeTarget {
51 53
    /// Root row for the current working directory.
a.rs +0 -0
b.rs +0 -0