diff --git a/src/Avalonia.Base/Input/IInputRoot.cs b/src/Avalonia.Base/Input/IInputRoot.cs
index f358658005..8e2a5d6932 100644
--- a/src/Avalonia.Base/Input/IInputRoot.cs
+++ b/src/Avalonia.Base/Input/IInputRoot.cs
@@ -26,7 +26,7 @@ namespace Avalonia.Input
/// Represents a contract for accessing platform-specific settings and information.
///
///
- /// Focus manager can be null only if window wasn't initialized yet.
+ /// PlatformSettings can be null only if window wasn't initialized yet.
///
IPlatformSettings? PlatformSettings { get; }
diff --git a/src/Avalonia.Controls/Application.cs b/src/Avalonia.Controls/Application.cs
index b98ad123f1..1187ada8a7 100644
--- a/src/Avalonia.Controls/Application.cs
+++ b/src/Avalonia.Controls/Application.cs
@@ -180,6 +180,17 @@ namespace Avalonia
///
public IApplicationLifetime? ApplicationLifetime { get; set; }
+ ///
+ /// Represents a contract for accessing platform-specific settings and information.
+ ///
+ ///
+ /// PlatformSettings can be null only if application wasn't initialized yet.
+ /// 's is an equivalent API
+ /// which should always be preferred over a global one,
+ /// as specific top levels might have different settings set-up.
+ ///
+ public IPlatformSettings? PlatformSettings => AvaloniaLocator.Current.GetService();
+
event Action>? IGlobalStyles.GlobalStylesAdded
{
add => _stylesAdded += value;
@@ -229,10 +240,12 @@ namespace Avalonia
var focusManager = new FocusManager();
InputManager = new InputManager();
- var settings = AvaloniaLocator.Current.GetRequiredService();
- settings.ColorValuesChanged += OnColorValuesChanged;
- OnColorValuesChanged(settings, settings.GetColorValues());
-
+ if (PlatformSettings is { } settings)
+ {
+ settings.ColorValuesChanged += OnColorValuesChanged;
+ OnColorValuesChanged(settings, settings.GetColorValues());
+ }
+
AvaloniaLocator.CurrentMutable
.Bind().ToTransient()
.Bind().ToConstant(this)
@@ -242,7 +255,7 @@ namespace Avalonia
.Bind().ToConstant(InputManager)
.Bind().ToTransient()
.Bind().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() == null)
AvaloniaLocator.CurrentMutable
diff --git a/src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs b/src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs
index a4ef15f950..a796a5704d 100644
--- a/src/Avalonia.Themes.Fluent/Accents/SystemAccentColors.cs
+++ b/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();
- }
-
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