-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathThemeService.cs
More file actions
83 lines (76 loc) · 2.46 KB
/
ThemeService.cs
File metadata and controls
83 lines (76 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Terminal.Gui;
public class ThemeService
{
private const string ThemeFileName = "theme.json";
private static ThemeService _instance;
public static ThemeService Instance => _instance ?? (_instance = new ThemeService());
public Theme CurrentTheme { get; private set; }
private ThemeService()
{
LoadTheme();
}
public void LoadTheme(string filePath = null)
{
string themeFile = filePath ?? ThemeFileName;
if (File.Exists(themeFile))
{
try
{
var json = File.ReadAllText(themeFile);
// Try to parse as nested config { "Theme": { ... } }
var configObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
if (configObj != null && configObj.ContainsKey("Theme"))
{
var themeJson = JsonConvert.SerializeObject(configObj["Theme"]);
CurrentTheme = JsonConvert.DeserializeObject<Theme>(themeJson);
}
else
{
// Fallback: try to parse as direct Theme
CurrentTheme = JsonConvert.DeserializeObject<Theme>(json);
}
}
catch
{
CurrentTheme = Theme.Default;
}
}
else
{
CurrentTheme = Theme.Default;
}
}
public Color GetColor(string key)
{
if (CurrentTheme.Colors.TryGetValue(key, out var color))
return color;
// Fallback to default theme color for the key
if (Theme.Default.Colors.TryGetValue(key, out var defaultColor))
return defaultColor;
// If not found in default, fallback to white
return Color.White;
}
}
public class Theme
{
public Dictionary<string, Color> Colors { get; set; } = new Dictionary<string, Color>();
public static Theme Default => new Theme
{
Colors = new Dictionary<string, Color>
{
{ "Background", Color.Black },
{ "Foreground", Color.White },
{ "Accent", Color.Cyan },
{ "Error", Color.Red },
{ "Warning", Color.BrightYellow },
{ "Info", Color.BrightBlue },
{ "String", Color.White },
{ "Comment", Color.Green },
{ "Secondary", Color.Gray }
}
};
}