refactor: rewrite pages switcher

- Rename `QuickLauncher` to `LauncherPagesCommandPalette`
- Auto hide groups if there's no visible choices
- Do not show current active page in `Tabs` group
- Supports to use `Tab` to jump between groups

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo
2025-10-27 17:23:00 +08:00
parent ac660aafa5
commit cceaf31baf
6 changed files with 90 additions and 48 deletions

View File

@@ -43,10 +43,10 @@ namespace SourceGit.ViewModels
}
}
public QuickLauncher QuickLauncher
public IDisposable CommandPalette
{
get => _quickLauncher;
set => SetProperty(ref _quickLauncher, value);
get => _commandPalette;
set => SetProperty(ref _commandPalette, value);
}
public Launcher(string startupRepo)
@@ -376,6 +376,18 @@ namespace SourceGit.ViewModels
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(node.Id);
}
public void OpenCommandPalette(IDisposable commandPalette)
{
_commandPalette?.Dispose();
CommandPalette = commandPalette;
}
public void CancelCommandPalette()
{
_commandPalette?.Dispose();
CommandPalette = null;
}
public void DispatchNotification(string pageId, string message, bool isError)
{
if (!Dispatcher.UIThread.CheckAccess())
@@ -481,6 +493,6 @@ namespace SourceGit.ViewModels
private LauncherPage _activePage = null;
private bool _ignoreIndexChange = false;
private string _title = string.Empty;
private QuickLauncher _quickLauncher = null;
private IDisposable _commandPalette = null;
}
}

View File

@@ -4,7 +4,7 @@ using CommunityToolkit.Mvvm.ComponentModel;
namespace SourceGit.ViewModels
{
public class QuickLauncher : ObservableObject
public class LauncherPagesCommandPalette : ObservableObject, IDisposable
{
public List<LauncherPage> VisiblePages
{
@@ -48,7 +48,7 @@ namespace SourceGit.ViewModels
}
}
public QuickLauncher(Launcher launcher)
public LauncherPagesCommandPalette(Launcher launcher)
{
_launcher = launcher;
@@ -61,6 +61,17 @@ namespace SourceGit.ViewModels
UpdateVisible();
}
public void Dispose()
{
_launcher = null;
_opened.Clear();
_visiblePages.Clear();
_visibleRepos.Clear();
_searchFilter = null;
_selectedPage = null;
_selectedRepo = null;
}
public void ClearFilter()
{
SearchFilter = string.Empty;
@@ -73,19 +84,13 @@ namespace SourceGit.ViewModels
else if (_selectedRepo != null)
_launcher.OpenRepositoryInTab(_selectedRepo, null);
_launcher.QuickLauncher = null;
_launcher.CancelCommandPalette();
}
private void UpdateVisible()
{
var pages = new List<LauncherPage>();
foreach (var page in _launcher.Pages)
{
if (string.IsNullOrEmpty(_searchFilter) ||
page.Node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) ||
(page.Node.IsRepository && page.Node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase)))
pages.Add(page);
}
CollectVisiblePages(pages);
var repos = new List<RepositoryNode>();
CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes);
@@ -94,6 +99,20 @@ namespace SourceGit.ViewModels
VisibleRepos = repos;
}
private void CollectVisiblePages(List<LauncherPage> pages)
{
foreach (var page in _launcher.Pages)
{
if (page == _launcher.ActivePage)
continue;
if (string.IsNullOrEmpty(_searchFilter) ||
page.Node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) ||
(page.Node.IsRepository && page.Node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase)))
pages.Add(page);
}
}
private void CollectVisibleRepository(List<RepositoryNode> outs, List<RepositoryNode> nodes)
{
foreach (var node in nodes)

View File

@@ -90,7 +90,7 @@
</Button>
<!-- Pages Switcher Toggle Button -->
<Button Grid.Column="2" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenQuickLauncher" HotKey="{OnPlatform Ctrl+P, macOS=⌘+P}">
<Button Grid.Column="2" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenPagesCommandPalette" HotKey="{OnPlatform Ctrl+P, macOS=⌘+P}">
<ToolTip.Tip>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{DynamicResource Text.Launcher.Pages}"
@@ -123,17 +123,17 @@
</ContentControl.DataTemplates>
</ContentControl>
<!-- Quick Launcher Popup -->
<!-- Command Palette -->
<Border Grid.Row="0" Grid.RowSpan="2"
Background="Transparent"
IsVisible="{Binding QuickLauncher, Converter={x:Static ObjectConverters.IsNotNull}}"
PointerPressed="OnCloseQuickLauncher">
IsVisible="{Binding CommandPalette, Converter={x:Static ObjectConverters.IsNotNull}}"
PointerPressed="OnCloseCommandPalette">
<Border Width="400" HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
<Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
<ContentControl Margin="16,10,16,12" Content="{Binding QuickLauncher}">
<ContentControl Margin="16,10,16,12" Content="{Binding CommandPalette}">
<ContentControl.DataTemplates>
<DataTemplate DataType="vm:QuickLauncher">
<v:QuickLauncher/>
<DataTemplate DataType="vm:LauncherPagesCommandPalette">
<v:LauncherPagesCommandPalette/>
</DataTemplate>
</ContentControl.DataTemplates>
</ContentControl>

View File

@@ -250,8 +250,8 @@ namespace SourceGit.Views
}
else if (e.Key == Key.Escape)
{
if (vm.QuickLauncher != null)
vm.QuickLauncher = null;
if (vm.CommandPalette != null)
vm.CancelCommandPalette();
else
vm.ActivePage.CancelPopup();
@@ -355,17 +355,17 @@ namespace SourceGit.Views
e.Handled = true;
}
private void OnOpenQuickLauncher(object sender, RoutedEventArgs e)
private void OnOpenPagesCommandPalette(object sender, RoutedEventArgs e)
{
if (DataContext is ViewModels.Launcher launcher)
launcher.QuickLauncher = new ViewModels.QuickLauncher(launcher);
launcher.OpenCommandPalette(new ViewModels.LauncherPagesCommandPalette(launcher));
e.Handled = true;
}
private void OnCloseQuickLauncher(object sender, PointerPressedEventArgs e)
private void OnCloseCommandPalette(object sender, PointerPressedEventArgs e)
{
if (e.Source == sender && DataContext is ViewModels.Launcher launcher)
launcher.QuickLauncher = null;
launcher.CancelCommandPalette();
e.Handled = true;
}

View File

@@ -6,8 +6,8 @@
xmlns:v="using:SourceGit.Views"
xmlns:c="using:SourceGit.Converters"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="SourceGit.Views.QuickLauncher"
x:DataType="vm:QuickLauncher">
x:Class="SourceGit.Views.LauncherPagesCommandPalette"
x:DataType="vm:LauncherPagesCommandPalette">
<Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto">
<TextBox Grid.Row="0"
x:Name="FilterTextBox"
@@ -45,7 +45,8 @@
Margin="6,8,0,4"
Text="{DynamicResource Text.Launcher.Pages}"
FontWeight="Bold"
Foreground="{DynamicResource Brush.FG2}"/>
Foreground="{DynamicResource Brush.FG2}"
IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
<ListBox Grid.Row="2"
x:Name="PageListBox"
@@ -58,7 +59,8 @@
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding VisiblePages, Mode=OneWay}"
SelectedItem="{Binding SelectedPage, Mode=TwoWay}">
SelectedItem="{Binding SelectedPage, Mode=TwoWay}"
IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="8,0"/>
@@ -115,7 +117,8 @@
Margin="6,8,0,4"
Text="{DynamicResource Text.Launcher.OpenRepository}"
FontWeight="Bold"
Foreground="{DynamicResource Brush.FG2}"/>
Foreground="{DynamicResource Brush.FG2}"
IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
<ListBox Grid.Row="4"
x:Name="RepoListBox"
@@ -128,7 +131,8 @@
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding VisibleRepos, Mode=OneWay}"
SelectedItem="{Binding SelectedRepo, Mode=TwoWay}">
SelectedItem="{Binding SelectedRepo, Mode=TwoWay}"
IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="8,0"/>

View File

@@ -3,9 +3,9 @@ using Avalonia.Input;
namespace SourceGit.Views
{
public partial class QuickLauncher : UserControl
public partial class LauncherPagesCommandPalette : UserControl
{
public QuickLauncher()
public LauncherPagesCommandPalette()
{
InitializeComponent();
}
@@ -14,22 +14,22 @@ namespace SourceGit.Views
{
base.OnKeyDown(e);
if (DataContext is not ViewModels.QuickLauncher switcher)
if (DataContext is not ViewModels.LauncherPagesCommandPalette vm)
return;
if (e.Key == Key.Enter)
{
switcher.OpenOrSwitchTo();
vm.OpenOrSwitchTo();
e.Handled = true;
}
else if (e.Key == Key.Up)
{
if (RepoListBox.IsKeyboardFocusWithin)
{
if (switcher.VisiblePages.Count > 0)
if (vm.VisiblePages.Count > 0)
{
PageListBox.Focus(NavigationMethod.Directional);
switcher.SelectedPage = switcher.VisiblePages[^1];
vm.SelectedPage = vm.VisiblePages[^1];
}
else
{
@@ -47,19 +47,19 @@ namespace SourceGit.Views
return;
}
}
else if (e.Key == Key.Down)
else if (e.Key == Key.Down || e.Key == Key.Tab)
{
if (FilterTextBox.IsKeyboardFocusWithin)
{
if (switcher.VisiblePages.Count > 0)
if (vm.VisiblePages.Count > 0)
{
PageListBox.Focus(NavigationMethod.Directional);
switcher.SelectedPage = switcher.VisiblePages[0];
vm.SelectedPage = vm.VisiblePages[0];
}
else if (switcher.VisibleRepos.Count > 0)
else if (vm.VisibleRepos.Count > 0)
{
RepoListBox.Focus(NavigationMethod.Directional);
switcher.SelectedRepo = switcher.VisibleRepos[0];
vm.SelectedRepo = vm.VisibleRepos[0];
}
e.Handled = true;
@@ -68,23 +68,30 @@ namespace SourceGit.Views
if (PageListBox.IsKeyboardFocusWithin)
{
if (switcher.VisibleRepos.Count > 0)
if (vm.VisibleRepos.Count > 0)
{
RepoListBox.Focus(NavigationMethod.Directional);
switcher.SelectedRepo = switcher.VisibleRepos[0];
vm.SelectedRepo = vm.VisibleRepos[0];
}
e.Handled = true;
return;
}
if (RepoListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
{
FilterTextBox.Focus(NavigationMethod.Directional);
e.Handled = true;
return;
}
}
}
private void OnItemTapped(object sender, TappedEventArgs e)
{
if (DataContext is ViewModels.QuickLauncher switcher)
if (DataContext is ViewModels.LauncherPagesCommandPalette vm)
{
switcher.OpenOrSwitchTo();
vm.OpenOrSwitchTo();
e.Handled = true;
}
}