Skip to content

Commit 4bc10ba

Browse files
committed
micro optimsation - 15% faster measuring on textboxes
1 parent 5d319eb commit 4bc10ba

File tree

4 files changed

+168
-185
lines changed

4 files changed

+168
-185
lines changed

MainDemo.Wpf/MaterialDesignDemo.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
<Compile Include="ProvingGround.xaml.cs">
151151
<DependentUpon>ProvingGround.xaml</DependentUpon>
152152
</Compile>
153+
<Compile Include="ProvingGroundStuff\MeasuringTextBox.cs" />
153154
<Compile Include="Shadows.xaml.cs">
154155
<DependentUpon>Shadows.xaml</DependentUpon>
155156
</Compile>
@@ -416,9 +417,7 @@
416417
<ItemGroup>
417418
<Resource Include="favicon.ico" />
418419
</ItemGroup>
419-
<ItemGroup>
420-
<Folder Include="ProvingGroundStuff\" />
421-
</ItemGroup>
420+
<ItemGroup />
422421
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
423422
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
424423
Other similar extension points exist, see Microsoft.Common.targets.

MainDemo.Wpf/ProvingGround.xaml

Lines changed: 15 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
77
xmlns:converters="clr-namespace:MaterialDesignThemes.Wpf.Converters;assembly=MaterialDesignThemes.Wpf"
88
xmlns:wpfExample="clr-namespace:MaterialDesignColors.WpfExample"
9+
xmlns:provingGroundStuff="clr-namespace:MaterialDesignDemo.ProvingGroundStuff"
910
mc:Ignorable="d"
1011
d:DesignWidth="477" d:DesignHeight="272"
1112
d:DataContext="{d:DesignInstance wpfExample:ProvingGroundViewModel, d:IsDesignTimeCreatable=False}">
1213
<UserControl.Resources>
1314
<ResourceDictionary>
1415
<ResourceDictionary.MergedDictionaries>
15-
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Button.xaml" />
16+
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.TextBox.xaml" />
1617
<!--
1718
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Light.xaml" />
1819
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.ProgressBar.xaml" />
@@ -23,115 +24,24 @@
2324
</ResourceDictionary.MergedDictionaries>
2425

2526

27+
28+
<Style TargetType="{x:Type provingGroundStuff:MeasuringTextBox}" BasedOn="{StaticResource MaterialDesignTextBox}" />
2629

2730

2831

2932
</ResourceDictionary>
3033
</UserControl.Resources>
3134

32-
<materialDesign:DrawerHost>
33-
<materialDesign:DrawerHost.LeftDrawerContent>
34-
<StackPanel Margin="16">
35-
<TextBlock Margin="4" HorizontalAlignment="Center">LEFT FIELD</TextBlock>
36-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
37-
CommandParameter="{x:Static Dock.Left}"
38-
Margin="4" HorizontalAlignment="Center"
39-
Style="{DynamicResource MaterialDesignFlatButton}">
40-
CLOSE THIS
41-
</Button>
42-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
43-
Margin="4" HorizontalAlignment="Center"
44-
Style="{DynamicResource MaterialDesignFlatButton}">
45-
CLOSE ALL
46-
</Button>
47-
</StackPanel>
48-
</materialDesign:DrawerHost.LeftDrawerContent>
49-
<materialDesign:DrawerHost.TopDrawerContent>
50-
<StackPanel Margin="16" HorizontalAlignment="Center" Orientation="Horizontal">
51-
<TextBlock Margin="4" VerticalAlignment="Center">TOP BANANA</TextBlock>
52-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
53-
Style="{DynamicResource MaterialDesignFlatButton}"
54-
Margin="4" VerticalAlignment="Center">
55-
CLOSE ALL
56-
</Button>
57-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
58-
CommandParameter="{x:Static Dock.Top}"
59-
Style="{DynamicResource MaterialDesignFlatButton}"
60-
Margin="4" VerticalAlignment="Center">
61-
CLOSE THIS
62-
</Button>
63-
</StackPanel>
64-
</materialDesign:DrawerHost.TopDrawerContent>
65-
<materialDesign:DrawerHost.RightDrawerContent>
66-
<StackPanel Margin="16">
67-
<TextBlock Margin="4" HorizontalAlignment="Center">THE RIGHT STUFF</TextBlock>
68-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
69-
CommandParameter="{x:Static Dock.Right}"
70-
Margin="4" HorizontalAlignment="Center"
71-
Style="{DynamicResource MaterialDesignFlatButton}">
72-
CLOSE THIS
73-
</Button>
74-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
75-
Margin="4" HorizontalAlignment="Center"
76-
Style="{DynamicResource MaterialDesignFlatButton}">
77-
CLOSE ALL
78-
</Button>
79-
</StackPanel>
80-
</materialDesign:DrawerHost.RightDrawerContent>
81-
<materialDesign:DrawerHost.BottomDrawerContent>
82-
<StackPanel Margin="16" HorizontalAlignment="Center" Orientation="Horizontal">
83-
<TextBlock Margin="4" VerticalAlignment="Center">BOTTOM BRACKET</TextBlock>
84-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
85-
Style="{DynamicResource MaterialDesignFlatButton}"
86-
Margin="4" VerticalAlignment="Center">
87-
CLOSE ALL
88-
</Button>
89-
<Button Command="{x:Static materialDesign:DrawerHost.CloseDrawerCommand}"
90-
CommandParameter="{x:Static Dock.Bottom}"
91-
Style="{DynamicResource MaterialDesignFlatButton}"
92-
Margin="4" VerticalAlignment="Center">
93-
CLOSE THIS
94-
</Button>
95-
</StackPanel>
96-
</materialDesign:DrawerHost.BottomDrawerContent>
97-
98-
<Grid VerticalAlignment="Center" HorizontalAlignment="Center">
99-
<Grid.RowDefinitions>
100-
<RowDefinition />
101-
<RowDefinition />
102-
<RowDefinition />
103-
</Grid.RowDefinitions>
104-
<Grid.ColumnDefinitions>
105-
<ColumnDefinition />
106-
<ColumnDefinition />
107-
<ColumnDefinition />
108-
</Grid.ColumnDefinitions>
109-
<Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
110-
CommandParameter="{x:Static Dock.Left}"
111-
Grid.Row="1" Grid.Column="0" Margin="4">
112-
<materialDesign:PackIcon Kind="ArrowLeft" />
113-
</Button>
114-
<Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
115-
CommandParameter="{x:Static Dock.Top}"
116-
Grid.Row="0" Grid.Column="1" Margin="4">
117-
<materialDesign:PackIcon Kind="ArrowUp" />
118-
</Button>
119-
<Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
120-
CommandParameter="{x:Static Dock.Right}"
121-
Grid.Row="1" Grid.Column="2" Margin="4">
122-
<materialDesign:PackIcon Kind="ArrowRight" />
123-
</Button>
124-
<Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
125-
CommandParameter="{x:Static Dock.Bottom}"
126-
Grid.Row="2" Grid.Column="1" Margin="4">
127-
<materialDesign:PackIcon Kind="ArrowDown" />
128-
</Button>
129-
<Button Command="{x:Static materialDesign:DrawerHost.OpenDrawerCommand}"
130-
Grid.Row="1" Grid.Column="1" Margin="4"
131-
Style="{DynamicResource MaterialDesignRaisedAccentButton}">
132-
<materialDesign:PackIcon Kind="ArrowAll" />
133-
</Button>
134-
</Grid>
135-
</materialDesign:DrawerHost>
35+
<Grid HorizontalAlignment="Left">
36+
<StackPanel>
37+
<provingGroundStuff:MeasuringTextBox>Hello World</provingGroundStuff:MeasuringTextBox>
38+
<provingGroundStuff:MeasuringTextBox>Hello World</provingGroundStuff:MeasuringTextBox>
39+
<provingGroundStuff:MeasuringTextBox>Hello World</provingGroundStuff:MeasuringTextBox>
40+
<provingGroundStuff:MeasuringTextBox>Hello World</provingGroundStuff:MeasuringTextBox>
41+
<provingGroundStuff:MeasuringTextBox>Hello World</provingGroundStuff:MeasuringTextBox>
42+
<provingGroundStuff:MeasuringTextBox materialDesign:HintAssist.Hint="Float">Hello World</provingGroundStuff:MeasuringTextBox>
43+
<provingGroundStuff:MeasuringTextBox materialDesign:HintAssist.IsFloating="True" materialDesign:HintAssist.Hint="Float">Hello World</provingGroundStuff:MeasuringTextBox>
44+
</StackPanel>
45+
</Grid>
13646

13747
</UserControl>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
using System.Windows.Controls;
9+
using MaterialDesignThemes.Wpf;
10+
11+
namespace MaterialDesignDemo.ProvingGroundStuff
12+
{
13+
public class MeasuringTextBox : TextBox
14+
{
15+
static MeasuringTextBox()
16+
{
17+
DefaultStyleKeyProperty.OverrideMetadata(typeof(MeasuringTextBox), new FrameworkPropertyMetadata(typeof(TextBox)));
18+
}
19+
20+
protected override Size ArrangeOverride(Size arrangeBounds)
21+
{
22+
var stopwatch = Stopwatch.StartNew();
23+
var result = base.ArrangeOverride(arrangeBounds);
24+
stopwatch.Stop();
25+
Debug.WriteLine($"Arrange: {stopwatch.ElapsedMilliseconds} - {stopwatch.ElapsedTicks} - ({GetHashCode()})");
26+
return result;
27+
}
28+
29+
30+
protected override Size MeasureOverride(Size constraint)
31+
{
32+
var stopwatch = Stopwatch.StartNew();
33+
var result = base.MeasureOverride(constraint);
34+
stopwatch.Stop();
35+
Debug.WriteLine($"Measure: {stopwatch.ElapsedMilliseconds} - {stopwatch.ElapsedTicks} - ({GetHashCode()})");
36+
return result;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)