feature: support compact folders display mode in changes tree view (#1674) (#1729)

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-08-14 15:18:36 +08:00
parent ce23e8cb20
commit 237d4bc7e5
13 changed files with 82 additions and 19 deletions

View File

@@ -526,6 +526,7 @@
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">APPEARANCE</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">Default Font</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">Editor Tab Width</x:String>
<x:String x:Key="Text.Preferences.Appearance.EnableCompactFolders" xml:space="preserve">Enable compact folders in changes tree</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">Font Size</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">Default</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">Editor</x:String>

View File

@@ -530,6 +530,7 @@
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">外观配置</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">缺省字体</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">编辑器制表符宽度</x:String>
<x:String x:Key="Text.Preferences.Appearance.EnableCompactFolders" xml:space="preserve">在变更列表树中启用紧凑文件夹模式</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">字体大小</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">默认</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">代码编辑器</x:String>

View File

@@ -530,6 +530,7 @@
<x:String x:Key="Text.Preferences.Appearance" xml:space="preserve">外觀設定</x:String>
<x:String x:Key="Text.Preferences.Appearance.DefaultFont" xml:space="preserve">預設字型</x:String>
<x:String x:Key="Text.Preferences.Appearance.EditorTabWidth" xml:space="preserve">編輯器 Tab 寬度</x:String>
<x:String x:Key="Text.Preferences.Appearance.EnableCompactFolders" xml:space="preserve">在變更樹中啟用精簡文件夾顯示模式</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize" xml:space="preserve">字型大小</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Default" xml:space="preserve">預設</x:String>
<x:String x:Key="Text.Preferences.Appearance.FontSize.Editor" xml:space="preserve">程式碼</x:String>

View File

@@ -1,5 +1,5 @@
using System.Collections.Generic;
using System.IO;
using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
@@ -7,6 +7,7 @@ namespace SourceGit.ViewModels
public class ChangeTreeNode : ObservableObject
{
public string FullPath { get; set; }
public string DisplayName { get; set; }
public int Depth { get; private set; } = 0;
public Models.Change Change { get; set; } = null;
public List<ChangeTreeNode> Children { get; set; } = new List<ChangeTreeNode>();
@@ -32,22 +33,22 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _isExpanded, value);
}
public ChangeTreeNode(Models.Change c, int depth)
public ChangeTreeNode(Models.Change c)
{
FullPath = c.Path;
Depth = depth;
DisplayName = Path.GetFileName(c.Path);
Change = c;
IsExpanded = false;
}
public ChangeTreeNode(string path, bool isExpanded, int depth)
public ChangeTreeNode(string path, bool isExpanded)
{
FullPath = path;
Depth = depth;
DisplayName = Path.GetFileName(path);
IsExpanded = isExpanded;
}
public static List<ChangeTreeNode> Build(IList<Models.Change> changes, HashSet<string> folded)
public static List<ChangeTreeNode> Build(IList<Models.Change> changes, HashSet<string> folded, bool compactFolders)
{
var nodes = new List<ChangeTreeNode>();
var folders = new Dictionary<string, ChangeTreeNode>();
@@ -57,12 +58,11 @@ namespace SourceGit.ViewModels
var sepIdx = c.Path.IndexOf('/');
if (sepIdx == -1)
{
nodes.Add(new ChangeTreeNode(c, 0));
nodes.Add(new ChangeTreeNode(c));
}
else
{
ChangeTreeNode lastFolder = null;
int depth = 0;
while (sepIdx != -1)
{
@@ -73,27 +73,32 @@ namespace SourceGit.ViewModels
}
else if (lastFolder == null)
{
lastFolder = new ChangeTreeNode(folder, !folded.Contains(folder), depth);
lastFolder = new ChangeTreeNode(folder, !folded.Contains(folder));
folders.Add(folder, lastFolder);
InsertFolder(nodes, lastFolder);
}
else
{
var cur = new ChangeTreeNode(folder, !folded.Contains(folder), depth);
var cur = new ChangeTreeNode(folder, !folded.Contains(folder));
folders.Add(folder, cur);
InsertFolder(lastFolder.Children, cur);
lastFolder = cur;
}
depth++;
sepIdx = c.Path.IndexOf('/', sepIdx + 1);
}
lastFolder?.Children.Add(new ChangeTreeNode(c, depth));
lastFolder?.Children.Add(new ChangeTreeNode(c));
}
}
Sort(nodes);
if (compactFolders)
{
foreach (var node in nodes)
Compact(node);
}
SortAndSetDepth(nodes, 0);
folders.Clear();
return nodes;
@@ -113,12 +118,37 @@ namespace SourceGit.ViewModels
collection.Add(subFolder);
}
private static void Sort(List<ChangeTreeNode> nodes)
private static void Compact(ChangeTreeNode node)
{
var childrenCount = node.Children.Count;
if (childrenCount == 0)
return;
if (childrenCount > 1)
{
foreach (var c in node.Children)
Compact(c);
return;
}
var child = node.Children[0];
if (child.Change != null)
return;
node.FullPath = $"{node.FullPath}/{child.DisplayName}";
node.DisplayName = $"{node.DisplayName} / {child.DisplayName}";
node.IsExpanded = child.IsExpanded;
node.Children = child.Children;
Compact(node);
}
private static void SortAndSetDepth(List<ChangeTreeNode> nodes, int depth)
{
foreach (var node in nodes)
{
node.Depth = depth;
if (node.IsFolder)
Sort(node.Children);
SortAndSetDepth(node.Children, depth + 1);
}
nodes.Sort((l, r) =>

View File

@@ -267,6 +267,12 @@ namespace SourceGit.ViewModels
set => SetProperty(ref _lfsImageActiveIdx, value);
}
public bool EnableCompactFoldersInChangesTree
{
get => _enableCompactFoldersInChangesTree;
set => SetProperty(ref _enableCompactFoldersInChangesTree, value);
}
public Models.ChangeViewMode UnstagedChangeViewMode
{
get => _unstagedChangeViewMode;
@@ -718,6 +724,7 @@ namespace SourceGit.ViewModels
private bool _useFullTextDiff = false;
private bool _useBlockNavigationInDiffView = false;
private int _lfsImageActiveIdx = 0;
private bool _enableCompactFoldersInChangesTree = false;
private Models.ChangeViewMode _unstagedChangeViewMode = Models.ChangeViewMode.List;
private Models.ChangeViewMode _stagedChangeViewMode = Models.ChangeViewMode.List;

View File

@@ -125,6 +125,7 @@
<!-- Changes -->
<Border Grid.Row="1" Margin="0,4,0,0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
<v:ChangeCollectionView ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=CommitChangeViewMode}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding VisibleChanges}"
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
ContextRequested="OnChangeContextRequested"/>

View File

@@ -65,8 +65,8 @@
IsVisible="{Binding !IsFolder}"/>
<StackPanel Grid.Column="2" Orientation="Horizontal" Margin="4,0,0,0">
<TextBlock Classes="primary" Text="{Binding ConflictMarker}" Foreground="DarkOrange" FontWeight="Bold" Margin="0,0,4,0" IsVisible="{Binding ShowConflictMarker}"/>
<TextBlock Classes="primary" Text="{Binding FullPath, Converter={x:Static c:PathConverters.PureFileName}}"/>
<TextBlock Classes="primary" Text="{Binding ConflictMarker, Mode=OneWay}" Foreground="DarkOrange" FontWeight="Bold" Margin="0,0,4,0" IsVisible="{Binding ShowConflictMarker}"/>
<TextBlock Classes="primary" Text="{Binding DisplayName, Mode=OneWay}"/>
</StackPanel>
</Grid>
</DataTemplate>

View File

@@ -109,6 +109,15 @@ namespace SourceGit.Views
set => SetValue(ViewModeProperty, value);
}
public static readonly StyledProperty<bool> EnableCompactFoldersProperty =
AvaloniaProperty.Register<ChangeCollectionView, bool>(nameof(EnableCompactFolders), false);
public bool EnableCompactFolders
{
get => GetValue(EnableCompactFoldersProperty);
set => SetValue(EnableCompactFoldersProperty, value);
}
public static readonly StyledProperty<List<Models.Change>> ChangesProperty =
AvaloniaProperty.Register<ChangeCollectionView, List<Models.Change>>(nameof(Changes));
@@ -254,6 +263,9 @@ namespace SourceGit.Views
UpdateDataSource(false);
else if (change.Property == SelectedChangesProperty)
UpdateSelection();
if (change.Property == EnableCompactFoldersProperty && ViewMode == Models.ChangeViewMode.Tree)
UpdateDataSource(true);
}
private void OnRowDataContextChanged(object sender, EventArgs e)
@@ -384,7 +396,7 @@ namespace SourceGit.Views
}
var tree = new ViewModels.ChangeCollectionAsTree();
tree.Tree = ViewModels.ChangeTreeNode.Build(changes, oldFolded);
tree.Tree = ViewModels.ChangeTreeNode.Build(changes, oldFolded, EnableCompactFolders);
var rows = new List<ViewModels.ChangeTreeNode>();
MakeTreeRows(rows, tree.Tree);

View File

@@ -48,6 +48,7 @@
<Border Grid.Row="1" Margin="0,4,0,0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
<v:ChangeCollectionView SelectionMode="Single"
ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=CommitChangeViewMode}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding VisibleChanges}"
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
ContextRequested="OnChangeContextRequested"/>

View File

@@ -159,7 +159,7 @@
<TabItem.Header>
<TextBlock Classes="tab_header" Text="{DynamicResource Text.Preferences.Appearance}"/>
</TabItem.Header>
<Grid Margin="8" RowDefinitions="32,32,32,32,32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<Grid Margin="8" RowDefinitions="32,32,32,32,32,32,32,32,32,Auto" ColumnDefinitions="Auto,*">
<TextBlock Grid.Row="0" Grid.Column="0"
Text="{DynamicResource Text.Preferences.Appearance.Theme}"
HorizontalAlignment="Right"
@@ -266,6 +266,11 @@
IsChecked="{Binding UseFixedTabWidth, Mode=TwoWay}"/>
<CheckBox Grid.Row="8" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.EnableCompactFolders}"
IsChecked="{Binding EnableCompactFoldersInChangesTree, Mode=TwoWay}"/>
<CheckBox Grid.Row="9" Grid.Column="1"
Height="32"
Content="{DynamicResource Text.Preferences.Appearance.UseNativeWindowFrame}"
IsChecked="{Binding UseSystemWindowFrame, Mode=OneTime}"

View File

@@ -97,6 +97,7 @@
<!-- Changes -->
<Border Grid.Row="1" Margin="0,4,0,0" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1" Background="{DynamicResource Brush.Contents}">
<v:ChangeCollectionView ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=CommitChangeViewMode}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding VisibleChanges}"
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
ContextRequested="OnChangeContextRequested"/>

View File

@@ -128,6 +128,7 @@
<!-- View Changes -->
<Border Grid.Row="4" Background="{DynamicResource Brush.Contents}">
<v:ChangeCollectionView ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StashChangeViewMode, Mode=OneWay}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding Changes}"
SelectedChanges="{Binding SelectedChanges, Mode=TwoWay}"
ContextRequested="OnChangeContextRequested"/>

View File

@@ -129,6 +129,7 @@
SelectionMode="Multiple"
Background="{DynamicResource Brush.Contents}"
ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=UnstagedChangeViewMode}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding VisibleUnstaged}"
SelectedChanges="{Binding SelectedUnstaged, Mode=TwoWay}"
ContextRequested="OnUnstagedContextRequested"
@@ -183,6 +184,7 @@
SelectionMode="Multiple"
Background="{DynamicResource Brush.Contents}"
ViewMode="{Binding Source={x:Static vm:Preferences.Instance}, Path=StagedChangeViewMode}"
EnableCompactFolders="{Binding Source={x:Static vm:Preferences.Instance}, Path=EnableCompactFoldersInChangesTree}"
Changes="{Binding VisibleStaged}"
SelectedChanges="{Binding SelectedStaged, Mode=TwoWay}"
ContextRequested="OnStagedContextRequested"