8000 refactor: rewrite workspace switcher (#1267) · sourcegit-scm/sourcegit@4c1ba71 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4c1ba71

Browse files
committed
refactor: rewrite workspace switcher (#1267)
Signed-off-by: leo <longshuang@msn.cn>
1 parent bd55340 commit 4c1ba71

10 files changed

+287
-51
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@
386386
<x:String x:Key="Text.Hotkeys.Global.GotoPrevTab" xml:space="preserve">Go to previous page</x:String>
387387
<x:String x:Key="Text.Hotkeys.Global.NewTab" xml:space="preserve">Create new page</x:String>
388388
<x:String x:Key="Text.Hotkeys.Global.OpenPreferences" xml:space="preserve">Open Preferences dialog</x:String>
389-
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">Switch to corresponding workspace</x:String>
389+
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">Switch active workspace</x:String>
390390
<x:String x:Key="Text.Hotkeys.Repo" xml:space="preserve">REPOSITORY</x:String>
391391
<x:String x:Key="Text.Hotkeys.Repo.Commit" xml:space="preserve">Commit staged changes</x:String>
392392
<x:String x:Key="Text.Hotkeys.Repo.CommitAndPush" xml:space="preserve">Commit and push staged changes</x:String>
@@ -635,6 +635,7 @@
635635
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">Use relative time in histories</x:String>
636636
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">View Logs</x:String>
637637
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">Visit '{0}' in Browser</x:String>
638+
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">Switch Workspace</x:String>
638639
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">WORKTREES</x:String>
639640
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">Add Worktree</x:String>
640641
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">Prune</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@
639639
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">在提交列表中使用相对时间</x:String>
640640
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">查看命令日志</x:String>
641641
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">访问远程仓库 '{0}'</x:String>
642+
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">切换工作区</x:String>
642643
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">工作树列表</x:String>
643644
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">新增工作树</x:String>
644645
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">清理</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@
639639
<x:String x:Key="Text.Repository.UseRelativeTimeInHistories" xml:space="preserve">在提交列表中使用相對時間</x:String>
640640
<x:String x:Key="Text.Repository.ViewLogs" xml:space="preserve">檢視 Git 指令記錄</x:String>
641641
<x:String x:Key="Text.Repository.Visit" xml:space="preserve">檢視遠端存放庫 '{0}'</x:String>
642+
<x:String x:Key="Text.Repository.WorkspaceSwitcher" xml:space="preserve">切換工作區</x:String>
642643
<x:String x:Key="Text.Repository.Worktrees" xml:space="preserve">工作區列表</x:String>
643644
<x:String x:Key="Text.Repository.Worktrees.Add" xml:space="preserve">新增工作區</x:String>
644645
<x:String x:Key="Text.Repository.Worktrees.Prune" xml:space="preserve">清理</x:String>

src/ViewModels/Launcher.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ public AvaloniaList<LauncherPage> Pages
2323
private set;
2424
}
2525

26+
public WorkspaceSwitcher WorkspaceSwitcher
27+
{
28+
get => _workspaceSwitcher;
29+
set => SetProperty(ref _workspaceSwitcher, value);
30+
}
31+
2632
public Workspace ActiveWorkspace
2733
{
2834
get => _activeWorkspace;
@@ -130,8 +136,21 @@ public void Quit(double width, double height)
130136
_ignoreIndexChange = false;
131137
}
132138

139+
public void OpenWorkspaceSwitcher()
140+
{
141+
WorkspaceSwitcher = new WorkspaceSwitcher(this);
142+
}
143+
144+
public void CancelWorkspaceSwitcher()
145+
{
146+
WorkspaceSwitcher = null;
147+
}
148+
133149
public void SwitchWorkspace(Workspace to)
134150
{
151+
if (to.IsActive)
152+
return;
153+
135154
foreach (var one in Pages)
136155
{
137156
if (!one.CanCreatePopup() || one.Data is Repository { IsAutoFetching: true })
@@ -599,5 +618,6 @@ private void UpdateTitle()
599618
private LauncherPage _activePage = null;
600619
private bool _ignoreIndexChange = false;
601620
private string _title = string.Empty;
621+
private WorkspaceSwitcher _workspaceSwitcher = null;
602622
}
603623
}

src/ViewModels/WorkspaceSwitcher.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using CommunityToolkit.Mvvm.ComponentModel;
4+
5+
namespace SourceGit.ViewModels
6+
{
7+
public class WorkspaceSwitcher : ObservableObject
8+
{
9+
public List<Workspace> VisibleWorkspaces
10+
{
11+
get => _visibleWorkspaces;
12+
private set => SetProperty(ref _visibleWorkspaces, value);
13+
}
14+
15+
public string SearchFilter
16+
{
17+
get => _searchFilter;
18+
set
19+
{
20+
if (SetProperty(ref _searchFilter, value))
21+
UpdateVisibleWorkspaces();
22+
}
23+
}
24+
25+
public Workspace SelectedWorkspace
26+
{
27+
get => _selectedWorkspace;
28+
set => SetProperty(ref _selectedWorkspace, value);
29+
}
30+
31+
public WorkspaceSwitcher(Launcher launcher)
32+
{
33+
_launcher = launcher;
34+
UpdateVisibleWorkspaces();
35+
}
36+
37+
public void ClearFilter()
38+
{
39+
SearchFilter = string.Empty;
40+
}
41+
42+
public void Switch()
43+
{
44+
if (_selectedWorkspace is { })
45+
_launcher.SwitchWorkspace(_selectedWorkspace);
46+
47+
_launcher.CancelWorkspaceSwitcher();
48+
}
49+
50+
private void UpdateVisibleWorkspaces()
51+
{
52+
var visible = new List<Workspace>();
53+
if (string.IsNullOrEmpty(_searchFilter))
54+
{
55+
visible.AddRange(Preferences.Instance.Workspaces);
56+
}
57+
else
58+
{
59+
foreach (var workspace in Preferences.Instance.Workspaces)
60+
{
61+
if (workspace.Name.Contains(_searchFilter, StringComparison.OrdinalIgnoreCase))
62+
visible.Add(workspace);
63+
}
64+
}
65+
66+
VisibleWorkspaces = visible;
67+
SelectedWorkspace = visible.Count == 0 ? null : visible[0];
68+
}
69+
70+
private Launcher _launcher = null;
71+
private List<Workspace> _visibleWorkspaces = null;
72+
private string _searchFilter = string.Empty;
73+
private Workspace _selectedWorkspace = null;
74+
}
75+
}

src/Views/Hotkeys.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
<TextBlock Grid.Row="7" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Ctrl+Q, macOS=⌘+Q}"/>
7171
<TextBlock Grid.Row="7" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Quit}" />
7272

73-
<TextBlock Grid.Row="8" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Alt+[1..9], macOS=⌥+[1..9]}"/>
73+
<TextBlock Grid.Row="8" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Ctrl+P, macOS=⌘+P}"/>
7474
<TextBlock Grid.Row="8" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.SwitchWorkspace}" />
7575
</Grid>
7676

src/Views/Launcher.axaml

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<Path Width="12" Height="12" Data="{StaticResource Icons.Menu}"/>
7373
</Button>
7474

75-
<!-- Workspace Switcher -->
75+
<!-- Workspace Dropdown Menu -->
< 10000 /code>
7676
<Button Grid.Column="1" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenWorkspaceMenu">
7777
<ToolTip.Tip>
7878
<StackPanel Orientation="Horizontal">
@@ -85,10 +85,10 @@
8585
Fill="{Binding ActiveWorkspace.Brush}"/>
8686
</Button>
8787

88-
<!-- Pages Tabs-->
88+
<!-- Pages Tabs -->
8989
<v:LauncherTabBar Grid.Column="2" Height="30" Margin="0,0,16,0" VerticalAlignment="Bottom"/>
9090

91-
<!-- Caption Buttons (Windows/Linux)-->
91+
<!-- Caption Buttons (Windows/Linux) -->
9292
<Border Grid.Column="3" Margin="16,0,0,0" IsVisible="{Binding #ThisControl.HasRightCaptionButton}">
9393
<v:CaptionButtons Height="30" VerticalAlignment="Top"/>
9494
</Border>
@@ -102,5 +102,22 @@
102102
</DataTemplate>
103103
</ContentControl.DataTemplates>
104104
</ContentControl>
105+
106+
<!-- Workspace Switcher -->
107+
<Border Grid.Row="1"
108+
Background="Transparent"
109+
IsVisible="{Binding WorkspaceSwitcher, Converter={x:Static ObjectConverters.IsNotNull}}">
110+
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Effect="drop-shadow(0 0 12 #A0000000)">
111+
<Border Background="{DynamicResource Brush.Popup}" CornerRadius="8">
112+
<ContentControl Margin="16" Content="{Binding WorkspaceSwitcher}">
113+
<ContentControl.DataTemplates>
114+
<DataTemplate DataType="vm:WorkspaceSwitcher">
115+
<v:WorkspaceSwitcher/>
116+
</DataTemplate>
117+
</ContentControl.DataTemplates>
118+
</ContentControl>
119+
</Border>
120+
</Border>
121+
</Border>
105122
</Grid>
106123
</v:ChromelessWindow>

src/Views/Launcher.axaml.cs

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ protected override void OnKeyDown(KeyEventArgs e)
157157

158158
if (e.KeyModifiers.HasFlag(OperatingSystem.IsMacOS() ? KeyModifiers.Meta : KeyModifiers.Control))
159159
{
160+
if (e.Key == Key.P)
161+
{
162+
vm.OpenWorkspaceSwitcher();
163+
e.Handled = true;
164+
}
165+
160166
if (e.Key == Key.W)
161167
{
162168
vm.CloseTab(null);
@@ -248,17 +254,10 @@ protected override void OnKeyDown(KeyEventArgs e)
248254
}
249255
}
250256
}
251-
else if (e.KeyModifiers.HasFlag(KeyModifiers.Alt))
252-
{
253-
if (SwitchWorkspace(e.Key))
254-
{
255-
e.Handled = true;
256-
return;
257-
}
258-
}
259257
else if (e.Key == Key.Escape)
260258
{
261259
vm.ActivePage.CancelPopup();
260+
vm.CancelWorkspaceSwitcher();
262261
e.Handled = true;
263262
return;
264263
}
@@ -314,44 +313,6 @@ private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)
314313

315314
e.Handled = true;
316315
}
317-
318-
private bool SwitchWorkspace(Key eKey)
319-
{
320-
var exec = (ViewModels.Launcher l, int idx) =>
321-
{
322-
var pref = ViewModels.Preferences.Instance;
323-
if (idx < pref.Workspaces.Count)
324-
l.SwitchWorkspace(pref.Workspaces[idx]);
325-
return true; // Alt+1..9 (or Option+1..9) always mark handled
326-
};
327-
328-
if (DataContext is ViewModels.Launcher launcher)
329-
{
330-
switch (eKey)
331-
{
332-
case Key.D1 or Key.NumPad1:
333-
return exec(launcher, 0);
334-
case Key.D2 or Key.NumPad2:
335-
return exec(launcher, 1);
336-
case Key.D3 or Key.NumPad3:
337-
return exec(launcher, 2);
338-
case Key.D4 or Key.NumPad4:
339-
return exec(launcher, 3);
340-
case Key.D5 or Key.NumPad5:
341-
return exec(launcher, 4);
342-
case Key.D6 or Key.NumPad6:
343-
return exec(launcher, 5);
344-
case Key.D7 or Key.NumPad7:
345-
return exec(launcher, 6);
346-
case Key.D8 or Key.NumPad8:
347-
return exec(launcher, 7);
348-
case Key.D9 or Key.NumPad9:
349-
return exec(launcher, 8);
350-
}
351-
}
352-
353-
return false;
354-
}
355316

356317
private KeyModifiers _unhandledModifiers = KeyModifiers.None;
357318
private WindowState _lastWindowState = WindowState.Normal;

src/Views/WorkspaceSwitcher.axaml

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:vm="using:SourceGit.ViewModels"
6+
xmlns:v="using:SourceGit.Views"
7+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
8+
x:Class="SourceGit.Views.WorkspaceSwitcher"
9+
x:DataType="vm:WorkspaceSwitcher">
10+
<Grid RowDefinitions="28,Auto,Auto">
11+
<TextBlock Grid.Row="0"
12+
Text="{DynamicResource Text.Repository.WorkspaceSwitcher}"
13+
FontWeight="Bold"
14+
HorizontalAlignment="Center" VerticalAlignment="Top"/>
15+
16+
<TextBox Grid.Row="1"
17+
Height="24"
18+
Margin="4,0"
19+
BorderThickness="1"
20+
CornerRadius="12"
21+
Text="{Binding SearchFilter, Mode=TwoWay}"
22+
KeyDown="OnSearchBoxKeyDown"
23+
BorderBrush="{DynamicResource Brush.Border2}"
24+
VerticalContentAlignment="Center"
25+
v:AutoFocusBehaviour.IsEnabled="True">
26+
<TextBox.InnerLeftContent>
27+
<Path Width="14" Height="14"
28+
Margin="6,0,0,0"
29+
Fill="{DynamicResource Brush.FG2}"
30+
Data="{StaticResource Icons.Search}"/>
31+
</TextBox.InnerLeftContent>
32+
33+
<TextBox.InnerRightContent>
34+
<Button Classes="icon_button"
35+
Width="16"
36+
Margin="0,0,6,0"
37+
Command="{Binding ClearFilter}"
38+
IsVisible="{Binding SearchFilter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
39+
HorizontalAlignment="Right">
40+
<Path Width="14" Height="14"
41+
Margin="0,1,0,0"
42+
Fill="{DynamicResource Brush.FG1}"
43+
Data="{StaticResource Icons.Clear}"/>
44+
</Button>
45+
</TextBox.InnerRightContent>
46+
</TextBox>
47+
48+
<ListBox Grid.Row="2"
49+
x:Name="WorkspaceListBox"
50+
Width="300"
51+
MaxHeight="400"
52+
Margin="0,8,0,0"
53+
BorderThickness="0"
54+
SelectionMode="Single"
55+
Background="Transparent"
56+
Focusable="True"
57+
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
58+
ScrollViewer.VerticalScrollBarVisibility="Auto"
59+
ItemsSource="{Binding VisibleWorkspaces, Mode=OneWay}"
60+
SelectedItem="{Binding SelectedWorkspace, Mode=TwoWay}">
61+
<ListBox.Styles>
62+
<Style Selector="ListBoxItem">
63+
<Setter Property="Padding" Value="8,0"/>
64+
<Setter Property="MinHeight" Value="26"/>
65+
<Setter Property="CornerRadius" Value="4"/>
66+
</Style>
67+
68+
<Style Selector="ListBox">
69+
<Setter Property="FocusAdorner">
70+
<FocusAdornerTemplate>
71+
<Grid/>
72+
</FocusAdornerTemplate>
73+
</Setter>
74+
</Style>
75+
</ListBox.Styles>
76+
77+
<ListBox.ItemsPanel>
78+
<ItemsPanelTemplate>
79+
<StackPanel Orientation="Vertical"/>
80+
</ItemsPanelTemplate>
81+
</ListBox.ItemsPanel>
82+
83+
<ListBox.ItemTemplate>
84+
<DataTemplate DataType="vm:Workspace">
85+
<Grid ColumnDefinitions="Auto,*" VerticalAlignment="Center" Background="Transparent" DoubleTapped="OnItemDoubleTapped">
86+
<Path Grid.Column="0"
87+
Width="12" Height="12"
88+
Fill="{Binding Brush}"
89+
Data="{StaticResource Icons.Workspace}"
90+
IsVisible="{Binding !IsActive}"
91+
IsHitTestVisible="False"/>
92+
<Path Grid.Column="0"
93+
Width="12" Height="12"
94+
Fill="{Binding Brush}"
95+
Data="{StaticResource Icons.Check}"
96+
IsVisible="{Binding IsActive}"
97+
IsHitTestVisible="False"/>
98+
<TextBlock Grid.Column="1"
99+
Margin="8,0,0,0"
100+
Classes="primary"
101+
VerticalAlignment="Center"
102+
Text="{Binding Name}"
103+
IsHitTestVisible="False"
104+
TextTrimming="CharacterEllipsis"/>
105+
</Grid>
106+
</DataTemplate>
107+
</ListBox.ItemTemplate>
108+
</ListBox>
109+
</Grid>
110+
</UserControl>
111+

0 commit comments

Comments
 (0)
0