Skip to content

Commit f121975

Browse files
committed
code_review: PR #1328
* remove hotkey to open workspace dropdown menu * call orignal `ViewModels.Launcher.SwitchWorkspace` directly in view * add missing translation for Chinese Signed-off-by: leo <[email protected]>
1 parent ea320d2 commit f121975

File tree

8 files changed

+113
-121
lines changed

8 files changed

+113
-121
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +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.OpenWorkspaces" xml:space="preserve">Open Workspaces dialog</x:String>
390-
<x:String x:Key="Text.Hotkeys.Global.OpenWorkspaceAtIndex" xml:space="preserve">Switch to corresponding workspace</x:String>
389+
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">Switch to corresponding workspace</x:String>
391390
<x:String x:Key="Text.Hotkeys.Repo" xml:space="preserve">REPOSITORY</x:String>
392391
<x:String x:Key="Text.Hotkeys.Repo.Commit" xml:space="preserve">Commit staged changes</x:String>
393392
<x:String x:Key="Text.Hotkeys.Repo.CommitAndPush" xml:space="preserve">Commit and push staged changes</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,7 @@
390390
<x:String x:Key="Text.Hotkeys.Global.GotoPrevTab" xml:space="preserve">切换到上一个页面</x:String>
391391
<x:String x:Key="Text.Hotkeys.Global.NewTab" xml:space="preserve">新建页面</x:String>
392392
<x:String x:Key="Text.Hotkeys.Global.OpenPreferences" xml:space="preserve">打开偏好设置面板</x:String>
393+
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">切换工作区</x:String>
393394
<x:String x:Key="Text.Hotkeys.Repo" xml:space="preserve">仓库页面快捷键</x:String>
394395
<x:String x:Key="Text.Hotkeys.Repo.Commit" xml:space="preserve">提交暂存区更改</x:String>
395396
<x:String x:Key="Text.Hotkeys.Repo.CommitAndPush" 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
@@ -390,6 +390,7 @@
390390
<x:String x:Key="Text.Hotkeys.Global.GotoPrevTab" xml:space="preserve">切換到上一個頁面</x:String>
391391
<x:String x:Key="Text.Hotkeys.Global.NewTab" xml:space="preserve">新增頁面</x:String>
392392
<x:String x:Key="Text.Hotkeys.Global.OpenPreferences" xml:space="preserve">開啟偏好設定面板</x:String>
393+
<x:String x:Key="Text.Hotkeys.Global.SwitchWorkspace" xml:space="preserve">切換工作區</x:String>
393394
<x:String x:Key="Text.Hotkeys.Repo" xml:space="preserve">存放庫頁面快速鍵</x:String>
394395
<x:String x:Key="Text.Hotkeys.Repo.Commit" xml:space="preserve">提交暫存區變更</x:String>
395396
<x:String x:Key="Text.Hotkeys.Repo.CommitAndPush" xml:space="preserve">提交暫存區變更並推送</x:String>

src/ViewModels/Launcher.cs

Lines changed: 61 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,66 @@ public void Quit(double width, double height)
130130
_ignoreIndexChange = false;
131131
}
132132

133+
public void SwitchWorkspace(Workspace to)
134+
{
135+
foreach (var one in Pages)
136+
{
137+
if (!one.CanCreatePopup() || one.Data is Repository { IsAutoFetching: true })
138+
{
139+
App.RaiseException(null, "You have unfinished task(s) in opened pages. Please wait!!!");
140+
return;
141+
}
142+
}
143+
144+
_ignoreIndexChange = true;
145+
146+
var pref = Preferences.Instance;
147+
foreach (var w in pref.Workspaces)
148+
w.IsActive = false;
149+
150+
ActiveWorkspace = to;
151+
to.IsActive = true;
152+
153+
foreach (var one in Pages)
154+
CloseRepositoryInTab(one, false);
155+
156+
Pages.Clear();
157+
AddNewTab();
158+
159+
var repos = to.Repositories.ToArray();
160+
foreach (var repo in repos)
161+
{
162+
var node = pref.FindNode(repo);
163+
if (node == null)
164+
{
165+
node = new RepositoryNode()
166+
{
167+
Id = repo,
168+
Name = Path.GetFileName(repo),
169+
Bookmark = 0,
170+
IsRepository = true,
171+
};
172+
}
173+
174+
OpenRepositoryInTab(node, null);
175+
}
176+
177+
var activeIdx = to.ActiveIdx;
178+
if (activeIdx >= 0 && activeIdx < Pages.Count)
179+
{
180+
ActivePage = Pages[activeIdx];
181+
}
182+
else
183+
{
184+
ActivePage = Pages[0];
185+
to.ActiveIdx = 0;
186+
}
187+
188+
_ignoreIndexChange = false;
189+
Preferences.Instance.Save();
190+
GC.Collect();
191+
}
192+
133193
public void AddNewTab()
134194
{
135195
var page = new LauncherPage();
@@ -463,14 +523,6 @@ public ContextMenu CreateContextForPageTab(LauncherPage page)
463523
return menu;
464524
}
465525

466-
public void SwitchWorkspace(int idx)
467-
{
468-
var pref = Preferences.Instance;
469-
if (idx >= pref.Workspaces.Count || pref.Workspaces[idx].IsActive) return;
470-
471-
SwitchWorkspace(pref.Workspaces[idx]);
472-
}
473-
474526
private string GetRepositoryGitDir(string repo)
475527
{
476528
var fullpath = Path.Combine(repo, ".git");
@@ -501,66 +553,6 @@ private string GetRepositoryGitDir(string repo)
501553

502554
return new Commands.QueryGitDir(repo).Result();
503555
}
504-
505-
private void SwitchWorkspace(Workspace to)
506-
{
507-
foreach (var one in Pages)
508-
{
509-
if (!one.CanCreatePopup() || one.Data is Repository { IsAutoFetching: true })
510-
{
511-
App.RaiseException(null, "You have unfinished task(s) in opened pages. Please wait!!!");
512-
return;
513-
}
514-
}
515-
516-
_ignoreIndexChange = true;
517-
518-
var pref = Preferences.Instance;
519-
foreach (var w in pref.Workspaces)
520-
w.IsActive = false;
521-
522-
ActiveWorkspace = to;
523-
to.IsActive = true;
524-
525-
foreach (var one in Pages)
526-
CloseRepositoryInTab(one, false);
527-
528-
Pages.Clear();
529-
AddNewTab();
530-
531-
var repos = to.Repositories.ToArray();
532-
foreach (var repo in repos)
533-
{
534-
var node = pref.FindNode(repo);
535-
if (node == null)
536-
{
537-
node = new RepositoryNode()
538-
{
539-
Id = repo,
540-
Name = Path.GetFileName(repo),
541-
Bookmark = 0,
542-
IsRepository = true,
543-
};
544-
}
545-
546-
OpenRepositoryInTab(node, null);
547-
}
548-
549-
var activeIdx = to.ActiveIdx;
550-
if (activeIdx >= 0 && activeIdx < Pages.Count)
551-
{
552-
ActivePage = Pages[activeIdx];
553-
}
554-
else
555-
{
556-
ActivePage = Pages[0];
557-
to.ActiveIdx = 0;
558-
}
559-
560-
_ignoreIndexChange = false;
561-
Preferences.Instance.Save();
562-
GC.Collect();
563-
}
564556

565557
private void CloseRepositoryInTab(LauncherPage page, bool removeFromWorkspace = true)
566558
{
@@ -581,7 +573,7 @@ private void UpdateTitle()
581573
return;
582574

583575
var workspace = _activeWorkspace.Name;
584-
if (_activePage is { Data: Repository repo })
576+
if (_activePage is { Data: Repository })
585577
{
586578
var node = _activePage.Node;
587579
var name = node.Name;

src/Views/Avatar.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ public override void Render(DrawingContext context)
7878
if (switches[idx])
7979
context.FillRectangle(brush, new Rect(x, y, stepX, stepY));
8080

81-
if (switches[idx+1])
81+
if (switches[idx + 1])
8282
context.FillRectangle(brush, new Rect(x + stepX, y, stepX, stepY));
8383

84-
if (switches[idx+2])
84+
if (switches[idx + 2])
8585
context.FillRectangle(brush, new Rect(x + stepX * 2, y, stepX, stepY));
8686
}
8787

@@ -94,7 +94,7 @@ public override void Render(DrawingContext context)
9494
if (switches[idx])
9595
context.FillRectangle(brush, new Rect(x, y, stepX, stepY));
9696

97-
if (switches[idx-1])
97+
if (switches[idx - 1])
9898
context.FillRectangle(brush, new Rect(x + stepX, y, stepX, stepY));
9999
}
100100
}

src/Views/Hotkeys.axaml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
FontSize="{Binding Source={x:Static vm:Preferences.Instance}, Path=DefaultFontSize, Converter={x:Static c:DoubleConverters.Increase}}"
4646
Margin="0,0,0,8"/>
4747

48-
<Grid RowDefinitions="20,20,20,20,20,20,20,20,20,20" ColumnDefinitions="150,*">
48+
<Grid RowDefinitions="20,20,20,20,20,20,20,20,20" ColumnDefinitions="150,*">
4949
<TextBlock Grid.Row="0" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Ctrl+Shift+P, macOS=⌘+\,}"/>
5050
<TextBlock Grid.Row="0" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.OpenPreferences}"/>
5151

@@ -70,11 +70,8 @@
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+Space, macOS=⌥+␣}"/>
74-
<TextBlock Grid.Row="8" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.OpenWorkspaces}" />
75-
76-
<TextBlock Grid.Row="9" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Alt+1 - Alt+9, macOS=⌥+1 - ⌥+9}"/>
77-
<TextBlock Grid.Row="9" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.OpenWorkspaceAtIndex}" />
73+
<TextBlock Grid.Row="8" Grid.Column="0" Classes="primary bold" Text="{OnPlatform Alt+[1..9], macOS=⌥+[1..9]}"/>
74+
<TextBlock Grid.Row="8" Grid.Column="1" Margin="16,0,0,0" Text="{DynamicResource Text.Hotkeys.Global.SwitchWorkspace}" />
7875
</Grid>
7976

8077
<TextBlock Text="{DynamicResource Text.Hotkeys.Repo}"

src/Views/Launcher.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
</Button>
7474

7575
<!-- Workspace Switcher -->
76-
<Button Grid.Column="1" Classes="icon_button" Name="WorkspacesButton" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenWorkspaceMenu">
76+
<Button Grid.Column="1" Classes="icon_button" VerticalAlignment="Bottom" Margin="0,0,0,1" Click="OnOpenWorkspaceMenu">
7777
<ToolTip.Tip>
7878
<StackPanel Orientation="Horizontal">
7979
<TextBlock Text="{DynamicResource Text.Workspace}" FontWeight="Bold" Foreground="{DynamicResource Brush.FG2}"/>

src/Views/Launcher.axaml.cs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
100100

101101
if (change.Property == WindowStateProperty)
102102
{
103-
_lastWindowState = (WindowState)change.OldValue;
103+
_lastWindowState = (WindowState)change.OldValue!;
104104

105105
var state = (WindowState)change.NewValue!;
106106
if (!OperatingSystem.IsMacOS() && !UseSystemWindowFrame)
@@ -250,24 +250,11 @@ protected override void OnKeyDown(KeyEventArgs e)
250250
}
251251
else if (e.KeyModifiers.HasFlag(KeyModifiers.Alt))
252252
{
253-
if (e.Key == Key.Space && DataContext is ViewModels.Launcher launcher)
253+
if (SwitchWorkspace(e.Key))
254254
{
255-
var menu = launcher.CreateContextForWorkspace();
256-
var workspacesButton = this.FindControl<Button>("WorkspacesButton");
257-
if (menu != null)
258-
{
259-
menu.PlacementTarget = workspacesButton;
260-
menu.Placement = PlacementMode.BottomEdgeAlignedLeft;
261-
menu.Open(workspacesButton);
262-
}
263-
}
264-
else
265-
{
266-
SwitchToWorkspaceIndex(e.Key);
255+
e.Handled = true;
256+
return;
267257
}
268-
269-
e.Handled = true;
270-
return;
271258
}
272259
else if (e.Key == Key.Escape)
273260
{
@@ -303,29 +290,6 @@ protected override void OnKeyDown(KeyEventArgs e)
303290
}
304291
}
305292

306-
private void SwitchToWorkspaceIndex(Key eKey)
307-
{
308-
int newIndex;
309-
switch (eKey)
310-
{
311-
case Key.D1 or Key.NumPad1: newIndex = 0; break;
312-
case Key.D2 or Key.NumPad2: newIndex = 1; break;
313-
case Key.D3 or Key.NumPad3: newIndex = 2; break;
314-
case Key.D4 or Key.NumPad4: newIndex = 3; break;
315-
case Key.D5 or Key.NumPad5: newIndex = 4; break;
316-
case Key.D6 or Key.NumPad6: newIndex = 5; break;
317-
case Key.D7 or Key.NumPad7: newIndex = 6; break;
318-
case Key.D8 or Key.NumPad8: newIndex = 7; break;
319-
case Key.D9 or Key.NumPad9: newIndex = 8; break;
320-
default: return;
321-
}
322-
323-
if (DataContext is ViewModels.Launcher launcher)
324-
{
325-
launcher.SwitchWorkspace(newIndex);
326-
}
327-
}
328-
329293
protected override void OnKeyUp(KeyEventArgs e)
330294
{
331295
base.OnKeyUp(e);
@@ -350,6 +314,44 @@ private void OnOpenWorkspaceMenu(object sender, RoutedEventArgs e)
350314

351315
e.Handled = true;
352316
}
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+
}
353355

354356
private KeyModifiers _unhandledModifiers = KeyModifiers.None;
355357
private WindowState _lastWindowState = WindowState.Normal;

0 commit comments

Comments
 (0)