From b9b0899d4894fcf8a68b6744438971e33040b3a6 Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 13 May 2021 10:55:25 +0800 Subject: [PATCH] feature: goto diff view by double click changed file in commit information panel --- src/Views/Controls/Tree.cs | 10 ++++++ src/Views/Controls/TreeItem.cs | 1 - src/Views/Widgets/CommitChanges.xaml.cs | 41 +++++++++++++++++++++++-- src/Views/Widgets/CommitDetail.xaml | 3 +- src/Views/Widgets/CommitDetail.xaml.cs | 15 ++++++++- src/Views/Widgets/Histories.xaml.cs | 2 +- 6 files changed, 66 insertions(+), 6 deletions(-) diff --git a/src/Views/Controls/Tree.cs b/src/Views/Controls/Tree.cs index e88dee37..4d4a257a 100644 --- a/src/Views/Controls/Tree.cs +++ b/src/Views/Controls/Tree.cs @@ -75,6 +75,16 @@ namespace SourceGit.Views.Controls { RaiseEvent(new RoutedEventArgs(SelectionChangedEvent)); } + public void Select(object dataContext) { + if (Selected.Count == 1 && Selected[0] == dataContext) return; + + var item = FindItemByDataContext(this, dataContext); + if (item != null) { + AddSelected(item, true); + item.BringIntoView(); + } + } + protected override DependencyObject GetContainerForItemOverride() { return new TreeItem(0, Indent); } diff --git a/src/Views/Controls/TreeItem.cs b/src/Views/Controls/TreeItem.cs index fb1fc19f..feecec9a 100644 --- a/src/Views/Controls/TreeItem.cs +++ b/src/Views/Controls/TreeItem.cs @@ -27,7 +27,6 @@ namespace SourceGit.Views.Controls { this.indent = indent; Padding = new Thickness(indent * depth, 0, 0, 0); - RequestBringIntoView += (o, e) => e.Handled = true; } protected override DependencyObject GetContainerForItemOverride() { diff --git a/src/Views/Widgets/CommitChanges.xaml.cs b/src/Views/Widgets/CommitChanges.xaml.cs index baca6f3c..8bb12247 100644 --- a/src/Views/Widgets/CommitChanges.xaml.cs +++ b/src/Views/Widgets/CommitChanges.xaml.cs @@ -16,6 +16,7 @@ namespace SourceGit.Views.Widgets { private List range = null; private List cachedChanges = new List(); private string filter = null; + private bool isSelecting = false; public class ChangeNode { public string Path { get; set; } = ""; @@ -37,7 +38,28 @@ namespace SourceGit.Views.Widgets { UpdateVisible(); } - public void UpdateVisible() { + public void Select(Models.Change change) { + isSelecting = true; + + switch (modeSwitcher.Mode) { + case Models.Change.DisplayMode.Tree: + var node = FindNodeByChange(modeTree.ItemsSource as List, change); + modeTree.Select(node); + break; + case Models.Change.DisplayMode.List: + modeList.SelectedItem = change; + modeList.ScrollIntoView(change); + break; + case Models.Change.DisplayMode.Grid: + modeGrid.SelectedItem = change; + modeGrid.ScrollIntoView(change); + break; + } + + isSelecting = false; + } + + private void UpdateVisible() { Task.Run(() => { // 筛选出可见的列表 List visible; @@ -115,6 +137,21 @@ namespace SourceGit.Views.Widgets { }); } + private ChangeNode FindNodeByChange(List nodes, Models.Change change) { + if (nodes == null || nodes.Count == 0) return null; + + foreach (var node in nodes) { + if (node.IsFolder) { + var found = FindNodeByChange(node.Children, change); + if (found != null) return found; + } else if (node.Change == change) { + return node; + } + } + + return null; + } + private void SortFileNodes(List nodes) { nodes.Sort((l, r) => { if (l.IsFolder == r.IsFolder) { @@ -237,7 +274,7 @@ namespace SourceGit.Views.Widgets { } private void OnRequestBringIntoView(object sender, RequestBringIntoViewEventArgs e) { - e.Handled = true; + if (!isSelecting) e.Handled = true; } private void OnTreeSelectionChanged(object sender, RoutedEventArgs e) { diff --git a/src/Views/Widgets/CommitDetail.xaml b/src/Views/Widgets/CommitDetail.xaml index e8279fe8..d84883d7 100644 --- a/src/Views/Widgets/CommitDetail.xaml +++ b/src/Views/Widgets/CommitDetail.xaml @@ -8,7 +8,7 @@ xmlns:widgets="clr-namespace:SourceGit.Views.Widgets" mc:Ignorable="d" d:DesignHeight="450" d:DesignWidth="800"> - + @@ -203,6 +203,7 @@ diff --git a/src/Views/Widgets/CommitDetail.xaml.cs b/src/Views/Widgets/CommitDetail.xaml.cs index cbce62e8..7d9385fe 100644 --- a/src/Views/Widgets/CommitDetail.xaml.cs +++ b/src/Views/Widgets/CommitDetail.xaml.cs @@ -4,6 +4,7 @@ using System.IO; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; +using System.Windows.Input; using System.Windows.Navigation; namespace SourceGit.Views.Widgets { @@ -88,11 +89,23 @@ namespace SourceGit.Views.Widgets { } #endregion - #region INFORMATION + #region EVENTS private void OnNavigateParent(object sender, RequestNavigateEventArgs e) { Models.Watcher.Get(repo)?.NavigateTo(e.Uri.OriginalString); } + private void OnChangeListMouseDoubleClick(object sender, MouseButtonEventArgs e) { + var row = sender as DataGridRow; + if (row == null) return; + + var change = row.DataContext as Models.Change; + if (change == null) return; + + body.SelectedIndex = 1; + changeContainer.Select(change); + e.Handled = true; + } + private void OnChangeListContextMenuOpening(object sender, ContextMenuEventArgs e) { var row = sender as DataGridRow; if (row == null) return; diff --git a/src/Views/Widgets/Histories.xaml.cs b/src/Views/Widgets/Histories.xaml.cs index 8466fe97..b8b68999 100644 --- a/src/Views/Widgets/Histories.xaml.cs +++ b/src/Views/Widgets/Histories.xaml.cs @@ -35,7 +35,7 @@ namespace SourceGit.Views.Widgets { foreach (var item in commitList.ItemsSource) { var c = item as Models.Commit; - if (c.SHA.Contains(commit)) { + if (c.SHA.StartsWith(commit, StringComparison.Ordinal)) { commitList.SelectedItem = c; commitList.ScrollIntoView(c); break;