-
Notifications
You must be signed in to change notification settings - Fork 32
/
StyleManager.cs
77 lines (67 loc) · 2.88 KB
/
StyleManager.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
using System;
using Avalonia.Controls;
using Avalonia.Markup.Xaml.Styling;
namespace Citrus.Avalonia.Sandbox
{
public sealed class StyleManager
{
public enum Theme { Citrus, Sea, Rust, Candy, Magma }
private readonly StyleInclude _magmaStyle = CreateStyle("avares://Citrus.Avalonia/Magma.xaml");
private readonly StyleInclude _candyStyle = CreateStyle("avares://Citrus.Avalonia/Candy.xaml");
private readonly StyleInclude _citrusStyle = CreateStyle("avares://Citrus.Avalonia/Citrus.xaml");
private readonly StyleInclude _rustStyle = CreateStyle("avares://Citrus.Avalonia/Rust.xaml");
private readonly StyleInclude _seaStyle = CreateStyle("avares://Citrus.Avalonia/Sea.xaml");
private readonly Window _window;
public StyleManager(Window window)
{
_window = window;
// We add the style to the window styles section, so it
// will override the default style defined in App.xaml.
if (window.Styles.Count == 0)
window.Styles.Add(_citrusStyle);
// If there are styles defined already, we assume that
// the first style imported it related to citrus.
// This allows one to override citrus styles.
else window.Styles[0] = _citrusStyle;
}
public Theme CurrentTheme { get; private set; } = Theme.Citrus;
public void UseTheme(Theme theme)
{
// Here, we change the first style in the main window styles
// section, and the main window instantly refreshes. Remember
// to invoke such methods from the UI thread.
_window.Styles[0] = theme switch
{
Theme.Citrus => _citrusStyle,
Theme.Sea => _seaStyle,
Theme.Rust => _rustStyle,
Theme.Candy => _candyStyle,
Theme.Magma => _magmaStyle,
_ => throw new ArgumentOutOfRangeException(nameof(theme))
};
CurrentTheme = theme;
}
public void UseNextTheme()
{
// This method allows to support switching among all
// supported color schemes one by one.
UseTheme(CurrentTheme switch
{
Theme.Citrus => Theme.Sea,
Theme.Sea => Theme.Rust,
Theme.Rust => Theme.Candy,
Theme.Candy => Theme.Magma,
Theme.Magma => Theme.Citrus,
_ => throw new ArgumentOutOfRangeException(nameof(CurrentTheme))
});
}
private static StyleInclude CreateStyle(string url)
{
var self = new Uri("resm:Styles?assembly=Citrus.Avalonia.Sandbox");
return new StyleInclude(self)
{
Source = new Uri(url)
};
}
}
}