csharpc-sharpdotnetxamlavaloniauicross-platformcross-platform-xamlavaloniaguimulti-platformuser-interfacedotnetcore
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.6 KiB
54 lines
1.6 KiB
using System.Collections.Generic;
|
|
using Avalonia.Controls;
|
|
using Avalonia.Controls.Platform;
|
|
|
|
#nullable enable
|
|
|
|
namespace Avalonia.Win32
|
|
{
|
|
internal class Win32NativeToManagedMenuExporter : INativeMenuExporter
|
|
{
|
|
private NativeMenu? _nativeMenu;
|
|
|
|
public void SetNativeMenu(NativeMenu? nativeMenu)
|
|
{
|
|
_nativeMenu = nativeMenu;
|
|
}
|
|
|
|
private IEnumerable<MenuItem> Populate(NativeMenu nativeMenu)
|
|
{
|
|
foreach (var menuItem in nativeMenu.Items)
|
|
{
|
|
if (menuItem is NativeMenuItemSeparator)
|
|
{
|
|
yield return new MenuItem { Header = "-" };
|
|
}
|
|
else if (menuItem is NativeMenuItem item)
|
|
{
|
|
var newItem = new MenuItem { Header = item.Header, Icon = item.Icon, Command = item.Command, CommandParameter = item.CommandParameter };
|
|
|
|
if (item.Menu != null)
|
|
{
|
|
newItem.Items = Populate(item.Menu);
|
|
}
|
|
else if (item.HasClickHandlers && item is INativeMenuItemExporterEventsImplBridge bridge)
|
|
{
|
|
newItem.Click += (_, __) => bridge.RaiseClicked();
|
|
}
|
|
|
|
yield return newItem;
|
|
}
|
|
}
|
|
}
|
|
|
|
public IEnumerable<MenuItem>? GetMenu()
|
|
{
|
|
if (_nativeMenu != null)
|
|
{
|
|
return Populate(_nativeMenu);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|