Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[macOS] Implement IsEnabled property in MenuFlyoutItems #14920

Merged
merged 28 commits into from
Aug 28, 2023
Merged

Conversation

jsuarezruiz
Copy link
Contributor

@jsuarezruiz jsuarezruiz commented May 4, 2023

Description of Change

Implement IsEnabled property in MenuFlyoutItems on Catalyst.

(Work in progress)

Menu
Captura de pantalla 2023-05-04 a las 17 44 23

Flyout
flyoutitems-isenabled

Issues Fixed

Fixes #9583
Fixes #15896

@jsuarezruiz jsuarezruiz added t/bug Something isn't working platform/macOS 🍏 macOS / Mac Catalyst area-controls-contextmenu ContextMenu labels May 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

Thank you for your pull request. We are auto-formatting your source code to follow our code guidelines.

@Eilon Eilon added the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 5, 2023

Thank you for your pull request. We are auto-formatting your source code to follow our code guidelines.

@jsuarezruiz jsuarezruiz marked this pull request as ready for review June 19, 2023 09:37
@jsuarezruiz jsuarezruiz marked this pull request as draft June 28, 2023 15:46
@samhouts samhouts added this to the .NET 8 GA milestone Jul 26, 2023
@hartez
Copy link
Contributor

hartez commented Aug 15, 2023

Also, what happens with Command.CanExecute?

I just tested this, and when the Command's CanExecute changes, the state of the menu item changes.

@hartez
Copy link
Contributor

hartez commented Aug 15, 2023

Also, is the IsEnabled not propagating to all the children in the xplat code?

It doesn't appear to propagate; setting a parent Menu Item to IsEnabled=False does not modify the child item in any way. On Windows, it doesn't matter - disabling a menu item with sub items disables it completely, to the point that you can't even reveal the sub items. So their state becomes moot.

So we need to answer the question of what should happen here - should disabling a menu with sub items propagate the xplat property? Or should we just handle that on the platform side (which is doable in Catalyst, it just takes a little extra code).

@PureWeen @mattleibow thoughts?

@hartez hartez marked this pull request as ready for review August 17, 2023 02:30
@hartez hartez requested a review from a team as a code owner August 17, 2023 02:30
@hartez hartez self-requested a review August 17, 2023 02:30
@mattleibow
Copy link
Member

We probably should use the same method that I did for VisualElement. We do propagate that enabled value, but not actually - otherwise we end up not knowing if we should re-enable.

On the base menu element, you have a protected property:

protected virtual bool IsEnabledCore
{
get
{
if (_isEnabledExplicit == false)
{
// If the explicitly set value is false, then nothing else matters
// And we can save the effort of a Parent check
return false;
}
var parent = Parent as VisualElement;
if (parent is not null && !parent.IsEnabled)
return false;
return _isEnabledExplicit;
}
}

The real IsEnabled property will coerce the value and make sure to do the math but preserve the original value:

static object CoerceIsEnabledProperty(BindableObject bindable, object value)
{
if (bindable is VisualElement visualElement)
{
visualElement._isEnabledExplicit = (bool)value;
return visualElement.IsEnabledCore;
}
return false;
}

The coersion will fire for all controls and disable if something is disabled, but caches the real value in a field.

And it will propagate the enabled value:

void IPropertyPropagationController.PropagatePropertyChanged(string propertyName)
{
if (propertyName == null || propertyName == IsEnabledProperty.PropertyName)
this.RefreshPropertyValue(IsEnabledProperty, _isEnabledExplicit);

In the derived menu items, it just overrides the IsEnabledCore with any additional things - like the command:

void ICommandElement.CanExecuteChanged(object sender, EventArgs e) =>
RefreshIsEnabledProperty();
protected override bool IsEnabledCore =>
base.IsEnabledCore && CommandElement.GetCanExecute(this);

Copy link
Member

@mattleibow mattleibow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe separate blocks?

src/Core/src/Handlers/MenuBarItem/MenuBarItemHandler.cs Outdated Show resolved Hide resolved
Co-authored-by: Matthew Leibowitz <mattleibow@live.com>
@PureWeen PureWeen enabled auto-merge (squash) August 28, 2023 18:48
@PureWeen PureWeen merged commit cce1a2a into main Aug 28, 2023
38 checks passed
@PureWeen PureWeen deleted the fix-9583 branch August 28, 2023 19:30
@mattleibow
Copy link
Member

woohoo!

@github-actions github-actions bot locked and limited conversation to collaborators Dec 10, 2023
@Eilon Eilon removed the legacy-area-controls Label, Button, CheckBox, Slider, Stepper, Switch, Picker, Entry, Editor label May 10, 2024
@samhouts samhouts added the fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 label Aug 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-controls-contextmenu ContextMenu fixed-in-8.0.0-rc.1.9171 Look for this fix in 8.0.0-rc.1.9171 platform/macOS 🍏 macOS / Mac Catalyst t/bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Disable MenuFlyoutItem IsEnabled currently doesn't do anything when set on a ContextMenu
6 participants