Files
HellionChat/HellionChat/Themes/ThemeRegistry.cs
T
JonKazama-Hellion 4000bbd199
Security / scan (push) Successful in 12s
chore: reformat after editorconfig update
Updated .editorconfig to set indent_style=space and indent_size=4 for C# files. Reformat all .cs files to apply the new indentation settings. No code logic changes, just whitespace reformatting.
also updated some comments in files in shorter and Precise way. No logic changes, just comment rewording for clarity and conciseness.
2026-05-10 19:54:39 +02:00

134 lines
4.4 KiB
C#

using HellionChat.Themes.Builtin;
namespace HellionChat.Themes;
public sealed class ThemeRegistry
{
public const string DefaultSlug = HellionArctic.Slug;
private readonly Dictionary<string, Theme> _builtIns;
private readonly Dictionary<string, (Theme Theme, DateTime Stamp)> _customCache = new(
StringComparer.OrdinalIgnoreCase
);
private readonly string? _customThemesDir;
private Theme _active;
public ThemeRegistry(string? customThemesDir = null)
{
_builtIns = new Dictionary<string, Theme>(StringComparer.OrdinalIgnoreCase)
{
{ HellionArctic.Slug, HellionArctic.Build() },
{ HellionSpectrum.Slug, HellionSpectrum.Build() },
{ Chat2Classic.Slug, Chat2Classic.Build() },
{ EventHorizon.Slug, EventHorizon.Build() },
{ MoonlitBloom.Slug, MoonlitBloom.Build() },
{ NightBlue.Slug, NightBlue.Build() },
{ IndigoViolet.Slug, IndigoViolet.Build() },
{ ForgeMerchantman.Slug, ForgeMerchantman.Build() },
{ MintGrove.Slug, MintGrove.Build() },
{ SynthwaveSunset.Slug, SynthwaveSunset.Build() },
};
// Centralised so Build() factories stay free of cache plumbing.
foreach (var theme in _builtIns.Values)
theme.RecomputeAbgrCache();
_active = _builtIns[DefaultSlug];
_customThemesDir = customThemesDir;
}
public Theme Active => _active;
public Theme Get(string slug)
{
if (_builtIns.TryGetValue(slug, out var b))
return b;
var custom = LoadCustomBySlug(slug);
if (custom != null)
return custom;
return _builtIns[DefaultSlug];
}
public IEnumerable<Theme> AllBuiltIns() => _builtIns.Values;
public IEnumerable<Theme> AllCustom() => RefreshCustomCache();
public void Switch(string slug)
{
var theme = Get(slug);
// Defensive — ensures any future theme source always gets a populated cache.
theme.RecomputeAbgrCache();
_active = theme;
}
// 0x80070020 = SHARING_VIOLATION, 0x80070021 = LOCK_VIOLATION.
// Other IO failures are permanent — theme is dropped instead of retried.
internal static bool IsRecoverableFileLock(Exception? ex)
{
if (ex is not IOException io)
return false;
var code = (uint)io.HResult;
return code == 0x80070020u || code == 0x80070021u;
}
// Custom themes are loaded lazily, cached by LastWriteTime.
// A changed JSON is reloaded on the next lookup.
private Theme? LoadCustomBySlug(string slug)
{
if (_customThemesDir is null)
return null;
if (!Directory.Exists(_customThemesDir))
return null;
foreach (var theme in RefreshCustomCache())
if (string.Equals(theme.Slug, slug, StringComparison.OrdinalIgnoreCase))
return theme;
return null;
}
private IEnumerable<Theme> RefreshCustomCache()
{
if (_customThemesDir is null || !Directory.Exists(_customThemesDir))
yield break;
var seenSlugs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (var path in Directory.EnumerateFiles(_customThemesDir, "*.json"))
{
Theme? theme = null;
var stamp = File.GetLastWriteTimeUtc(path);
var key = path;
if (_customCache.TryGetValue(key, out var cached) && cached.Stamp == stamp)
{
theme = cached.Theme;
}
else
{
try
{
theme = ThemeJsonLoader.LoadFromFile(path);
theme.RecomputeAbgrCache();
_customCache[key] = (theme, stamp);
}
catch (Exception ex) when (IsRecoverableFileLock(ex))
{
// Editor mid-save: keep last known good, retry on next refresh.
Plugin.Log.Debug(
$"Custom theme {Path.GetFileName(path)} is locked, keeping last known good"
);
if (cached.Theme is not null)
theme = cached.Theme;
}
catch (Exception)
{
continue;
}
}
if (theme is not null && seenSlugs.Add(theme.Slug))
yield return theme;
}
}
}