Skip to content

Commit e0d3540

Browse files
committed
feature: move main menu to macOS system menu bar
1 parent 764cf24 commit e0d3540

File tree

7 files changed

+66
-34
lines changed

7 files changed

+66
-34
lines changed

src/App.axaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<Application xmlns="https://github.com/avaloniaui"
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:s="using:SourceGit"
34
x:Class="SourceGit.App"
45
Name="SourceGit"
56
RequestedThemeVariant="Dark">
@@ -21,4 +22,16 @@
2122
<StyleInclude Source="avares://AvaloniaEdit/Themes/Fluent/AvaloniaEdit.xaml" />
2223
<StyleInclude Source="/Resources/Styles.axaml"/>
2324
</Application.Styles>
25+
26+
<NativeMenu.Menu>
27+
<NativeMenu>
28+
<NativeMenuItem Header="{DynamicResource Text.About}" Command="{x:Static s:App.OpenAboutCommand}"/>
29+
<NativeMenuItem Header="{DynamicResource Text.Hotkeys}" Command="{x:Static s:App.OpenHotkeysCommand}"/>
30+
<NativeMenuItem Header="{DynamicResource Text.SelfUpdate}" Command="{x:Static s:App.CheckForUpdateCommand}"/>
31+
<NativeMenuItem Header="-"/>
32+
<NativeMenuItem Header="{DynamicResource Text.Preference}" Command="{x:Static s:App.OpenPreferenceCommand}"/>
33+
<NativeMenuItem Header="-"/>
34+
<NativeMenuItem Header="{DynamicResource Text.Quit}" Command="{x:Static s:App.QuitCommand}"/>
35+
</NativeMenu>
36+
</NativeMenu.Menu>
2437
</Application>

src/App.axaml.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using System.Text.Json;
77
using System.Threading.Tasks;
8+
using System.Windows.Input;
89

910
using Avalonia;
1011
using Avalonia.Controls;
@@ -18,9 +19,27 @@
1819

1920
namespace SourceGit
2021
{
21-
public partial class App : Application
22+
public class SimpleCommand : ICommand
2223
{
24+
public event EventHandler CanExecuteChanged
25+
{
26+
add { }
27+
remove { }
28+
}
29+
30+
public SimpleCommand(Action action)
31+
{
32+
_action = action;
33+
}
34+
35+
public bool CanExecute(object parameter) => _action != null;
36+
public void Execute(object parameter) => _action?.Invoke();
2337

38+
private Action _action = null;
39+
}
40+
41+
public partial class App : Application
42+
{
2443
[STAThread]
2544
public static void Main(string[] args)
2645
{
@@ -67,6 +86,31 @@ public static AppBuilder BuildAvaloniaApp()
6786
return builder;
6887
}
6988

89+
public static readonly SimpleCommand OpenPreferenceCommand = new SimpleCommand(() =>
90+
{
91+
var dialog = new Views.Preference();
92+
dialog.ShowDialog(GetTopLevel() as Window);
93+
});
94+
95+
public static readonly SimpleCommand OpenHotkeysCommand = new SimpleCommand(() =>
96+
{
97+
var dialog = new Views.Hotkeys();
98+
dialog.ShowDialog(GetTopLevel() as Window);
99+
});
100+
101+
public static readonly SimpleCommand OpenAboutCommand = new SimpleCommand(() =>
102+
{
103+
var dialog = new Views.About();
104+
dialog.ShowDialog(GetTopLevel() as Window);
105+
});
106+
107+
public static readonly SimpleCommand CheckForUpdateCommand = new SimpleCommand(() =>
108+
{
109+
Check4Update(true);
110+
});
111+
112+
public static readonly SimpleCommand QuitCommand = new SimpleCommand(Quit);
113+
70114
public static void RaiseException(string context, string message)
71115
{
72116
if (Current is App app && app._notificationReceiver != null)

src/Native/MacOS.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public void SetupApp(AppBuilder builder)
2222

2323
builder.With(new MacOSPlatformOptions()
2424
{
25-
DisableNativeMenus = true,
2625
DisableDefaultApplicationMenuItems = true,
2726
});
2827
}

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
<x:String x:Key="Text.PushTag" xml:space="preserve">Push Tag To Remote</x:String>
311311
<x:String x:Key="Text.PushTag.Remote" xml:space="preserve">Remote :</x:String>
312312
<x:String x:Key="Text.PushTag.Tag" xml:space="preserve">Tag :</x:String>
313+
<x:String x:Key="Text.Quit" xml:space="preserve">Quit</x:String>
313314
<x:String x:Key="Text.Rebase" xml:space="preserve">Rebase Current Branch</x:String>
314315
<x:String x:Key="Text.Rebase.AutoStash" xml:space="preserve">Stash &amp; reapply local changes</x:String>
315316
<x:String x:Key="Text.Rebase.On" xml:space="preserve">On :</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@
310310
<x:String x:Key="Text.PushTag" xml:space="preserve">推送标签到远程仓库</x:String>
311311
<x:String x:Key="Text.PushTag.Remote" xml:space="preserve">远程仓库 :</x:String>
312312
<x:String x:Key="Text.PushTag.Tag" xml:space="preserve">标签 :</x:String>
313+
<x:String x:Key="Text.Quit" xml:space="preserve">退出</x:String>
313314
<x:String x:Key="Text.Rebase" xml:space="preserve">变基(rebase)操作</x:String>
314315
<x:String x:Key="Text.Rebase.AutoStash" xml:space="preserve">自动贮藏并恢复本地变更</x:String>
315316
<x:String x:Key="Text.Rebase.On" xml:space="preserve">目标提交 :</x:String>

src/Views/Launcher.axaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
33
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:s="using:SourceGit"
56
xmlns:vm="using:SourceGit.ViewModels"
67
xmlns:m="using:SourceGit.Models"
78
xmlns:c="using:SourceGit.Converters"
@@ -49,31 +50,31 @@
4950
</Border>
5051

5152
<!-- Menu -->
52-
<Button Grid.Column="{OnPlatform 0, macOS=2}" Classes="icon_button" VerticalAlignment="Bottom">
53+
<Button Grid.Column="0" Classes="icon_button" VerticalAlignment="Bottom" IsVisible="{OnPlatform True, macOS=False}">
5354
<Button.Margin>
5455
<OnPlatform Default="4,0,2,3" macOS="4,0,6,3"/>
5556
</Button.Margin>
5657

5758
<Button.Flyout>
5859
<MenuFlyout Placement="BottomEdgeAlignedLeft" VerticalOffset="-8">
59-
<MenuItem Header="{DynamicResource Text.Preference}" Click="OpenPreference">
60+
<MenuItem Header="{DynamicResource Text.Preference}" Command="{x:Static s:App.OpenPreferenceCommand}">
6061
<MenuItem.Icon>
6162
<Path Width="14" Height="14" Data="{StaticResource Icons.Settings2}"/>
6263
</MenuItem.Icon>
6364
</MenuItem>
64-
<MenuItem Header="{DynamicResource Text.Hotkeys}" Click="OpenHotkeys">
65+
<MenuItem Header="{DynamicResource Text.Hotkeys}" Command="{x:Static s:App.OpenHotkeysCommand}">
6566
<MenuItem.Icon>
6667
<Path Width="14" Height="14" Data="{StaticResource Icons.Hotkeys}"/>
6768
</MenuItem.Icon>
6869
</MenuItem>
6970
<MenuItem Header="-"/>
70-
<MenuItem Header="{DynamicResource Text.SelfUpdate}" Click="Check4Update">
71+
<MenuItem Header="{DynamicResource Text.SelfUpdate}" Command="{x:Static s:App.CheckForUpdateCommand}">
7172
<MenuItem.Icon>
7273
<Path Width="14" Height="14" Data="{StaticResource Icons.SoftwareUpdate}"/>
7374
</MenuItem.Icon>
7475
</MenuItem>
7576
<MenuItem Header="-"/>
76-
<MenuItem Header="{DynamicResource Text.About}" Click="OpenAboutDialog">
77+
<MenuItem Header="{DynamicResource Text.About}" Command="{x:Static s:App.OpenAboutCommand}">
7778
<MenuItem.Icon>
7879
<Path Width="14" Height="14" Data="{StaticResource Icons.Info}"/>
7980
</MenuItem.Icon>

src/Views/Launcher.axaml.cs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -337,33 +337,6 @@ private void OnPopupCancelByClickMask(object sender, PointerPressedEventArgs e)
337337
OnPopupCancel(sender, e);
338338
}
339339

340-
private async void OpenPreference(object sender, RoutedEventArgs e)
341-
{
342-
var dialog = new Preference();
343-
await dialog.ShowDialog(this);
344-
e.Handled = true;
345-
}
346-
347-
private async void OpenHotkeys(object sender, RoutedEventArgs e)
348-
{
349-
var dialog = new Hotkeys();
350-
await dialog.ShowDialog(this);
351-
e.Handled = true;
352-
}
353-
354-
private void Check4Update(object sender, RoutedEventArgs e)
355-
{
356-
App.Check4Update(true);
357-
e.Handled = true;
358-
}
359-
360-
private async void OpenAboutDialog(object sender, RoutedEventArgs e)
361-
{
362-
var dialog = new About();
363-
await dialog.ShowDialog(this);
364-
e.Handled = true;
365-
}
366-
367340
private bool _pressedTab = false;
368341
private Point _pressedTabPosition = new Point();
369342
private bool _startDrag = false;

0 commit comments

Comments
 (0)