diff --git a/se5/AmericanToBritish.Tests/PluginAppTests.cs b/se5/AmericanToBritish.Tests/PluginAppTests.cs new file mode 100644 index 00000000..736f8689 --- /dev/null +++ b/se5/AmericanToBritish.Tests/PluginAppTests.cs @@ -0,0 +1,27 @@ +using Avalonia.Styling; +using SubtitleEdit.Plugins.Shared; + +namespace AmericanToBritish.Tests; + +/// Plugin-Shared's theme variant choice, shared by every plugin built on PluginApp. +public class PluginAppTests +{ + [Theory] + [InlineData("Nord", true, "Dark")] + [InlineData("System", true, "Dark")] + [InlineData("Dark", false, "Light")] + [InlineData("Light", false, "Light")] + public void ThemeColors_IsDark_Wins(string theme, bool isDark, string expected) + { + var request = new PluginRequest { Theme = theme, ThemeColors = new PluginThemeColors { IsDark = isDark } }; + Assert.Equal(expected, PluginApp.ResolveThemeVariant(request).Key); + } + + [Theory] + [InlineData("Dark", "Dark")] + [InlineData("dark", "Dark")] + [InlineData("Nord", "Light")] + [InlineData("", "Light")] + public void NoThemeColors_FallsBackToName(string theme, string expected) => + Assert.Equal(expected, PluginApp.ResolveThemeVariant(new PluginRequest { Theme = theme }).Key); +} diff --git a/se5/Plugin-Shared/PluginApp.cs b/se5/Plugin-Shared/PluginApp.cs index de28b9eb..7c699d35 100644 --- a/se5/Plugin-Shared/PluginApp.cs +++ b/se5/Plugin-Shared/PluginApp.cs @@ -25,7 +25,7 @@ public override void OnFrameworkInitializationCompleted() if (PendingRequest is not null) { - RequestedThemeVariant = ResolveThemeVariant(PendingRequest.Theme); + RequestedThemeVariant = ResolveThemeVariant(PendingRequest); PluginThemeStyles.Apply(PendingRequest.ThemeColors); if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) @@ -37,6 +37,16 @@ public override void OnFrameworkInitializationCompleted() base.OnFrameworkInitializationCompleted(); } + /// + /// Dark or light controls to match Subtitle Edit. wins: + /// it is true for every dark theme (Nord, Cinema, ...) and for "System" on a dark OS, which the + /// theme name alone does not tell. Older SE versions send no colors, so the name is the fallback. + /// + public static ThemeVariant ResolveThemeVariant(PluginRequest request) => + request.ThemeColors is { } colors + ? colors.IsDark ? ThemeVariant.Dark : ThemeVariant.Light + : ResolveThemeVariant(request.Theme); + /// Map the SE theme string ("Dark" / "Light" / "System" / ...) to an Avalonia . public static ThemeVariant ResolveThemeVariant(string? seThemeName) { diff --git a/se5/TypewriterEffect/App.axaml.cs b/se5/TypewriterEffect/App.axaml.cs index a10904ab..7bc80a6b 100644 --- a/se5/TypewriterEffect/App.axaml.cs +++ b/se5/TypewriterEffect/App.axaml.cs @@ -21,9 +21,12 @@ public override void OnFrameworkInitializationCompleted() if (PendingRequest is not null) { - RequestedThemeVariant = string.Equals(PendingRequest.Theme, "Dark", System.StringComparison.OrdinalIgnoreCase) - ? ThemeVariant.Dark - : ThemeVariant.Light; + // IsDark covers every dark theme and "System" on a dark OS; older SE versions only send the name. + RequestedThemeVariant = PendingRequest.ThemeColors is { } colors + ? colors.IsDark ? ThemeVariant.Dark : ThemeVariant.Light + : string.Equals(PendingRequest.Theme, "Dark", System.StringComparison.OrdinalIgnoreCase) + ? ThemeVariant.Dark + : ThemeVariant.Light; PluginThemeStyles.Apply(PendingRequest.ThemeColors);