From 5514c56a298fa7c572f50126a563e558ddf96bdf Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 6 Jun 2024 18:09:35 +0800 Subject: [PATCH] refactor: more efficient way to update the visibility of tab splitters --- src/Converters/LauncherPageConverters.cs | 43 ------------------------ src/ViewModels/Launcher.cs | 40 +++++++++------------- src/ViewModels/LauncherPage.cs | 9 +++-- src/Views/Launcher.axaml | 20 ++++------- 4 files changed, 29 insertions(+), 83 deletions(-) delete mode 100644 src/Converters/LauncherPageConverters.cs diff --git a/src/Converters/LauncherPageConverters.cs b/src/Converters/LauncherPageConverters.cs deleted file mode 100644 index 8a322ad6..00000000 --- a/src/Converters/LauncherPageConverters.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System.Collections.Generic; - -using Avalonia.Collections; -using Avalonia.Data.Converters; - -namespace SourceGit.Converters -{ - public static class LauncherPageConverters - { - public static readonly FuncMultiValueConverter ToTabSeperatorVisible = - new FuncMultiValueConverter(v => - { - if (v == null) - return false; - - var array = new List(); - array.AddRange(v); - if (array.Count != 3) - return false; - - var self = array[0] as ViewModels.LauncherPage; - if (self == null) - return false; - - var selected = array[1] as ViewModels.LauncherPage; - if (selected == null) - return true; - - var collections = array[2] as AvaloniaList; - if (collections == null) - return true; - - if (self == selected) - return false; - - var selfIdx = collections.IndexOf(self); - if (selfIdx == collections.Count - 1) - return true; - - return collections[selfIdx + 1] != selected; - }); - } -} diff --git a/src/ViewModels/Launcher.cs b/src/ViewModels/Launcher.cs index 5bdd3c18..3c08a8fa 100644 --- a/src/ViewModels/Launcher.cs +++ b/src/ViewModels/Launcher.cs @@ -23,6 +23,7 @@ namespace SourceGit.ViewModels if (SetProperty(ref _activePage, value)) { PopupHost.Active = value; + UpdateTabSplitterVisible(); } } } @@ -157,13 +158,12 @@ namespace SourceGit.ViewModels ActivePage = Pages[removeIdx == Pages.Count - 1 ? removeIdx - 1 : removeIdx + 1]; CloseRepositoryInTab(page); Pages.RemoveAt(removeIdx); - OnPropertyChanged(nameof(Pages)); } else if (removeIdx + 1 == activeIdx) { CloseRepositoryInTab(page); Pages.RemoveAt(removeIdx); - OnPropertyChanged(nameof(Pages)); + UpdateTabSplitterVisible(); } else { @@ -174,42 +174,25 @@ namespace SourceGit.ViewModels GC.Collect(); } - public void CloseOtherTabs(object param) + public void CloseOtherTabs() { if (Pages.Count == 1) return; - var page = param as LauncherPage; - if (page == null) - page = _activePage; - - ActivePage = page; - + var id = ActivePage.Node.Id; foreach (var one in Pages) { - if (one.Node.Id != page.Node.Id) + if (one.Node.Id != id) CloseRepositoryInTab(one); } - Pages = new AvaloniaList { page }; - OnPropertyChanged(nameof(Pages)); - + Pages = new AvaloniaList { ActivePage }; GC.Collect(); } - public void CloseRightTabs(object param) + public void CloseRightTabs() { - LauncherPage page = param as LauncherPage; - if (page == null) - page = _activePage; - - var endIdx = Pages.IndexOf(page); - var activeIdx = Pages.IndexOf(_activePage); - if (endIdx < activeIdx) - { - ActivePage = page; - } - + var endIdx = Pages.IndexOf(ActivePage); for (var i = Pages.Count - 1; i > endIdx; i--) { CloseRepositoryInTab(Pages[i]); @@ -275,6 +258,13 @@ namespace SourceGit.ViewModels page.Data = null; } + private void UpdateTabSplitterVisible() + { + var activePageIdx = ActivePage == null ? -1 : Pages.IndexOf(ActivePage); + for (int i = 0; i < Pages.Count; i++) + Pages[i].IsTabSplitterVisible = (activePageIdx != i && activePageIdx != i + 1); + } + private LauncherPage _activePage = null; } } diff --git a/src/ViewModels/LauncherPage.cs b/src/ViewModels/LauncherPage.cs index 5fce2bf3..703e5eca 100644 --- a/src/ViewModels/LauncherPage.cs +++ b/src/ViewModels/LauncherPage.cs @@ -18,6 +18,12 @@ namespace SourceGit.ViewModels set => SetProperty(ref _data, value); } + public bool IsTabSplitterVisible + { + get => _isTabSplitterVisible; + set => SetProperty(ref _isTabSplitterVisible, value); + } + public AvaloniaList Notifications { get; @@ -50,12 +56,11 @@ namespace SourceGit.ViewModels public void DismissNotification(object param) { if (param is Models.Notification notice) - { Notifications.Remove(notice); - } } private RepositoryNode _node = null; private object _data = null; + private bool _isTabSplitterVisible = true; } } diff --git a/src/Views/Launcher.axaml b/src/Views/Launcher.axaml index 4ba10c30..16e242bc 100644 --- a/src/Views/Launcher.axaml +++ b/src/Views/Launcher.axaml @@ -125,11 +125,9 @@ CommandParameter="{Binding}" InputGesture="{OnPlatform Ctrl+W, macOS=⌘+W}"/> + Command="{Binding #me.((vm:Launcher)DataContext).CloseOtherTabs}"/> + Command="{Binding #me.((vm:Launcher)DataContext).CloseRightTabs}"/> @@ -210,15 +208,11 @@ - - - - - - - - - +