Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Maui.Controls.Sample.Platform
| ConfigChanges.UiMode
| ConfigChanges.SmallestScreenSize
| ConfigChanges.KeyboardHidden
| ConfigChanges.Density
| ConfigChanges.Density
)]
[IntentFilter(
new[] { Microsoft.Maui.ApplicationModel.Platform.Intent.ActionAppAction },
Expand Down
27 changes: 24 additions & 3 deletions src/Controls/src/Core/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ internal Application(bool setCurrentApplication)

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

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

/// <include file="../../docs/Microsoft.Maui.Controls/Application.xml" path="//Member[@MemberName='Quit']/Docs/*" />
Expand Down Expand Up @@ -164,15 +165,34 @@ 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();
}
}

/// <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,6 +231,7 @@ public event EventHandler<AppThemeChangedEventArgs> RequestedThemeChanged
}

bool _themeChangedFiring;
AppTheme _platformAppTheme = AppTheme.Unspecified;
AppTheme _lastAppTheme = AppTheme.Unspecified;
AppTheme _userAppTheme = AppTheme.Unspecified;

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

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

TriggerThemeChangedActual();
PlatformAppTheme = AppInfo.RequestedTheme;
}
Comment on lines 112 to 115
Copy link
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
4 changes: 3 additions & 1 deletion src/Controls/src/Core/HandlerImpl/Window/Window.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Graphics;
Expand Down Expand Up @@ -561,7 +562,8 @@ private protected override void OnHandlerChangingCore(HandlerChangingEventArgs a

if (FlowDirection == FlowDirection.MatchParent && mauiContext != null)
{
FlowController.EffectiveFlowDirection = mauiContext.GetFlowDirection().ToEffectiveFlowDirection(true);
var flowDirection = AppInfo.Current.RequestedLayoutDirection.ToFlowDirection();
FlowController.EffectiveFlowDirection = flowDirection.ToEffectiveFlowDirection(true);
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/Controls/tests/Core.UnitTests/MockPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ public class MockApplication : Application
public MockApplication()
{
}

public void NotifyThemeChanged() =>
(this as IApplication).ThemeChanged();
}

internal class MockTicker : Ticker
Expand Down
29 changes: 18 additions & 11 deletions src/Controls/tests/Xaml.UnitTests/OnAppThemeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace Microsoft.Maui.Controls.Xaml.UnitTests
public class OnAppThemeTests : BaseTestFixture
{
MockAppInfo mockAppInfo;
MockApplication mockApp;

[SetUp]
public override void Setup()
{
base.Setup();
AppInfo.SetCurrent(mockAppInfo = new MockAppInfo());
Application.Current = new MockApplication();
Application.Current = mockApp = new MockApplication();
}

[TearDown]
Expand All @@ -36,11 +37,11 @@ public void OnAppThemeExtensionLightDarkColor()
xmlns:x=""http://schemas.microsoft.com/winfx/2009/xaml"" TextColor=""{AppThemeBinding Light = Green, Dark = Red}
"">This text is green or red depending on Light (or default) or Dark</Label>";

mockAppInfo.RequestedTheme = AppTheme.Light;
SetAppTheme(AppTheme.Light);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);

mockAppInfo.RequestedTheme = AppTheme.Dark;
SetAppTheme(AppTheme.Dark);
label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Red, label.TextColor);
}
Expand All @@ -58,11 +59,11 @@ public void OnAppThemeLightDarkColor()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Light;
SetAppTheme(AppTheme.Light);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);

mockAppInfo.RequestedTheme = AppTheme.Dark;
SetAppTheme(AppTheme.Dark);
label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Red, label.TextColor);
}
Expand All @@ -80,7 +81,7 @@ public void OnAppThemeUnspecifiedThemeDefaultsToLightColor()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Unspecified;
SetAppTheme(AppTheme.Unspecified);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);
}
Expand All @@ -98,7 +99,7 @@ public void OnAppThemeUnspecifiedLightColorDefaultsToDefault()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Light;
SetAppTheme(AppTheme.Light);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);
}
Expand All @@ -116,11 +117,11 @@ public void AppThemeColorLightDark()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Light;
SetAppTheme(AppTheme.Light);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);

mockAppInfo.RequestedTheme = AppTheme.Dark;
SetAppTheme(AppTheme.Dark);
label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Red, label.TextColor);
}
Expand All @@ -138,7 +139,7 @@ public void AppThemeColorUnspecifiedThemeDefaultsToLightColor()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Unspecified;
SetAppTheme(AppTheme.Unspecified);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);
}
Expand All @@ -156,9 +157,15 @@ public void AppThemeColorUnspecifiedLightColorDefaultsToDefault()
</Label.TextColor>
</Label> ";

mockAppInfo.RequestedTheme = AppTheme.Unspecified;
SetAppTheme(AppTheme.Unspecified);
var label = new Label().LoadFromXaml(xaml);
Assert.AreEqual(Colors.Green, label.TextColor);
}

void SetAppTheme(AppTheme theme)
{
mockAppInfo.RequestedTheme = theme;
mockApp.NotifyThemeChanged();
}
}
}
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
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;
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();
}
}