|
2 | 2 | using MaterialDesignColors;
|
3 | 3 | using MaterialDesignThemes.Wpf;
|
4 | 4 |
|
5 |
| -namespace MaterialDesignDemo.Domain |
| 5 | +namespace MaterialDesignDemo.Domain; |
| 6 | + |
| 7 | +internal class ColorToolViewModel : ViewModelBase |
6 | 8 | {
|
7 |
| - internal class ColorToolViewModel : ViewModelBase |
8 |
| - { |
9 |
| - private readonly PaletteHelper _paletteHelper = new PaletteHelper(); |
| 9 | + private readonly PaletteHelper _paletteHelper = new(); |
10 | 10 |
|
11 |
| - private ColorScheme _activeScheme; |
12 |
| - public ColorScheme ActiveScheme |
| 11 | + private ColorScheme _activeScheme; |
| 12 | + public ColorScheme ActiveScheme |
| 13 | + { |
| 14 | + get => _activeScheme; |
| 15 | + set |
13 | 16 | {
|
14 |
| - get => _activeScheme; |
15 |
| - set |
| 17 | + if (_activeScheme != value) |
16 | 18 | {
|
17 |
| - if (_activeScheme != value) |
18 |
| - { |
19 |
| - _activeScheme = value; |
20 |
| - OnPropertyChanged(); |
21 |
| - } |
| 19 | + _activeScheme = value; |
| 20 | + OnPropertyChanged(); |
22 | 21 | }
|
23 | 22 | }
|
| 23 | + } |
24 | 24 |
|
25 |
| - private Color? _selectedColor; |
26 |
| - public Color? SelectedColor |
| 25 | + private Color? _selectedColor; |
| 26 | + public Color? SelectedColor |
| 27 | + { |
| 28 | + get => _selectedColor; |
| 29 | + set |
27 | 30 | {
|
28 |
| - get => _selectedColor; |
29 |
| - set |
| 31 | + if (_selectedColor != value) |
30 | 32 | {
|
31 |
| - if (_selectedColor != value) |
| 33 | + _selectedColor = value; |
| 34 | + OnPropertyChanged(); |
| 35 | + |
| 36 | + // if we are triggering a change internally its a hue change and the colors will match |
| 37 | + // so we don't want to trigger a custom color change. |
| 38 | + var currentSchemeColor = ActiveScheme switch |
32 | 39 | {
|
33 |
| - _selectedColor = value; |
34 |
| - OnPropertyChanged(); |
35 |
| - |
36 |
| - // if we are triggering a change internally its a hue change and the colors will match |
37 |
| - // so we don't want to trigger a custom color change. |
38 |
| - var currentSchemeColor = ActiveScheme switch |
39 |
| - { |
40 |
| - ColorScheme.Primary => _primaryColor, |
41 |
| - ColorScheme.Secondary => _secondaryColor, |
42 |
| - ColorScheme.PrimaryForeground => _primaryForegroundColor, |
43 |
| - ColorScheme.SecondaryForeground => _secondaryForegroundColor, |
44 |
| - _ => throw new NotSupportedException($"{ActiveScheme} is not a handled ColorScheme.. Ye daft programmer!") |
45 |
| - }; |
46 |
| - |
47 |
| - if (_selectedColor != currentSchemeColor && value is Color color) |
48 |
| - { |
49 |
| - ChangeCustomColor(color); |
50 |
| - } |
| 40 | + ColorScheme.Primary => _primaryColor, |
| 41 | + ColorScheme.Secondary => _secondaryColor, |
| 42 | + ColorScheme.PrimaryForeground => _primaryForegroundColor, |
| 43 | + ColorScheme.SecondaryForeground => _secondaryForegroundColor, |
| 44 | + _ => throw new NotSupportedException($"{ActiveScheme} is not a handled ColorScheme.. Ye daft programmer!") |
| 45 | + }; |
| 46 | + |
| 47 | + if (_selectedColor != currentSchemeColor && value is Color color) |
| 48 | + { |
| 49 | + ChangeCustomColor(color); |
51 | 50 | }
|
52 | 51 | }
|
53 | 52 | }
|
| 53 | + } |
54 | 54 |
|
55 |
| - public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches; |
| 55 | + public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches; |
56 | 56 |
|
57 |
| - public ICommand ChangeCustomHueCommand { get; } |
| 57 | + public ICommand ChangeCustomHueCommand { get; } |
58 | 58 |
|
59 |
| - public ICommand ChangeHueCommand { get; } |
60 |
| - public ICommand ChangeToPrimaryCommand { get; } |
61 |
| - public ICommand ChangeToSecondaryCommand { get; } |
62 |
| - public ICommand ChangeToPrimaryForegroundCommand { get; } |
63 |
| - public ICommand ChangeToSecondaryForegroundCommand { get; } |
| 59 | + public ICommand ChangeHueCommand { get; } |
| 60 | + public ICommand ChangeToPrimaryCommand { get; } |
| 61 | + public ICommand ChangeToSecondaryCommand { get; } |
| 62 | + public ICommand ChangeToPrimaryForegroundCommand { get; } |
| 63 | + public ICommand ChangeToSecondaryForegroundCommand { get; } |
64 | 64 |
|
65 |
| - public ICommand ToggleBaseCommand { get; } |
| 65 | + public ICommand ToggleBaseCommand { get; } |
66 | 66 |
|
67 |
| - private void ApplyBase(bool isDark) |
68 |
| - { |
69 |
| - ITheme theme = _paletteHelper.GetTheme(); |
70 |
| - IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme(); |
71 |
| - theme.SetBaseTheme(baseTheme); |
72 |
| - _paletteHelper.SetTheme(theme); |
73 |
| - } |
| 67 | + private void ApplyBase(bool isDark) |
| 68 | + { |
| 69 | + ITheme theme = _paletteHelper.GetTheme(); |
| 70 | + IBaseTheme baseTheme = isDark ? new MaterialDesignDarkTheme() : (IBaseTheme)new MaterialDesignLightTheme(); |
| 71 | + theme.SetBaseTheme(baseTheme); |
| 72 | + _paletteHelper.SetTheme(theme); |
| 73 | + } |
74 | 74 |
|
75 |
| - public ColorToolViewModel() |
76 |
| - { |
77 |
| - ToggleBaseCommand = new AnotherCommandImplementation(o => ApplyBase((bool)o!)); |
78 |
| - ChangeHueCommand = new AnotherCommandImplementation(ChangeHue); |
79 |
| - ChangeCustomHueCommand = new AnotherCommandImplementation(ChangeCustomColor); |
80 |
| - ChangeToPrimaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Primary)); |
81 |
| - ChangeToSecondaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Secondary)); |
82 |
| - ChangeToPrimaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.PrimaryForeground)); |
83 |
| - ChangeToSecondaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.SecondaryForeground)); |
| 75 | + public ColorToolViewModel() |
| 76 | + { |
| 77 | + ToggleBaseCommand = new AnotherCommandImplementation(o => ApplyBase((bool)o!)); |
| 78 | + ChangeHueCommand = new AnotherCommandImplementation(ChangeHue); |
| 79 | + ChangeCustomHueCommand = new AnotherCommandImplementation(ChangeCustomColor); |
| 80 | + ChangeToPrimaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Primary)); |
| 81 | + ChangeToSecondaryCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.Secondary)); |
| 82 | + ChangeToPrimaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.PrimaryForeground)); |
| 83 | + ChangeToSecondaryForegroundCommand = new AnotherCommandImplementation(o => ChangeScheme(ColorScheme.SecondaryForeground)); |
84 | 84 |
|
85 | 85 |
|
86 |
| - ITheme theme = _paletteHelper.GetTheme(); |
| 86 | + ITheme theme = _paletteHelper.GetTheme(); |
87 | 87 |
|
88 |
| - _primaryColor = theme.PrimaryMid.Color; |
89 |
| - _secondaryColor = theme.SecondaryMid.Color; |
| 88 | + _primaryColor = theme.PrimaryMid.Color; |
| 89 | + _secondaryColor = theme.SecondaryMid.Color; |
90 | 90 |
|
91 |
| - SelectedColor = _primaryColor; |
92 |
| - } |
| 91 | + SelectedColor = _primaryColor; |
| 92 | + } |
93 | 93 |
|
94 |
| - private void ChangeCustomColor(object? obj) |
95 |
| - { |
96 |
| - var color = (Color)obj!; |
| 94 | + private void ChangeCustomColor(object? obj) |
| 95 | + { |
| 96 | + var color = (Color)obj!; |
97 | 97 |
|
98 |
| - if (ActiveScheme == ColorScheme.Primary) |
99 |
| - { |
100 |
| - _paletteHelper.ChangePrimaryColor(color); |
101 |
| - _primaryColor = color; |
102 |
| - } |
103 |
| - else if (ActiveScheme == ColorScheme.Secondary) |
104 |
| - { |
105 |
| - _paletteHelper.ChangeSecondaryColor(color); |
106 |
| - _secondaryColor = color; |
107 |
| - } |
108 |
| - else if (ActiveScheme == ColorScheme.PrimaryForeground) |
109 |
| - { |
110 |
| - SetPrimaryForegroundToSingleColor(color); |
111 |
| - _primaryForegroundColor = color; |
112 |
| - } |
113 |
| - else if (ActiveScheme == ColorScheme.SecondaryForeground) |
114 |
| - { |
115 |
| - SetSecondaryForegroundToSingleColor(color); |
116 |
| - _secondaryForegroundColor = color; |
117 |
| - } |
| 98 | + if (ActiveScheme == ColorScheme.Primary) |
| 99 | + { |
| 100 | + _paletteHelper.ChangePrimaryColor(color); |
| 101 | + _primaryColor = color; |
118 | 102 | }
|
| 103 | + else if (ActiveScheme == ColorScheme.Secondary) |
| 104 | + { |
| 105 | + _paletteHelper.ChangeSecondaryColor(color); |
| 106 | + _secondaryColor = color; |
| 107 | + } |
| 108 | + else if (ActiveScheme == ColorScheme.PrimaryForeground) |
| 109 | + { |
| 110 | + SetPrimaryForegroundToSingleColor(color); |
| 111 | + _primaryForegroundColor = color; |
| 112 | + } |
| 113 | + else if (ActiveScheme == ColorScheme.SecondaryForeground) |
| 114 | + { |
| 115 | + SetSecondaryForegroundToSingleColor(color); |
| 116 | + _secondaryForegroundColor = color; |
| 117 | + } |
| 118 | + } |
119 | 119 |
|
120 |
| - private void ChangeScheme(ColorScheme scheme) |
| 120 | + private void ChangeScheme(ColorScheme scheme) |
| 121 | + { |
| 122 | + ActiveScheme = scheme; |
| 123 | + if (ActiveScheme == ColorScheme.Primary) |
121 | 124 | {
|
122 |
| - ActiveScheme = scheme; |
123 |
| - if (ActiveScheme == ColorScheme.Primary) |
124 |
| - { |
125 |
| - SelectedColor = _primaryColor; |
126 |
| - } |
127 |
| - else if (ActiveScheme == ColorScheme.Secondary) |
128 |
| - { |
129 |
| - SelectedColor = _secondaryColor; |
130 |
| - } |
131 |
| - else if (ActiveScheme == ColorScheme.PrimaryForeground) |
132 |
| - { |
133 |
| - SelectedColor = _primaryForegroundColor; |
134 |
| - } |
135 |
| - else if (ActiveScheme == ColorScheme.SecondaryForeground) |
136 |
| - { |
137 |
| - SelectedColor = _secondaryForegroundColor; |
138 |
| - } |
| 125 | + SelectedColor = _primaryColor; |
| 126 | + } |
| 127 | + else if (ActiveScheme == ColorScheme.Secondary) |
| 128 | + { |
| 129 | + SelectedColor = _secondaryColor; |
| 130 | + } |
| 131 | + else if (ActiveScheme == ColorScheme.PrimaryForeground) |
| 132 | + { |
| 133 | + SelectedColor = _primaryForegroundColor; |
| 134 | + } |
| 135 | + else if (ActiveScheme == ColorScheme.SecondaryForeground) |
| 136 | + { |
| 137 | + SelectedColor = _secondaryForegroundColor; |
139 | 138 | }
|
| 139 | + } |
140 | 140 |
|
141 |
| - private Color? _primaryColor; |
| 141 | + private Color? _primaryColor; |
142 | 142 |
|
143 |
| - private Color? _secondaryColor; |
| 143 | + private Color? _secondaryColor; |
144 | 144 |
|
145 |
| - private Color? _primaryForegroundColor; |
| 145 | + private Color? _primaryForegroundColor; |
146 | 146 |
|
147 |
| - private Color? _secondaryForegroundColor; |
| 147 | + private Color? _secondaryForegroundColor; |
148 | 148 |
|
149 |
| - private void ChangeHue(object? obj) |
150 |
| - { |
151 |
| - var hue = (Color)obj!; |
| 149 | + private void ChangeHue(object? obj) |
| 150 | + { |
| 151 | + var hue = (Color)obj!; |
152 | 152 |
|
153 |
| - SelectedColor = hue; |
154 |
| - if (ActiveScheme == ColorScheme.Primary) |
155 |
| - { |
156 |
| - _paletteHelper.ChangePrimaryColor(hue); |
157 |
| - _primaryColor = hue; |
158 |
| - _primaryForegroundColor = _paletteHelper.GetTheme().PrimaryMid.GetForegroundColor(); |
159 |
| - } |
160 |
| - else if (ActiveScheme == ColorScheme.Secondary) |
161 |
| - { |
162 |
| - _paletteHelper.ChangeSecondaryColor(hue); |
163 |
| - _secondaryColor = hue; |
164 |
| - _secondaryForegroundColor = _paletteHelper.GetTheme().SecondaryMid.GetForegroundColor(); |
165 |
| - } |
166 |
| - else if (ActiveScheme == ColorScheme.PrimaryForeground) |
167 |
| - { |
168 |
| - SetPrimaryForegroundToSingleColor(hue); |
169 |
| - _primaryForegroundColor = hue; |
170 |
| - } |
171 |
| - else if (ActiveScheme == ColorScheme.SecondaryForeground) |
172 |
| - { |
173 |
| - SetSecondaryForegroundToSingleColor(hue); |
174 |
| - _secondaryForegroundColor = hue; |
175 |
| - } |
| 153 | + SelectedColor = hue; |
| 154 | + if (ActiveScheme == ColorScheme.Primary) |
| 155 | + { |
| 156 | + _paletteHelper.ChangePrimaryColor(hue); |
| 157 | + _primaryColor = hue; |
| 158 | + _primaryForegroundColor = _paletteHelper.GetTheme().PrimaryMid.GetForegroundColor(); |
176 | 159 | }
|
177 |
| - |
178 |
| - private void SetPrimaryForegroundToSingleColor(Color color) |
| 160 | + else if (ActiveScheme == ColorScheme.Secondary) |
| 161 | + { |
| 162 | + _paletteHelper.ChangeSecondaryColor(hue); |
| 163 | + _secondaryColor = hue; |
| 164 | + _secondaryForegroundColor = _paletteHelper.GetTheme().SecondaryMid.GetForegroundColor(); |
| 165 | + } |
| 166 | + else if (ActiveScheme == ColorScheme.PrimaryForeground) |
| 167 | + { |
| 168 | + SetPrimaryForegroundToSingleColor(hue); |
| 169 | + _primaryForegroundColor = hue; |
| 170 | + } |
| 171 | + else if (ActiveScheme == ColorScheme.SecondaryForeground) |
179 | 172 | {
|
180 |
| - ITheme theme = _paletteHelper.GetTheme(); |
| 173 | + SetSecondaryForegroundToSingleColor(hue); |
| 174 | + _secondaryForegroundColor = hue; |
| 175 | + } |
| 176 | + } |
181 | 177 |
|
182 |
| - theme.PrimaryLight = new ColorPair(theme.PrimaryLight.Color, color); |
183 |
| - theme.PrimaryMid = new ColorPair(theme.PrimaryMid.Color, color); |
184 |
| - theme.PrimaryDark = new ColorPair(theme.PrimaryDark.Color, color); |
| 178 | + private void SetPrimaryForegroundToSingleColor(Color color) |
| 179 | + { |
| 180 | + ITheme theme = _paletteHelper.GetTheme(); |
185 | 181 |
|
186 |
| - _paletteHelper.SetTheme(theme); |
187 |
| - } |
| 182 | + theme.PrimaryLight = new ColorPair(theme.PrimaryLight.Color, color); |
| 183 | + theme.PrimaryMid = new ColorPair(theme.PrimaryMid.Color, color); |
| 184 | + theme.PrimaryDark = new ColorPair(theme.PrimaryDark.Color, color); |
188 | 185 |
|
189 |
| - private void SetSecondaryForegroundToSingleColor(Color color) |
190 |
| - { |
191 |
| - ITheme theme = _paletteHelper.GetTheme(); |
| 186 | + _paletteHelper.SetTheme(theme); |
| 187 | + } |
| 188 | + |
| 189 | + private void SetSecondaryForegroundToSingleColor(Color color) |
| 190 | + { |
| 191 | + ITheme theme = _paletteHelper.GetTheme(); |
192 | 192 |
|
193 |
| - theme.SecondaryLight = new ColorPair(theme.SecondaryLight.Color, color); |
194 |
| - theme.SecondaryMid = new ColorPair(theme.SecondaryMid.Color, color); |
195 |
| - theme.SecondaryDark = new ColorPair(theme.SecondaryDark.Color, color); |
| 193 | + theme.SecondaryLight = new ColorPair(theme.SecondaryLight.Color, color); |
| 194 | + theme.SecondaryMid = new ColorPair(theme.SecondaryMid.Color, color); |
| 195 | + theme.SecondaryDark = new ColorPair(theme.SecondaryDark.Color, color); |
196 | 196 |
|
197 |
| - _paletteHelper.SetTheme(theme); |
198 |
| - } |
| 197 | + _paletteHelper.SetTheme(theme); |
199 | 198 | }
|
200 | 199 | }
|
0 commit comments