diff --git a/native/Avalonia.Native/src/OSX/menu.h b/native/Avalonia.Native/src/OSX/menu.h index 1bea2ccc8d6..eb706d3e07b 100644 --- a/native/Avalonia.Native/src/OSX/menu.h +++ b/native/Avalonia.Native/src/OSX/menu.h @@ -51,7 +51,9 @@ class AvnAppMenuItem : public ComSingleObject virtual HRESULT SetAction (IAvnPredicateCallback* predicate, IAvnActionCallback* callback) override; virtual HRESULT SetIsChecked (bool isChecked) override; - + + virtual HRESULT SetIsVisible (bool isVisible) override; + virtual HRESULT SetToggleType (AvnMenuItemToggleType toggleType) override; virtual HRESULT SetIcon (void* data, size_t length) override; diff --git a/native/Avalonia.Native/src/OSX/menu.mm b/native/Avalonia.Native/src/OSX/menu.mm index 3905987aab4..7a7edcb1cb1 100644 --- a/native/Avalonia.Native/src/OSX/menu.mm +++ b/native/Avalonia.Native/src/OSX/menu.mm @@ -205,6 +205,17 @@ - (void)didSelectItem:(nullable id)sender } } +HRESULT AvnAppMenuItem::SetIsVisible (bool isVisible) +{ + START_COM_CALL; + + @autoreleasepool + { + [_native setHidden:!isVisible]; + return S_OK; + } +} + HRESULT AvnAppMenuItem::SetToggleType(AvnMenuItemToggleType toggleType) { START_COM_CALL; diff --git a/samples/ControlCatalog/App.xaml b/samples/ControlCatalog/App.xaml index 519fb3ec7a9..3999191696a 100644 --- a/samples/ControlCatalog/App.xaml +++ b/samples/ControlCatalog/App.xaml @@ -75,6 +75,7 @@ + diff --git a/src/Avalonia.Controls/NativeMenuBarPresenter.cs b/src/Avalonia.Controls/NativeMenuBarPresenter.cs index be93f9dc852..c5637d3dd42 100644 --- a/src/Avalonia.Controls/NativeMenuBarPresenter.cs +++ b/src/Avalonia.Controls/NativeMenuBarPresenter.cs @@ -26,6 +26,7 @@ internal class NativeMenuBarPresenter : Menu [!MenuItem.IconProperty] = nativeItem.GetObservable(NativeMenuItem.IconProperty) .Select(i => i is { } bitmap ? new Image { Source = bitmap } : null).ToBinding(), [!MenuItem.IsEnabledProperty] = nativeItem.GetObservable(NativeMenuItem.IsEnabledProperty).ToBinding(), + [!MenuItem.IsVisibleProperty] = nativeItem.GetObservable(NativeMenuItem.IsVisibleProperty).ToBinding(), [!MenuItem.CommandProperty] = nativeItem.GetObservable(NativeMenuItem.CommandProperty).ToBinding(), [!MenuItem.CommandParameterProperty] = nativeItem.GetObservable(NativeMenuItem.CommandParameterProperty).ToBinding(), diff --git a/src/Avalonia.Controls/NativeMenuItem.cs b/src/Avalonia.Controls/NativeMenuItem.cs index 72f71027689..d67e6a58fc0 100644 --- a/src/Avalonia.Controls/NativeMenuItem.cs +++ b/src/Avalonia.Controls/NativeMenuItem.cs @@ -144,6 +144,21 @@ public bool IsEnabled set => SetValue(IsEnabledProperty, value); } + /// + /// Defines the property. + /// + public static readonly StyledProperty IsVisibleProperty = + Visual.IsVisibleProperty.AddOwner(); + + /// + /// Gets or sets a value indicating whether this menu item is visible. + /// + public bool IsVisible + { + get => GetValue(IsVisibleProperty); + set => SetValue(IsVisibleProperty, value); + } + void CanExecuteChanged() { SetCurrentValue(IsEnabledProperty, Command?.CanExecute(CommandParameter) ?? true); diff --git a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs index ef9b7c26c3a..fcc118bd31f 100644 --- a/src/Avalonia.FreeDesktop/DBusMenuExporter.cs +++ b/src/Avalonia.FreeDesktop/DBusMenuExporter.cs @@ -243,6 +243,13 @@ private int GetId(NativeMenuItemBase item) return new DBusVariantItem("b", new DBusBoolItem(false)); return null; } + + if (name == "visible") { + if (!item.IsVisible) + return new DBusVariantItem("b", new DBusBoolItem(false)); + return new DBusVariantItem("b", new DBusBoolItem(true)); + } + if (name == "shortcut") { if (item.Gesture is null) diff --git a/src/Avalonia.Native/IAvnMenuItem.cs b/src/Avalonia.Native/IAvnMenuItem.cs index 50f64deccac..275a0888215 100644 --- a/src/Avalonia.Native/IAvnMenuItem.cs +++ b/src/Avalonia.Native/IAvnMenuItem.cs @@ -40,6 +40,7 @@ private void UpdateTitle(string title) private void UpdateToolTip(string toolTip) => SetToolTip(toolTip ?? ""); + private void UpdateIsVisible(bool isVisible) => SetIsVisible(isVisible.AsComBool()); private void UpdateIsChecked(bool isChecked) => SetIsChecked(isChecked.AsComBool()); private void UpdateToggleType(NativeMenuItemToggleType toggleType) @@ -121,6 +122,8 @@ internal void Initialize(NativeMenuItemBase nativeMenuItem) UpdateIsChecked(item.IsChecked); + UpdateIsVisible(item.IsVisible); + _propertyDisposables.Add(ManagedMenuItem.GetObservable(NativeMenuItem.HeaderProperty) .Subscribe(x => UpdateTitle(x))); @@ -139,6 +142,9 @@ internal void Initialize(NativeMenuItemBase nativeMenuItem) _propertyDisposables.Add(ManagedMenuItem.GetObservable(NativeMenuItem.IsCheckedProperty) .Subscribe(x => UpdateIsChecked(x))); + _propertyDisposables.Add(ManagedMenuItem.GetObservable(NativeMenuItem.IsVisibleProperty) + .Subscribe(x => UpdateIsVisible(x))); + _propertyDisposables.Add(ManagedMenuItem.GetObservable(NativeMenuItem.IconProperty) .Subscribe(x => UpdateIcon(x))); } diff --git a/src/Avalonia.Native/avn.idl b/src/Avalonia.Native/avn.idl index 27da2fc8947..3a715c33eec 100644 --- a/src/Avalonia.Native/avn.idl +++ b/src/Avalonia.Native/avn.idl @@ -1026,6 +1026,7 @@ interface IAvnMenuItem : IUnknown HRESULT SetGesture(AvnKey key, AvnInputModifiers modifiers); HRESULT SetAction(IAvnPredicateCallback* predicate, IAvnActionCallback* callback); HRESULT SetIsChecked(bool isChecked); + HRESULT SetIsVisible(bool isVisible); HRESULT SetToggleType(AvnMenuItemToggleType toggleType); HRESULT SetIcon(void* data, size_t length); }