From b03ee19e5481f6c7f94dcf2a58c7f248cde9c5d3 Mon Sep 17 00:00:00 2001 From: leo Date: Sat, 6 Jul 2024 20:46:26 +0800 Subject: [PATCH] refactor: trigger nagivation in BranchTree --- src/Views/BranchTree.axaml.cs | 30 ++++++++++------- src/Views/Repository.axaml.cs | 61 +++++++++-------------------------- 2 files changed, 33 insertions(+), 58 deletions(-) diff --git a/src/Views/BranchTree.axaml.cs b/src/Views/BranchTree.axaml.cs index 4fe4fce5..cb0d1c58 100644 --- a/src/Views/BranchTree.axaml.cs +++ b/src/Views/BranchTree.axaml.cs @@ -17,7 +17,7 @@ namespace SourceGit.Views public class BranchTreeNodeIcon : UserControl { public static readonly StyledProperty NodeProperty = - AvaloniaProperty.Register(nameof(Node), null); + AvaloniaProperty.Register(nameof(Node)); public ViewModels.BranchTreeNode Node { @@ -26,7 +26,7 @@ namespace SourceGit.Views } public static readonly StyledProperty IsExpandedProperty = - AvaloniaProperty.Register(nameof(IsExpanded), false); + AvaloniaProperty.Register(nameof(IsExpanded)); public bool IsExpanded { @@ -36,8 +36,8 @@ namespace SourceGit.Views static BranchTreeNodeIcon() { - NodeProperty.Changed.AddClassHandler((icon, e) => icon.UpdateContent()); - IsExpandedProperty.Changed.AddClassHandler((icon, e) => icon.UpdateContent()); + NodeProperty.Changed.AddClassHandler((icon, _) => icon.UpdateContent()); + IsExpandedProperty.Changed.AddClassHandler((icon, _) => icon.UpdateContent()); } private void UpdateContent() @@ -90,7 +90,7 @@ namespace SourceGit.Views public partial class BranchTree : UserControl { public static readonly StyledProperty> NodesProperty = - AvaloniaProperty.Register>(nameof(Nodes), null); + AvaloniaProperty.Register>(nameof(Nodes)); public List Nodes { @@ -148,12 +148,8 @@ namespace SourceGit.Views } } - private void OnNodesSelectionChanged(object sender, SelectionChangedEventArgs e) + private void OnNodesSelectionChanged(object _, SelectionChangedEventArgs e) { - var selected = BranchesPresenter.SelectedItems; - if (selected == null || selected.Count == 0) - return; - foreach (var item in e.AddedItems) { if (item is ViewModels.BranchTreeNode node) @@ -165,6 +161,16 @@ namespace SourceGit.Views if (item is ViewModels.BranchTreeNode node) node.IsSelected = false; } + + var selected = BranchesPresenter.SelectedItems; + if (selected == null || selected.Count == 0) + return; + + if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) + { + var repo = DataContext as ViewModels.Repository; + repo?.NavigateToCommit(branch.Head); + } var prev = null as ViewModels.BranchTreeNode; foreach (var row in Rows) @@ -189,7 +195,7 @@ namespace SourceGit.Views RaiseEvent(new RoutedEventArgs(SelectionChangedEvent)); } - private void OnTreeContextRequested(object sender, ContextRequestedEventArgs e) + private void OnTreeContextRequested(object _1, ContextRequestedEventArgs _2) { var repo = DataContext as ViewModels.Repository; if (repo?.Settings == null) @@ -237,7 +243,7 @@ namespace SourceGit.Views } } - private void OnDoubleTappedBranchNode(object sender, TappedEventArgs e) + private void OnDoubleTappedBranchNode(object sender, TappedEventArgs _) { if (sender is Grid { DataContext: ViewModels.BranchTreeNode node }) { diff --git a/src/Views/Repository.axaml.cs b/src/Views/Repository.axaml.cs index f5612d70..e2ffc0d6 100644 --- a/src/Views/Repository.axaml.cs +++ b/src/Views/Repository.axaml.cs @@ -175,7 +175,7 @@ namespace SourceGit.Views e.Handled = true; } - private async void OpenStatistics(object sender, RoutedEventArgs e) + private async void OpenStatistics(object _, RoutedEventArgs e) { if (DataContext is ViewModels.Repository repo && TopLevel.GetTopLevel(this) is Window owner) { @@ -187,11 +187,11 @@ namespace SourceGit.Views private void OnSearchCommitPanelPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) { - if (e.Property == IsVisibleProperty && sender is Grid { IsVisible: true} grid) + if (e.Property == IsVisibleProperty && sender is Grid { IsVisible: true}) txtSearchCommitsBox.Focus(); } - private void OnSearchKeyDown(object sender, KeyEventArgs e) + private void OnSearchKeyDown(object _, KeyEventArgs e) { if (e.Key == Key.Enter) { @@ -204,55 +204,33 @@ namespace SourceGit.Views private void OnSearchResultDataGridSelectionChanged(object sender, SelectionChangedEventArgs e) { - if (sender is DataGrid { SelectedItem: not null } grid && DataContext is ViewModels.Repository repo) + if (sender is DataGrid { SelectedItem: Models.Commit commit } && DataContext is ViewModels.Repository repo) { - var commit = grid.SelectedItem as Models.Commit; repo.NavigateToCommit(commit.SHA); } e.Handled = true; } - private void OnLocalBranchTreeSelectionChanged(object sender, RoutedEventArgs e) + private void OnLocalBranchTreeSelectionChanged(object _1, RoutedEventArgs _2) { - if (sender is BranchTree tree && DataContext is ViewModels.Repository repo) - { - var selected = tree.BranchesPresenter.SelectedItems; - if (selected == null || selected.Count == 0) - return; - - remoteBranchTree.UnselectAll(); - tagsList.SelectedItem = null; - - if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) - repo.NavigateToCommit(branch.Head); - } + remoteBranchTree.UnselectAll(); + tagsList.SelectedItem = null; } - private void OnRemoteBranchTreeSelectionChanged(object sender, RoutedEventArgs e) + private void OnRemoteBranchTreeSelectionChanged(object _1, RoutedEventArgs _2) { - if (sender is BranchTree tree && DataContext is ViewModels.Repository repo) - { - var selected = tree.BranchesPresenter.SelectedItems; - if (selected == null || selected.Count == 0) - return; - - localBranchTree.UnselectAll(); - tagsList.SelectedItem = null; - - if (selected.Count == 1 && selected[0] is ViewModels.BranchTreeNode { Backend: Models.Branch branch }) - repo.NavigateToCommit(branch.Head); - } + localBranchTree.UnselectAll(); + tagsList.SelectedItem = null; } - private void OnTagDataGridSelectionChanged(object sender, SelectionChangedEventArgs e) + private void OnTagDataGridSelectionChanged(object sender, SelectionChangedEventArgs _) { - if (sender is DataGrid { SelectedItem: not null } grid) + if (sender is DataGrid { SelectedItem: Models.Tag tag }) { localBranchTree.UnselectAll(); remoteBranchTree.UnselectAll(); - var tag = grid.SelectedItem as Models.Tag; if (DataContext is ViewModels.Repository repo) repo.NavigateToCommit(tag.SHA); } @@ -272,18 +250,9 @@ namespace SourceGit.Views private void OnToggleTagFilter(object sender, RoutedEventArgs e) { - if (sender is ToggleButton toggle) + if (sender is ToggleButton { DataContext: Models.Tag tag } toggle && DataContext is ViewModels.Repository repo) { - var filter = string.Empty; - if (toggle.DataContext is Models.Tag tag) - { - filter = tag.Name; - } - - if (!string.IsNullOrEmpty(filter) && DataContext is ViewModels.Repository repo) - { - repo.UpdateFilter(filter, toggle.IsChecked == true); - } + repo.UpdateFilter(tag.Name, toggle.IsChecked == true); } e.Handled = true; @@ -335,7 +304,7 @@ namespace SourceGit.Views e.Handled = true; } - private void OnLeftSidebarDataGridPropertyChanged(object sender, AvaloniaPropertyChangedEventArgs e) + private void OnLeftSidebarDataGridPropertyChanged(object _, AvaloniaPropertyChangedEventArgs e) { if (e.Property == DataGrid.ItemsSourceProperty || e.Property == DataGrid.IsVisibleProperty) {