using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using Avalonia; using Avalonia.Media; using Avalonia.Media.Imaging; using Avalonia.Platform; using Avalonia.Styling; namespace UltraParadise.Linux { internal static class Ui { public static Color Back, Side, Panel, Line, Text, Dim, Orange, Good, Warn, Bad; public static Color Bright, Soft, Faint, Thumb, Hover, OnAccent, Card, Sunk, Select, AccentHover; public static bool Light; public static double Scale = 1; public static string FontName; public static Bitmap BgImage; public static string BgFit = "cover", OverlayCorner = "top-left"; public static int BgOpacity = 30, OverlayOpacity = 93, OverlaySize = 100; public static bool BannerArt = true; public static event Action Changed; public static readonly Dictionary Values = new(StringComparer.OrdinalIgnoreCase); public static readonly string[] ColorKeys = { "accent", "background", "sidebar", "panel", "line", "text", "dim", "good", "warn", "bad" }; public static readonly string[] ColorNames = { "Accent", "Background", "Sidebar and header", "Panels and boxes", "Lines and borders", "Text", "Dim text", "Good / On", "Warning", "Bad / Error" }; public static readonly string[] PresetIds = { "dark", "light", "midnight", "contrast", "paradise", "system" }; public static readonly string[] PresetNames = { "Dark", "Light", "Midnight", "High Contrast", "Paradise", "Follow System" }; public static readonly string[] Fits = { "cover", "fit", "stretch", "center", "tile" }; public static readonly string[] Corners = { "top-left", "top-right", "bottom-left", "bottom-right" }; private static readonly string[] OtherKeys = { "base", "name", "font", "scale", "image", "image.opacity", "image.fit", "banner", "overlay.corner", "overlay.opacity" }; private static readonly List> brushes = new(); static Ui() { Apply(); } public static string FilePath => Path.Combine(Dirs.Config, "theme.ini"); public static IBrush B(Color c) { SolidColorBrush b = new(c); if (brushes.Count > 4000) brushes.RemoveAll(w => !w.TryGetTarget(out _)); brushes.Add(new WeakReference(b)); return b; } private static Dictionary Preset(string id) { string[] v = id switch { "light" => new[] { "e8650a", "f3f3f1", "e6e6e3", "ffffff", "d4d4cf", "1e1e20", "6b6b70", "2f9e44", "b86e00", "d63b2f" }, "midnight" => new[] { "ff7a18", "000000", "000000", "0c0c0e", "1e1e23", "e6e6ea", "6c6c75", "84d65c", "ffb03c", "e85448" }, "contrast" => new[] { "ffd400", "000000", "000000", "000000", "ffffff", "ffffff", "d0d0d0", "00ff66", "ffd400", "ff4040" }, "paradise" => new[] { "ff5fa2", "1a1320", "140f19", "241a2c", "3a2c46", "f0e6f5", "9a88a8", "7fe0a0", "ffc060", "ff5a6a" }, _ => new[] { "ff7a18", "141416", "101011", "1c1c1f", "2c2c31", "ececef", "9a9aa3", "50c878", "ffb03c", "eb5050" }, }; Dictionary d = new(); for (int i = 0; i < ColorKeys.Length; i++) d[ColorKeys[i]] = "#" + v[i]; return d; } public static bool SystemLight() { try { var ps = Application.Current?.PlatformSettings; if (ps != null) return ps.GetColorValues().ThemeVariant == PlatformThemeVariant.Light; } catch (Exception) { } return false; } public static string Base { get { if (!Values.TryGetValue("base", out string b)) return "dark"; b = b.Trim().ToLowerInvariant(); return PresetIds.Contains(b) ? b : "dark"; } } private static string Resolved => Base == "system" ? (SystemLight() ? "light" : "dark") : Base; public static string PresetName { get { int i = Array.IndexOf(PresetIds, Base); bool custom = ColorKeys.Any(k => Values.ContainsKey(k)); return (i >= 0 ? PresetNames[i] : "Dark") + (custom ? " (customised)" : ""); } } public static bool TryHex(string s, out Color c) { c = default; if (s == null) return false; s = s.Trim().TrimStart('#'); if (s.Length == 3) s = new string(new[] { s[0], s[0], s[1], s[1], s[2], s[2] }); if (s.Length != 6 || !int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int v)) return false; c = Color.FromRgb((byte)((v >> 16) & 255), (byte)((v >> 8) & 255), (byte)(v & 255)); return true; } public static string Hex(Color c) => "#" + c.R.ToString("x2") + c.G.ToString("x2") + c.B.ToString("x2"); public static Color ColorOf(string key) { if (Values.TryGetValue(key, out string v) && TryHex(v, out Color c)) return c; TryHex(Preset(Resolved)[key], out c); return c; } public static Color Mix(Color a, Color b, double t) => Color.FromArgb((byte)(a.A + (b.A - a.A) * t), (byte)(a.R + (b.R - a.R) * t), (byte)(a.G + (b.G - a.G) * t), (byte)(a.B + (b.B - a.B) * t)); public static Color Alpha(Color c, byte a) => Color.FromArgb(a, c.R, c.G, c.B); public static double Lum(Color c) => (0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B) / 255.0; private static int Int(string k, int def, int min, int max) { if (!Values.TryGetValue(k, out string v) || !int.TryParse(v.Trim().TrimEnd('%'), NumberStyles.Integer, CultureInfo.InvariantCulture, out int i)) return def; return Math.Clamp(i, min, max); } private static string Pick(string k, string[] allowed, string def) { if (!Values.TryGetValue(k, out string v)) return def; v = v.Trim().ToLowerInvariant(); return allowed.Contains(v) ? v : def; } public static void Apply() { Orange = ColorOf("accent"); Back = ColorOf("background"); Side = ColorOf("sidebar"); Panel = ColorOf("panel"); Line = ColorOf("line"); Text = ColorOf("text"); Dim = ColorOf("dim"); Good = ColorOf("good"); Warn = ColorOf("warn"); Bad = ColorOf("bad"); Light = Lum(Back) > 0.5; Bright = Light ? Mix(Text, Colors.Black, 0.5) : Mix(Text, Colors.White, 0.6); Soft = Mix(Text, Back, 0.22); Faint = Mix(Dim, Back, 0.35); Thumb = Mix(Panel, Text, 0.05); Hover = Mix(Panel, Text, Light ? 0.07 : 0.06); Sunk = Mix(Back, Panel, 0.45); Select = Mix(Side, Orange, Light ? 0.14 : 0.16); AccentHover = Mix(Orange, Light ? Colors.Black : Colors.White, 0.15); OnAccent = Lum(Orange) > 0.45 ? Color.FromRgb(20, 20, 21) : Colors.White; FontName = Values.TryGetValue("font", out string f) && f.Trim().Length > 0 ? f.Trim() : null; Scale = Int("scale", 100, 80, 160) / 100.0; BgOpacity = Int("image.opacity", 30, 0, 100); BgFit = Pick("image.fit", Fits, "cover"); BannerArt = Pick("banner", new[] { "art", "plain" }, "art") == "art"; OverlayCorner = Pick("overlay.corner", Corners, "top-left"); OverlayOpacity = Int("overlay.opacity", 93, 30, 100); OverlaySize = Int("overlay.size", 100, 60, 160); Bitmap old = BgImage; BgImage = Values.TryGetValue("image", out string img) ? LoadImage(img.Trim()) : null; if (old != null && old != BgImage) old.Dispose(); Card = BgImage != null && BgOpacity > 0 ? Alpha(Panel, 225) : Panel; } private static Bitmap LoadImage(string path) { try { if (path.StartsWith("~/", StringComparison.Ordinal)) path = Path.Combine(Dirs.Home, path[2..]); if (path.Length == 0 || !File.Exists(path) || new FileInfo(path).Length > 40L * 1024 * 1024) return null; using FileStream fs = File.OpenRead(path); return new Bitmap(fs); } catch (Exception) { return null; } } public static void Load() { Values.Clear(); try { if (File.Exists(FilePath)) Parse(File.ReadAllLines(FilePath), Values); } catch (Exception) { } Apply(); } public static void Parse(string[] lines, Dictionary into) { foreach (string raw in lines) { string l = raw; int hash = l.IndexOf(" #", StringComparison.Ordinal); if (hash >= 0) l = l[..hash]; l = l.Trim(); if (l.Length == 0 || l[0] == '#' || l[0] == ';' || l[0] == '[') continue; int eq = l.IndexOf('='); if (eq <= 0) continue; string k = l[..eq].Trim().ToLowerInvariant(), v = l[(eq + 1)..].Trim(); if (!ColorKeys.Contains(k) && !OtherKeys.Contains(k)) continue; if (v.Length > 400) continue; into[k] = v; } } public static string Serialize() { StringBuilder sb = new(); sb.AppendLine("# Ultra Paradise CU:Trainer theme"); sb.AppendLine("# Edit this file and click Reload Theme File in the Appearance tab. The same file works in the Windows, Linux and Mac trainers."); sb.AppendLine("#"); sb.AppendLine("# base dark | light | midnight | contrast | paradise | system (the preset everything starts from)"); sb.AppendLine("# accent, background, sidebar, panel, line, text, dim, good, warn, bad colours as #rrggbb (leave one out to use the preset's)"); sb.AppendLine("# font any installed font name scale 80 to 160 (percent)"); sb.AppendLine("# image path to a background picture image.opacity 0 to 100 image.fit cover | fit | stretch | center | tile"); sb.AppendLine("# banner art | plain overlay.corner top-left | top-right | bottom-left | bottom-right overlay.opacity 30 to 100"); sb.AppendLine(); sb.AppendLine("base = " + Base); if (Values.TryGetValue("name", out string n)) sb.AppendLine("name = " + n); foreach (string k in ColorKeys) if (Values.TryGetValue(k, out string v)) sb.AppendLine(k + " = " + v); foreach (string k in OtherKeys) if (k != "base" && k != "name" && Values.TryGetValue(k, out string v)) sb.AppendLine(k + " = " + v); return sb.ToString(); } public static void Save() { try { Private.Dir(Dirs.Config); Private.Write(FilePath, new UTF8Encoding(false).GetBytes(Serialize())); } catch (Exception) { } } private static void Change(Action a) { Color[] before = Tokens(); a(); Apply(); Fire(before); } public static void Set(string k, string v) => Change(() => { if (v == null) Values.Remove(k); else Values[k] = v; Save(); }); public static void ResetAll() => Change(() => { Values.Clear(); Save(); }); public static void UsePreset(string id) => Change(() => { foreach (string k in ColorKeys) Values.Remove(k); Values["base"] = id; Save(); }); public static void Reload() => Change(() => { Values.Clear(); try { if (File.Exists(FilePath)) Parse(File.ReadAllLines(FilePath), Values); } catch (Exception) { } }); public static void SystemChanged() { if (Base == "system") Change(() => { }); } public static string Import(string path) { try { if (new FileInfo(path).Length > 64 * 1024) return "That file is too big to be a theme."; Dictionary d = new(StringComparer.OrdinalIgnoreCase); Parse(File.ReadAllLines(path), d); if (d.Count == 0) return "That file has no theme settings in it."; Change(() => { Values.Clear(); foreach (var kv in d) Values[kv.Key] = kv.Value; Save(); }); return null; } catch (Exception e) { return "Couldn't read that file: " + e.Message; } } public static string Export(string path) { try { File.WriteAllText(path, Serialize(), new UTF8Encoding(false)); return null; } catch (Exception e) { return "Couldn't save it: " + e.Message; } } public static Color[] Tokens() => new[] { Back, Side, Panel, Line, Text, Dim, Orange, Good, Warn, Bad, Bright, Soft, Faint, Thumb, Hover, OnAccent, Card, Sunk, Select, AccentHover }; private static void Fire(Color[] before) { Color[] after = Tokens(); brushes.RemoveAll(w => !w.TryGetTarget(out _)); foreach (WeakReference w in brushes) { if (!w.TryGetTarget(out SolidColorBrush b)) continue; for (int i = 0; i < before.Length; i++) if (b.Color == before[i]) { b.Color = after[i]; break; } } ApplyApp(); Changed?.Invoke(before); } public static void ApplyApp() { Application app = Application.Current; if (app == null) return; app.RequestedThemeVariant = Light ? ThemeVariant.Light : ThemeVariant.Dark; app.Resources["SystemAccentColor"] = Orange; app.Resources["SystemAccentColorLight1"] = Mix(Orange, Colors.White, 0.15); app.Resources["SystemAccentColorLight2"] = Mix(Orange, Colors.White, 0.3); app.Resources["SystemAccentColorLight3"] = Mix(Orange, Colors.White, 0.45); app.Resources["SystemAccentColorDark1"] = Mix(Orange, Colors.Black, 0.12); app.Resources["SystemAccentColorDark2"] = Mix(Orange, Colors.Black, 0.25); app.Resources["SystemAccentColorDark3"] = Mix(Orange, Colors.Black, 0.4); } public static FontFamily Font => FontName != null ? new FontFamily(FontName + ", Inter, sans-serif") : FontFamily.Default; public static string OverlayMessage => "THEME corner=" + OverlayCorner + "|alpha=" + OverlayOpacity + "|size=" + OverlaySize + "|accent=" + Hex(Orange).TrimStart('#'); } }