fix: bounds not updated after left side bar layout updated

This commit is contained in:
leo 2024-07-06 23:50:54 +08:00
parent b03ee19e54
commit 294f856150
4 changed files with 174 additions and 153 deletions

View file

@ -10,13 +10,11 @@
x:Name="ThisControl"> x:Name="ThisControl">
<DataGrid x:Name="BranchesPresenter" <DataGrid x:Name="BranchesPresenter"
ItemsSource="{Binding #ThisControl.Rows}" ItemsSource="{Binding #ThisControl.Rows}"
Height="{Binding #ThisControl.Height}"
Background="Transparent" Background="Transparent"
RowHeight="24" RowHeight="24"
CanUserReorderColumns="False" CanUserReorderColumns="False"
CanUserResizeColumns="False" CanUserResizeColumns="False"
CanUserSortColumns="False" CanUserSortColumns="False"
HorizontalAlignment="Stretch"
HorizontalScrollBarVisibility="Disabled" HorizontalScrollBarVisibility="Disabled"
VerticalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
HeadersVisibility="None" HeadersVisibility="None"
@ -48,27 +46,27 @@
<Setter Property="Opacity" Value=".8"/> <Setter Property="Opacity" Value=".8"/>
</Style> </Style>
</DataGrid.Styles> </DataGrid.Styles>
<DataGrid.Columns> <DataGrid.Columns>
<DataGridTemplateColumn Width="*"> <DataGridTemplateColumn Width="*">
<DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellTemplate>
<DataTemplate x:DataType="vm:BranchTreeNode"> <DataTemplate x:DataType="vm:BranchTreeNode">
<Grid Height="24" <Grid Height="24"
Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}" Margin="{Binding Depth, Converter={x:Static c:IntConverters.ToTreeMargin}}"
ColumnDefinitions="16,20,*,Auto,Auto" ColumnDefinitions="16,20,*,Auto,Auto"
Background="Transparent" Background="Transparent"
DoubleTapped="OnDoubleTappedBranchNode" DoubleTapped="OnDoubleTappedBranchNode"
ToolTip.Tip="{Binding Tooltip}"> ToolTip.Tip="{Binding Tooltip}">
<!-- Tree Expander --> <!-- Tree Expander -->
<ToggleButton Grid.Column="0" <ToggleButton Grid.Column="0"
Classes="tree_expander" Classes="tree_expander"
Focusable="False" Focusable="False"
HorizontalAlignment="Center" HorizontalAlignment="Center"
IsChecked="{Binding IsExpanded}" IsChecked="{Binding IsExpanded}"
IsHitTestVisible="False" IsHitTestVisible="False"
IsVisible="{Binding !IsBranch}"/> IsVisible="{Binding !IsBranch}"/>
<!-- Icon --> <!-- Icon -->
<v:BranchTreeNodeIcon Grid.Column="1" <v:BranchTreeNodeIcon Grid.Column="1"
Node="{Binding}" Node="{Binding}"
@ -79,14 +77,14 @@
Text="{Binding Name}" Text="{Binding Name}"
Classes="monospace" Classes="monospace"
FontWeight="{Binding NameFontWeight}"/> FontWeight="{Binding NameFontWeight}"/>
<!-- Tracking status --> <!-- Tracking status -->
<Border Grid.Column="3" <Border Grid.Column="3"
Margin="8,0" Margin="8,0"
Height="18" Height="18"
CornerRadius="9" CornerRadius="9"
VerticalAlignment="Center" VerticalAlignment="Center"
Background="{DynamicResource Brush.Badge}" Background="{DynamicResource Brush.Badge}"
IsVisible="{Binding IsUpstreamTrackStatusVisible}"> IsVisible="{Binding IsUpstreamTrackStatusVisible}">
<TextBlock Classes="monospace" FontSize="10" HorizontalAlignment="Center" Margin="9,0" Text="{Binding UpstreamTrackStatus}" Foreground="{DynamicResource Brush.BadgeFG}"/> <TextBlock Classes="monospace" FontSize="10" HorizontalAlignment="Center" Margin="9,0" Text="{Binding UpstreamTrackStatus}" Foreground="{DynamicResource Brush.BadgeFG}"/>
</Border> </Border>

View file

@ -113,6 +113,15 @@ namespace SourceGit.Views
remove { RemoveHandler(SelectionChangedEvent, value); } remove { RemoveHandler(SelectionChangedEvent, value); }
} }
public static readonly RoutedEvent<RoutedEventArgs> RowsChangedEvent =
RoutedEvent.Register<BranchTree, RoutedEventArgs>(nameof(RowsChanged), RoutingStrategies.Tunnel | RoutingStrategies.Bubble);
public event EventHandler<RoutedEventArgs> RowsChanged
{
add { AddHandler(RowsChangedEvent, value); }
remove { RemoveHandler(RowsChangedEvent, value); }
}
public BranchTree() public BranchTree()
{ {
InitializeComponent(); InitializeComponent();
@ -123,6 +132,14 @@ namespace SourceGit.Views
BranchesPresenter.SelectedItem = null; 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) protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{ {
base.OnPropertyChanged(change); base.OnPropertyChanged(change);
@ -138,18 +155,20 @@ namespace SourceGit.Views
Rows.AddRange(rows); Rows.AddRange(rows);
} }
var repo = this.FindAncestorOfType<Repository>(); RaiseEvent(new RoutedEventArgs(RowsChangedEvent));
repo?.UpdateLeftSidebarLayout();
} }
else if (change.Property == IsVisibleProperty) else if (change.Property == IsVisibleProperty)
{ {
var repo = this.FindAncestorOfType<Repository>(); RaiseEvent(new RoutedEventArgs(RowsChangedEvent));
repo?.UpdateLeftSidebarLayout();
} }
} }
private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e) private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e)
{ {
var repo = DataContext as ViewModels.Repository;
if (repo?.Settings == null)
return;
foreach (var item in e.AddedItems) foreach (var item in e.AddedItems)
{ {
if (item is ViewModels.BranchTreeNode node) if (item is ViewModels.BranchTreeNode node)
@ -167,10 +186,7 @@ namespace SourceGit.Views
return; return;
if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch })
{ repo.NavigateToCommit(branch.Head);
var repo = DataContext as ViewModels.Repository;
repo?.NavigateToCommit(branch.Head);
}
var prev = null as ViewModels.BranchTreeNode; var prev = null as ViewModels.BranchTreeNode;
foreach (var row in Rows) foreach (var row in Rows)
@ -285,8 +301,7 @@ namespace SourceGit.Views
rows.RemoveRange(idx + 1, removeCount); rows.RemoveRange(idx + 1, removeCount);
} }
var repo = this.FindAncestorOfType<Repository>(); RaiseEvent(new RoutedEventArgs(RowsChangedEvent));
repo?.UpdateLeftSidebarLayout();
} }
} }
} }

View file

@ -241,7 +241,8 @@
Margin="8,0,4,0" Margin="8,0,4,0"
Nodes="{Binding LocalBranchTrees}" Nodes="{Binding LocalBranchTrees}"
IsVisible="{Binding IsLocalBranchGroupExpanded}" IsVisible="{Binding IsLocalBranchGroupExpanded}"
SelectionChanged="OnLocalBranchTreeSelectionChanged"/> SelectionChanged="OnLocalBranchTreeSelectionChanged"
RowsChanged="OnBranchTreeRowsChanged"/>
<!-- Remotes --> <!-- Remotes -->
<ToggleButton Grid.Row="2" Classes="group_expander" IsChecked="{Binding IsRemoteGroupExpanded, Mode=TwoWay}"> <ToggleButton Grid.Row="2" Classes="group_expander" IsChecked="{Binding IsRemoteGroupExpanded, Mode=TwoWay}">
@ -257,7 +258,8 @@
Margin="8,0,4,0" Margin="8,0,4,0"
Nodes="{Binding RemoteBranchTrees}" Nodes="{Binding RemoteBranchTrees}"
IsVisible="{Binding IsRemoteGroupExpanded}" IsVisible="{Binding IsRemoteGroupExpanded}"
SelectionChanged="OnRemoteBranchTreeSelectionChanged"/> SelectionChanged="OnRemoteBranchTreeSelectionChanged"
RowsChanged="OnBranchTreeRowsChanged"/>
<!-- Tags --> <!-- Tags -->
<ToggleButton Grid.Row="4" Classes="group_expander" IsChecked="{Binding IsTagGroupExpanded, Mode=TwoWay}"> <ToggleButton Grid.Row="4" Classes="group_expander" IsChecked="{Binding IsTagGroupExpanded, Mode=TwoWay}">

View file

@ -15,132 +15,10 @@ namespace SourceGit.Views
InitializeComponent(); 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) protected override void OnLoaded(RoutedEventArgs e)
{ {
base.OnLoaded(e); base.OnLoaded(e);
UpdateLeftSidebarLayout();
if (DataContext is ViewModels.Repository { IsSearching: false })
UpdateLeftSidebarLayout();
} }
private void OpenWithExternalTools(object sender, RoutedEventArgs e) private void OpenWithExternalTools(object sender, RoutedEventArgs e)
@ -211,7 +89,13 @@ namespace SourceGit.Views
e.Handled = true; e.Handled = true;
} }
private void OnBranchTreeRowsChanged(object _, RoutedEventArgs e)
{
UpdateLeftSidebarLayout();
e.Handled = true;
}
private void OnLocalBranchTreeSelectionChanged(object _1, RoutedEventArgs _2) private void OnLocalBranchTreeSelectionChanged(object _1, RoutedEventArgs _2)
{ {
remoteBranchTree.UnselectAll(); remoteBranchTree.UnselectAll();
@ -311,5 +195,127 @@ namespace SourceGit.Views
UpdateLeftSidebarLayout(); 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();
}
} }
} }