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

Remove IPlatformServices.RequestedTheme and use Essentials #4964

Merged
merged 11 commits into from
Feb 28, 2022
4 changes: 2 additions & 2 deletions .nuspec/maui-blazor.aotprofile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1697,8 +1697,8 @@ Methods:
Microsoft.Maui.Controls.IVisual Microsoft.Maui.Controls.VisualMarker:get_MatchParent ()
Microsoft.Maui.Controls.LayoutOptions Microsoft.Maui.Controls.View:get_HorizontalOptions ()
Microsoft.Maui.Controls.LayoutOptions Microsoft.Maui.Controls.View:get_VerticalOptions ()
Microsoft.Maui.Controls.OSAppTheme Microsoft.Maui.Controls.Application:get_RequestedTheme ()
Microsoft.Maui.Controls.OSAppTheme Microsoft.Maui.Controls.Compatibility.Forms/AndroidPlatformServices:get_RequestedTheme ()
Microsoft.Maui.Controls.AppTheme Microsoft.Maui.Controls.Application:get_RequestedTheme ()
Microsoft.Maui.Controls.AppTheme Microsoft.Maui.Controls.Compatibility.Forms/AndroidPlatformServices:get_RequestedTheme ()
Microsoft.Maui.Controls.Page Microsoft.Maui.Controls.Application:get_MainPage ()
Microsoft.Maui.Controls.Page Microsoft.Maui.Controls.Window:get_Page ()
Microsoft.Maui.Controls.ResourceDictionary Microsoft.Maui.Controls.Application:get_Resources ()
Expand Down
4 changes: 2 additions & 2 deletions .nuspec/maui.aotprofile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,8 @@ Methods:
Microsoft.Maui.Controls.IVisual Microsoft.Maui.Controls.VisualMarker:get_MatchParent ()
Microsoft.Maui.Controls.LayoutOptions Microsoft.Maui.Controls.View:get_HorizontalOptions ()
Microsoft.Maui.Controls.LayoutOptions Microsoft.Maui.Controls.View:get_VerticalOptions ()
Microsoft.Maui.Controls.OSAppTheme Microsoft.Maui.Controls.Application:get_RequestedTheme ()
Microsoft.Maui.Controls.OSAppTheme Microsoft.Maui.Controls.Compatibility.Forms/AndroidPlatformServices:get_RequestedTheme ()
Microsoft.Maui.Controls.AppTheme Microsoft.Maui.Controls.Application:get_RequestedTheme ()
Microsoft.Maui.Controls.AppTheme Microsoft.Maui.Controls.Compatibility.Forms/AndroidPlatformServices:get_RequestedTheme ()
Microsoft.Maui.Controls.Page Microsoft.Maui.Controls.Application:get_MainPage ()
Microsoft.Maui.Controls.Page Microsoft.Maui.Controls.Window:get_Page ()
Microsoft.Maui.Controls.ResourceDictionary Microsoft.Maui.Controls.Application:get_Resources ()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public override void OnConfigurationChanged(Configuration newConfig)
base.OnConfigurationChanged(newConfig);
ConfigurationChanged?.Invoke(this, new EventArgs());

Microsoft.Maui.Controls.Application.Current?.TriggerThemeChanged(new AppThemeChangedEventArgs(Microsoft.Maui.Controls.Application.Current.RequestedTheme));
Microsoft.Maui.Controls.Application.Current?.ThemeChanged();
}

public override bool OnOptionsItemSelected(IMenuItem item)
Expand Down
31 changes: 1 addition & 30 deletions src/Compatibility/Core/src/Android/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,7 @@ static void SetupInit(
// because AndroidPlatformServices needs a current activity to launch URIs from
Profile.FramePartition("Device.PlatformServices");

var androidServices = new AndroidPlatformServices(activity);

Device.PlatformServices = androidServices;
Device.DefaultRendererAssembly = typeof(Forms).Assembly;

Profile.FramePartition("RegisterAll");

Expand Down Expand Up @@ -317,32 +315,5 @@ protected override Expression VisitMember(MemberExpression node)
return base.VisitMember(node);
}
}

class AndroidPlatformServices : IPlatformServices
{
readonly Context _context;

public AndroidPlatformServices(Context context)
{
_context = context;
}

public OSAppTheme RequestedTheme
{
get
{
var nightMode = _context.Resources.Configuration.UiMode & UiMode.NightMask;
switch (nightMode)
{
case UiMode.NightYes:
return OSAppTheme.Dark;
case UiMode.NightNo:
return OSAppTheme.Light;
default:
return OSAppTheme.Unspecified;
};
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/GTK/GtkPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, dou
return Platform.GetNativeSize(view, widthConstraint, heightConstraint);
}

public OSAppTheme RequestedTheme => OSAppTheme.Unspecified;
public AppTheme RequestedTheme => AppTheme.Unspecified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Platform;
using Microsoft.Maui.Controls.Platform.Compatibility;
using Microsoft.Maui.Essentials;
using Microsoft.Maui.Graphics;
using AColor = Android.Graphics.Color;
using ARect = Android.Graphics.Rect;
Expand Down Expand Up @@ -98,7 +99,7 @@ void IAppearanceObserver.OnAppearanceChanged(ShellAppearance appearance)
public static readonly Color DefaultUnselectedColor = Color.FromRgba(255, 255, 255, 180);
internal static Color DefaultBottomNavigationViewBackgroundColor => ResolveThemeColor(Colors.White, Color.FromArgb("#1B3147"));

internal static bool IsDarkTheme => (Application.Current?.RequestedTheme == OSAppTheme.Dark);
internal static bool IsDarkTheme => (Application.Current?.RequestedTheme == AppTheme.Dark);

static Color ResolveThemeColor(Color light, Color dark)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/Tizen/TizenPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, dou
return Platform.GetNativeSize(view, widthConstraint, heightConstraint);
}

public OSAppTheme RequestedTheme => OSAppTheme.Unspecified;
public AppTheme RequestedTheme => AppTheme.Unspecified;

static MD5 CreateChecksum()
{
Expand Down
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/WPF/WPFPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, dou
return Platform.GetNativeSize(view, widthConstraint, heightConstraint);
}

public OSAppTheme RequestedTheme => OSAppTheme.Unspecified;
public AppTheme RequestedTheme => AppTheme.Unspecified;
}
}
4 changes: 1 addition & 3 deletions src/Compatibility/Core/src/Windows/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ static void SetupInit(

Registrar.ExtraAssemblies = rendererAssemblies?.ToArray();

var platformServices = new WindowsPlatformServices();

Device.PlatformServices = platformServices;
Device.DefaultRendererAssembly = typeof(Forms).Assembly;

if (mainWindow != null)
{
Expand Down
41 changes: 0 additions & 41 deletions src/Compatibility/Core/src/Windows/WindowsPlatformServices.cs

This file was deleted.

87 changes: 2 additions & 85 deletions src/Compatibility/Core/src/iOS/Forms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ static void SetupInit(IMauiContext context, InitializationOptions? maybeOptions
var aquaAppearance = NSAppearance.GetAppearance(interfaceStyle == "Dark" ? NSAppearance.NameDarkAqua : NSAppearance.NameAqua);
NSApplication.SharedApplication.Appearance = aquaAppearance;

Application.Current?.TriggerThemeChanged(new AppThemeChangedEventArgs(interfaceStyle == "Dark" ? OSAppTheme.Dark : OSAppTheme.Light));
Application.Current?.TriggerThemeChanged(new AppThemeChangedEventArgs(interfaceStyle == "Dark" ? AppTheme.Dark : AppTheme.Light));
});
}

Expand All @@ -172,9 +172,8 @@ static void SetupInit(IMauiContext context, InitializationOptions? maybeOptions
NSApplication.SharedApplication.Appearance = aquaAppearance;
}
#endif
var platformServices = new IOSPlatformServices();

Device.PlatformServices = platformServices;
Device.DefaultRendererAssembly = typeof(Forms).Assembly;

if (maybeOptions?.Flags.HasFlag(InitializationFlags.SkipRenderers) != true)
RegisterCompatRenderers(context);
Expand Down Expand Up @@ -234,87 +233,5 @@ protected override Expression VisitMember(MemberExpression node)
return base.VisitMember(node);
}
}

class IOSPlatformServices : IPlatformServices
{
public OSAppTheme RequestedTheme
{
get
{
#if __IOS__ || __TVOS__
if (!IsiOS13OrNewer)
return OSAppTheme.Unspecified;
var uiStyle = GetCurrentUIViewController()?.TraitCollection?.UserInterfaceStyle ??
UITraitCollection.CurrentTraitCollection.UserInterfaceStyle;

switch (uiStyle)
{
case UIUserInterfaceStyle.Light:
return OSAppTheme.Light;
case UIUserInterfaceStyle.Dark:
return OSAppTheme.Dark;
default:
return OSAppTheme.Unspecified;
};
#else
return AppearanceIsDark() ? OSAppTheme.Dark : OSAppTheme.Light;
#endif
}
}

#if __MACOS__
bool AppearanceIsDark()
{
if (IsMojaveOrNewer)
{
var appearance = NSApplication.SharedApplication.EffectiveAppearance;
var matchedAppearance = appearance.FindBestMatch(new string[] { NSAppearance.NameAqua, NSAppearance.NameDarkAqua });

return matchedAppearance == NSAppearance.NameDarkAqua;
}
else
{
return false;
}
}
#endif

#if __IOS__ || __TVOS__

static UIViewController GetCurrentUIViewController() =>
GetCurrentViewController(false);

static UIViewController GetCurrentViewController(bool throwIfNull = true)
{
UIViewController viewController = null;

var window = UIApplication.SharedApplication.GetKeyWindow();

if (window != null && window.WindowLevel == UIWindowLevel.Normal)
viewController = window.RootViewController;

if (viewController == null)
{
window = UIApplication.SharedApplication
.Windows
.OrderByDescending(w => w.WindowLevel)
.FirstOrDefault(w => w.RootViewController != null && w.WindowLevel == UIWindowLevel.Normal);

if (window == null && throwIfNull)
throw new InvalidOperationException("Could not find current view controller.");
else
viewController = window?.RootViewController;
}

while (viewController?.PresentedViewController != null)
viewController = viewController.PresentedViewController;

if (throwIfNull && viewController == null)
throw new InvalidOperationException("Could not find current view controller.");

return viewController;
}
#endif
}
}
}
2 changes: 1 addition & 1 deletion src/Compatibility/Core/src/iOS/Renderers/PageRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ public override void TraitCollectionDidChange(UITraitCollection previousTraitCol
if (Forms.IsiOS13OrNewer &&
previousTraitCollection.UserInterfaceStyle != TraitCollection.UserInterfaceStyle &&
UIApplication.SharedApplication.ApplicationState != UIApplicationState.Background)
Application.Current?.TriggerThemeChanged(new AppThemeChangedEventArgs(Application.Current.RequestedTheme));
Application.Current?.ThemeChanged();
}

bool ShouldUseSafeArea()
Expand Down
2 changes: 1 addition & 1 deletion src/Controls/DualScreen/test/MockPlatformServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public SizeRequest GetNativeSize(VisualElement view, double widthConstraint, dou
return new SizeRequest(new Size(100, 20));
}

public OSAppTheme RequestedTheme => OSAppTheme.Unspecified;
public AppTheme RequestedTheme => AppTheme.Unspecified;
}

internal class MockDeserializer : Internals.IDeserializer
Expand Down
2 changes: 2 additions & 0 deletions src/Controls/samples/Controls.Sample/XamlApp.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public XamlApp(IServiceProvider services, ITextService textService)

Debug.WriteLine($"The injected text service had a message: '{textService.GetText()}'");

Debug.WriteLine($"Current app theme: {RequestedTheme}");

RequestedThemeChanged += (sender, args) =>
{
// Respond to the theme change
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/src/Core/AppThemeBinding.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Microsoft.Maui.Essentials;

namespace Microsoft.Maui.Controls
{
Expand Down Expand Up @@ -79,9 +80,9 @@ object GetValue()
switch (Application.Current.RequestedTheme)
{
default:
case OSAppTheme.Light:
case AppTheme.Light:
return _isLightSet ? Light : Default;
case OSAppTheme.Dark:
case AppTheme.Dark:
return _isDarkSet ? Dark : Default;
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/Controls/src/Core/AppThemeChangedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
using System;
using Microsoft.Maui.Essentials;

namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/AppThemeChangedEventArgs.xml" path="Type[@FullName='Microsoft.Maui.Controls.AppThemeChangedEventArgs']/Docs" />
public class AppThemeChangedEventArgs : EventArgs
{
/// <include file="../../docs/Microsoft.Maui.Controls/AppThemeChangedEventArgs.xml" path="//Member[@MemberName='.ctor']/Docs" />
public AppThemeChangedEventArgs(OSAppTheme appTheme) =>
public AppThemeChangedEventArgs(AppTheme appTheme) =>
RequestedTheme = appTheme;

/// <include file="../../docs/Microsoft.Maui.Controls/AppThemeChangedEventArgs.xml" path="//Member[@MemberName='RequestedTheme']/Docs" />
public OSAppTheme RequestedTheme { get; }
public AppTheme RequestedTheme { get; }
}
}
Loading