Skip to content

Commit cceaf31

Browse files
committed
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>
1 parent ac660aa commit cceaf31

File tree

6 files changed

+89
-47
lines changed

6 files changed

+89
-47
lines changed

src/ViewModels/Launcher.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ public LauncherPage ActivePage
4343
}
4444
}
4545

46-
public QuickLauncher QuickLauncher
46+
public IDisposable CommandPalette
4747
{
48-
get => _quickLauncher;
49-
set => SetProperty(ref _quickLauncher, value);
48+
get => _commandPalette;
49+
set => SetProperty(ref _commandPalette, value);
5050
}
5151

5252
public Launcher(string startupRepo)
@@ -376,6 +376,18 @@ public void OpenRepositoryInTab(RepositoryNode node, LauncherPage page)
376376
ActiveWorkspace.ActiveIdx = ActiveWorkspace.Repositories.IndexOf(node.Id);
377377
}
378378

379+
public void OpenCommandPalette(IDisposable commandPalette)
380+
{
381+
_commandPalette?.Dispose();
382+
CommandPalette = commandPalette;
383+
}
384+
385+
public void CancelCommandPalette()
386+
{
387+
_commandPalette?.Dispose();
388+
CommandPalette = null;
389+
}
390+
379391
public void DispatchNotification(string pageId, string message, bool isError)
380392
{
381393
if (!Dispatcher.UIThread.CheckAccess())
@@ -481,6 +493,6 @@ private void UpdateTitle()
481493
private LauncherPage _activePage = null;
482494
private bool _ignoreIndexChange = false;
483495
private string _title = string.Empty;
484-
private QuickLauncher _quickLauncher = null;
496+
private IDisposable _commandPalette = null;
485497
}
486498
}

src/ViewModels/QuickLauncher.cs renamed to src/ViewModels/LauncherPagesCommandPalette.cs

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SourceGit.ViewModels
66
{
7-
public class QuickLauncher : ObservableObject
7+
public class LauncherPagesCommandPalette : ObservableObject, IDisposable
88
{
99
public List<LauncherPage> VisiblePages
1010
{
@@ -48,7 +48,7 @@ public RepositoryNode SelectedRepo
4848
}
4949
}
5050

51-
public QuickLauncher(Launcher launcher)
51+
public LauncherPagesCommandPalette(Launcher launcher)
5252
{
5353
_launcher = launcher;
5454

@@ -61,6 +61,17 @@ public QuickLauncher(Launcher launcher)
6161
UpdateVisible();
6262
}
6363

64+
public void Dispose()
65+
{
66+
_launcher = null;
67+
_opened.Clear();
68+
_visiblePages.Clear();
69+
_visibleRepos.Clear();
70+
_searchFilter = null;
71+
_selectedPage = null;
72+
_selectedRepo = null;
73+
}
74+
6475
public void ClearFilter()
6576
{
6677
SearchFilter = string.Empty;
@@ -73,25 +84,33 @@ public void OpenOrSwitchTo()
7384
else if (_selectedRepo != null)
7485
_launcher.OpenRepositoryInTab(_selectedRepo, null);
7586

76-
_launcher.QuickLauncher = null;
87+
_launcher.CancelCommandPalette();
7788
}
7889

7990
private void UpdateVisible()
8091
{
8192
var pages = new List<LauncherPage>();
93+
CollectVisiblePages(pages);
94+
95+
var repos = new List<RepositoryNode>();
96+
CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes);
97+
98+
VisiblePages = pages;
99+
VisibleRepos = repos;
100+
}
101+
102+
private void CollectVisiblePages(List<LauncherPage> pages)
103+
{
82104
foreach (var page in _launcher.Pages)
83105
{
106+
if (page == _launcher.ActivePage)
107+
continue;
108+
84109
if (string.IsNullOrEmpty(_searchFilter) ||
85110
page.Node.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase) ||
86111
(page.Node.IsRepository && page.Node.Id.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase)))
87112
pages.Add(page);
88113
}
89-
90-
var repos = new List<RepositoryNode>();
91-
CollectVisibleRepository(repos, Preferences.Instance.RepositoryNodes);
92-
93-
VisiblePages = pages;
94-
VisibleRepos = repos;
95114
}
96115

97116
private void CollectVisibleRepository(List<RepositoryNode> outs, List<RepositoryNode> nodes)

src/Views/Launcher.axaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
</Button>
9191

9292
<!-- Pages Switcher Toggle Button -->
93-
<Button Grid.Column="2" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenQuickLauncher" HotKey="{OnPlatform Ctrl+P, macOS=⌘+P}">
93+
<Button Grid.Column="2" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenPagesCommandPalette" HotKey="{OnPlatform Ctrl+P, macOS=⌘+P}">
9494
<ToolTip.Tip>
9595
<StackPanel Orientation="Horizontal">
9696
<TextBlock Text="{DynamicResource Text.Launcher.Pages}"
@@ -123,17 +123,17 @@
123123
</ContentControl.DataTemplates>
124124
</ContentControl>
125125

126-
<!-- Quick Launcher Popup -->
126+
<!-- Command Palette -->
127127
<Border Grid.Row="0" Grid.RowSpan="2"
128128
Background="Transparent"
129-
IsVisible="{Binding QuickLauncher, Converter={x:Static ObjectConverters.IsNotNull}}"
130-
PointerPressed="OnCloseQuickLauncher">
129+
IsVisible="{Binding CommandPalette, Converter={x:Static ObjectConverters.IsNotNull}}"
130+
PointerPressed="OnCloseCommandPalette">
131131
<Border Width="400" HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
132132
<Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
133-
<ContentControl Margin="16,10,16,12" Content="{Binding QuickLauncher}">
133+
<ContentControl Margin="16,10,16,12" Content="{Binding CommandPalette}">
134134
<ContentControl.DataTemplates>
135-
<DataTemplate DataType="vm:QuickLauncher">
136-
<v:QuickLauncher/>
135+
<DataTemplate DataType="vm:LauncherPagesCommandPalette">
136+
<v:LauncherPagesCommandPalette/>
137137
</DataTemplate>
138138
</ContentControl.DataTemplates>
139139
</ContentControl>

src/Views/Launcher.axaml.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ protected override async void OnKeyDown(KeyEventArgs e)
250250
}
251251
else if (e.Key == Key.Escape)
252252
{
253-
if (vm.QuickLauncher != null)
254-
vm.QuickLauncher = null;
253+
if (vm.CommandPalette != null)
254+
vm.CancelCommandPalette();
255255
else
256256
vm.ActivePage.CancelPopup();
257257

@@ -355,17 +355,17 @@ private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)
355355
e.Handled = true;
356356
}
357357

358-
private void OnOpenQuickLauncher(object sender, RoutedEventArgs e)
358+
private void OnOpenPagesCommandPalette(object sender, RoutedEventArgs e)
359359
{
360360
if (DataContext is ViewModels.Launcher launcher)
361-
launcher.QuickLauncher = new ViewModels.QuickLauncher(launcher);
361+
launcher.OpenCommandPalette(new ViewModels.LauncherPagesCommandPalette(launcher));
362362
e.Handled = true;
363363
}
364364

365-
private void OnCloseQuickLauncher(object sender, PointerPressedEventArgs e)
365+
private void OnCloseCommandPalette(object sender, PointerPressedEventArgs e)
366366
{
367367
if (e.Source == sender && DataContext is ViewModels.Launcher launcher)
368-
launcher.QuickLauncher = null;
368+
launcher.CancelCommandPalette();
369369
e.Handled = true;
370370
}
371371

src/Views/QuickLauncher.axaml renamed to src/Views/LauncherPagesCommandPalette.axaml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
xmlns:v="using:SourceGit.Views"
77
xmlns:c="using:SourceGit.Converters"
88
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
9-
x:Class="SourceGit.Views.QuickLauncher"
10-
x:DataType="vm:QuickLauncher">
9+
x:Class="SourceGit.Views.LauncherPagesCommandPalette"
10+
x:DataType="vm:LauncherPagesCommandPalette">
1111
<Grid RowDefinitions="Auto,Auto,Auto,Auto,Auto">
1212
<TextBox Grid.Row="0"
1313
x:Name="FilterTextBox"
@@ -45,7 +45,8 @@
4545
Margin="6,8,0,4"
4646
Text="{DynamicResource Text.Launcher.Pages}"
4747
FontWeight="Bold"
48-
Foreground="{DynamicResource Brush.FG2}"/>
48+
Foreground="{DynamicResource Brush.FG2}"
49+
IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
4950

5051
<ListBox Grid.Row="2"
5152
x:Name="PageListBox"
@@ -58,7 +59,8 @@
5859
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
5960
ScrollViewer.VerticalScrollBarVisibility="Auto"
6061
ItemsSource="{Binding VisiblePages, Mode=OneWay}"
61-
SelectedItem="{Binding SelectedPage, Mode=TwoWay}">
62+
SelectedItem="{Binding SelectedPage, Mode=TwoWay}"
63+
IsVisible="{Binding VisiblePages, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
6264
<ListBox.Styles>
6365
<Style Selector="ListBoxItem">
6466
<Setter Property="Padding" Value="8,0"/>
@@ -115,7 +117,8 @@
115117
Margin="6,8,0,4"
116118
Text="{DynamicResource Text.Launcher.OpenRepository}"
117119
FontWeight="Bold"
118-
Foreground="{DynamicResource Brush.FG2}"/>
120+
Foreground="{DynamicResource Brush.FG2}"
121+
IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}"/>
119122

120123
<ListBox Grid.Row="4"
121124
x:Name="RepoListBox"
@@ -128,7 +131,8 @@
128131
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
129132
ScrollViewer.VerticalScrollBarVisibility="Auto"
130133
ItemsSource="{Binding VisibleRepos, Mode=OneWay}"
131-
SelectedItem="{Binding SelectedRepo, Mode=TwoWay}">
134+
SelectedItem="{Binding SelectedRepo, Mode=TwoWay}"
135+
IsVisible="{Binding VisibleRepos, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
132136
<ListBox.Styles>
133137
<Style Selector="ListBoxItem">
134138
<Setter Property="Padding" Value="8,0"/>

src/Views/QuickLauncher.axaml.cs renamed to src/Views/LauncherPagesCommandPalette.axaml.cs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
namespace SourceGit.Views
55
{
6-
public partial class QuickLauncher : UserControl
6+
public partial class LauncherPagesCommandPalette : UserControl
77
{
8-
public QuickLauncher()
8+
public LauncherPagesCommandPalette()
99
{
1010
InitializeComponent();
1111
}
@@ -14,22 +14,22 @@ protected override void OnKeyDown(KeyEventArgs e)
1414
{
1515
base.OnKeyDown(e);
1616

17-
if (DataContext is not ViewModels.QuickLauncher switcher)
17+
if (DataContext is not ViewModels.LauncherPagesCommandPalette vm)
1818
return;
1919

2020
if (e.Key == Key.Enter)
2121
{
22-
switcher.OpenOrSwitchTo();
22+
vm.OpenOrSwitchTo();
2323
e.Handled = true;
2424
}
2525
else if (e.Key == Key.Up)
2626
{
2727
if (RepoListBox.IsKeyboardFocusWithin)
2828
{
29-
if (switcher.VisiblePages.Count > 0)
29+
if (vm.VisiblePages.Count > 0)
3030
{
3131
PageListBox.Focus(NavigationMethod.Directional);
32-
switcher.SelectedPage = switcher.VisiblePages[^1];
32+
vm.SelectedPage = vm.VisiblePages[^1];
3333
}
3434
else
3535
{
@@ -47,19 +47,19 @@ protected override void OnKeyDown(KeyEventArgs e)
4747
return;
4848
}
4949
}
50-
else if (e.Key == Key.Down)
50+
else if (e.Key == Key.Down || e.Key == Key.Tab)
5151
{
5252
if (FilterTextBox.IsKeyboardFocusWithin)
5353
{
54-
if (switcher.VisiblePages.Count > 0)
54+
if (vm.VisiblePages.Count > 0)
5555
{
5656
PageListBox.Focus(NavigationMethod.Directional);
57-
switcher.SelectedPage = switcher.VisiblePages[0];
57+
vm.SelectedPage = vm.VisiblePages[0];
5858
}
59-
else if (switcher.VisibleRepos.Count > 0)
59+
else if (vm.VisibleRepos.Count > 0)
6060
{
6161
RepoListBox.Focus(NavigationMethod.Directional);
62-
switcher.SelectedRepo = switcher.VisibleRepos[0];
62+
vm.SelectedRepo = vm.VisibleRepos[0];
6363
}
6464

6565
e.Handled = true;
@@ -68,23 +68,30 @@ protected override void OnKeyDown(KeyEventArgs e)
6868

6969
if (PageListBox.IsKeyboardFocusWithin)
7070
{
71-
if (switcher.VisibleRepos.Count > 0)
71+
if (vm.VisibleRepos.Count > 0)
7272
{
7373
RepoListBox.Focus(NavigationMethod.Directional);
74-
switcher.SelectedRepo = switcher.VisibleRepos[0];
74+
vm.SelectedRepo = vm.VisibleRepos[0];
7575
}
7676

7777
e.Handled = true;
7878
return;
7979
}
80+
81+
if (RepoListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
82+
{
83+
FilterTextBox.Focus(NavigationMethod.Directional);
84+
e.Handled = true;
85+
return;
86+
}
8087
}
8188
}
8289

8390
private void OnItemTapped(object sender, TappedEventArgs e)
8491
{
85-
if (DataContext is ViewModels.QuickLauncher switcher)
92+
if (DataContext is ViewModels.LauncherPagesCommandPalette vm)
8693
{
87-
switcher.OpenOrSwitchTo();
94+
vm.OpenOrSwitchTo();
8895
e.Handled = true;
8996
}
9097
}

0 commit comments

Comments
 (0)