Skip to content

Commit 4a9b88c

Browse files
authored
Use color adjustment settings on "Color Tool" page too (#2392)
* Refactor theme settings into separate control * Use the theme settings in the 'Color Tool' page
1 parent 4252ca5 commit 4a9b88c

File tree

7 files changed

+257
-246
lines changed

7 files changed

+257
-246
lines changed

MainDemo.Wpf/ColorTool.xaml

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,22 +122,9 @@
122122
</UserControl.Resources>
123123

124124
<DockPanel>
125-
<StackPanel
125+
<local:ThemeSettings
126126
DockPanel.Dock="Top"
127-
Orientation="Horizontal"
128-
Margin="8">
129-
<TextBlock
130-
VerticalAlignment="Center"
131-
Text="Light"/>
132-
133-
<ToggleButton
134-
Margin="8 0 16 0"
135-
IsChecked="{Binding IsDarkTheme}"/>
136-
137-
<TextBlock
138-
VerticalAlignment="Center"
139-
Text="Dark"/>
140-
</StackPanel>
127+
Margin="8" />
141128

142129
<DockPanel>
143130
<!-- Selection controls -->

MainDemo.Wpf/Domain/ColorToolViewModel.cs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ public Color? SelectedColor
5555
}
5656
}
5757

58-
private bool _isDarkTheme;
59-
public bool IsDarkTheme
60-
{
61-
get => _isDarkTheme;
62-
set
63-
{
64-
if (SetProperty(ref _isDarkTheme, value))
65-
{
66-
ApplyBase(value);
67-
}
68-
}
69-
}
70-
7158
public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
7259

7360
public ICommand ChangeCustomHueCommand { get; }
@@ -105,16 +92,6 @@ public ColorToolViewModel()
10592
_secondaryColor = theme.SecondaryMid.Color;
10693

10794
SelectedColor = _primaryColor;
108-
109-
IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;
110-
111-
if (_paletteHelper.GetThemeManager() is { } themeManager)
112-
{
113-
themeManager.ThemeChanged += (_, e) =>
114-
{
115-
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
116-
};
117-
}
11895
}
11996

12097
private void ChangeCustomColor(object obj)

MainDemo.Wpf/Domain/PaletteSelectorViewModel.cs

Lines changed: 0 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using System.Windows.Input;
54
using MaterialDesignColors;
65
using MaterialDesignThemes.Wpf;
@@ -12,123 +11,6 @@ public class PaletteSelectorViewModel : ViewModelBase
1211
public PaletteSelectorViewModel()
1312
{
1413
Swatches = new SwatchesProvider().Swatches;
15-
16-
PaletteHelper paletteHelper = new PaletteHelper();
17-
ITheme theme = paletteHelper.GetTheme();
18-
19-
IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;
20-
21-
if (theme is Theme internalTheme)
22-
{
23-
_isColorAdjusted = internalTheme.ColorAdjustment is not null;
24-
25-
var colorAdjustment = internalTheme.ColorAdjustment ?? new ColorAdjustment();
26-
_desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
27-
_contrastValue = colorAdjustment.Contrast;
28-
_colorSelectionValue = colorAdjustment.Colors;
29-
}
30-
31-
if (paletteHelper.GetThemeManager() is { } themeManager)
32-
{
33-
themeManager.ThemeChanged += (_, e) =>
34-
{
35-
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
36-
};
37-
}
38-
}
39-
40-
private bool _isDarkTheme;
41-
public bool IsDarkTheme
42-
{
43-
get => _isDarkTheme;
44-
set
45-
{
46-
if (SetProperty(ref _isDarkTheme, value))
47-
{
48-
ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
49-
}
50-
}
51-
}
52-
53-
private bool _isColorAdjusted;
54-
public bool IsColorAdjusted
55-
{
56-
get => _isColorAdjusted;
57-
set
58-
{
59-
if (SetProperty(ref _isColorAdjusted, value))
60-
{
61-
ModifyTheme(theme =>
62-
{
63-
if (theme is Theme internalTheme)
64-
{
65-
internalTheme.ColorAdjustment = value
66-
? new ColorAdjustment
67-
{
68-
DesiredContrastRatio = DesiredContrastRatio,
69-
Contrast = ContrastValue,
70-
Colors = ColorSelectionValue
71-
}
72-
: null;
73-
}
74-
});
75-
}
76-
}
77-
}
78-
79-
private float _desiredContrastRatio = 4.5f;
80-
public float DesiredContrastRatio
81-
{
82-
get => _desiredContrastRatio;
83-
set
84-
{
85-
if (SetProperty(ref _desiredContrastRatio, value))
86-
{
87-
ModifyTheme(theme =>
88-
{
89-
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
90-
internalTheme.ColorAdjustment.DesiredContrastRatio = value;
91-
});
92-
}
93-
}
94-
}
95-
96-
public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();
97-
98-
private Contrast _contrastValue;
99-
public Contrast ContrastValue
100-
{
101-
get => _contrastValue;
102-
set
103-
{
104-
if (SetProperty(ref _contrastValue, value))
105-
{
106-
ModifyTheme(theme =>
107-
{
108-
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
109-
internalTheme.ColorAdjustment.Contrast = value;
110-
});
111-
}
112-
}
113-
}
114-
115-
public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();
116-
117-
private ColorSelection _colorSelectionValue;
118-
public ColorSelection ColorSelectionValue
119-
{
120-
get => _colorSelectionValue;
121-
set
122-
{
123-
if (SetProperty(ref _colorSelectionValue, value))
124-
{
125-
ModifyTheme(theme =>
126-
{
127-
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
128-
internalTheme.ColorAdjustment.Colors = value;
129-
});
130-
}
131-
}
13214
}
13315

13416
public IEnumerable<Swatch> Swatches { get; }
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using MaterialDesignThemes.Wpf;
5+
6+
namespace MaterialDesignDemo.Domain
7+
{
8+
public class ThemeSettingsViewModel : ViewModelBase
9+
{
10+
public ThemeSettingsViewModel()
11+
{
12+
PaletteHelper paletteHelper = new PaletteHelper();
13+
ITheme theme = paletteHelper.GetTheme();
14+
15+
IsDarkTheme = theme.GetBaseTheme() == BaseTheme.Dark;
16+
17+
if (theme is Theme internalTheme)
18+
{
19+
_isColorAdjusted = internalTheme.ColorAdjustment is not null;
20+
21+
var colorAdjustment = internalTheme.ColorAdjustment ?? new ColorAdjustment();
22+
_desiredContrastRatio = colorAdjustment.DesiredContrastRatio;
23+
_contrastValue = colorAdjustment.Contrast;
24+
_colorSelectionValue = colorAdjustment.Colors;
25+
}
26+
27+
if (paletteHelper.GetThemeManager() is { } themeManager)
28+
{
29+
themeManager.ThemeChanged += (_, e) =>
30+
{
31+
IsDarkTheme = e.NewTheme?.GetBaseTheme() == BaseTheme.Dark;
32+
};
33+
}
34+
}
35+
36+
private bool _isDarkTheme;
37+
public bool IsDarkTheme
38+
{
39+
get => _isDarkTheme;
40+
set
41+
{
42+
if (SetProperty(ref _isDarkTheme, value))
43+
{
44+
ModifyTheme(theme => theme.SetBaseTheme(value ? Theme.Dark : Theme.Light));
45+
}
46+
}
47+
}
48+
49+
private bool _isColorAdjusted;
50+
public bool IsColorAdjusted
51+
{
52+
get => _isColorAdjusted;
53+
set
54+
{
55+
if (SetProperty(ref _isColorAdjusted, value))
56+
{
57+
ModifyTheme(theme =>
58+
{
59+
if (theme is Theme internalTheme)
60+
{
61+
internalTheme.ColorAdjustment = value
62+
? new ColorAdjustment
63+
{
64+
DesiredContrastRatio = DesiredContrastRatio,
65+
Contrast = ContrastValue,
66+
Colors = ColorSelectionValue
67+
}
68+
: null;
69+
}
70+
});
71+
}
72+
}
73+
}
74+
75+
private float _desiredContrastRatio = 4.5f;
76+
public float DesiredContrastRatio
77+
{
78+
get => _desiredContrastRatio;
79+
set
80+
{
81+
if (SetProperty(ref _desiredContrastRatio, value))
82+
{
83+
ModifyTheme(theme =>
84+
{
85+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
86+
internalTheme.ColorAdjustment.DesiredContrastRatio = value;
87+
});
88+
}
89+
}
90+
}
91+
92+
public IEnumerable<Contrast> ContrastValues => Enum.GetValues(typeof(Contrast)).Cast<Contrast>();
93+
94+
private Contrast _contrastValue;
95+
public Contrast ContrastValue
96+
{
97+
get => _contrastValue;
98+
set
99+
{
100+
if (SetProperty(ref _contrastValue, value))
101+
{
102+
ModifyTheme(theme =>
103+
{
104+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
105+
internalTheme.ColorAdjustment.Contrast = value;
106+
});
107+
}
108+
}
109+
}
110+
111+
public IEnumerable<ColorSelection> ColorSelectionValues => Enum.GetValues(typeof(ColorSelection)).Cast<ColorSelection>();
112+
113+
private ColorSelection _colorSelectionValue;
114+
public ColorSelection ColorSelectionValue
115+
{
116+
get => _colorSelectionValue;
117+
set
118+
{
119+
if (SetProperty(ref _colorSelectionValue, value))
120+
{
121+
ModifyTheme(theme =>
122+
{
123+
if (theme is Theme internalTheme && internalTheme.ColorAdjustment != null)
124+
internalTheme.ColorAdjustment.Colors = value;
125+
});
126+
}
127+
}
128+
}
129+
130+
private static void ModifyTheme(Action<ITheme> modificationAction)
131+
{
132+
var paletteHelper = new PaletteHelper();
133+
ITheme theme = paletteHelper.GetTheme();
134+
135+
modificationAction?.Invoke(theme);
136+
137+
paletteHelper.SetTheme(theme);
138+
}
139+
}
140+
}

0 commit comments

Comments
 (0)