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

[Android & iOS] Use the correct colors by themes with Alerts on Android and iOS #13318

Merged
merged 10 commits into from
Mar 14, 2023
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;

namespace Microsoft.Maui.Controls.Core.UnitTests
{
Expand All @@ -14,6 +15,8 @@ class ApplicationStub : IApplication

public string Property { get; set; } = "Default";

public AppTheme UserAppTheme { get; set; }

public IWindow CreateWindow(IActivationState activationState)
{
throw new System.NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Controls;

namespace Microsoft.Maui.DeviceTests.Stubs
Expand Down Expand Up @@ -64,6 +65,8 @@ void OnActivated(object sender, UI.Xaml.WindowActivatedEventArgs args)

public IElement Parent => null;

public AppTheme UserAppTheme { get; set; }

public void CloseWindow(IWindow window)
{
Handler?.Invoke(nameof(IApplication.CloseWindow), window);
Expand Down
10 changes: 10 additions & 0 deletions src/Core/src/Core/IApplication.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;

namespace Microsoft.Maui
{
Expand Down Expand Up @@ -31,6 +32,15 @@ public interface IApplication : IElement
/// <param name="window">The window to close.</param>
void CloseWindow(IWindow window);

/// <summary>
/// Gets the current requested theme by the system for your application.
/// The return value will be one of the following:
/// - Unspecified
/// - Light
/// - Dark
/// </summary>
AppTheme UserAppTheme { get; }

/// <summary>
/// Notify a theme change.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using AndroidX.AppCompat.App;
using AndroidX.Window.Layout;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Devices;
using Microsoft.Maui.Hosting;

Expand Down Expand Up @@ -76,7 +78,10 @@ static void OnConfigureWindow(IAndroidLifecycleBuilder android)
{
if (IPlatformApplication.Current is IPlatformApplication platformApplication)
{
platformApplication.Application?.ThemeChanged();
var application = platformApplication.Application;

application?.UpdateNightMode();
application?.ThemeChanged();
}

var mauiWindow = activity.GetWindow();
Expand Down
21 changes: 21 additions & 0 deletions src/Core/src/Platform/Android/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using Android.App;
using Android.Content;
using Android.OS;
using AndroidX.AppCompat.App;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.LifecycleEvents;

Expand Down Expand Up @@ -60,5 +62,24 @@ public static Bundle ToBundle(this IPersistedState? state)

return userInfo;
}

public static void UpdateNightMode(this IApplication application)
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Should this be called inside ThemeChanged so if the theme changes this gets updated also? Seems this way it will only run initially.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is invoked every time the theme changes here

Do you mean to invoke it inside the ThemeChanged method? ThemeChanged fires the RequestedThemeChanged event from the .NET MAUI Application. I could move it inside but we would have to either use compiler directives or create a service or similar and use the interface from Application.

{
if (application is null)
return;

switch (application.UserAppTheme)
{
case AppTheme.Light:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;
break;
case AppTheme.Dark:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightYes;
break;
default:
AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightFollowSystem;
break;
}
}
}
}
25 changes: 25 additions & 0 deletions src/Core/src/Platform/iOS/ApplicationExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.Versioning;
using Foundation;
using Microsoft.Extensions.Logging;
using Microsoft.Maui.ApplicationModel;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.LifecycleEvents;
using ObjCRuntime;
Expand Down Expand Up @@ -125,5 +126,29 @@ public static NSUserActivity ToUserActivity(this IPersistedState? state, string

return userActivity;
}

public static void UpdateUserInterfaceStyle(this IApplication application)
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
{
if (application is null)
return;

var currentViewController = WindowStateManager.Default.GetCurrentUIViewController(false);
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved

if (currentViewController is null)
return;

switch (application.UserAppTheme)
{
case AppTheme.Light:
currentViewController.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
break;
case AppTheme.Dark:
currentViewController.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Dark;
break;
default:
currentViewController.OverrideUserInterfaceStyle = UIUserInterfaceStyle.Unspecified;
break;
}
}
}
}
5 changes: 3 additions & 2 deletions src/Core/src/Platform/iOS/PageViewController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Maui.Handlers;
using ObjCRuntime;
using Microsoft.Maui.ApplicationModel;
using UIKit;

namespace Microsoft.Maui.Platform
Expand Down Expand Up @@ -28,6 +27,8 @@ public override void TraitCollectionDidChange(UITraitCollection? previousTraitCo
if (CurrentView?.Handler is ElementHandler handler)
{
var application = handler.GetRequiredService<IApplication>();

application?.UpdateUserInterfaceStyle();
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
application?.ThemeChanged();
}

Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/PublicAPI/net-android/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
Microsoft.Maui.FontSize.Equals(Microsoft.Maui.FontSize other) -> bool
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand All @@ -22,6 +23,7 @@ static Microsoft.Maui.Layouts.LayoutExtensions.ArrangeContentUnbounded(this Micr
Microsoft.Maui.IWebView.UserAgent.get -> string?
Microsoft.Maui.IWebView.UserAgent.set -> void
static Microsoft.Maui.Handlers.WebViewHandler.MapUserAgent(Microsoft.Maui.Handlers.IWebViewHandler! handler, Microsoft.Maui.IWebView! webView) -> void
static Microsoft.Maui.Platform.ApplicationExtensions.UpdateNightMode(this Microsoft.Maui.IApplication! application) -> void
static Microsoft.Maui.Platform.ViewGroupExtensions.TryGetFirstChildOfType<T>(this Android.Views.ViewGroup! viewGroup, out T? result) -> bool
static Microsoft.Maui.Platform.WebViewExtensions.UpdateUserAgent(this Android.Webkit.WebView! platformWebView, Microsoft.Maui.IWebView! webView) -> void
*REMOVED*Microsoft.Maui.WeakEventManager.HandleEvent(object! sender, object! args, string! eventName) -> void
Expand Down
2 changes: 2 additions & 0 deletions src/Core/src/PublicAPI/net-ios/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Microsoft.Maui.Handlers.SwipeItemButton
Microsoft.Maui.Handlers.SwipeItemButton.FrameChanged -> System.EventHandler?
Microsoft.Maui.Handlers.SwipeItemButton.SwipeItemButton() -> void
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.LifecycleEvents.iOSLifecycle.PerformFetch
Expand Down Expand Up @@ -29,6 +30,7 @@ override Microsoft.Maui.Platform.MauiTextView.WillMoveToWindow(UIKit.UIWindow? w
Microsoft.Maui.IWebView.UserAgent.get -> string?
Microsoft.Maui.IWebView.UserAgent.set -> void
static Microsoft.Maui.Handlers.WebViewHandler.MapUserAgent(Microsoft.Maui.Handlers.IWebViewHandler! handler, Microsoft.Maui.IWebView! webView) -> void
static Microsoft.Maui.Platform.ApplicationExtensions.UpdateUserInterfaceStyle(this Microsoft.Maui.IApplication! application) -> void
static Microsoft.Maui.Platform.WebViewExtensions.UpdateUserAgent(this WebKit.WKWebView! platformWebView, Microsoft.Maui.IWebView! webView) -> void
*REMOVED*Microsoft.Maui.WeakEventManager.HandleEvent(object! sender, object! args, string! eventName) -> void
Microsoft.Maui.WeakEventManager.HandleEvent(object? sender, object? args, string! eventName) -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Microsoft.Maui.Handlers.SwipeItemButton
Microsoft.Maui.Handlers.SwipeItemButton.FrameChanged -> System.EventHandler?
Microsoft.Maui.Handlers.SwipeItemButton.SwipeItemButton() -> void
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand All @@ -28,6 +29,7 @@ override Microsoft.Maui.Platform.MauiTextView.WillMoveToWindow(UIKit.UIWindow? w
Microsoft.Maui.IWebView.UserAgent.get -> string?
Microsoft.Maui.IWebView.UserAgent.set -> void
static Microsoft.Maui.Handlers.WebViewHandler.MapUserAgent(Microsoft.Maui.Handlers.IWebViewHandler! handler, Microsoft.Maui.IWebView! webView) -> void
static Microsoft.Maui.Platform.ApplicationExtensions.UpdateUserInterfaceStyle(this Microsoft.Maui.IApplication! application) -> void
static Microsoft.Maui.Platform.WebViewExtensions.UpdateUserAgent(this WebKit.WKWebView! platformWebView, Microsoft.Maui.IWebView! webView) -> void
*REMOVED*Microsoft.Maui.WeakEventManager.HandleEvent(object! sender, object! args, string! eventName) -> void
Microsoft.Maui.WeakEventManager.HandleEvent(object? sender, object? args, string! eventName) -> void
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-tizen/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.Platform.MauiWebView.MauiWebView(Microsoft.Maui.Handlers.WebViewHandler! handler) -> void
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
Microsoft.Maui.IApplication.UserAppTheme.get -> Microsoft.Maui.ApplicationModel.AppTheme
Microsoft.Maui.Hosting.MauiApp.DisposeAsync() -> System.Threading.Tasks.ValueTask
Microsoft.Maui.Layouts.FlexBasis.Equals(Microsoft.Maui.Layouts.FlexBasis other) -> bool
Microsoft.Maui.SizeRequest.Equals(Microsoft.Maui.SizeRequest other) -> bool
Expand Down
3 changes: 3 additions & 0 deletions src/Core/tests/Benchmarks/Stubs/ApplicationStub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;

namespace Microsoft.Maui.Handlers.Benchmarks
{
Expand All @@ -12,6 +13,8 @@ class ApplicationStub : IApplication

public IReadOnlyList<IWindow> Windows => _windows.AsReadOnly();

public AppTheme UserAppTheme { get; set; }

public IWindow CreateWindow(IActivationState state)
{
_windows.Add(new WindowStub());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;

namespace Microsoft.Maui.DeviceTests.Stubs
{
Expand All @@ -12,6 +13,8 @@ class CoreApplicationStub : IApplication

public IReadOnlyList<IWindow> Windows => _windows.AsReadOnly();

public AppTheme UserAppTheme { get; set; }

public IWindow CreateWindow(IActivationState state)
{
_windows.Add(new WindowStub());
Expand Down
3 changes: 3 additions & 0 deletions src/Core/tests/UnitTests/TestClasses/ApplicationStub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using Microsoft.Maui.ApplicationModel;

namespace Microsoft.Maui.UnitTests
{
Expand All @@ -12,6 +13,8 @@ class ApplicationStub : IApplication

public IReadOnlyList<IWindow> Windows => _windows.AsReadOnly();

public AppTheme UserAppTheme { get; set; }

public string Property { get; set; } = "Default";

public IWindow CreateWindow(IActivationState activationState)
Expand Down