From 294f856150d69fbc11b5d010f518b1e4e68489e4 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 6 Jul 2024 23:50:54 +0800 Subject: [PATCH] fix: bounds not updated after left side bar layout updated --- src/Views/BranchTree.axaml | 32 ++--- src/Views/BranchTree.axaml.cs | 35 +++-- src/Views/Repository.axaml | 6 +- src/Views/Repository.axaml.cs | 254 +++++++++++++++++----------------- 4 files changed, 174 insertions(+), 153 deletions(-) diff --git a/src/Views/BranchTree.axaml b/src/Views/BranchTree.axaml index 3ccadbc9..66eca538 100644 --- a/src/Views/BranchTree.axaml +++ b/src/Views/BranchTree.axaml @@ -10,13 +10,11 @@ x:Name="ThisControl"> - + - + - - + - + - diff --git a/src/Views/BranchTree.axaml.cs b/src/Views/BranchTree.axaml.cs index cb0d1c58..95e70f48 100644 --- a/src/Views/BranchTree.axaml.cs +++ b/src/Views/BranchTree.axaml.cs @@ -113,6 +113,15 @@ namespace SourceGit.Views remove { RemoveHandler(SelectionChangedEvent, value); } } + public static readonly RoutedEvent RowsChangedEvent = + RoutedEvent.Register(nameof(RowsChanged), RoutingStrategies.Tunnel | RoutingStrategies.Bubble); + + public event EventHandler RowsChanged + { + add { AddHandler(RowsChangedEvent, value); } + remove { RemoveHandler(RowsChangedEvent, value); } + } + public BranchTree() { InitializeComponent(); @@ -123,6 +132,14 @@ namespace SourceGit.Views BranchesPresenter.SelectedItem = null; } + protected override void OnSizeChanged(SizeChangedEventArgs e) + { + base.OnSizeChanged(e); + + if (Bounds.Height >= 23.0) + BranchesPresenter.Height = Bounds.Height; + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); @@ -138,18 +155,20 @@ namespace SourceGit.Views Rows.AddRange(rows); } - var repo = this.FindAncestorOfType(); - repo?.UpdateLeftSidebarLayout(); + RaiseEvent(new RoutedEventArgs(RowsChangedEvent)); } else if (change.Property == IsVisibleProperty) { - var repo = this.FindAncestorOfType(); - repo?.UpdateLeftSidebarLayout(); + RaiseEvent(new RoutedEventArgs(RowsChangedEvent)); } } private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e) { + var repo = DataContext as ViewModels.Repository; + if (repo?.Settings == null) + return; + foreach (var item in e.AddedItems) { if (item is ViewModels.BranchTreeNode node) @@ -167,10 +186,7 @@ namespace SourceGit.Views return; if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) - { - var repo = DataContext as ViewModels.Repository; - repo?.NavigateToCommit(branch.Head); - } + repo.NavigateToCommit(branch.Head); var prev = null as ViewModels.BranchTreeNode; foreach (var row in Rows) @@ -285,8 +301,7 @@ namespace SourceGit.Views rows.RemoveRange(idx + 1, removeCount); } - var repo = this.FindAncestorOfType(); - repo?.UpdateLeftSidebarLayout(); + RaiseEvent(new RoutedEventArgs(RowsChangedEvent)); } } } diff --git a/src/Views/Repository.axaml b/src/Views/Repository.axaml index 2ffcc485..2c3ecbe2 100644 --- a/src/Views/Repository.axaml +++ b/src/Views/Repository.axaml @@ -241,7 +241,8 @@ Margin="8,0,4,0" Nodes="{Binding LocalBranchTrees}" IsVisible="{Binding IsLocalBranchGroupExpanded}" - SelectionChanged="OnLocalBranchTreeSelectionChanged"/> + SelectionChanged="OnLocalBranchTreeSelectionChanged" + RowsChanged="OnBranchTreeRowsChanged"/> @@ -257,7 +258,8 @@ Margin="8,0,4,0" Nodes="{Binding RemoteBranchTrees}" IsVisible="{Binding IsRemoteGroupExpanded}" - SelectionChanged="OnRemoteBranchTreeSelectionChanged"/> + SelectionChanged="OnRemoteBranchTreeSelectionChanged" + RowsChanged="OnBranchTreeRowsChanged"/> diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index e2ffc0d6..0e3d7a19 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -15,132 +15,10 @@ namespace SourceGit.Views InitializeComponent(); } - public void UpdateLeftSidebarLayout() - { - var vm = DataContext as ViewModels.Repository; - if (vm == null || vm.Settings == null) - return; - - if (!IsLoaded) - return; - - var leftHeight = leftSidebarGroups.Bounds.Height - 28.0 * 5; - var localBranchRows = vm.IsLocalBranchGroupExpanded ? localBranchTree.Rows.Count : 0; - var remoteBranchRows = vm.IsRemoteGroupExpanded ? remoteBranchTree.Rows.Count : 0; - var desiredBranches = (localBranchRows + remoteBranchRows) * 24.0; - var desiredTag = vm.IsTagGroupExpanded ? tagsList.RowHeight * vm.VisibleTags.Count : 0; - var desiredSubmodule = vm.IsSubmoduleGroupExpanded ? submoduleList.RowHeight * vm.Submodules.Count : 0; - var desiredWorktree = vm.IsWorktreeGroupExpanded ? worktreeList.RowHeight * vm.Worktrees.Count : 0; - var desiredOthers = desiredTag + desiredSubmodule + desiredWorktree; - var hasOverflow = (desiredBranches + desiredOthers > leftHeight); - - if (vm.IsTagGroupExpanded) - { - var height = desiredTag; - if (hasOverflow) - { - var test = leftHeight - desiredBranches - desiredSubmodule - desiredWorktree; - if (test < 0) - height = Math.Min(200, height); - else - height = Math.Max(200, test); - } - - leftHeight -= height; - tagsList.Height = height; - hasOverflow = (desiredBranches + desiredSubmodule + desiredWorktree) > leftHeight; - } - - if (vm.IsSubmoduleGroupExpanded) - { - var height = desiredSubmodule; - if (hasOverflow) - { - var test = leftHeight - desiredBranches - desiredWorktree; - if (test < 0) - height = Math.Min(200, height); - else - height = Math.Max(200, test); - } - - leftHeight -= height; - submoduleList.Height = height; - hasOverflow = (desiredBranches + desiredWorktree) > leftHeight; - } - - if (vm.IsWorktreeGroupExpanded) - { - var height = desiredWorktree; - if (hasOverflow) - { - var test = leftHeight - desiredBranches; - if (test < 0) - height = Math.Min(200, height); - else - height = Math.Max(200, test); - } - - leftHeight -= height; - worktreeList.Height = height; - } - - if (desiredBranches > leftHeight) - { - var local = localBranchRows * 24.0; - var remote = remoteBranchRows * 24.0; - var half = leftHeight / 2; - if (vm.IsLocalBranchGroupExpanded) - { - if (vm.IsRemoteGroupExpanded) - { - if (local < half) - { - localBranchTree.Height = local; - remoteBranchTree.Height = leftHeight - local; - } - else if (remote < half) - { - remoteBranchTree.Height = remote; - localBranchTree.Height = leftHeight - remote; - } - else - { - localBranchTree.Height = half; - remoteBranchTree.Height = half; - } - } - else - { - localBranchTree.Height = leftHeight; - } - } - else if (vm.IsRemoteGroupExpanded) - { - remoteBranchTree.Height = leftHeight; - } - } - else - { - if (vm.IsLocalBranchGroupExpanded) - { - var height = localBranchRows * 24; - localBranchTree.Height = height; - } - - if (vm.IsRemoteGroupExpanded) - { - var height = remoteBranchRows * 24; - remoteBranchTree.Height = height; - } - } - } - protected override void OnLoaded(RoutedEventArgs e) { base.OnLoaded(e); - - if (DataContext is ViewModels.Repository { IsSearching: false }) - UpdateLeftSidebarLayout(); + UpdateLeftSidebarLayout(); } private void OpenWithExternalTools(object sender, RoutedEventArgs e) @@ -211,7 +89,13 @@ namespace SourceGit.Views e.Handled = true; } - + + private void OnBranchTreeRowsChanged(object _, RoutedEventArgs e) + { + UpdateLeftSidebarLayout(); + e.Handled = true; + } + private void OnLocalBranchTreeSelectionChanged(object _1, RoutedEventArgs _2) { remoteBranchTree.UnselectAll(); @@ -311,5 +195,127 @@ namespace SourceGit.Views UpdateLeftSidebarLayout(); } } + + private void UpdateLeftSidebarLayout() + { + var vm = DataContext as ViewModels.Repository; + if (vm == null || vm.Settings == null) + return; + + if (!IsLoaded) + return; + + var leftHeight = leftSidebarGroups.Bounds.Height - 28.0 * 5; + var localBranchRows = vm.IsLocalBranchGroupExpanded ? localBranchTree.Rows.Count : 0; + var remoteBranchRows = vm.IsRemoteGroupExpanded ? remoteBranchTree.Rows.Count : 0; + var desiredBranches = (localBranchRows + remoteBranchRows) * 24.0; + var desiredTag = vm.IsTagGroupExpanded ? tagsList.RowHeight * vm.VisibleTags.Count : 0; + var desiredSubmodule = vm.IsSubmoduleGroupExpanded ? submoduleList.RowHeight * vm.Submodules.Count : 0; + var desiredWorktree = vm.IsWorktreeGroupExpanded ? worktreeList.RowHeight * vm.Worktrees.Count : 0; + var desiredOthers = desiredTag + desiredSubmodule + desiredWorktree; + var hasOverflow = (desiredBranches + desiredOthers > leftHeight); + + if (vm.IsTagGroupExpanded) + { + var height = desiredTag; + if (hasOverflow) + { + var test = leftHeight - desiredBranches - desiredSubmodule - desiredWorktree; + if (test < 0) + height = Math.Min(200, height); + else + height = Math.Max(200, test); + } + + leftHeight -= height; + tagsList.Height = height; + hasOverflow = (desiredBranches + desiredSubmodule + desiredWorktree) > leftHeight; + } + + if (vm.IsSubmoduleGroupExpanded) + { + var height = desiredSubmodule; + if (hasOverflow) + { + var test = leftHeight - desiredBranches - desiredWorktree; + if (test < 0) + height = Math.Min(200, height); + else + height = Math.Max(200, test); + } + + leftHeight -= height; + submoduleList.Height = height; + hasOverflow = (desiredBranches + desiredWorktree) > leftHeight; + } + + if (vm.IsWorktreeGroupExpanded) + { + var height = desiredWorktree; + if (hasOverflow) + { + var test = leftHeight - desiredBranches; + if (test < 0) + height = Math.Min(200, height); + else + height = Math.Max(200, test); + } + + leftHeight -= height; + worktreeList.Height = height; + } + + if (desiredBranches > leftHeight) + { + var local = localBranchRows * 24.0; + var remote = remoteBranchRows * 24.0; + var half = leftHeight / 2; + if (vm.IsLocalBranchGroupExpanded) + { + if (vm.IsRemoteGroupExpanded) + { + if (local < half) + { + localBranchTree.Height = local; + remoteBranchTree.Height = leftHeight - local; + } + else if (remote < half) + { + remoteBranchTree.Height = remote; + localBranchTree.Height = leftHeight - remote; + } + else + { + localBranchTree.Height = half; + remoteBranchTree.Height = half; + } + } + else + { + localBranchTree.Height = leftHeight; + } + } + else if (vm.IsRemoteGroupExpanded) + { + remoteBranchTree.Height = leftHeight; + } + } + else + { + if (vm.IsLocalBranchGroupExpanded) + { + var height = localBranchRows * 24; + localBranchTree.Height = height; + } + + if (vm.IsRemoteGroupExpanded) + { + var height = remoteBranchRows * 24; + remoteBranchTree.Height = height; + } + } + + leftSidebarGroups.InvalidateMeasure(); + } } }