Browse Source

Revert "Fluent2: use OS accent shades on Windows"

This reverts part of the commit 71bbc901a5.
pull/21771/head
Max Katz 1 month ago
parent
commit
134817fe59
  1. 31
      src/Avalonia.Base/Platform/PlatformColorValues.cs
  2. 20
      src/Avalonia.Themes.Fluent2/Accents/SystemAccentColors.cs
  3. 7
      src/Avalonia.Themes.Fluent2/README.md
  4. 8
      src/Windows/Avalonia.Win32/Win32PlatformSettings.cs
  5. 43
      tests/Avalonia.Themes.Fluent2.UnitTests/PaletteTests.cs

31
src/Avalonia.Base/Platform/PlatformColorValues.cs

@ -61,37 +61,6 @@ public record PlatformColorValues
init => _accentColor3 = value;
}
/// <summary>
/// Dark shade 1 of <see cref="AccentColor1"/>, when the platform provides one (Windows does).
/// Null when the platform only reports the base accent color; consumers are expected to compute a shade instead.
/// </summary>
public Color? AccentColorDark1 { get; init; }
/// <summary>
/// Dark shade 2 of <see cref="AccentColor1"/>, when the platform provides one. See <see cref="AccentColorDark1"/>.
/// </summary>
public Color? AccentColorDark2 { get; init; }
/// <summary>
/// Dark shade 3 of <see cref="AccentColor1"/>, when the platform provides one. See <see cref="AccentColorDark1"/>.
/// </summary>
public Color? AccentColorDark3 { get; init; }
/// <summary>
/// Light shade 1 of <see cref="AccentColor1"/>, when the platform provides one. See <see cref="AccentColorDark1"/>.
/// </summary>
public Color? AccentColorLight1 { get; init; }
/// <summary>
/// Light shade 2 of <see cref="AccentColor1"/>, when the platform provides one. See <see cref="AccentColorDark1"/>.
/// </summary>
public Color? AccentColorLight2 { get; init; }
/// <summary>
/// Light shade 3 of <see cref="AccentColor1"/>, when the platform provides one. See <see cref="AccentColorDark1"/>.
/// </summary>
public Color? AccentColorLight3 { get; init; }
public PlatformColorValues()
{
AccentColor1 = DefaultAccent;

20
src/Avalonia.Themes.Fluent2/Accents/SystemAccentColors.cs

@ -20,8 +20,7 @@ internal sealed class SystemAccentColors : ResourceProvider
private static readonly Color s_defaultSystemAccentColor = Color.FromRgb(0, 120, 215);
// Static shade values from WinUI (matching the Uno/WinUI SystemResources defaults). Used when
// the platform provides no accent color, so the out-of-the-box palette matches WinUI exactly.
// When the platform reports the OS-computed shades (Windows does, via UISettings) those are
// used verbatim; otherwise an OS-provided base accent gets HSL-computed shades.
// OS-provided accent colors get computed shades instead.
private static readonly Color s_defaultSystemAccentColorDark1 = Color.FromUInt32(0xFF005A9E);
private static readonly Color s_defaultSystemAccentColorDark2 = Color.FromUInt32(0xFF004275);
private static readonly Color s_defaultSystemAccentColorDark3 = Color.FromUInt32(0xFF002642);
@ -118,22 +117,11 @@ internal sealed class SystemAccentColors : ResourceProvider
{
_invalidateColors = false;
var colorValues = GetFromOwner(Owner)?.GetColorValues();
var platformSettings = GetFromOwner(Owner);
_systemAccentColor = colorValues?.AccentColor1 ?? s_defaultSystemAccentColor;
_systemAccentColor = platformSettings?.GetColorValues().AccentColor1 ?? s_defaultSystemAccentColor;
if (colorValues is
{
AccentColorDark1: { } dark1, AccentColorDark2: { } dark2, AccentColorDark3: { } dark3,
AccentColorLight1: { } light1, AccentColorLight2: { } light2, AccentColorLight3: { } light3
})
{
// The platform reported the OS-computed shades (Windows, via UISettings);
// use them verbatim so the palette matches the OS exactly.
(_systemAccentColorDark1, _systemAccentColorDark2, _systemAccentColorDark3) = (dark1, dark2, dark3);
(_systemAccentColorLight1, _systemAccentColorLight2, _systemAccentColorLight3) = (light1, light2, light3);
}
else if (_systemAccentColor == s_defaultSystemAccentColor)
if (_systemAccentColor == s_defaultSystemAccentColor)
{
// Platforms without a real accent color report the default; use WinUI's static
// shade values so the out-of-the-box palette matches WinUI exactly.

7
src/Avalonia.Themes.Fluent2/README.md

@ -47,7 +47,6 @@ Migrating from `Avalonia.Themes.Fluent`: change the package reference and swap
| Scroll bars | 16 px rail | **12 px rail, 2 px collapsed thumb** |
| Window background | AltHigh | **SolidBackgroundFillColorBase** (#F3F3F3/#202020) |
| Default accent shades | HSL-computed | **WinUI static values** (when no OS accent) |
| OS accent shades (Windows) | HSL-computed | **read from the OS** (`UISettings` Light1–3/Dark1–3) |
One deliberate deviation from WinUI (following FluentAvalonia's lead): CheckBox
and RadioButton do **not** get WinUI's forced 120 px `MinWidth` — they size to
@ -80,10 +79,8 @@ English-only for now.
- No HighContrast variant yet (same as v1).
- AnimatedIcon glyph animations are approximated with static glyphs and simple
transitions.
- On Windows the six accent shades (`SystemAccentColorDark1`…`Light3`) come
straight from the OS; on platforms that report only a base accent color
(macOS, Linux) the shades are HSL-computed and can deviate slightly from
Windows' palette algorithm.
- OS-provided accent colors still use HSL-computed shades, which can deviate
slightly from Windows' palette algorithm.
## Compatibility tests

8
src/Windows/Avalonia.Win32/Win32PlatformSettings.cs

@ -86,13 +86,7 @@ internal class Win32PlatformSettings : DefaultPlatformSettings
PlatformThemeVariant.Dark :
PlatformThemeVariant.Light,
ContrastPreference = ColorContrastPreference.NoPreference,
AccentColor1 = accent,
AccentColorDark1 = uiSettings.GetColorValue(UIColorType.AccentDark1).ToAvalonia(),
AccentColorDark2 = uiSettings.GetColorValue(UIColorType.AccentDark2).ToAvalonia(),
AccentColorDark3 = uiSettings.GetColorValue(UIColorType.AccentDark3).ToAvalonia(),
AccentColorLight1 = uiSettings.GetColorValue(UIColorType.AccentLight1).ToAvalonia(),
AccentColorLight2 = uiSettings.GetColorValue(UIColorType.AccentLight2).ToAvalonia(),
AccentColorLight3 = uiSettings.GetColorValue(UIColorType.AccentLight3).ToAvalonia()
AccentColor1 = accent
};
}
}

43
tests/Avalonia.Themes.Fluent2.UnitTests/PaletteTests.cs

@ -1,6 +1,5 @@
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Styling;
namespace Avalonia.Themes.Fluent2.UnitTests;
@ -66,48 +65,6 @@ public class PaletteTests
}
}
[AvaloniaFact]
public void Platform_provided_accent_shades_take_precedence_over_computed_ones()
{
var originalSettings = AvaloniaLocator.Current.GetService<IPlatformSettings>();
Assert.NotNull(originalSettings);
AvaloniaLocator.CurrentMutable.Bind<IPlatformSettings>().ToConstant(new ShadeReportingPlatformSettings());
var theme = new Fluent2Theme();
var app = Application.Current!;
app.Styles.Add(theme);
try
{
Assert.True(app.TryGetResource("SystemAccentColor", ThemeVariant.Light, out var accent));
Assert.Equal(Colors.Crimson, Assert.IsType<Color>(accent));
// The OS-reported shades are used verbatim, not HSL-computed from the base color.
Assert.True(app.TryGetResource("SystemAccentColorDark1", ThemeVariant.Light, out var dark1));
Assert.Equal(Colors.DarkRed, Assert.IsType<Color>(dark1));
Assert.True(app.TryGetResource("SystemAccentColorLight3", ThemeVariant.Light, out var light3));
Assert.Equal(Colors.MistyRose, Assert.IsType<Color>(light3));
}
finally
{
app.Styles.Remove(theme);
AvaloniaLocator.CurrentMutable.Bind<IPlatformSettings>().ToConstant(originalSettings);
}
}
private sealed class ShadeReportingPlatformSettings : DefaultPlatformSettings
{
public override PlatformColorValues GetColorValues() => new()
{
AccentColor1 = Colors.Crimson,
AccentColorDark1 = Colors.DarkRed,
AccentColorDark2 = Colors.Maroon,
AccentColorDark3 = Colors.Brown,
AccentColorLight1 = Colors.Salmon,
AccentColorLight2 = Colors.LightSalmon,
AccentColorLight3 = Colors.MistyRose,
};
}
[AvaloniaFact]
public void Legacy_palette_colors_derive_fluent2_tokens()
{

Loading…
Cancel
Save