-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
147 lines (135 loc) · 5.5 KB
/
App.axaml.cs
File metadata and controls
147 lines (135 loc) · 5.5 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.IO;
using System.Text.RegularExpressions;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;
using ColorSplitter.Views;
namespace ColorSplitter;
public partial class App : Application
{
public static string CurrentTheme = Config.GetEntry("theme") ?? "avares://Color-Splitter/Styles/light.axaml";
public static bool SavedIsDark =
Config.GetEntry("isDarkTheme") == null || bool.Parse(Config.GetEntry("isDarkTheme") ?? "true");
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
LoadTheme(CurrentTheme, SavedIsDark);
}
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
desktop.MainWindow = new MainWindow();
base.OnFrameworkInitializationCompleted();
}
private static void ThemeFailed()
{
// This function is for if it failed to load a theme, will revert to previous, or will decide to use darkmode if all else fails.
try
{
// Tries loading back to our original loaded theme.
var Resource = (IStyle)AvaloniaXamlLoader.Load(
new Uri(CurrentTheme)
);
Current.RequestedThemeVariant = SavedIsDark ? ThemeVariant.Dark : ThemeVariant.Light;
if (Current.Styles.Count > 3)
Current.Styles.Remove(Current.Styles[3]);
Current.Styles.Add(Resource);
}
catch
{
// Tries loading our default theme. Purpose of this is if a theme somehow vanished.
var Resource = (IStyle)AvaloniaXamlLoader.Load(
new Uri("avares://Color-Splitter/Styles/dark.axaml")
);
Current.RequestedThemeVariant = ThemeVariant.Dark;
if (Current.Styles.Count > 3)
Current.Styles.Remove(Current.Styles[3]);
Current.Styles.Add(Resource);
CurrentTheme = "avares://Color-Splitter/Styles/dark.axaml";
SavedIsDark = true;
}
}
public static string? LoadThemeFromString(string themeText, bool isDark = true, string themeUri = "")
{
var OutputMessage = "";
try
{
// Tries loading as runtime uncompiled.
var TextInput = themeText;
TextInput = Regex.Replace(TextInput, @"file:./", AppDomain.CurrentDomain.BaseDirectory);
if (themeUri != "")
{
TextInput = Regex.Replace(TextInput, @"style:./",
Regex.Replace(themeUri, @"\\(?:.(?!\\))+$", "") + "\\");
}
else
{
OutputMessage += "- You have not saved this theme, so it won't parse style:./.\n\n";
}
Match isCodeDark = Regex.Match(TextInput, @"<!--#DarkTheme-->");
Match isCodeLight = Regex.Match(TextInput, @"<!--#LightTheme-->");
if (isCodeDark.Success && isCodeLight.Success) throw new Exception("My brother in christ, you cannot have both DarkTheme and LightTheme.");
if (isCodeDark.Success) isDark = true;
if (isCodeLight.Success) isDark = false;
var Resource = AvaloniaRuntimeXamlLoader.Parse<Styles>(
TextInput
);
Current.RequestedThemeVariant = isDark ? ThemeVariant.Dark : ThemeVariant.Light;
Current.Styles.Remove(Current.Styles[3]);
Current.Styles.Add(Resource);
if (themeUri != "")
{
CurrentTheme = themeUri;
SavedIsDark = isDark;
}
}
catch (Exception ex)
{
ThemeFailed();
Console.WriteLine(ex);
OutputMessage += "# Theme has failed to load successfully due to an error.\n" + ex.Message;
return OutputMessage;
}
OutputMessage += "# Theme loaded successfully!\n";
return OutputMessage;
}
public static string? LoadTheme(string themeUri, bool isDark = true)
{
// I tried to do this before it was initialised, but "Replace" would prevent the "GetEntry" from ever being
// - null, which means it would never "default" to dark theme if there is no entry.
if (themeUri.Contains("avares://Autodraw")) themeUri = themeUri.Replace("avares://Autodraw", "avares://Color-Splitter");
// Behold, terrible bruteforce-ey code! Performance be damned!
try
{
Console.WriteLine("Loading Compiled Code");
// Tries loading as Compiled.
var Resource = (IStyle)AvaloniaXamlLoader.Load(
new Uri(themeUri)
);
Current.RequestedThemeVariant = isDark ? ThemeVariant.Dark : ThemeVariant.Light;
//Current.Styles.Remove(Current.Styles[3]);
Current.Styles.Add(Resource);
CurrentTheme = themeUri;
SavedIsDark = isDark;
}
catch
{
try
{
// Tries loading as runtime uncompiled.
Console.WriteLine("Loading Uncompiled Runtime Code");
var TextInput = File.ReadAllText(themeUri);
return LoadThemeFromString(TextInput, isDark, themeUri);
}
catch (Exception ex)
{
Console.WriteLine(":(");
ThemeFailed();
return ex.Message;
}
}
return null;
}
}