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 @@ -52,7 +52,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 @@ -169,15 +170,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 @@ -216,6 +236,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 @@ -112,10 +112,7 @@ public virtual void CloseWindow(Window window)

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

TriggerThemeChangedActual();
PlatformAppTheme = AppInfo.RequestedTheme;
}

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 @@ -6,6 +6,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 @@ -562,7 +563,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();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need the 'as'?

Copy link
Member

Choose a reason for hiding this comment

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

If we want these changes we should do a PR to main and not lose the changes in this backport PR.

I don't think we need to block the backport PR on this, but perhaps we get a PR to main with the fixes before we complete this backport PR?

}

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;
}
}
}
32 changes: 19 additions & 13 deletions src/Essentials/src/AppInfo/AppInfo.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ class AppInfoImplementation : IAppInfo
{
static readonly Lazy<string> _name = new Lazy<string>(() => Application.Context.ApplicationInfo.LoadLabel(Application.Context.PackageManager));
static readonly Lazy<string> _packageName = new Lazy<string>(() => Application.Context.PackageName);
#pragma warning disable 618
#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 618
static readonly Lazy<AppTheme> _requestedTheme = new Lazy<AppTheme>(GetRequestedTheme);
static readonly Lazy<LayoutDirection> _layoutDirection = new Lazy<LayoutDirection>(GetLayoutDirection);
#pragma warning restore CS0618, CA1416, CA1422

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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (config == null)
if (config is 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();
}
}