Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions se5/AmericanToBritish.Tests/PluginAppTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Avalonia.Styling;
using SubtitleEdit.Plugins.Shared;

namespace AmericanToBritish.Tests;

/// <summary>Plugin-Shared's theme variant choice, shared by every plugin built on PluginApp.</summary>
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);
}
12 changes: 11 additions & 1 deletion se5/Plugin-Shared/PluginApp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -37,6 +37,16 @@ public override void OnFrameworkInitializationCompleted()
base.OnFrameworkInitializationCompleted();
}

/// <summary>
/// Dark or light controls to match Subtitle Edit. <see cref="PluginThemeColors.IsDark"/> 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.
/// </summary>
public static ThemeVariant ResolveThemeVariant(PluginRequest request) =>
request.ThemeColors is { } colors
? colors.IsDark ? ThemeVariant.Dark : ThemeVariant.Light
: ResolveThemeVariant(request.Theme);

/// <summary>Map the SE theme string ("Dark" / "Light" / "System" / ...) to an Avalonia <see cref="ThemeVariant"/>.</summary>
public static ThemeVariant ResolveThemeVariant(string? seThemeName)
{
Expand Down
9 changes: 6 additions & 3 deletions se5/TypewriterEffect/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
Loading