This repository has been archived by the owner on Oct 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowExtensions.cs
122 lines (111 loc) · 4.65 KB
/
WindowExtensions.cs
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
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Threading;
using System;
using System.IO;
using System.Linq;
namespace SilverCraft.AvaloniaUtils
{
public record class StylingChangeData(bool? IsTransparent,string? SAPColor, WindowTransparencyLevel[]? SAPTransparency);
public static class WindowExtensions
{
public static IEnvBackend envBackend = new ModernDotFileBackend(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SilverCraftAvaloniav1Shared",
"dotfile.json"));
public static EventHandler<StylingChangeData> OnStyleChange;
static bool DisSAPTransparency => envBackend.GetBool("DisableSAPTransparency") == true;
public static Color ReadColor(this string varname, Color? def = null)
{
var color = envBackend.GetString(varname);
if (color == null) return def ?? Colors.Coral;
if (Color.TryParse(color, out var c)) return c;
if (Enum.TryParse(color, out KnownColor kc))
{
c = kc.ToColor();
}
else
{
return def ?? Colors.Coral;
}
return c;
}
public static Color? ParseColor(string value)
{
if (Color.TryParse(value, out var c)) return c;
if (Enum.TryParse(value, out KnownColor kc))
{
return kc.ToColor();
}
else
{
return null;
}
}
public static IBrush ParseBackground(this string? color, IBrush? def = null)
{
if (color == null) return def ?? new SolidColorBrush(Colors.Coral, DisSAPTransparency ? 1 : 0.3);
if (color.Contains(','))
{
var colors = color.Split(',');
var perpart = 1d / colors.Length;
double alreadygiven = 0;
var gradient = new LinearGradientBrush();
foreach (var c in colors)
{
var cc = ParseColor(c);
if (cc == null) continue;
gradient.GradientStops.Add(new((Color)cc, alreadygiven));
alreadygiven += perpart;
}
return gradient;
}
var co = ParseColor(color);
return co ==null? (def ?? new SolidColorBrush(Colors.Coral, DisSAPTransparency ? 1 : 0.3)) :new SolidColorBrush((Color)co, envBackend.GetBool("DisableSAPTransparency") == true ? 1 : 0.3);
}
public static Color ToColor(this KnownColor kc) => Color.FromUInt32((uint)kc);
public static WindowTransparencyLevel GetTransparencyLevelFromString(string s) =>
s switch
{
"AcrylicBlur" => WindowTransparencyLevel.AcrylicBlur,
"Transparent" => WindowTransparencyLevel.Transparent,
"Mica" => WindowTransparencyLevel.Mica,
_=> WindowTransparencyLevel.AcrylicBlur
};
public static WindowTransparencyLevel[] GetTransparencyLevelsFromString(string s)
{
return s.Split(',').Select(GetTransparencyLevelFromString).ToArray();
}
public static void DoAfterInitTasks(this Window w, bool firstrun, IBrush? def = null)
{
w.TransparencyLevelHint =
GetTransparencyLevelsFromString(envBackend.GetString("SAPTransparency") ?? "Mica,AcrylicBlur,None");
w.Background = ParseBackground(envBackend.GetString("SAPColor"), def: def);
if (!firstrun) return;
EventHandler<StylingChangeData> x = (_, y) =>
{
if(y!=null)
{
if (y.SAPTransparency != null)
{
Dispatcher.UIThread.InvokeAsync(() => w.TransparencyLevelHint = y.SAPTransparency);
}
if (y.SAPColor != null)
{
Dispatcher.UIThread.InvokeAsync(() => w.Background = ParseBackground(y.SAPColor, def));
return;
}
if (y.IsTransparent != null)
{
Dispatcher.UIThread.InvokeAsync(() => w.Background = ParseBackground(y.SAPColor, def));
}
}
else
{
Dispatcher.UIThread.InvokeAsync(() => w.DoAfterInitTasks(false, def: def));
}
};
OnStyleChange += x;
w.Closing += (_, _) => { if (OnStyleChange != null) { OnStyleChange -= x; } };
}
}
}