Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

Skip theme.json files over 64 KB, log when one sets no usable colour, and test that its names all map to brushes

Kayzel66bad80b0a9b
diff --git a/docs/CUSTOM-THEMES.md b/docs/CUSTOM-THEMES.md
index 6deed09a..411a4885 100644
--- a/docs/CUSTOM-THEMES.md
+++ b/docs/CUSTOM-THEMES.md
@@ -27,9 +27,14 @@ Filee's own colours can be replaced with a `theme.json` file in the folder that
```
- `dark` and `light` are both optional, and so is every colour in them. Anything left out keeps Filee's colour.
-- Colours are `#RRGGBB` or `#AARRGGBB`. Unknown names and other values are ignored and written to the log.
+- Colours are `#RRGGBB` or `#AARRGGBB`. Unknown names and other values are ignored and written to the log, and so is
+ a file with no usable colour (for example `"Dark"` with a capital D) or one over 64 KB.
- The accent colour, corner radius and light/dark choice stay in **Settings > Theme**.
- Filee reads the file when it starts and whenever a setting is saved. Restart Filee, or change any setting, after
editing it. Deleting the file brings the built-in colours back.
Keep `text` and `textSecondary` readable on `page`, `surface` and `surfaceAlt`: they are drawn on all three.
+
+Keep `dark` colours dark and `light` colours light. Text boxes, drop-downs, check boxes, menus, tooltips and plain
+buttons come from Avalonia's Fluent theme and keep its standard light/dark look; they do not follow `theme.json`. A
+white `page` in the `dark` variant would put white-on-white text boxes on it.
diff --git a/src/Filee.App/Services/ThemeService.cs b/src/Filee.App/Services/ThemeService.cs
index 1830a641..30889d3f 100644
--- a/src/Filee.App/Services/ThemeService.cs
+++ b/src/Filee.App/Services/ThemeService.cs
@@ -26,8 +26,11 @@ public sealed class ThemeService(IPlatformServices platform)
private const string SystemFallbacks =
"Segoe UI, Nirmala UI, Malgun Gothic, Microsoft YaHei, PingFang SC, Apple SD Gothic Neo, Kohinoor Devanagari, sans-serif";
- /// <summary>theme.json colour names → the brushes in Styles/Soft.axaml they replace.</summary>
- private static readonly IReadOnlyDictionary<string, string> CustomBrushKeys = new Dictionary<string, string>
+ /// <summary>
+ /// theme.json colour names → the brushes in Styles/Soft.axaml they replace. Must cover exactly
+ /// <see cref="CustomTheme.Keys"/> (a test checks).
+ /// </summary>
+ internal static readonly IReadOnlyDictionary<string, string> CustomBrushKeys = new Dictionary<string, string>
{
["page"] = "PageBrush",
["surface"] = "SurfaceBrush",
diff --git a/src/Filee.Core/Settings/CustomTheme.cs b/src/Filee.Core/Settings/CustomTheme.cs
index 65a02383..86f2b592 100644
--- a/src/Filee.Core/Settings/CustomTheme.cs
+++ b/src/Filee.Core/Settings/CustomTheme.cs
@@ -15,6 +15,12 @@ public sealed partial class CustomTheme
/// <summary>File name inside the data directory (<see cref="UserDataStore.Directory"/>).</summary>
public const string FileName = "theme.json";
+ /// <summary>
+ /// Larger files are skipped: Load runs on the UI thread at start-up and on every settings save (the Softness
+ /// slider saves on each tick), and a real theme is well under 1 KB.
+ /// </summary>
+ public const long MaxBytes = 64 * 1024;
+
/// <summary>Colour names a theme may set.</summary>
public static readonly IReadOnlyList<string> Keys =
["page", "surface", "surfaceAlt", "border", "text", "textSecondary", "danger", "success", "warning"];
@@ -37,8 +43,14 @@ public sealed partial class CustomTheme
{
log ??= NullLogger.Instance;
var path = Path.Combine(directory, FileName);
- if (!File.Exists(path))
+ var info = new FileInfo(path);
+ if (!info.Exists)
return null;
+ if (info.Length > MaxBytes)
+ {
+ log.LogWarning("Custom theme {Path} is larger than {Max} bytes; using the built-in colours", path, MaxBytes);
+ return null;
+ }
CustomTheme? theme;
try
@@ -51,12 +63,18 @@ public sealed partial class CustomTheme
log.LogWarning(ex, "Custom theme {Path} could not be read; using the built-in colours", path);
return null;
}
- if (theme is null)
+ if (theme is not null)
+ {
+ theme.Light = Clean(theme.Light, "light", path, log);
+ theme.Dark = Clean(theme.Dark, "dark", path, log);
+ }
+ if (theme is null || theme.IsEmpty)
+ {
+ // Also covers "Dark" or "Light" with a capital: variant names are case-sensitive, colour names are not.
+ log.LogWarning("Custom theme {Path} sets no colours Filee can use (expected \"light\"/\"dark\" objects of #RRGGBB values)", path);
return null;
-
- theme.Light = Clean(theme.Light, "light", path, log);
- theme.Dark = Clean(theme.Dark, "dark", path, log);
- return theme.IsEmpty ? null : theme;
+ }
+ return theme;
}
private static Dictionary<string, string> Clean(Dictionary<string, string>? colors, string variant, string path, ILogger log)
diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.cs
index 1f5c4178..e398335e 100644
--- a/tests/Filee.App.Tests/RenderTests.cs
+++ b/tests/Filee.App.Tests/RenderTests.cs
@@ -650,6 +650,12 @@ public class RenderTests
}
}
+ [Fact]
+ public void Every_custom_theme_name_maps_to_a_brush()
+ {
+ Assert.Equal(CustomTheme.Keys.Order(), ThemeService.CustomBrushKeys.Keys.Order());
+ }
+
private static Avalonia.Media.Color BrushColor(Window window, string key, Avalonia.Styling.ThemeVariant? variant = null)
{
Assert.True(window.TryFindResource(key, variant ?? window.ActualThemeVariant, out var value), key);
diff --git a/tests/Filee.Core.Tests/CustomThemeTests.cs b/tests/Filee.Core.Tests/CustomThemeTests.cs
index 41e4c215..a363cdb8 100644
--- a/tests/Filee.Core.Tests/CustomThemeTests.cs
+++ b/tests/Filee.Core.Tests/CustomThemeTests.cs
@@ -1,6 +1,7 @@
// theme.json: reading, dropping what Filee cannot use, and leaving the user's file alone.
using Filee.Core.Settings;
+using Microsoft.Extensions.Logging;
namespace Filee.Core.Tests;
@@ -60,9 +61,48 @@ public class CustomThemeTests
Assert.Equal(content, File.ReadAllText(path));
}
+ [Theory]
+ [InlineData("""{ "Dark": { "page": "#101820" } }""")]
+ [InlineData("{}")]
+ [InlineData("null")]
+ public void A_file_with_no_usable_colour_says_so_in_the_log(string content)
+ {
+ using var dir = new TempDir();
+ dir.File(CustomTheme.FileName, content);
+ var log = new ListLogger();
+
+ Assert.Null(CustomTheme.Load(dir.Path, log));
+ Assert.Contains(log.Messages, m => m.Contains("sets no colours Filee can use"));
+ }
+
+ [Fact]
+ public void A_file_over_the_size_cap_is_skipped_without_reading_it()
+ {
+ using var dir = new TempDir();
+ var padding = new string(' ', (int)CustomTheme.MaxBytes);
+ dir.File(CustomTheme.FileName, $$"""{ "dark": { "page": "#101820" } }{{padding}}""");
+ var log = new ListLogger();
+
+ Assert.Null(CustomTheme.Load(dir.Path, log));
+ Assert.Contains(log.Messages, m => m.Contains("is larger than"));
+ }
+
[Fact]
public void Every_name_is_unique()
{
Assert.Equal(CustomTheme.Keys.Count, CustomTheme.Keys.Distinct(StringComparer.OrdinalIgnoreCase).Count());
}
}
+
+/// <summary>Collects formatted log messages.</summary>
+internal sealed class ListLogger : ILogger
+{
+ public List<string> Messages { get; } = [];
+
+ public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
+
+ public bool IsEnabled(LogLevel logLevel) => true;
+
+ public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) =>
+ Messages.Add(formatter(state, exception));
+}