Browse Source

[OSX] AvaloniaNativeMenuExporter, populates the users menu with the standard OSX items.

pull/7341/head
Dan Walmsley 4 years ago
parent
commit
74adcfd17b
  1. 111
      src/Avalonia.Native/AvaloniaNativeMenuExporter.cs

111
src/Avalonia.Native/AvaloniaNativeMenuExporter.cs

@ -70,74 +70,76 @@ namespace Avalonia.Native
var result = new NativeMenu(); var result = new NativeMenu();
var aboutItem = new NativeMenuItem("About Avalonia"); var aboutItem = new NativeMenuItem("About Avalonia");
aboutItem.Click += async (sender, e) =>
aboutItem.Click += async (_, _) =>
{ {
var dialog = new AboutAvaloniaDialog(); var dialog = new AboutAvaloniaDialog();
var mainWindow = (Application.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow; if (Application.Current is
{ ApplicationLifetime: IClassicDesktopStyleApplicationLifetime { MainWindow: { } mainWindow } })
await dialog.ShowDialog(mainWindow); {
await dialog.ShowDialog(mainWindow);
}
}; };
result.Add(aboutItem); result.Add(aboutItem);
var macOpts = AvaloniaLocator.Current.GetService<MacOSPlatformOptions>() ?? new MacOSPlatformOptions(); return result;
if (!macOpts.DisableDefaultApplicationMenuItems) }
{
result.Add(new NativeMenuItemSeparator());
var servicesMenu = new NativeMenuItem("Services"); private void PopulateStandardOSXMenuItems(NativeMenu appMenu)
servicesMenu.Menu = new NativeMenu {
{ appMenu.Add(new NativeMenuItemSeparator());
[MacOSNativeMenuCommands.IsServicesSubmenuProperty] = true
};
result.Add(servicesMenu);
result.Add(new NativeMenuItemSeparator()); var servicesMenu = new NativeMenuItem("Services");
servicesMenu.Menu = new NativeMenu { [MacOSNativeMenuCommands.IsServicesSubmenuProperty] = true };
var hideItem = new NativeMenuItem("Hide " + Application.Current.Name) appMenu.Add(servicesMenu);
{
Gesture = new KeyGesture(Key.H, KeyModifiers.Meta)
};
hideItem.Click += (sender, args) =>
{
_applicationCommands.HideApp();
};
result.Add(hideItem);
appMenu.Add(new NativeMenuItemSeparator());
var hideOthersItem = new NativeMenuItem("Hide Others") var hideItem = new NativeMenuItem("Hide " + (Application.Current?.Name ?? "Application"))
{ {
Gesture = new KeyGesture(Key.Q, KeyModifiers.Meta | KeyModifiers.Alt) Gesture = new KeyGesture(Key.H, KeyModifiers.Meta)
}; };
hideOthersItem.Click += (sender, args) =>
{
_applicationCommands.HideOthers();
};
result.Add(hideOthersItem);
hideItem.Click += (_, _) =>
{
_applicationCommands.HideApp();
};
var showAllItem = new NativeMenuItem("Show All"); appMenu.Add(hideItem);
showAllItem.Click += (sender, args) =>
{ var hideOthersItem = new NativeMenuItem("Hide Others")
_applicationCommands.ShowAll(); {
}; Gesture = new KeyGesture(Key.Q, KeyModifiers.Meta | KeyModifiers.Alt)
result.Add(showAllItem); };
hideOthersItem.Click += (_, _) =>
{
_applicationCommands.HideOthers();
};
appMenu.Add(hideOthersItem);
result.Add(new NativeMenuItemSeparator()); var showAllItem = new NativeMenuItem("Show All");
showAllItem.Click += (_, _) =>
{
_applicationCommands.ShowAll();
};
var quitItem = new NativeMenuItem("Quit") appMenu.Add(showAllItem);
{
Gesture = new KeyGesture(Key.Q, KeyModifiers.Meta)
};
quitItem.Click += (sender, args) =>
{
(Application.Current.ApplicationLifetime as IControlledApplicationLifetime)?.Shutdown();
};
result.Add(quitItem);
}
appMenu.Add(new NativeMenuItemSeparator());
return result; var quitItem = new NativeMenuItem("Quit") { Gesture = new KeyGesture(Key.Q, KeyModifiers.Meta) };
quitItem.Click += (_, _) =>
{
if (Application.Current is { ApplicationLifetime: IControlledApplicationLifetime lifetime })
{
lifetime.Shutdown();
}
};
appMenu.Add(quitItem);
} }
private void DoLayoutReset(bool forceUpdate = false) private void DoLayoutReset(bool forceUpdate = false)
@ -220,6 +222,13 @@ namespace Avalonia.Native
_nativeMenu.Initialize(this, appMenuHolder, ""); _nativeMenu.Initialize(this, appMenuHolder, "");
var macOpts = AvaloniaLocator.Current.GetService<MacOSPlatformOptions>();
if (macOpts == null || !macOpts.DisableDefaultApplicationMenuItems)
{
PopulateStandardOSXMenuItems(menu);
}
setMenu = true; setMenu = true;
} }

Loading…
Cancel
Save