Skip to content

Commit

Permalink
IsVisible for NativeMenuItem(s) (#14567)
Browse files Browse the repository at this point in the history
* IsVisible for nativemenuitems

* implementation for windows

* Update native/Avalonia.Native/src/OSX/menu.mm

Co-authored-by: jp2masa <jp2masa@users.noreply.github.com>

* Update menu.h

* address review changes

* add hidden option to ControlCatalog

---------

Co-authored-by: jp2masa <jp2masa@users.noreply.github.com>
Co-authored-by: Max Katz <maxkatz6@outlook.com>
  • Loading branch information
3 people authored Feb 20, 2024
1 parent 7fb2663 commit d6f5e7d
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 1 deletion.
4 changes: 3 additions & 1 deletion native/Avalonia.Native/src/OSX/menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ class AvnAppMenuItem : public ComSingleObject<IAvnMenuItem, &IID_IAvnMenuItem>
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;
Expand Down
11 changes: 11 additions & 0 deletions native/Avalonia.Native/src/OSX/menu.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions samples/ControlCatalog/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
<NativeMenuItem Header="Option 3" ToggleType="CheckBox" IsChecked="True" />
<NativeMenuItem Icon="/Assets/test_icon.ico" Header="Restore Defaults" Command="{Binding RestoreDefault}" />
<NativeMenuItem Header="Disabled option" IsEnabled="False" />
<NativeMenuItem Header="Hidden option" IsVisible="False" />
</NativeMenu>
</NativeMenuItem>
<NativeMenuItem Header="Exit" Command="{Binding ExitCommand}" />
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Controls/NativeMenuBarPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 15 additions & 0 deletions src/Avalonia.Controls/NativeMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,21 @@ public bool IsEnabled
set => SetValue(IsEnabledProperty, value);
}

/// <summary>
/// Defines the <see cref="IsVisible"/> property.
/// </summary>
public static readonly StyledProperty<bool> IsVisibleProperty =
Visual.IsVisibleProperty.AddOwner<NativeMenuItem>();

/// <summary>
/// Gets or sets a value indicating whether this menu item is visible.
/// </summary>
public bool IsVisible
{
get => GetValue(IsVisibleProperty);
set => SetValue(IsVisibleProperty, value);
}

void CanExecuteChanged()
{
SetCurrentValue(IsEnabledProperty, Command?.CanExecute(CommandParameter) ?? true);
Expand Down
7 changes: 7 additions & 0 deletions src/Avalonia.FreeDesktop/DBusMenuExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions src/Avalonia.Native/IAvnMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)));

Expand All @@ -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)));
}
Expand Down
1 change: 1 addition & 0 deletions src/Avalonia.Native/avn.idl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit d6f5e7d

Please sign in to comment.