Skip to content

Commit

Permalink
(GH-3869) Use more BooleanBoxes for better dependency properties perf…
Browse files Browse the repository at this point in the history
…ormance
  • Loading branch information
punker76 committed Jun 29, 2020
1 parent a915b56 commit 7bf17ee
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/MahApps.Metro/Behaviors/DatePickerTextBoxBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Windows.Controls.Primitives;
using System.Windows.Threading;
using MahApps.Metro.Controls;
using MahApps.Metro.ValueBoxes;
using Microsoft.Xaml.Behaviors;

namespace MahApps.Metro.Behaviors
Expand Down Expand Up @@ -29,7 +30,7 @@ private void OnTextChanged(object sender, TextChangedEventArgs e)

private void SetHasTextProperty()
{
this.AssociatedObject.TemplatedParent?.SetValue(TextBoxHelper.HasTextProperty, this.AssociatedObject.Text.Length > 0);
this.AssociatedObject.TemplatedParent?.SetCurrentValue(TextBoxHelper.HasTextProperty, BooleanBoxes.Box(this.AssociatedObject.Text.Length > 0));
}
}
}
14 changes: 7 additions & 7 deletions src/MahApps.Metro/Controls/CustomValidationPopup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private void CustomValidationPopup_Loaded(object sender, RoutedEventArgs e)
return;
}

this.SetValue(CanShowPropertyKey, false);
this.SetValue(CanShowPropertyKey, BooleanBoxes.FalseBox);
var canShow = true;

if (this.scrollViewer != null)
Expand Down Expand Up @@ -206,7 +206,7 @@ private void CustomValidationPopup_Loaded(object sender, RoutedEventArgs e)
}

this.RefreshPosition();
this.SetValue(CanShowPropertyKey, canShow);
this.SetValue(CanShowPropertyKey, BooleanBoxes.Box(canShow));

this.OnLoaded();

Expand All @@ -222,25 +222,25 @@ private void Flyout_OpeningFinished(object sender, RoutedEventArgs e)
var isOpen = Validation.GetHasError(adornedElement) && adornedElement.IsKeyboardFocusWithin;
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.Box(isOpen));

this.SetValue(CanShowPropertyKey, true);
this.SetValue(CanShowPropertyKey, BooleanBoxes.TrueBox);
}

private void Flyout_IsOpenChanged(object sender, RoutedEventArgs e)
{
this.RefreshPosition();
this.SetValue(CanShowPropertyKey, false);
this.SetValue(CanShowPropertyKey, BooleanBoxes.FalseBox);
}

private void Flyout_ClosingFinished(object sender, RoutedEventArgs e)
{
this.RefreshPosition();
this.SetValue(CanShowPropertyKey, false);
this.SetValue(CanShowPropertyKey, BooleanBoxes.FalseBox);
}

private void OnTransitionStarted(object sender, RoutedEventArgs e)
{
this.RefreshPosition();
this.SetValue(CanShowPropertyKey, false);
this.SetValue(CanShowPropertyKey, BooleanBoxes.FalseBox);
}

private void OnTransitionCompleted(object sender, RoutedEventArgs e)
Expand All @@ -251,7 +251,7 @@ private void OnTransitionCompleted(object sender, RoutedEventArgs e)
var isOpen = Validation.GetHasError(adornedElement) && adornedElement.IsKeyboardFocusWithin;
this.SetCurrentValue(IsOpenProperty, BooleanBoxes.Box(isOpen));

this.SetValue(CanShowPropertyKey, true);
this.SetValue(CanShowPropertyKey, BooleanBoxes.TrueBox);
}

private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
Expand Down
19 changes: 14 additions & 5 deletions src/MahApps.Metro/Controls/Dialogs/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows.Controls;
using System.Windows.Threading;
using ControlzEx.Theming;
using MahApps.Metro.ValueBoxes;

namespace MahApps.Metro.Controls.Dialogs
{
Expand Down Expand Up @@ -292,13 +293,14 @@ private static Task HandleOverlayOnHide(MetroDialogSettings settings, MetroWindo
{
if (window.metroActiveDialogContainer.Children.Count == 0)
{
window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, true);
window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, BooleanBoxes.TrueBox);
window.RestoreFocus();
}
else
{
var onTopShownDialogSettings = window.metroActiveDialogContainer.Children.OfType<BaseMetroDialog>().LastOrDefault()?.DialogSettings;
window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, window.ShowDialogsOverTitleBar || onTopShownDialogSettings == null || onTopShownDialogSettings.OwnerCanCloseWithDialog);
var isCloseButtonEnabled = window.ShowDialogsOverTitleBar || onTopShownDialogSettings == null || onTopShownDialogSettings.OwnerCanCloseWithDialog;
window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, BooleanBoxes.Box(isCloseButtonEnabled));
}
});
});
Expand All @@ -308,7 +310,14 @@ private static Task HandleOverlayOnHide(MetroDialogSettings settings, MetroWindo

private static Task HandleOverlayOnShow(MetroDialogSettings settings, MetroWindow window)
{
return Task.Factory.StartNew(() => { window.Invoke(() => window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, window.ShowDialogsOverTitleBar || settings == null || settings.OwnerCanCloseWithDialog)); })
return Task.Factory.StartNew(() =>
{
window.Invoke(() =>
{
var isCloseButtonEnabled = window.ShowDialogsOverTitleBar || settings == null || settings.OwnerCanCloseWithDialog;
window.SetValue(MetroWindow.IsCloseButtonEnabledWithDialogPropertyKey, BooleanBoxes.Box(isCloseButtonEnabled));
});
})
.ContinueWith(task =>
{
return window.Invoke(() =>
Expand Down Expand Up @@ -510,7 +519,7 @@ private static void AddDialog(this MetroWindow window, BaseMetroDialog dialog)

window.metroActiveDialogContainer.Children.Add(dialog); //add the dialog to the container}

window.SetValue(MetroWindow.IsAnyDialogOpenPropertyKey, true);
window.SetValue(MetroWindow.IsAnyDialogOpenPropertyKey, BooleanBoxes.TrueBox);
}

private static void RemoveDialog(this MetroWindow window, BaseMetroDialog dialog)
Expand All @@ -532,7 +541,7 @@ private static void RemoveDialog(this MetroWindow window, BaseMetroDialog dialog
window.metroInactiveDialogContainer.Children.Remove(dialog);
}

window.SetValue(MetroWindow.IsAnyDialogOpenPropertyKey, window.metroActiveDialogContainer.Children.Count > 0);
window.SetValue(MetroWindow.IsAnyDialogOpenPropertyKey, BooleanBoxes.Box(window.metroActiveDialogContainer.Children.Count > 0));
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MahApps.Metro/Controls/Helper/TextBoxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ private static void SetTextLength<TDependencyObject>(TDependencyObject sender, F
{
var value = funcTextLength(sender);
sender.SetValue(TextLengthProperty, value);
sender.SetValue(HasTextProperty, value >= 1);
sender.SetValue(HasTextProperty, BooleanBoxes.Box(value >= 1));
}
}

Expand Down Expand Up @@ -993,7 +993,7 @@ private static void ComboBoxLoaded(object sender, RoutedEventArgs e)
{
if (sender is ComboBox comboBox)
{
comboBox.SetValue(HasTextProperty, !string.IsNullOrWhiteSpace(comboBox.Text) || comboBox.SelectedItem != null);
comboBox.SetValue(HasTextProperty, BooleanBoxes.Box(!string.IsNullOrWhiteSpace(comboBox.Text) || comboBox.SelectedItem != null));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/MahApps.Metro/Controls/MetroContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private void AfterLoadedStoryboardCurrentTimeInvalidated(object sender, System.E
{
if (clock.CurrentState == ClockState.Active)
{
this.SetValue(IsTransitioningPropertyKey, true);
this.SetValue(IsTransitioningPropertyKey, BooleanBoxes.TrueBox);
this.RaiseEvent(new RoutedEventArgs(TransitionStartedEvent));
}
}
Expand All @@ -235,7 +235,7 @@ private void AfterLoadedStoryboardCompleted(object sender, System.EventArgs e)
}

this.InvalidateVisual();
this.SetValue(IsTransitioningPropertyKey, false);
this.SetValue(IsTransitioningPropertyKey, BooleanBoxes.FalseBox);
this.RaiseEvent(new RoutedEventArgs(TransitionCompletedEvent));
}

Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Controls/MetroThumbContentControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
// now capture the mouse for the drag action
this.CaptureMouse();
// so now we are in dragging mode
this.SetValue(IsDraggingPropertyKey, true);
this.SetValue(IsDraggingPropertyKey, BooleanBoxes.TrueBox);
// get the mouse points
this.startDragPoint = e.GetPosition(this);
this.oldDragScreenPoint = this.startDragScreenPoint = this.PointToScreen(this.startDragPoint);
Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Controls/TimePicker/DateTimePicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public DayOfWeek FirstDayOfWeek
public bool IsTodayHighlighted
{
get => (bool)this.GetValue(IsTodayHighlightedProperty);
set => this.SetValue(IsTodayHighlightedProperty, value);
set => this.SetValue(IsTodayHighlightedProperty, BooleanBoxes.Box(value));
}

/// <summary>Identifies the <see cref="SelectedDateFormat"/> dependency property.</summary>
Expand Down
4 changes: 2 additions & 2 deletions src/MahApps.Metro/Controls/TimePicker/TimePickerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public IEnumerable<int> SourceSeconds
/// <summary>Identifies the <see cref="IsDropDownOpen"/> dependency property.</summary>
public static readonly DependencyProperty IsDropDownOpenProperty
= DatePicker.IsDropDownOpenProperty.AddOwner(typeof(TimePickerBase),
new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, OnCoerceIsDropDownOpen));
new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsDropDownOpenChanged, OnCoerceIsDropDownOpen));

/// <summary>
/// Gets or sets a value indicating whether the drop-down for a <see cref="TimePickerBase"/> box is currently
Expand All @@ -171,7 +171,7 @@ public static readonly DependencyProperty IsDropDownOpenProperty
public bool IsDropDownOpen
{
get => (bool)this.GetValue(IsDropDownOpenProperty);
set => this.SetValue(IsDropDownOpenProperty, value);
set => this.SetValue(IsDropDownOpenProperty, BooleanBoxes.Box(value));
}

private static object OnCoerceIsDropDownOpen(DependencyObject d, object baseValue)
Expand Down

0 comments on commit 7bf17ee

Please sign in to comment.