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

Setting Background to null will update rendering on Android and iOS #24726

Closed
wants to merge 1 commit into from

Conversation

spadapet
Copy link
Contributor

@spadapet spadapet commented Sep 11, 2024

Description of Change

When Background was set to null for some ViewElements, the Background of the platform element wasn't updated at all for Android and iOS. I just removed the "if" check that skipped setting Background to null.

Issues Fixed

Fixes #24725
Fixes #22914

(maybe more if you look at similar bugs linked in those bugs)

@spadapet spadapet self-assigned this Sep 11, 2024
@spadapet spadapet requested a review from a team as a code owner September 11, 2024 22:40
Copy link
Member

@PureWeen PureWeen left a comment

Choose a reason for hiding this comment

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

Yea, this is somewhat intentional, but we didn't really stay consistent here with WinUI unfortunately which makes this confusing.

The one problem with null is that it's somewhat undefined. Like, when you set the xplat background to null what does that mean?

For WinUI when you set it to null it basically reverts the style back to the templated style. This choice is a bit random because in WinUI land setting a background to "null" actually has a different meaning then reverting it or setting it to transparent. I wish we never would have done this on WinUI because now it's just confusing.

So, in theory for Android and iOS if we were to stay consistent,setting to null would mean revert to platform defaults (I haven't quite looked here if that's what you're doing)

It becomes really, really hard to define what "null" means across the spectrum of all properties you can possibly set and then trying to extract those from the platform.

Realistically if you want to "Clear" a property what you should do is call "ClearValue" on the property at which point it will revert back to whatever your style is for that property.

We haven't really seen a lot of movement on this being a problem for users that "null" is essentially a noop, so we haven't really made an effort to make "null" do anything.

@spadapet
Copy link
Contributor Author

spadapet commented Sep 11, 2024

Yea, this is somewhat intentional, but we didn't really stay consistent here with WinUI unfortunately which makes this confusing.

@PureWeen thanks for looking, I understand more now.

I think in the code I'm changing, the final computed Background brush is being used. So it's not just what the Label has directly set on it, but it takes applied styles into account.

My change is just expanding a previous PR from @rmarinho :

That PR limited the fix to Layout, I made it include everything.

I started looking into this based on customer feedback. A customer showed me a bug where he was using XAML Hot Reload, typed Background="Yellow" on a Label, then deleted it. But the Label stayed yellow. The same bug applies to ContentPage, Border, etc.

Plus my bug is a dupe of another customer's bug:

So there are some customers hitting the bug. I just don't know all of the little details of background=null. If this PR doesn't get in, then I could add a workaround in XAML Hot Reload, but that is not ideal since it involves rebuilding the element from scratch.

@mattleibow mattleibow added the area-theme Themes, theming label Sep 12, 2024
@PureWeen
Copy link
Member

PureWeen commented Sep 21, 2024

Plus my bug is a dupe of another customer's bug:

Yea, this is somewhat the same point. The fact that this works on Windows I think was a huge mistake on our part. Making sure to define "null" for every platform color property starts to gets a bit complicated.

Like, let's say that the user has set the background to "yellow" in styles on initial load and then they change it to purple and then set it to null. What should we do here?

The idea with MAUI is that all color-based properties would have a default color. That's why we include a style.xaml with MAUI templates. Then whenever the user calls "ClearValue" that would just reset to the defined style color.

I started looking into this based on customer feedback. A customer showed me a bug where he was using XAML Hot Reload, typed Background="Yellow" on a Label, then deleted it. But the Label stayed yellow. The same bug applies to ContentPage, Border, etc.

I would be curious in XAML Hot reload what is happening when you set "Background" to "Yellow" and then you delete it. Is it setting "Background" to null? Or is it calling "ClearValue" with some level of setter specificity on "Background"

That PR limited the fix to Layout, I made it include everything.

Yea, @rmarinho's change was specific to layouts because we know what the default backgroundcolor for those controls are since they are just custom views. I don't know if we can generally apply this to every control. For example, have we confirmed that the default color for the Android Switch control is null? And a Button? And a slider?

@spadapet
Copy link
Contributor Author

@PureWeen,

The fact that this works on Windows I think was a huge mistake on our part. Making sure to define "null" for every platform color property starts to gets a bit complicated.

Like, let's say that the user has set the background to "yellow" in styles on initial load and then they change it to purple and then set it to null. What should we do here?

In WPF that would be clear: a null brush is transparent and not hit-testable. Maybe MAUI can't get that behavior on every platform. But in this bug, Background is never actually set to null, it's just that there is no style applied to the Label, so it ends up with the Brush.Default value (which is a null color). Normally Label's have a style with a default Background, so this bug only affects people that specifically remove default styles.

The idea with MAUI is that all color-based properties would have a default color. That's why we include a style.xaml with MAUI templates. Then whenever the user calls "ClearValue" that would just reset to the defined style color.

I would be curious in XAML Hot reload what is happening when you set "Background" to "Yellow" and then you delete it. Is it setting "Background" to null? Or is it calling "ClearValue" with some level of setter specificity on "Background"

BindableObject.ClearValue is called when XAML Hot Reload removes the Background property. I mentioned this above, but Background is never actually set to null.

image

That PR limited the fix to Layout, I made it include everything.

Yea, @rmarinho's change was specific to layouts because we know what the default backgroundcolor for those controls are since they are just custom views. I don't know if we can generally apply this to every control. For example, have we confirmed that the default color for the Android Switch control is null? And a Button? And a slider?

I see. If the multi-platform complexities make this difficult to fix in MAUI, I can look into having XAML Hot Reload rebuild the whole control when Background is removed, or just live with the bug since the behavior is undefined anyway.

@spadapet
Copy link
Contributor Author

spadapet commented Oct 4, 2024

Closing this, based on the discussion. This PR shouldn't break an intentional design with platform defaults.

@spadapet spadapet closed this Oct 4, 2024
@spadapet spadapet deleted the dev/peterspa/BackgroundNull branch October 4, 2024 21:38
@github-actions github-actions bot locked and limited conversation to collaborators Nov 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-theme Themes, theming
Projects
None yet
3 participants