cu-trainer/Ultra Paradise Linux/app/Theme.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Globalization; |
| 4 | using System.IO; |
| 5 | using System.Linq; |
| 6 | using System.Text; |
| 7 | using Avalonia; |
| 8 | using Avalonia.Media; |
| 9 | using Avalonia.Media.Imaging; |
| 10 | using Avalonia.Platform; |
| 11 | using Avalonia.Styling; |
| 12 | |
| 13 | namespace UltraParadise.Linux |
| 14 | { |
| 15 | internal static class Ui |
| 16 | { |
| 17 | public static Color Back, Side, Panel, Line, Text, Dim, Orange, Good, Warn, Bad; |
| 18 | public static Color Bright, Soft, Faint, Thumb, Hover, OnAccent, Card, Sunk, Select, AccentHover; |
| 19 | public static bool Light; |
| 20 | public static double Scale = 1; |
| 21 | public static string FontName; |
| 22 | public static Bitmap BgImage; |
| 23 | public static string BgFit = "cover", OverlayCorner = "top-left"; |
| 24 | public static int BgOpacity = 30, OverlayOpacity = 93, OverlaySize = 100; |
| 25 | public static bool BannerArt = true; |
| 26 | public static event Action<Color[]> Changed; |
| 27 | |
| 28 | public static readonly Dictionary<string, string> Values = new(StringComparer.OrdinalIgnoreCase); |
| 29 | public static readonly string[] ColorKeys = { "accent", "background", "sidebar", "panel", "line", "text", "dim", "good", "warn", "bad" }; |
| 30 | public static readonly string[] ColorNames = { "Accent", "Background", "Sidebar and header", "Panels and boxes", "Lines and borders", "Text", "Dim text", "Good / On", "Warning", "Bad / Error" }; |
| 31 | public static readonly string[] PresetIds = { "dark", "light", "midnight", "contrast", "paradise", "system" }; |
| 32 | public static readonly string[] PresetNames = { "Dark", "Light", "Midnight", "High Contrast", "Paradise", "Follow System" }; |
| 33 | public static readonly string[] Fits = { "cover", "fit", "stretch", "center", "tile" }; |
| 34 | public static readonly string[] Corners = { "top-left", "top-right", "bottom-left", "bottom-right" }; |
| 35 | private static readonly string[] OtherKeys = { "base", "name", "font", "scale", "image", "image.opacity", "image.fit", "banner", "overlay.corner", "overlay.opacity" }; |
| 36 | |
| 37 | private static readonly List<WeakReference<SolidColorBrush>> brushes = new(); |
| 38 | |
| 39 | static Ui() { Apply(); } |
| 40 | |
| 41 | public static string FilePath => Path.Combine(Dirs.Config, "theme.ini"); |
| 42 | |
| 43 | public static IBrush B(Color c) |
| 44 | { |
| 45 | SolidColorBrush b = new(c); |
| 46 | if (brushes.Count > 4000) brushes.RemoveAll(w => !w.TryGetTarget(out _)); |
| 47 | brushes.Add(new WeakReference<SolidColorBrush>(b)); |
| 48 | return b; |
| 49 | } |
| 50 | |
| 51 | private static Dictionary<string, string> Preset(string id) |
| 52 | { |
| 53 | string[] v = id switch |
| 54 | { |
| 55 | "light" => new[] { "e8650a", "f3f3f1", "e6e6e3", "ffffff", "d4d4cf", "1e1e20", "6b6b70", "2f9e44", "b86e00", "d63b2f" }, |
| 56 | "midnight" => new[] { "ff7a18", "000000", "000000", "0c0c0e", "1e1e23", "e6e6ea", "6c6c75", "84d65c", "ffb03c", "e85448" }, |
| 57 | "contrast" => new[] { "ffd400", "000000", "000000", "000000", "ffffff", "ffffff", "d0d0d0", "00ff66", "ffd400", "ff4040" }, |
| 58 | "paradise" => new[] { "ff5fa2", "1a1320", "140f19", "241a2c", "3a2c46", "f0e6f5", "9a88a8", "7fe0a0", "ffc060", "ff5a6a" }, |
| 59 | _ => new[] { "ff7a18", "141416", "101011", "1c1c1f", "2c2c31", "ececef", "9a9aa3", "50c878", "ffb03c", "eb5050" }, |
| 60 | }; |
| 61 | Dictionary<string, string> d = new(); |
| 62 | for (int i = 0; i < ColorKeys.Length; i++) d[ColorKeys[i]] = "#" + v[i]; |
| 63 | return d; |
| 64 | } |
| 65 | |
| 66 | public static bool SystemLight() |
| 67 | { |
| 68 | try |
| 69 | { |
| 70 | var ps = Application.Current?.PlatformSettings; |
| 71 | if (ps != null) return ps.GetColorValues().ThemeVariant == PlatformThemeVariant.Light; |
| 72 | } |
| 73 | catch (Exception) { } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | public static string Base |
| 78 | { |
| 79 | get |
| 80 | { |
| 81 | if (!Values.TryGetValue("base", out string b)) return "dark"; |
| 82 | b = b.Trim().ToLowerInvariant(); |
| 83 | return PresetIds.Contains(b) ? b : "dark"; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static string Resolved => Base == "system" ? (SystemLight() ? "light" : "dark") : Base; |
| 88 | |
| 89 | public static string PresetName |
| 90 | { |
| 91 | get |
| 92 | { |
| 93 | int i = Array.IndexOf(PresetIds, Base); |
| 94 | bool custom = ColorKeys.Any(k => Values.ContainsKey(k)); |
| 95 | return (i >= 0 ? PresetNames[i] : "Dark") + (custom ? " (customised)" : ""); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | public static bool TryHex(string s, out Color c) |
| 100 | { |
| 101 | c = default; |
| 102 | if (s == null) return false; |
| 103 | s = s.Trim().TrimStart('#'); |
| 104 | if (s.Length == 3) s = new string(new[] { s[0], s[0], s[1], s[1], s[2], s[2] }); |
| 105 | if (s.Length != 6 || !int.TryParse(s, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out int v)) return false; |
| 106 | c = Color.FromRgb((byte)((v >> 16) & 255), (byte)((v >> 8) & 255), (byte)(v & 255)); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | public static string Hex(Color c) => "#" + c.R.ToString("x2") + c.G.ToString("x2") + c.B.ToString("x2"); |
| 111 | |
| 112 | public static Color ColorOf(string key) |
| 113 | { |
| 114 | if (Values.TryGetValue(key, out string v) && TryHex(v, out Color c)) return c; |
| 115 | TryHex(Preset(Resolved)[key], out c); |
| 116 | return c; |
| 117 | } |
| 118 | |
| 119 | public static Color Mix(Color a, Color b, double t) => |
| 120 | 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)); |
| 121 | |
| 122 | public static Color Alpha(Color c, byte a) => Color.FromArgb(a, c.R, c.G, c.B); |
| 123 | |
| 124 | public static double Lum(Color c) => (0.2126 * c.R + 0.7152 * c.G + 0.0722 * c.B) / 255.0; |
| 125 | |
| 126 | private static int Int(string k, int def, int min, int max) |
| 127 | { |
| 128 | if (!Values.TryGetValue(k, out string v) || !int.TryParse(v.Trim().TrimEnd('%'), NumberStyles.Integer, CultureInfo.InvariantCulture, out int i)) return def; |
| 129 | return Math.Clamp(i, min, max); |
| 130 | } |
| 131 | |
| 132 | private static string Pick(string k, string[] allowed, string def) |
| 133 | { |
| 134 | if (!Values.TryGetValue(k, out string v)) return def; |
| 135 | v = v.Trim().ToLowerInvariant(); |
| 136 | return allowed.Contains(v) ? v : def; |
| 137 | } |
| 138 | |
| 139 | public static void Apply() |
| 140 | { |
| 141 | Orange = ColorOf("accent"); |
| 142 | Back = ColorOf("background"); |
| 143 | Side = ColorOf("sidebar"); |
| 144 | Panel = ColorOf("panel"); |
| 145 | Line = ColorOf("line"); |
| 146 | Text = ColorOf("text"); |
| 147 | Dim = ColorOf("dim"); |
| 148 | Good = ColorOf("good"); |
| 149 | Warn = ColorOf("warn"); |
| 150 | Bad = ColorOf("bad"); |
| 151 | Light = Lum(Back) > 0.5; |
| 152 | Bright = Light ? Mix(Text, Colors.Black, 0.5) : Mix(Text, Colors.White, 0.6); |
| 153 | Soft = Mix(Text, Back, 0.22); |
| 154 | Faint = Mix(Dim, Back, 0.35); |
| 155 | Thumb = Mix(Panel, Text, 0.05); |
| 156 | Hover = Mix(Panel, Text, Light ? 0.07 : 0.06); |
| 157 | Sunk = Mix(Back, Panel, 0.45); |
| 158 | Select = Mix(Side, Orange, Light ? 0.14 : 0.16); |
| 159 | AccentHover = Mix(Orange, Light ? Colors.Black : Colors.White, 0.15); |
| 160 | OnAccent = Lum(Orange) > 0.45 ? Color.FromRgb(20, 20, 21) : Colors.White; |
| 161 | |
| 162 | FontName = Values.TryGetValue("font", out string f) && f.Trim().Length > 0 ? f.Trim() : null; |
| 163 | Scale = Int("scale", 100, 80, 160) / 100.0; |
| 164 | BgOpacity = Int("image.opacity", 30, 0, 100); |
| 165 | BgFit = Pick("image.fit", Fits, "cover"); |
| 166 | BannerArt = Pick("banner", new[] { "art", "plain" }, "art") == "art"; |
| 167 | OverlayCorner = Pick("overlay.corner", Corners, "top-left"); |
| 168 | OverlayOpacity = Int("overlay.opacity", 93, 30, 100); |
| 169 | OverlaySize = Int("overlay.size", 100, 60, 160); |
| 170 | |
| 171 | Bitmap old = BgImage; |
| 172 | BgImage = Values.TryGetValue("image", out string img) ? LoadImage(img.Trim()) : null; |
| 173 | if (old != null && old != BgImage) old.Dispose(); |
| 174 | Card = BgImage != null && BgOpacity > 0 ? Alpha(Panel, 225) : Panel; |
| 175 | } |
| 176 | |
| 177 | private static Bitmap LoadImage(string path) |
| 178 | { |
| 179 | try |
| 180 | { |
| 181 | if (path.StartsWith("~/", StringComparison.Ordinal)) path = Path.Combine(Dirs.Home, path[2..]); |
| 182 | if (path.Length == 0 || !File.Exists(path) || new FileInfo(path).Length > 40L * 1024 * 1024) return null; |
| 183 | using FileStream fs = File.OpenRead(path); |
| 184 | return new Bitmap(fs); |
| 185 | } |
| 186 | catch (Exception) { return null; } |
| 187 | } |
| 188 | |
| 189 | public static void Load() |
| 190 | { |
| 191 | Values.Clear(); |
| 192 | try { if (File.Exists(FilePath)) Parse(File.ReadAllLines(FilePath), Values); } catch (Exception) { } |
| 193 | Apply(); |
| 194 | } |
| 195 | |
| 196 | public static void Parse(string[] lines, Dictionary<string, string> into) |
| 197 | { |
| 198 | foreach (string raw in lines) |
| 199 | { |
| 200 | string l = raw; |
| 201 | int hash = l.IndexOf(" #", StringComparison.Ordinal); |
| 202 | if (hash >= 0) l = l[..hash]; |
| 203 | l = l.Trim(); |
| 204 | if (l.Length == 0 || l[0] == '#' || l[0] == ';' || l[0] == '[') continue; |
| 205 | int eq = l.IndexOf('='); |
| 206 | if (eq <= 0) continue; |
| 207 | string k = l[..eq].Trim().ToLowerInvariant(), v = l[(eq + 1)..].Trim(); |
| 208 | if (!ColorKeys.Contains(k) && !OtherKeys.Contains(k)) continue; |
| 209 | if (v.Length > 400) continue; |
| 210 | into[k] = v; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | public static string Serialize() |
| 215 | { |
| 216 | StringBuilder sb = new(); |
| 217 | sb.AppendLine("# Ultra Paradise CU:Trainer theme"); |
| 218 | 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."); |
| 219 | sb.AppendLine("#"); |
| 220 | sb.AppendLine("# base dark | light | midnight | contrast | paradise | system (the preset everything starts from)"); |
| 221 | sb.AppendLine("# accent, background, sidebar, panel, line, text, dim, good, warn, bad colours as #rrggbb (leave one out to use the preset's)"); |
| 222 | sb.AppendLine("# font any installed font name scale 80 to 160 (percent)"); |
| 223 | sb.AppendLine("# image path to a background picture image.opacity 0 to 100 image.fit cover | fit | stretch | center | tile"); |
| 224 | sb.AppendLine("# banner art | plain overlay.corner top-left | top-right | bottom-left | bottom-right overlay.opacity 30 to 100"); |
| 225 | sb.AppendLine(); |
| 226 | sb.AppendLine("base = " + Base); |
| 227 | if (Values.TryGetValue("name", out string n)) sb.AppendLine("name = " + n); |
| 228 | foreach (string k in ColorKeys) if (Values.TryGetValue(k, out string v)) sb.AppendLine(k + " = " + v); |
| 229 | foreach (string k in OtherKeys) if (k != "base" && k != "name" && Values.TryGetValue(k, out string v)) sb.AppendLine(k + " = " + v); |
| 230 | return sb.ToString(); |
| 231 | } |
| 232 | |
| 233 | public static void Save() |
| 234 | { |
| 235 | try |
| 236 | { |
| 237 | Private.Dir(Dirs.Config); |
| 238 | Private.Write(FilePath, new UTF8Encoding(false).GetBytes(Serialize())); |
| 239 | } |
| 240 | catch (Exception) { } |
| 241 | } |
| 242 | |
| 243 | private static void Change(Action a) |
| 244 | { |
| 245 | Color[] before = Tokens(); |
| 246 | a(); |
| 247 | Apply(); |
| 248 | Fire(before); |
| 249 | } |
| 250 | |
| 251 | public static void Set(string k, string v) => Change(() => { if (v == null) Values.Remove(k); else Values[k] = v; Save(); }); |
| 252 | |
| 253 | public static void ResetAll() => Change(() => { Values.Clear(); Save(); }); |
| 254 | |
| 255 | public static void UsePreset(string id) => Change(() => { foreach (string k in ColorKeys) Values.Remove(k); Values["base"] = id; Save(); }); |
| 256 | |
| 257 | public static void Reload() => Change(() => |
| 258 | { |
| 259 | Values.Clear(); |
| 260 | try { if (File.Exists(FilePath)) Parse(File.ReadAllLines(FilePath), Values); } catch (Exception) { } |
| 261 | }); |
| 262 | |
| 263 | public static void SystemChanged() { if (Base == "system") Change(() => { }); } |
| 264 | |
| 265 | public static string Import(string path) |
| 266 | { |
| 267 | try |
| 268 | { |
| 269 | if (new FileInfo(path).Length > 64 * 1024) return "That file is too big to be a theme."; |
| 270 | Dictionary<string, string> d = new(StringComparer.OrdinalIgnoreCase); |
| 271 | Parse(File.ReadAllLines(path), d); |
| 272 | if (d.Count == 0) return "That file has no theme settings in it."; |
| 273 | Change(() => { Values.Clear(); foreach (var kv in d) Values[kv.Key] = kv.Value; Save(); }); |
| 274 | return null; |
| 275 | } |
| 276 | catch (Exception e) { return "Couldn't read that file: " + e.Message; } |
| 277 | } |
| 278 | |
| 279 | public static string Export(string path) |
| 280 | { |
| 281 | try { File.WriteAllText(path, Serialize(), new UTF8Encoding(false)); return null; } |
| 282 | catch (Exception e) { return "Couldn't save it: " + e.Message; } |
| 283 | } |
| 284 | |
| 285 | 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 }; |
| 286 | |
| 287 | private static void Fire(Color[] before) |
| 288 | { |
| 289 | Color[] after = Tokens(); |
| 290 | brushes.RemoveAll(w => !w.TryGetTarget(out _)); |
| 291 | foreach (WeakReference<SolidColorBrush> w in brushes) |
| 292 | { |
| 293 | if (!w.TryGetTarget(out SolidColorBrush b)) continue; |
| 294 | for (int i = 0; i < before.Length; i++) |
| 295 | if (b.Color == before[i]) { b.Color = after[i]; break; } |
| 296 | } |
| 297 | ApplyApp(); |
| 298 | Changed?.Invoke(before); |
| 299 | } |
| 300 | |
| 301 | public static void ApplyApp() |
| 302 | { |
| 303 | Application app = Application.Current; |
| 304 | if (app == null) return; |
| 305 | app.RequestedThemeVariant = Light ? ThemeVariant.Light : ThemeVariant.Dark; |
| 306 | app.Resources["SystemAccentColor"] = Orange; |
| 307 | app.Resources["SystemAccentColorLight1"] = Mix(Orange, Colors.White, 0.15); |
| 308 | app.Resources["SystemAccentColorLight2"] = Mix(Orange, Colors.White, 0.3); |
| 309 | app.Resources["SystemAccentColorLight3"] = Mix(Orange, Colors.White, 0.45); |
| 310 | app.Resources["SystemAccentColorDark1"] = Mix(Orange, Colors.Black, 0.12); |
| 311 | app.Resources["SystemAccentColorDark2"] = Mix(Orange, Colors.Black, 0.25); |
| 312 | app.Resources["SystemAccentColorDark3"] = Mix(Orange, Colors.Black, 0.4); |
| 313 | } |
| 314 | |
| 315 | public static FontFamily Font => FontName != null ? new FontFamily(FontName + ", Inter, sans-serif") : FontFamily.Default; |
| 316 | |
| 317 | public static string OverlayMessage => "THEME corner=" + OverlayCorner + "|alpha=" + OverlayOpacity + "|size=" + OverlaySize + "|accent=" + Hex(Orange).TrimStart('#'); |
| 318 | } |
| 319 | } |