Cache the dynamic AppTheme value in Controls (and not Essentials)#11200
Cache the dynamic AppTheme value in Controls (and not Essentials)#11200mattleibow merged 18 commits intomainfrom
Conversation
jonathanpeppers
left a comment
There was a problem hiding this comment.
What is the LOLs per second impact of this change?
You can try this app: https://github.com/jonathanpeppers/lols/tree/net7.0
|
what's the ETA of this issue being resolved? The RequestedThemeChanged-Event is flimsy at best in both Android and iOS. With the trigger reacting only once in a blue moon and the app turning completely blank if you change the theme from the settings menu in iOS and try to switch back to the running app. |
A bunch of changes so the code is fairly different.
| FlowController.EffectiveFlowDirection = mauiContext.GetFlowDirection().ToEffectiveFlowDirection(true); | ||
| var flowDirection = Application?.PlatformLayoutDirection.ToFlowDirection() ?? FlowDirection.LeftToRight; | ||
| FlowController.EffectiveFlowDirection = flowDirection.ToEffectiveFlowDirection(true); |
There was a problem hiding this comment.
Is this only called once per new Window created?
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
There was a problem hiding this comment.
Reverting the layout direction changes as this is a larger thing and affect locale. Android supports configuration changes on Application and Activity, and each have different effects and requirements.
I still reverted the lazy layout direction in the AppInfo as this just fires once when the handler changes - which should basically be once. This code also means that the Activity will need to restart if the locale changes. And this is done automatically by the OS because we don't have the ConfigChanges.Locale on the [Activity].
src/Controls/src/Core/HandlerImpl/Application/Application.Impl.cs
Outdated
Show resolved
Hide resolved
| FlowController.EffectiveFlowDirection = mauiContext.GetFlowDirection().ToEffectiveFlowDirection(true); | ||
| var flowDirection = Application?.PlatformLayoutDirection.ToFlowDirection() ?? FlowDirection.LeftToRight; | ||
| FlowController.EffectiveFlowDirection = flowDirection.ToEffectiveFlowDirection(true); |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| void IApplication.ThemeChanged() | ||
| { | ||
| if (UserAppTheme != AppTheme.Unspecified) | ||
| return; | ||
|
|
||
| TriggerThemeChangedActual(); | ||
| PlatformAppTheme = AppInfo.RequestedTheme; | ||
| } |
There was a problem hiding this comment.
Instead of being smart here, we just cache the new value from the OS and let the application decide what it wants to do. It is the same class, but now the property responds in a normal way to raise an event if the value has changed.
| static readonly Lazy<AppTheme> _requestedTheme = new Lazy<AppTheme>(GetRequestedTheme); | ||
| static readonly Lazy<LayoutDirection> _layoutDirection = new Lazy<LayoutDirection>(GetLayoutDirection); |
There was a problem hiding this comment.
We can't cache these as things in the Application.Context.Resources.Configuration are all dynamic - changeable by both the use and the OS internal processing.
|
/backport to net7.0 |
|
Started backporting to net7.0: https://github.com/dotnet/maui/actions/runs/4427242243 |
|
@Redth backporting to net7.0 failed, the patch most likely resulted in conflicts: $ git am --3way --ignore-whitespace --keep-non-patch changes.patch
Applying: Fix RequestedThemeChanged on Android
Using index info to reconstruct a base tree...
M src/Essentials/src/AppInfo/AppInfo.android.cs
Falling back to patching base and 3-way merge...
Auto-merging src/Essentials/src/AppInfo/AppInfo.android.cs
CONFLICT (content): Merge conflict in src/Essentials/src/AppInfo/AppInfo.android.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
Patch failed at 0001 Fix RequestedThemeChanged on Android
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
Error: The process '/usr/bin/git' failed with exit code 128Please backport manually! |
|
@Redth an error occurred while backporting to net7.0, please check the run log for details! Error: git am failed, most likely due to a merge conflict. |
…1200) Previously, the RequestedAppTheme and RequestedLayoutDiection values were cached on first access and this was incorrect as they can change at runtime - both by the OS and the user. This PR makes a few changes: Don't cache the theme and layout in Essentials as there is no way to update the cache Don't cache the layout anywhere because this is only accessed once when the window is [re]created Cache the theme in the Controls Application where the events can actually handle. --------- Co-authored-by: Rui Marinho <me@ruimarinho.net> Co-authored-by: Matthew Leibowitz <mattleibow@live.com> # Conflicts: # src/Essentials/src/AppInfo/AppInfo.android.cs
…1200) (#13963) Previously, the RequestedAppTheme and RequestedLayoutDiection values were cached on first access and this was incorrect as they can change at runtime - both by the OS and the user. This PR makes a few changes: Don't cache the theme and layout in Essentials as there is no way to update the cache Don't cache the layout anywhere because this is only accessed once when the window is [re]created Cache the theme in the Controls Application where the events can actually handle. --------- Co-authored-by: Rui Marinho <me@ruimarinho.net> Co-authored-by: Matthew Leibowitz <mattleibow@live.com> # Conflicts: # src/Essentials/src/AppInfo/AppInfo.android.cs Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
…1200) Previously, the RequestedAppTheme and RequestedLayoutDiection values were cached on first access and this was incorrect as they can change at runtime - both by the OS and the user. This PR makes a few changes: Don't cache the theme and layout in Essentials as there is no way to update the cache Don't cache the layout anywhere because this is only accessed once when the window is [re]created Cache the theme in the Controls Application where the events can actually handle. --------- Co-authored-by: Rui Marinho <me@ruimarinho.net> Co-authored-by: Matthew Leibowitz <mattleibow@live.com> # Conflicts: # src/Essentials/src/AppInfo/AppInfo.android.cs
…1200) (#14072) Previously, the RequestedAppTheme and RequestedLayoutDiection values were cached on first access and this was incorrect as they can change at runtime - both by the OS and the user. This PR makes a few changes: Don't cache the theme and layout in Essentials as there is no way to update the cache Don't cache the layout anywhere because this is only accessed once when the window is [re]created Cache the theme in the Controls Application where the events can actually handle. --------- Co-authored-by: Rui Marinho <me@ruimarinho.net> Co-authored-by: Matthew Leibowitz <mattleibow@live.com> # Conflicts: # src/Essentials/src/AppInfo/AppInfo.android.cs Co-authored-by: Javier Suárez <javiersuarezruiz@hotmail.com>
|
Not sure why but I am on the latest Visual Studio (17.6.2) Maui version, and I still see #8236 - it will only fire once (i.e. if I register in App constructor, RequestThemeChanged will fire exactly the first time the user manually changes the system theme. Subsequent changes do not raise the event (ie. breakpoint is not hit) This is NOT FIXED. public App()
{
RequestedThemeChanged += (s, a) => ConfigureTheme(a.RequestedTheme);
// ... more initialization logic
}
private void ConfigureTheme(AppTheme requestedTheme)
{
// On Android: Breakpoint here will only hit on first toggle between Light and Dark Mode
//ConfigChanges.UiMode is set in MainActivity.cs according to MS documentation
Log.Debug($"Requested {requestedTheme} theme.");
UserAppTheme = requestedTheme;
} |
Description of Change
Previously, the
RequestedAppThemeandRequestedLayoutDiectionvalues were cached on first access and this was incorrect as they can change at runtime - both by the OS and the user.This PR makes a few changes:
Issues Fixed
Fixes #8236 also related with #11005
Fixes (partially) #11299
Fixes #8606
This issue was inadvertently introduced in #7996 when all the Essentials fields were cached, when a few were dynamic.
Tasks
Device tests (not sure if this is possible as it requires a manual interaction)