Skip to content

Commit

Permalink
feat: Support for UISettings.GetColorValue
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Oct 31, 2020
1 parent 8e467c4 commit bdc1420
Show file tree
Hide file tree
Showing 14 changed files with 149 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public UISettingsTests()

public UISettingsTestsViewModel ViewModel { get; private set; }

private void UISettingsTests_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
private void UISettingsTests_DataContextChanged(DependencyObject sender, DataContextChangedEventArgs args)
{
ViewModel = (UISettingsTestsViewModel)args.NewValue;
}
Expand Down
13 changes: 0 additions & 13 deletions src/Uno.UI/UI/Xaml/Application.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ partial void OnSuspendingPartial()
Suspending?.Invoke(this, new Windows.ApplicationModel.SuspendingEventArgs(new Windows.ApplicationModel.SuspendingOperation(DateTime.Now.AddSeconds(30))));
}

private ApplicationTheme GetDefaultSystemTheme()
{
if ((int)Build.VERSION.SdkInt >= 28)
{
var uiModeFlags = Android.App.Application.Context.Resources.Configuration.UiMode & UiMode.NightMask;
if (uiModeFlags == UiMode.NightYes)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}

public void Exit()
{
Android.OS.Process.KillProcess(Android.OS.Process.MyPid());
Expand Down
7 changes: 0 additions & 7 deletions src/Uno.UI/UI/Xaml/Application.Skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,9 @@ private void Initialize()
}
}

private ApplicationTheme GetDefaultSystemTheme() => _coreWindowExtension.GetDefaultSystemTheme();

internal void ForceSetRequestedTheme(ApplicationTheme theme) => _requestedTheme = theme;
}

internal interface IApplicationExtension
{
ApplicationTheme GetDefaultSystemTheme();
}

internal interface IApplicationEvents
{
}
Expand Down
6 changes: 6 additions & 0 deletions src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#else
using View = Windows.UI.Xaml.UIElement;
using ViewGroup = Windows.UI.Xaml.UIElement;
using Uno.Helpers.Theming;
using Windows.UI.ViewManagement;
#endif

namespace Windows.UI.Xaml
Expand Down Expand Up @@ -161,6 +163,10 @@ internal void InitializationCompleted()

internal void RaiseRecoverableUnhandledException(Exception e) => UnhandledException?.Invoke(this, new UnhandledExceptionEventArgs(e, false));

private ApplicationTheme GetDefaultSystemTheme() =>
SystemThemeHelper.SystemTheme == SystemTheme.Light ?
ApplicationTheme.Light : ApplicationTheme.Dark;

private IDisposable WritePhaseEventTrace(int startEventId, int stopEventId)
{
if (_trace.IsEnabled)
Expand Down
13 changes: 0 additions & 13 deletions src/Uno.UI/UI/Xaml/Application.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,6 @@ private bool TryParseActivationUri(NSUrl url, out Uri uri)
return false;
}
}

private ApplicationTheme GetDefaultSystemTheme()
{
//Ensure the current device is running 12.0 or higher, because `TraitCollection.UserInterfaceStyle` was introduced in iOS 12.0
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
if (UIScreen.MainScreen.TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}
}
}
#endif
23 changes: 0 additions & 23 deletions src/Uno.UI/UI/Xaml/Application.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,6 @@ private bool TryHandleUrlActivation(NSUrl[] urls, ApplicationExecutionState prev
return handled;
}

/// <summary>
/// Based on <see cref="https://forums.developer.apple.com/thread/118974" />
/// </summary>
/// <returns>System theme</returns>
private ApplicationTheme GetDefaultSystemTheme()
{
var version = DeviceHelper.OperatingSystemVersion;
if (version >= new Version(10, 14))
{
var app = NSAppearance.CurrentAppearance?.FindBestMatch(new string[]
{
NSAppearance.NameAqua,
NSAppearance.NameDarkAqua
});

if (app == NSAppearance.NameDarkAqua)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}

private void SetCurrentLanguage()
{
var language = NSLocale.PreferredLanguages.ElementAtOrDefault(0);
Expand Down
27 changes: 0 additions & 27 deletions src/Uno.UI/UI/Xaml/Application.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,33 +103,6 @@ private void Initialize()
}
}

private ApplicationTheme GetDefaultSystemTheme()
{
var serializedTheme = WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.getDefaultSystemTheme()");

if (serializedTheme != null)
{
if (Enum.TryParse(serializedTheme, out ApplicationTheme theme))
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
{
this.Log().Info("Setting OS preferred theme: " + theme);
}
return theme;
}
else
{
throw new InvalidOperationException($"{serializedTheme} theme is not a supported OS theme");
}
}
//OS has no preference or API not implemented, use light as default
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
{
this.Log().Info("No preferred theme, using Light instead");
}
return ApplicationTheme.Light;
}

/// <summary>
/// Dispatch method from Javascript
/// </summary>
Expand Down
18 changes: 18 additions & 0 deletions src/Uno.UWP/Helpers/Theming/SystemThemeHelper.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Uno.Helpers.Theming
{
internal static partial class SystemThemeHelper
{
private static SystemTheme GetSystemTheme()
{
if ((int)Build.VERSION.SdkInt >= 28)
{
var uiModeFlags = Android.App.Application.Context.Resources.Configuration.UiMode & UiMode.NightMask;
if (uiModeFlags == UiMode.NightYes)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}
}
}
2 changes: 1 addition & 1 deletion src/Uno.UWP/Helpers/Theming/SystemThemeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Uno.Helpers.Theming
{
internal static class SystemThemeHelper
internal static partial class SystemThemeHelper
{
internal static SystemTheme SystemTheme => GetSystemTheme();
}
Expand Down
24 changes: 24 additions & 0 deletions src/Uno.UWP/Helpers/Theming/SystemThemeHelper.iOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uno.Helpers.Theming
{
internal static partial class SystemThemeHelper
{
private ApplicationTheme GetDefaultSystemTheme()
{
//Ensure the current device is running 12.0 or higher, because `TraitCollection.UserInterfaceStyle` was introduced in iOS 12.0
if (UIDevice.CurrentDevice.CheckSystemVersion(12, 0))
{
if (UIScreen.MainScreen.TraitCollection.UserInterfaceStyle == UIUserInterfaceStyle.Dark)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}
}
}
34 changes: 34 additions & 0 deletions src/Uno.UWP/Helpers/Theming/SystemThemeHelper.macOS.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uno.Helpers.Theming
{
internal static partial class SystemThemeHelper
{
/// <summary>
/// Based on <see cref="https://forums.developer.apple.com/thread/118974" />
/// </summary>
/// <returns>System theme</returns>
private ApplicationTheme GetDefaultSystemTheme()
{
var version = DeviceHelper.OperatingSystemVersion;
if (version >= new Version(10, 14))
{
var app = NSAppearance.CurrentAppearance?.FindBestMatch(new string[]
{
NSAppearance.NameAqua,
NSAppearance.NameDarkAqua
});

if (app == NSAppearance.NameDarkAqua)
{
return ApplicationTheme.Dark;
}
}
return ApplicationTheme.Light;
}
}
}
18 changes: 18 additions & 0 deletions src/Uno.UWP/Helpers/Theming/SystemThemeHelper.skia.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Uno.Helpers.Theming
{
internal static partial class SystemThemeHelper
{

}

internal interface ISystemThemeHelperExtension
{
SystemTheme GetDefaultSystemTheme();
}
}
37 changes: 37 additions & 0 deletions src/Uno.UWP/Helpers/Theming/SystemThemeHelper.wasm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Uno.Extensions;
using Uno.Foundation;
using Uno.Logging;

namespace Uno.Helpers.Theming
{
internal static partial class SystemThemeHelper
{
private static SystemTheme GetSystemTheme()
{
var serializedTheme = WebAssemblyRuntime.InvokeJS("Windows.UI.Xaml.Application.getDefaultSystemTheme()");

if (serializedTheme != null)
{
if (Enum.TryParse(serializedTheme, out SystemTheme theme))
{
if (typeof(SystemThemeHelper).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
{
typeof(SystemThemeHelper).Log().Info("Setting OS preferred theme: " + theme);
}
return theme;
}
else
{
throw new InvalidOperationException($"{serializedTheme} theme is not a supported OS theme");
}
}
//OS has no preference or API not implemented, use light as default
if (typeof(SystemThemeHelper).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Information))
{
typeof(SystemThemeHelper).Log().Info("No preferred theme, using Light instead");
}
return SystemTheme.Light;
}
}
}
11 changes: 10 additions & 1 deletion src/Uno.UWP/UI/ViewManagement/UISettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma warning disable 114 // new keyword hiding
using System.Collections.Concurrent;
using Uno;
using Uno.Helpers.Theming;
using Windows.Foundation;

namespace Windows.UI.ViewManagement
Expand Down Expand Up @@ -47,7 +48,15 @@ internal static void OnColorValuesChanged()

public Color GetColorValue(UIColorType desiredColor)
{
return Colors.Black;
var systemTheme = SystemThemeHelper.SystemTheme;
return desiredColor switch
{
UIColorType.Background =>
systemTheme == SystemTheme.Light ? Colors.White : Colors.Black,
UIColorType.Foreground =>
systemTheme == SystemTheme.Light ? Colors.Black : Colors.White,
_ => Colors.Transparent
};
}

[NotImplemented]
Expand Down

0 comments on commit bdc1420

Please sign in to comment.