Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions src/Controls/src/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ internal Application(bool setCurrentApplication)

_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<Application>>(() => new PlatformConfigurationRegistry<Application>(this));

_lastAppTheme = PlatformAppTheme;
_platformAppTheme = AppInfo.RequestedTheme;
_lastAppTheme = _platformAppTheme;

_platformLayoutDirection = AppInfo.RequestedLayoutDirection;
Comment thread
mattleibow marked this conversation as resolved.
Outdated
}

/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='Quit']/Docs/*" />
Expand Down Expand Up @@ -164,15 +167,46 @@ public AppTheme UserAppTheme
get => _userAppTheme;
set
{
if (_userAppTheme == value)
return;

_userAppTheme = value;

TriggerThemeChangedActual();
}
}

public AppTheme PlatformAppTheme => AppInfo.RequestedTheme;
public AppTheme PlatformAppTheme
{
get => _platformAppTheme;
private set
{
if (_platformAppTheme == value)
return;

_platformAppTheme = value;

TriggerThemeChangedActual();
}
}

internal LayoutDirection PlatformLayoutDirection
{
get => _platformLayoutDirection;
private set
{
if (_platformLayoutDirection == value)
return;

_platformLayoutDirection = value;
}
}

/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='RequestedTheme']/Docs/*" />
public AppTheme RequestedTheme => UserAppTheme != AppTheme.Unspecified ? UserAppTheme : PlatformAppTheme;
public AppTheme RequestedTheme =>
UserAppTheme != AppTheme.Unspecified
? UserAppTheme
: PlatformAppTheme;

static Color? _accentColor;
public static Color? AccentColor
Expand Down Expand Up @@ -211,8 +245,10 @@ public event EventHandler<AppThemeChangedEventArgs> RequestedThemeChanged
}

bool _themeChangedFiring;
AppTheme _platformAppTheme = AppTheme.Unspecified;
AppTheme _lastAppTheme = AppTheme.Unspecified;
AppTheme _userAppTheme = AppTheme.Unspecified;
LayoutDirection _platformLayoutDirection = LayoutDirection.Unknown;

void TriggerThemeChangedActual()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,8 @@ public virtual void CloseWindow(Window window)

void IApplication.ThemeChanged()
{
if (UserAppTheme != AppTheme.Unspecified)
return;

TriggerThemeChangedActual();
PlatformAppTheme = AppInfo.RequestedTheme;
PlatformLayoutDirection = AppInfo.RequestedLayoutDirection;
Comment thread
mattleibow marked this conversation as resolved.
Outdated
}
Comment on lines 112 to 115

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.


protected virtual Window CreateWindow(IActivationState? activationState)
Expand Down
3 changes: 2 additions & 1 deletion src/Controls/src/Core/HandlerImpl/Window/Window.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,8 @@ private protected override void OnHandlerChangingCore(HandlerChangingEventArgs a

if (FlowDirection == FlowDirection.MatchParent && mauiContext != null)
{
FlowController.EffectiveFlowDirection = mauiContext.GetFlowDirection().ToEffectiveFlowDirection(true);
var flowDirection = Application?.PlatformLayoutDirection.ToFlowDirection() ?? FlowDirection.LeftToRight;
FlowController.EffectiveFlowDirection = flowDirection.ToEffectiveFlowDirection(true);
Comment on lines -564 to +566

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this only called once per new Window created?

This comment was marked as outdated.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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].

}
}

Expand Down
10 changes: 0 additions & 10 deletions src/Core/src/MauiContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,5 @@ public static void InitializeScopedServices(this IMauiContext scopedContext)
foreach (var service in scopedServices)
service.Initialize(scopedContext.Services);
}

public static FlowDirection GetFlowDirection(this IMauiContext mauiContext)
{
var appInfo = AppInfo.Current;

if (appInfo.RequestedLayoutDirection == LayoutDirection.RightToLeft)
return FlowDirection.RightToLeft;

return FlowDirection.LeftToRight;
}
}
}
28 changes: 17 additions & 11 deletions src/Essentials/src/AppInfo/AppInfo.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class AppInfoImplementation : IAppInfo
#pragma warning disable CS0618, CA1416, CA1422 // Deprecated in API 33: https://developer.android.com/reference/android/content/pm/PackageManager#getPackageInfo(java.lang.String,%20int)
static readonly Lazy<PackageInfo> _packageInfo = new Lazy<PackageInfo>(() => Application.Context.PackageManager.GetPackageInfo(_packageName.Value, PackageInfoFlags.MetaData));
#pragma warning restore CS0618, CA1416, CA1422
static readonly Lazy<AppTheme> _requestedTheme = new Lazy<AppTheme>(GetRequestedTheme);
static readonly Lazy<LayoutDirection> _layoutDirection = new Lazy<LayoutDirection>(GetLayoutDirection);
Comment on lines -19 to -20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.


public string PackageName => _packageName.Value;

Expand All @@ -44,14 +42,21 @@ public void ShowSettingsUI()
context.StartActivity(settingsIntent);
}

static AppTheme GetRequestedTheme() => (Application.Context.Resources.Configuration.UiMode & UiMode.NightMask) switch
static AppTheme GetRequestedTheme()
{
UiMode.NightYes => AppTheme.Dark,
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};
var config = Application.Context.Resources?.Configuration;
Comment thread
mattleibow marked this conversation as resolved.
if (config == null)
return AppTheme.Unspecified;

return (config.UiMode & UiMode.NightMask) switch
{
UiMode.NightYes => AppTheme.Dark,
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};
}

public AppTheme RequestedTheme => _requestedTheme.Value;
public AppTheme RequestedTheme => GetRequestedTheme();

public AppPackagingModel PackagingModel => AppPackagingModel.Packaged;

Expand All @@ -61,10 +66,11 @@ static LayoutDirection GetLayoutDirection()
if (config == null)
return LayoutDirection.Unknown;

return (config.LayoutDirection == Android.Views.LayoutDirection.Rtl) ? LayoutDirection.RightToLeft :
LayoutDirection.LeftToRight;
return (config.LayoutDirection == Android.Views.LayoutDirection.Rtl)
? LayoutDirection.RightToLeft
: LayoutDirection.LeftToRight;
}

public LayoutDirection RequestedLayoutDirection => _layoutDirection.Value;
public LayoutDirection RequestedLayoutDirection => GetLayoutDirection();
}
}