feature<RevisionFiles>: enable to search file in revision files

This commit is contained in:
leo 2021-11-15 12:20:33 +08:00
parent b452456d9d
commit acc9840830
2 changed files with 159 additions and 94 deletions

View file

@ -21,8 +21,45 @@
<ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Border Grid.Column="0" Background="{DynamicResource Brush.Contents}" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1"> <Grid Grid.Column="0">
<controls:Tree x:Name="treeFiles" SelectionChanged="OnFilesSelectionChanged" ContextMenuOpening="OnFilesContextMenuOpening"> <Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Border
Grid.Column="0" Grid.ColumnSpan="2"
BorderBrush="{DynamicResource Brush.Border2}"
BorderThickness="1"/>
<Path
Grid.Column="0"
Width="14" Height="14"
Fill="{DynamicResource Brush.FG2}"
Data="{StaticResource Icon.Search}"
IsHitTestVisible="False"/>
<controls:TextEdit
Grid.Column="1"
Height="24"
Margin="0"
Placeholder="{DynamicResource Text.CommitViewer.Changes.Search}"
BorderThickness="0"
TextChanged="OnSearchFilterChanged"/>
</Grid>
<Border Grid.Row="1" Margin="0,4,0,0" Background="{DynamicResource Brush.Contents}" BorderBrush="{DynamicResource Brush.Border2}" BorderThickness="1">
<controls:Tree x:Name="treeFiles" SelectionChanged="OnFilesSelectionChanged">
<controls:Tree.ItemContainerStyle>
<Style TargetType="{x:Type controls:TreeItem}" BasedOn="{StaticResource Style.TreeItem}">
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}"/>
<EventSetter Event="ContextMenuOpening" Handler="OnFilesContextMenuOpening"/>
</Style>
</controls:Tree.ItemContainerStyle>
<controls:Tree.ItemTemplate> <controls:Tree.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Children}"> <HierarchicalDataTemplate ItemsSource="{Binding Children}">
<StackPanel Orientation="Horizontal" Height="24"> <StackPanel Orientation="Horizontal" Height="24">
@ -54,6 +91,7 @@
</controls:Tree.ItemTemplate> </controls:Tree.ItemTemplate>
</controls:Tree> </controls:Tree>
</Border> </Border>
</Grid>
<GridSplitter Grid.Column="1" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Transparent"/> <GridSplitter Grid.Column="1" Width="1" HorizontalAlignment="Center" VerticalAlignment="Stretch" Background="Transparent"/>

View file

@ -18,6 +18,8 @@ namespace SourceGit.Views.Widgets {
private string repo = null; private string repo = null;
private string sha = null; private string sha = null;
private bool isLFSEnabled = false; private bool isLFSEnabled = false;
private List<Models.Object> cached = new List<Models.Object>();
private string filter = null;
/// <summary> /// <summary>
/// 文件列表树节点 /// 文件列表树节点
@ -26,6 +28,7 @@ namespace SourceGit.Views.Widgets {
public Models.ObjectType Type { get; set; } = Models.ObjectType.None; public Models.ObjectType Type { get; set; } = Models.ObjectType.None;
public string Path { get; set; } = ""; public string Path { get; set; } = "";
public string SHA { get; set; } = null; public string SHA { get; set; } = null;
public bool IsExpanded { get; set; } = false;
public bool IsFolder => Type == Models.ObjectType.None; public bool IsFolder => Type == Models.ObjectType.None;
public List<FileNode> Children { get; set; } = new List<FileNode>(); public List<FileNode> Children { get; set; } = new List<FileNode>();
} }
@ -44,10 +47,32 @@ namespace SourceGit.Views.Widgets {
var objects = cmd.Result(); var objects = cmd.Result();
if (cmd.Ctx.IsCancelRequested) return; if (cmd.Ctx.IsCancelRequested) return;
cached = objects;
ShowVisibles();
});
}
public void Cleanup() {
treeFiles.ItemsSource = new List<FileNode>();
cached = new List<Models.Object>();
}
private void ShowVisibles() {
var nodes = new List<FileNode>(); var nodes = new List<FileNode>();
var folders = new Dictionary<string, FileNode>(); var folders = new Dictionary<string, FileNode>();
var visibles = new List<Models.Object>();
foreach (var obj in objects) { if (string.IsNullOrEmpty(filter)) {
visibles.AddRange(cached);
} else {
foreach (var obj in cached) {
if (obj.Path.ToUpper().Contains(filter)) visibles.Add(obj);
}
}
var expanded = visibles.Count <= 50;
foreach (var obj in visibles) {
var sepIdx = obj.Path.IndexOf('/'); var sepIdx = obj.Path.IndexOf('/');
if (sepIdx == -1) { if (sepIdx == -1) {
nodes.Add(new FileNode() { nodes.Add(new FileNode() {
@ -68,6 +93,7 @@ namespace SourceGit.Views.Widgets {
Type = Models.ObjectType.None, Type = Models.ObjectType.None,
Path = folder, Path = folder,
SHA = null, SHA = null,
IsExpanded = expanded,
}; };
nodes.Add(lastFolder); nodes.Add(lastFolder);
folders.Add(folder, lastFolder); folders.Add(folder, lastFolder);
@ -76,6 +102,7 @@ namespace SourceGit.Views.Widgets {
Type = Models.ObjectType.None, Type = Models.ObjectType.None,
Path = folder, Path = folder,
SHA = null, SHA = null,
IsExpanded = expanded,
}; };
folders.Add(folder, cur); folders.Add(folder, cur);
lastFolder.Children.Add(cur); lastFolder.Children.Add(cur);
@ -92,12 +119,10 @@ namespace SourceGit.Views.Widgets {
SHA = obj.SHA, SHA = obj.SHA,
}); });
} }
obj.Path = null;
} }
folders.Clear(); folders.Clear();
objects.Clear(); visibles.Clear();
SortFileNodes(nodes); SortFileNodes(nodes);
@ -105,11 +130,6 @@ namespace SourceGit.Views.Widgets {
treeFiles.ItemsSource = nodes; treeFiles.ItemsSource = nodes;
GC.Collect(); GC.Collect();
}); });
});
}
public void Cleanup() {
treeFiles.ItemsSource = new List<FileNode>();
} }
private void SortFileNodes(List<FileNode> nodes) { private void SortFileNodes(List<FileNode> nodes) {
@ -259,7 +279,7 @@ namespace SourceGit.Views.Widgets {
} }
private void OnFilesContextMenuOpening(object sender, ContextMenuEventArgs e) { private void OnFilesContextMenuOpening(object sender, ContextMenuEventArgs e) {
var item = treeFiles.FindItem(e.OriginalSource as DependencyObject); var item = sender as Controls.TreeItem;
if (item == null) return; if (item == null) return;
var node = item.DataContext as FileNode; var node = item.DataContext as FileNode;
@ -322,6 +342,13 @@ namespace SourceGit.Views.Widgets {
private void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) { private void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) {
e.Handled = true; e.Handled = true;
} }
private void OnSearchFilterChanged(object sender, TextChangedEventArgs e) {
var edit = sender as Controls.TextEdit;
filter = edit.Text.ToUpper();
Task.Run(() => ShowVisibles());
e.Handled = true;
}
#endregion #endregion
} }
} }