Browse Source

Introduce Application.PlatformSettings

pull/11564/head
Max Katz 3 years ago
parent
commit
6d57d3cf7d
  1. 2
      src/Avalonia.Base/Input/IInputRoot.cs
  2. 23
      src/Avalonia.Controls/Application.cs
  3. 34
      src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs

2
src/Avalonia.Base/Input/IInputRoot.cs

@ -26,7 +26,7 @@ namespace Avalonia.Input
/// Represents a contract for accessing platform-specific settings and information.
/// </summary>
/// <remarks>
/// Focus manager can be null only if window wasn't initialized yet.
/// PlatformSettings can be null only if window wasn't initialized yet.
/// </remarks>
IPlatformSettings? PlatformSettings { get; }

23
src/Avalonia.Controls/Application.cs

@ -180,6 +180,17 @@ namespace Avalonia
/// </summary>
public IApplicationLifetime? ApplicationLifetime { get; set; }
/// <summary>
/// Represents a contract for accessing platform-specific settings and information.
/// </summary>
/// <remarks>
/// PlatformSettings can be null only if application wasn't initialized yet.
/// <see cref="TopLevel"/>'s <see cref="TopLevel.PlatformSettings"/> is an equivalent API
/// which should always be preferred over a global one,
/// as specific top levels might have different settings set-up.
/// </remarks>
public IPlatformSettings? PlatformSettings => AvaloniaLocator.Current.GetService<IPlatformSettings>();
event Action<IReadOnlyList<IStyle>>? IGlobalStyles.GlobalStylesAdded
{
add => _stylesAdded += value;
@ -229,10 +240,12 @@ namespace Avalonia
var focusManager = new FocusManager();
InputManager = new InputManager();
var settings = AvaloniaLocator.Current.GetRequiredService<IPlatformSettings>();
settings.ColorValuesChanged += OnColorValuesChanged;
OnColorValuesChanged(settings, settings.GetColorValues());
if (PlatformSettings is { } settings)
{
settings.ColorValuesChanged += OnColorValuesChanged;
OnColorValuesChanged(settings, settings.GetColorValues());
}
AvaloniaLocator.CurrentMutable
.Bind<IAccessKeyHandler>().ToTransient<AccessKeyHandler>()
.Bind<IGlobalDataTemplates>().ToConstant(this)
@ -242,7 +255,7 @@ namespace Avalonia
.Bind<IInputManager>().ToConstant(InputManager)
.Bind<IKeyboardNavigationHandler>().ToTransient<KeyboardNavigationHandler>()
.Bind<IDragDropDevice>().ToConstant(DragDropDevice.Instance);
// TODO: Fix this, for now we keep this behavior since someone might be relying on it in 0.9.x
if (AvaloniaLocator.Current.GetService<IPlatformDragSource>() == null)
AvaloniaLocator.CurrentMutable

34
src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs

@ -16,18 +16,12 @@ internal class SystemAccentColors : IResourceProvider
public const string AccentLight2Key = "SystemAccentColorLight2";
public const string AccentLight3Key = "SystemAccentColorLight3";
private static readonly Color s_defaultSystemAccentColor = Color.FromRgb(0, 120, 215);
private readonly IPlatformSettings? _platformSettings;
private static readonly Color s_defaultSystemAccentColor = Color.FromRgb(0, 120, 215);
private bool _invalidateColors = true;
private Color _systemAccentColor;
private Color _systemAccentColorDark1, _systemAccentColorDark2, _systemAccentColorDark3;
private Color _systemAccentColorLight1, _systemAccentColorLight2, _systemAccentColorLight3;
public SystemAccentColors()
{
_platformSettings = AvaloniaLocator.Current.GetService<IPlatformSettings>();
}
public bool HasResources => true;
public bool TryGetResource(object key, ThemeVariant? theme, out object? value)
{
@ -96,10 +90,12 @@ internal class SystemAccentColors : IResourceProvider
Owner = owner;
OwnerChanged?.Invoke(this, EventArgs.Empty);
if (_platformSettings is not null)
if (GetFromOwner(owner) is { } platformSettings)
{
_platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged;
platformSettings.ColorValuesChanged += PlatformSettingsOnColorValuesChanged;
}
_invalidateColors = true;
}
}
@ -110,10 +106,12 @@ internal class SystemAccentColors : IResourceProvider
Owner = null;
OwnerChanged?.Invoke(this, EventArgs.Empty);
if (_platformSettings is not null)
if (GetFromOwner(owner) is { } platformSettings)
{
_platformSettings.ColorValuesChanged -= PlatformSettingsOnColorValuesChanged;
platformSettings.ColorValuesChanged -= PlatformSettingsOnColorValuesChanged;
}
_invalidateColors = true;
}
}
@ -122,13 +120,25 @@ internal class SystemAccentColors : IResourceProvider
if (_invalidateColors)
{
_invalidateColors = false;
var platformSettings = GetFromOwner(Owner);
_systemAccentColor = _platformSettings?.GetColorValues().AccentColor1 ?? s_defaultSystemAccentColor;
_systemAccentColor = platformSettings?.GetColorValues().AccentColor1 ?? s_defaultSystemAccentColor;
(_systemAccentColorDark1,_systemAccentColorDark2, _systemAccentColorDark3,
_systemAccentColorLight1, _systemAccentColorLight2, _systemAccentColorLight3) = CalculateAccentShades(_systemAccentColor);
}
}
private static IPlatformSettings? GetFromOwner(IResourceHost? owner)
{
return owner switch
{
Application app => app.PlatformSettings,
Visual visual => TopLevel.GetTopLevel(visual)?.PlatformSettings,
_ => null
};
}
public static (Color d1, Color d2, Color d3, Color l1, Color l2, Color l3) CalculateAccentShades(Color accentColor)
{
// dark1step = (hslAccent.L - SystemAccentColorDark1.L) * 255

Loading…
Cancel
Save