Skip to content

Commit a1caa2e

Browse files
authored
Performance improvements (#2955)
Fixes binding leaks. Freezes brushes to improve performance.
1 parent 087a0c8 commit a1caa2e

24 files changed

+362
-338
lines changed

MainDemo.Wpf/ColorTool.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,8 +534,8 @@
534534
<StackPanel Orientation="Horizontal">
535535
<TextBlock Width="80"
536536
VerticalAlignment="Center"
537-
Text="{Binding Name}" />
538-
<ItemsControl ItemTemplate="{StaticResource SwatchColorTemplate}" ItemsSource="{Binding Hues}">
537+
Text="{Binding Name, Mode=OneTime}" />
538+
<ItemsControl ItemTemplate="{StaticResource SwatchColorTemplate}" ItemsSource="{Binding Hues, Mode=OneTime}">
539539
<ItemsControl.ItemsPanel>
540540
<ItemsPanelTemplate>
541541
<VirtualizingStackPanel Orientation="Horizontal" />

MainDemo.Wpf/Domain/BindingProxy.cs

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
namespace MaterialDesignDemo.Domain
1+
namespace MaterialDesignDemo.Domain;
2+
3+
public class BindingProxy : DependencyObject
24
{
3-
public class BindingProxy : Freezable
5+
public object? Data
46
{
5-
protected override Freezable CreateInstanceCore() => new BindingProxy();
6-
7-
public object? Data
8-
{
9-
get => GetValue(DataProperty);
10-
set => SetValue(DataProperty, value);
11-
}
12-
13-
public static readonly DependencyProperty DataProperty =
14-
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
7+
get => GetValue(DataProperty);
8+
set => SetValue(DataProperty, value);
159
}
10+
11+
public static readonly DependencyProperty DataProperty =
12+
DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null));
1613
}

MainDemo.Wpf/Domain/ColorToolViewModel.cs

Lines changed: 151 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -2,199 +2,198 @@
22
using MaterialDesignColors;
33
using MaterialDesignThemes.Wpf;
44

5-
namespace MaterialDesignDemo.Domain
5+
namespace MaterialDesignDemo.Domain;
6+
7+
internal class ColorToolViewModel : ViewModelBase
68
{
7-
internal class ColorToolViewModel : ViewModelBase
8-
{
9-
private readonly PaletteHelper _paletteHelper = new PaletteHelper();
9+
private readonly PaletteHelper _paletteHelper = new();
1010

11-
private ColorScheme _activeScheme;
12-
public ColorScheme ActiveScheme
11+
private ColorScheme _activeScheme;
12+
public ColorScheme ActiveScheme
13+
{
14+
get => _activeScheme;
15+
set
1316
{
14-
get => _activeScheme;
15-
set
17+
if (_activeScheme != value)
1618
{
17-
if (_activeScheme != value)
18-
{
19-
_activeScheme = value;
20-
OnPropertyChanged();
21-
}
19+
_activeScheme = value;
20+
OnPropertyChanged();
2221
}
2322
}
23+
}
2424

25-
private Color? _selectedColor;
26-
public Color? SelectedColor
25+
private Color? _selectedColor;
26+
public Color? SelectedColor
27+
{
28+
get => _selectedColor;
29+
set
2730
{
28-
get => _selectedColor;
29-
set
31+
if (_selectedColor != value)
3032
{
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
3239
{
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);
5150
}
5251
}
5352
}
53+
}
5454

55-
public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
55+
public IEnumerable<ISwatch> Swatches { get; } = SwatchHelper.Swatches;
5656

57-
public ICommand ChangeCustomHueCommand { get; }
57+
public ICommand ChangeCustomHueCommand { get; }
5858

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; }
6464

65-
public ICommand ToggleBaseCommand { get; }
65+
public ICommand ToggleBaseCommand { get; }
6666

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+
}
7474

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));
8484

8585

86-
ITheme theme = _paletteHelper.GetTheme();
86+
ITheme theme = _paletteHelper.GetTheme();
8787

88-
_primaryColor = theme.PrimaryMid.Color;
89-
_secondaryColor = theme.SecondaryMid.Color;
88+
_primaryColor = theme.PrimaryMid.Color;
89+
_secondaryColor = theme.SecondaryMid.Color;
9090

91-
SelectedColor = _primaryColor;
92-
}
91+
SelectedColor = _primaryColor;
92+
}
9393

94-
private void ChangeCustomColor(object? obj)
95-
{
96-
var color = (Color)obj!;
94+
private void ChangeCustomColor(object? obj)
95+
{
96+
var color = (Color)obj!;
9797

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;
118102
}
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+
}
119119

120-
private void ChangeScheme(ColorScheme scheme)
120+
private void ChangeScheme(ColorScheme scheme)
121+
{
122+
ActiveScheme = scheme;
123+
if (ActiveScheme == ColorScheme.Primary)
121124
{
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;
139138
}
139+
}
140140

141-
private Color? _primaryColor;
141+
private Color? _primaryColor;
142142

143-
private Color? _secondaryColor;
143+
private Color? _secondaryColor;
144144

145-
private Color? _primaryForegroundColor;
145+
private Color? _primaryForegroundColor;
146146

147-
private Color? _secondaryForegroundColor;
147+
private Color? _secondaryForegroundColor;
148148

149-
private void ChangeHue(object? obj)
150-
{
151-
var hue = (Color)obj!;
149+
private void ChangeHue(object? obj)
150+
{
151+
var hue = (Color)obj!;
152152

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();
176159
}
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)
179172
{
180-
ITheme theme = _paletteHelper.GetTheme();
173+
SetSecondaryForegroundToSingleColor(hue);
174+
_secondaryForegroundColor = hue;
175+
}
176+
}
181177

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();
185181

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);
188185

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();
192192

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);
196196

197-
_paletteHelper.SetTheme(theme);
198-
}
197+
_paletteHelper.SetTheme(theme);
199198
}
200199
}

0 commit comments

Comments
 (0)