Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use BooleanBoxes for better dependency properties performance #3869

Merged
merged 2 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/MahApps.Metro/Actions/CloseFlyoutAction.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using MahApps.Metro.Controls;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Actions
{
Expand Down Expand Up @@ -26,7 +27,7 @@ protected override void Invoke(object parameter)
}
else
{
this.AssociatedFlyout?.SetCurrentValue(Flyout.IsOpenProperty, false);
this.AssociatedFlyout?.SetCurrentValue(Flyout.IsOpenProperty, BooleanBoxes.FalseBox);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/MahApps.Metro/Actions/CommandTriggerAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Windows;
using System.Windows.Input;
using MahApps.Metro.ValueBoxes;
using Microsoft.Xaml.Behaviors;

namespace MahApps.Metro.Actions
Expand Down Expand Up @@ -117,7 +118,7 @@ private void EnableDisableElement()
}

var command = this.Command;
this.AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, command == null || command.CanExecute(this.GetCommandParameter()));
this.AssociatedObject.SetCurrentValue(UIElement.IsEnabledProperty, BooleanBoxes.Box(command == null || command.CanExecute(this.GetCommandParameter())));
}

private void OnCommandCanExecuteChanged(object sender, EventArgs e)
Expand Down
11 changes: 6 additions & 5 deletions src/MahApps.Metro/Behaviors/PasswordBoxBindingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Windows.Controls;
using System.Windows.Documents;
using MahApps.Metro.Controls;
using MahApps.Metro.ValueBoxes;
using Microsoft.Xaml.Behaviors;

namespace MahApps.Metro.Behaviors
Expand Down Expand Up @@ -159,16 +160,16 @@ private static readonly DependencyProperty IsChangingProperty
= DependencyProperty.RegisterAttached("IsChanging",
typeof(bool),
typeof(PasswordBoxBindingBehavior),
new UIPropertyMetadata(false));
new UIPropertyMetadata(BooleanBoxes.FalseBox));

private static bool GetIsChanging(DependencyObject obj)
private static bool GetIsChanging(UIElement element)
{
return (bool)obj.GetValue(IsChangingProperty);
return (bool)element.GetValue(IsChangingProperty);
}

private static void SetIsChanging(DependencyObject obj, bool value)
private static void SetIsChanging(UIElement element, bool value)
{
obj.SetValue(IsChangingProperty, value);
element.SetValue(IsChangingProperty, BooleanBoxes.Box(value));
}

private static readonly DependencyProperty SelectionProperty
Expand Down
9 changes: 5 additions & 4 deletions src/MahApps.Metro/Behaviors/ReloadBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.Controls;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Behaviors
{
Expand All @@ -17,7 +18,7 @@ public static readonly DependencyProperty OnDataContextChangedProperty
= DependencyProperty.RegisterAttached("OnDataContextChanged",
typeof(bool),
typeof(ReloadBehavior),
new PropertyMetadata(OnOnDataContextChanged));
new PropertyMetadata(BooleanBoxes.FalseBox, OnOnDataContextChanged));

/// <summary>
/// Helper for getting <see cref="OnDataContextChangedProperty"/> from <paramref name="element"/>.
Expand All @@ -44,7 +45,7 @@ public static bool GetOnDataContextChanged(UIElement element)
[AttachedPropertyBrowsableForType(typeof(MetroContentControl))]
public static void SetOnDataContextChanged(UIElement element, bool value)
{
element.SetValue(OnDataContextChangedProperty, value);
element.SetValue(OnDataContextChangedProperty, BooleanBoxes.Box(value));
}

private static void OnOnDataContextChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
Expand All @@ -71,7 +72,7 @@ public static readonly DependencyProperty OnSelectedTabChangedProperty
= DependencyProperty.RegisterAttached("OnSelectedTabChanged",
typeof(bool),
typeof(ReloadBehavior),
new PropertyMetadata(OnSelectedTabChanged));
new PropertyMetadata(BooleanBoxes.FalseBox, OnSelectedTabChanged));

/// <summary>
/// Helper for getting <see cref="OnSelectedTabChangedProperty"/> from <paramref name="element"/>.
Expand Down Expand Up @@ -100,7 +101,7 @@ public static bool GetOnSelectedTabChanged(UIElement element)
[AttachedPropertyBrowsableForType(typeof(TransitioningContentControl))]
public static void SetOnSelectedTabChanged(UIElement element, bool value)
{
element.SetValue(OnSelectedTabChangedProperty, value);
element.SetValue(OnSelectedTabChangedProperty, BooleanBoxes.Box(value));
}

private static void OnSelectedTabChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
Expand Down
5 changes: 3 additions & 2 deletions src/MahApps.Metro/Behaviors/TiltBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Media;
using ControlzEx.Theming;
using MahApps.Metro.Controls;
using MahApps.Metro.ValueBoxes;
using Microsoft.Xaml.Behaviors;

namespace MahApps.Metro.Behaviors
Expand All @@ -17,12 +18,12 @@ public static readonly DependencyProperty KeepDraggingProperty
= DependencyProperty.Register(nameof(KeepDragging),
typeof(bool),
typeof(TiltBehavior),
new PropertyMetadata(true));
new PropertyMetadata(BooleanBoxes.TrueBox));

public bool KeepDragging
{
get => (bool)this.GetValue(KeepDraggingProperty);
set => this.SetValue(KeepDraggingProperty, value);
set => this.SetValue(KeepDraggingProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="TiltFactor"/> dependency property.</summary>
Expand Down
5 changes: 3 additions & 2 deletions src/MahApps.Metro/Controls/ClipBorder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls
{
Expand Down Expand Up @@ -220,7 +221,7 @@ public Brush Background
/// </summary>
public static readonly DependencyProperty OptimizeClipRenderingProperty =
DependencyProperty.Register(nameof(OptimizeClipRendering), typeof(bool), typeof(ClipBorder),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, FrameworkPropertyMetadataOptions.AffectsRender));

/// <summary>
/// Gets or sets the OptimizeClipRendering property. This dependency property
Expand All @@ -235,7 +236,7 @@ public Brush Background
public bool OptimizeClipRendering
{
get { return (bool)GetValue(OptimizeClipRenderingProperty); }
set { SetValue(OptimizeClipRenderingProperty, value); }
set { SetValue(OptimizeClipRenderingProperty, BooleanBoxes.Box(value)); }
}

#endregion
Expand Down
5 changes: 3 additions & 2 deletions src/MahApps.Metro/Controls/ContentControlEx.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Windows;
using System.Windows.Controls;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls
{
Expand Down Expand Up @@ -27,15 +28,15 @@ public static readonly DependencyProperty RecognizesAccessKeyProperty
= DependencyProperty.Register(nameof(RecognizesAccessKey),
typeof(bool),
typeof(ContentControlEx),
new FrameworkPropertyMetadata(false));
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));

/// <summary>
/// Determine if the inner ContentPresenter should use AccessText in its style
/// </summary>
public bool RecognizesAccessKey
{
get => (bool)this.GetValue(RecognizesAccessKeyProperty);
set => this.SetValue(RecognizesAccessKeyProperty, value);
set => this.SetValue(RecognizesAccessKeyProperty, BooleanBoxes.Box(value));
}

static ContentControlEx()
Expand Down
27 changes: 14 additions & 13 deletions src/MahApps.Metro/Controls/CustomValidationPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Interop;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls
{
Expand All @@ -29,31 +30,31 @@ public static readonly DependencyProperty CloseOnMouseLeftButtonDownProperty
= DependencyProperty.Register(nameof(CloseOnMouseLeftButtonDown),
typeof(bool),
typeof(CustomValidationPopup),
new PropertyMetadata(true));
new PropertyMetadata(BooleanBoxes.TrueBox));

/// <summary>
/// Gets or sets whether if the popup can be closed by left mouse button down.
/// </summary>
public bool CloseOnMouseLeftButtonDown
{
get => (bool)this.GetValue(CloseOnMouseLeftButtonDownProperty);
set => this.SetValue(CloseOnMouseLeftButtonDownProperty, value);
set => this.SetValue(CloseOnMouseLeftButtonDownProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="ShowValidationErrorOnMouseOver"/> dependency property.</summary>
public static readonly DependencyProperty ShowValidationErrorOnMouseOverProperty
= DependencyProperty.RegisterAttached(nameof(ShowValidationErrorOnMouseOver),
typeof(bool),
typeof(CustomValidationPopup),
new PropertyMetadata(false));
new PropertyMetadata(BooleanBoxes.FalseBox));

/// <summary>
/// Gets or sets whether the validation error text will be shown when hovering the validation triangle.
/// </summary>
public bool ShowValidationErrorOnMouseOver
{
get => (bool)this.GetValue(ShowValidationErrorOnMouseOverProperty);
set => this.SetValue(ShowValidationErrorOnMouseOverProperty, value);
set => this.SetValue(ShowValidationErrorOnMouseOverProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="AdornedElement"/> dependency property.</summary>
Expand All @@ -77,7 +78,7 @@ public static readonly DependencyPropertyKey CanShowPropertyKey
= DependencyProperty.RegisterReadOnly(nameof(CanShow),
typeof(bool),
typeof(CustomValidationPopup),
new PropertyMetadata(false));
new PropertyMetadata(BooleanBoxes.FalseBox));

/// <summary>Identifies the <see cref="CanShow"/> dependency property.</summary>
public static readonly DependencyProperty CanShowProperty = CanShowPropertyKey.DependencyProperty;
Expand All @@ -88,7 +89,7 @@ public static readonly DependencyPropertyKey CanShowPropertyKey
public bool CanShow
{
get => (bool)this.GetValue(CanShowProperty);
protected set => this.SetValue(CanShowPropertyKey, value);
protected set => this.SetValue(CanShowPropertyKey, BooleanBoxes.Box(value));
}

public CustomValidationPopup()
Expand All @@ -101,14 +102,14 @@ protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
if (this.CloseOnMouseLeftButtonDown)
{
this.SetCurrentValue(IsOpenProperty, false);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.FalseBox);
}
else
{
var adornedElement = this.AdornedElement;
if (adornedElement != null && ValidationHelper.GetCloseOnMouseLeftButtonDown(adornedElement))
{
this.SetCurrentValue(IsOpenProperty, false);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.FalseBox);
}
else
{
Expand Down Expand Up @@ -219,7 +220,7 @@ private void Flyout_OpeningFinished(object sender, RoutedEventArgs e)

var adornedElement = this.AdornedElement;
var isOpen = Validation.GetHasError(adornedElement) && adornedElement.IsKeyboardFocusWithin;
this.SetCurrentValue(IsOpenProperty, isOpen);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.Box(isOpen));

this.SetValue(CanShowPropertyKey, true);
}
Expand Down Expand Up @@ -248,7 +249,7 @@ private void OnTransitionCompleted(object sender, RoutedEventArgs e)

var adornedElement = this.AdornedElement;
var isOpen = Validation.GetHasError(adornedElement) && adornedElement.IsKeyboardFocusWithin;
this.SetCurrentValue(IsOpenProperty, isOpen);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.Box(isOpen));

this.SetValue(CanShowPropertyKey, true);
}
Expand All @@ -263,11 +264,11 @@ private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
var adornedElement = this.AdornedElement;
var isOpen = Validation.GetHasError(adornedElement) && adornedElement.IsKeyboardFocusWithin;
this.SetCurrentValue(IsOpenProperty, isOpen);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.Box(isOpen));
}
else
{
this.SetCurrentValue(IsOpenProperty, false);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.FalseBox);
}
}
}
Expand Down Expand Up @@ -362,7 +363,7 @@ private void OnHostWindowStateChanged(object sender, EventArgs e)
if (adornedElement != null)
{
this.PopupAnimation = PopupAnimation.None;
this.SetCurrentValue(IsOpenProperty, false);
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.FalseBox);
var errorTemplate = adornedElement.GetValue(Validation.ErrorTemplateProperty);
adornedElement.SetValue(Validation.ErrorTemplateProperty, null);
adornedElement.SetValue(Validation.ErrorTemplateProperty, errorTemplate);
Expand Down
9 changes: 5 additions & 4 deletions src/MahApps.Metro/Controls/Dialogs/LoginDialog.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls.Dialogs
{
Expand Down Expand Up @@ -90,12 +91,12 @@ public Visibility NegativeButtonButtonVisibility
}

/// <summary>Identifies the <see cref="ShouldHideUsername"/> dependency property.</summary>
public static readonly DependencyProperty ShouldHideUsernameProperty = DependencyProperty.Register(nameof(ShouldHideUsername), typeof(bool), typeof(LoginDialog), new PropertyMetadata(false));
public static readonly DependencyProperty ShouldHideUsernameProperty = DependencyProperty.Register(nameof(ShouldHideUsername), typeof(bool), typeof(LoginDialog), new PropertyMetadata(BooleanBoxes.FalseBox));

public bool ShouldHideUsername
{
get { return (bool)this.GetValue(ShouldHideUsernameProperty); }
set { this.SetValue(ShouldHideUsernameProperty, value); }
set { this.SetValue(ShouldHideUsernameProperty, BooleanBoxes.Box(value)); }
}

/// <summary>Identifies the <see cref="RememberCheckBoxVisibility"/> dependency property.</summary>
Expand All @@ -117,12 +118,12 @@ public string RememberCheckBoxText
}

/// <summary>Identifies the <see cref="RememberCheckBoxChecked"/> dependency property.</summary>
public static readonly DependencyProperty RememberCheckBoxCheckedProperty = DependencyProperty.Register(nameof(RememberCheckBoxChecked), typeof(bool), typeof(LoginDialog), new PropertyMetadata(false));
public static readonly DependencyProperty RememberCheckBoxCheckedProperty = DependencyProperty.Register(nameof(RememberCheckBoxChecked), typeof(bool), typeof(LoginDialog), new PropertyMetadata(BooleanBoxes.FalseBox));

public bool RememberCheckBoxChecked
{
get { return (bool)this.GetValue(RememberCheckBoxCheckedProperty); }
set { this.SetValue(RememberCheckBoxCheckedProperty, value); }
set { this.SetValue(RememberCheckBoxCheckedProperty, BooleanBoxes.Box(value)); }
}

internal LoginDialog()
Expand Down
5 changes: 3 additions & 2 deletions src/MahApps.Metro/Controls/Dialogs/ProgressDialog.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Threading;
using System.Windows;
using System.Windows.Media;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls.Dialogs
{
Expand All @@ -19,12 +20,12 @@ public string Message
}

/// <summary>Identifies the <see cref="IsCancelable"/> dependency property.</summary>
public static readonly DependencyProperty IsCancelableProperty = DependencyProperty.Register(nameof(IsCancelable), typeof(bool), typeof(ProgressDialog), new PropertyMetadata(default(bool), (s, e) => { ((ProgressDialog)s).PART_NegativeButton.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Hidden; }));
public static readonly DependencyProperty IsCancelableProperty = DependencyProperty.Register(nameof(IsCancelable), typeof(bool), typeof(ProgressDialog), new PropertyMetadata(BooleanBoxes.FalseBox, (s, e) => { ((ProgressDialog)s).PART_NegativeButton.Visibility = (bool)e.NewValue ? Visibility.Visible : Visibility.Hidden; }));

public bool IsCancelable
{
get { return (bool)this.GetValue(IsCancelableProperty); }
set { this.SetValue(IsCancelableProperty, value); }
set { this.SetValue(IsCancelableProperty, BooleanBoxes.Box(value)); }
}

/// <summary>Identifies the <see cref="NegativeButtonText"/> dependency property.</summary>
Expand Down
7 changes: 4 additions & 3 deletions src/MahApps.Metro/Controls/DropDownButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls
{
Expand Down Expand Up @@ -34,7 +35,7 @@ public static readonly DependencyProperty IsExpandedProperty
= DependencyProperty.Register(nameof(IsExpanded),
typeof(bool),
typeof(DropDownButton),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsExpandedPropertyChangedCallback));
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsExpandedPropertyChangedCallback));

private static void OnIsExpandedPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
Expand All @@ -58,7 +59,7 @@ protected virtual void SetContextMenuPlacementTarget(ContextMenu contextMenu)
public bool IsExpanded
{
get => (bool)this.GetValue(IsExpandedProperty);
set => this.SetValue(IsExpandedProperty, value);
set => this.SetValue(IsExpandedProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="ExtraTag"/> dependency property.</summary>
Expand Down Expand Up @@ -411,7 +412,7 @@ private void ButtonClick(object sender, RoutedEventArgs e)

if (this.contextMenu?.HasItems == true)
{
this.SetCurrentValue(IsExpandedProperty, true);
this.SetCurrentValue(IsExpandedProperty, BooleanBoxes.TrueBox);
}

e.RoutedEvent = ClickEvent;
Expand Down
Loading