Skip to content

Commit

Permalink
feat(Mac): use Native Menu
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic committed Sep 8, 2024
1 parent f160731 commit 5640ad6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/KubeUI/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,10 @@ public override void OnFrameworkInitializationCompleted()

if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = MainWindow.Build();
desktop.MainWindow.DataContext = _serviceProvider.GetRequiredService<MainViewModel>();
var vm = _serviceProvider.GetRequiredService<MainViewModel>();

desktop.MainWindow = MainWindow.Build(vm);
desktop.MainWindow.DataContext = vm;
TopLevel = TopLevel.GetTopLevel(desktop.MainWindow);
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
Expand Down
6 changes: 4 additions & 2 deletions src/KubeUI/Views/MainView.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Dock.Avalonia.Controls;
using System.Runtime.InteropServices;
using Dock.Avalonia.Controls;

namespace KubeUI.Views;

Expand Down Expand Up @@ -45,7 +46,8 @@ protected override object Build(MainViewModel vm) =>
.Header(Assets.Resources.MainView_Menu_Help_About)
.Command(vm.OpenAboutCommand),
])
]),
])
.IsVisible(() => !RuntimeInformation.IsOSPlatform(OSPlatform.OSX)),
new DockControl()
.Row(1)
.Margin(4)
Expand Down
63 changes: 61 additions & 2 deletions src/KubeUI/Views/MainVindow.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,50 @@
namespace KubeUI.Views;
using AvaloniaEdit.Utils;

namespace KubeUI.Views;

public static class MainWindow
{
public static Window Build() =>
public static Window Build(MainViewModel vm) =>
(Window)new Window()
.Title("KubeUI")
.UseLayoutRounding(true)
.SetProp(NativeMenu.MenuProperty, new NativeMenu()
.Items([
new NativeMenuItem()
.Header(Resources.MainView_Menu_File)
.Items([
new NativeMenuItem()
.Header(Resources.MainView_Menu_File_Clusters)
.Command(vm.OpenClustersCommand),
new NativeMenuItem()
.Header(Resources.MainView_Menu_File_Settings)
.Command(vm.OpenSettingsCommand),

new NativeMenuItemSeparator(),

new NativeMenuItem()
.Header(Resources.MainView_Menu_File_Exit)
.Command(vm.CloseCommand),
]),
new NativeMenuItem()
.Header(Resources.MainView_Menu_Window)
.Items([
new NativeMenuItem()
.Header(Resources.MainView_Menu_Window_ResetLayout)
.Command(vm.ResetLayoutCommand),
new NativeMenuItem()
.Header(Resources.MainView_Menu_Window_SwitchTheme)
.Command(vm.SwitchThemeCommand),
]),
new NativeMenuItem()
.Header(Resources.MainView_Menu_Help)
.Items([
new NativeMenuItem()
.Header(Resources.MainView_Menu_Help_About)
.Command(vm.OpenAboutCommand),
])
])
)
.Content(
new Panel()
.Children([
Expand All @@ -14,3 +53,23 @@ public static Window Build() =>
)
);
}


public static class WindowExtensions
{
public static NativeMenu Items(this NativeMenu menu, NativeMenuItemBase[] items)
{
menu.Items.AddRange(items);

return menu;
}

public static NativeMenuItem Items(this NativeMenuItem menu, NativeMenuItemBase[] items)
{
menu.Menu ??= [];

menu.Menu.Items.AddRange(items);

return menu;
}
}

0 comments on commit 5640ad6

Please sign in to comment.