Skip to content

Commit

Permalink
Merge pull request #3873 from MahApps/fix/GH-3872-ThemeManager_Threads
Browse files Browse the repository at this point in the history
Fix ThemeManager changes not working with different (STA) threads
  • Loading branch information
punker76 authored Jul 7, 2020
2 parents d57ce8a + f2c9f31 commit eeb66c3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 51 deletions.
2 changes: 1 addition & 1 deletion build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#tool "dotnet:?package=NuGetKeyVaultSignTool&version=1.2.18"
#tool "dotnet:?package=AzureSignTool&version=2.0.17"

#tool GitVersion.CommandLine&version=5.0.1
#tool GitVersion.CommandLine&version=5.3.7
#tool gitreleasemanager
#tool xunit.runner.console
#tool vswhere
Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Behaviors/TiltBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ protected override void OnAttached()

private void ThemeManagerIsThemeChanged(object sender, ThemeChangedEventArgs e)
{
this.RotatorParent?.Refresh();
this.Invoke(() => { this.RotatorParent?.Refresh(); });
}

protected override void OnDetaching()
Expand Down
2 changes: 1 addition & 1 deletion src/MahApps.Metro/Controls/Dialogs/BaseMetroDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private void BaseMetroDialogUnloaded(object sender, RoutedEventArgs e)

private void ThemeManagerIsThemeChanged(object sender, ThemeChangedEventArgs e)
{
this.HandleThemeChange();
this.Invoke(this.HandleThemeChange);
}

private static object TryGetResource(ControlzEx.Theming.Theme theme, string key)
Expand Down
52 changes: 26 additions & 26 deletions src/MahApps.Metro/Controls/Flyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ public event RoutedEventHandler ClosingFinished
public static readonly DependencyProperty IsPinnedProperty = DependencyProperty.Register(nameof(IsPinned), typeof(bool), typeof(Flyout), new PropertyMetadata(BooleanBoxes.TrueBox));
public static readonly DependencyProperty IsOpenProperty = DependencyProperty.Register(nameof(IsOpen), typeof(bool), typeof(Flyout), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, IsOpenedChanged));
public static readonly DependencyProperty AnimateOnPositionChangeProperty = DependencyProperty.Register(nameof(AnimateOnPositionChange), typeof(bool), typeof(Flyout), new PropertyMetadata(BooleanBoxes.TrueBox));
public static readonly DependencyProperty AnimateOpacityProperty = DependencyProperty.Register(nameof(AnimateOpacity), typeof(bool), typeof(Flyout), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, AnimateOpacityChanged));
public static readonly DependencyProperty AnimateOpacityProperty = DependencyProperty.Register(nameof(AnimateOpacity), typeof(bool), typeof(Flyout), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox, OnAnimateOpacityPropertyChanged));
public static readonly DependencyProperty IsModalProperty = DependencyProperty.Register(nameof(IsModal), typeof(bool), typeof(Flyout), new PropertyMetadata(BooleanBoxes.FalseBox));

public static readonly DependencyProperty CloseCommandProperty = DependencyProperty.RegisterAttached(nameof(CloseCommand), typeof(ICommand), typeof(Flyout), new UIPropertyMetadata(null));
public static readonly DependencyProperty CloseCommandParameterProperty = DependencyProperty.Register(nameof(CloseCommandParameter), typeof(object), typeof(Flyout), new PropertyMetadata(null));

public static readonly DependencyProperty ThemeProperty = DependencyProperty.Register(nameof(Theme), typeof(FlyoutTheme), typeof(Flyout), new FrameworkPropertyMetadata(FlyoutTheme.Dark, ThemeChanged));
public static readonly DependencyProperty ThemeProperty = DependencyProperty.Register(nameof(Theme), typeof(FlyoutTheme), typeof(Flyout), new FrameworkPropertyMetadata(FlyoutTheme.Dark, OnThemePropertyChanged));
public static readonly DependencyProperty ExternalCloseButtonProperty = DependencyProperty.Register(nameof(ExternalCloseButton), typeof(MouseButton), typeof(Flyout), new PropertyMetadata(MouseButton.Left));
public static readonly DependencyProperty CloseButtonVisibilityProperty = DependencyProperty.Register(nameof(CloseButtonVisibility), typeof(Visibility), typeof(Flyout), new FrameworkPropertyMetadata(Visibility.Visible));
public static readonly DependencyProperty CloseButtonIsCancelProperty = DependencyProperty.Register(nameof(CloseButtonIsCancel), typeof(bool), typeof(Flyout), new PropertyMetadata(BooleanBoxes.FalseBox));
Expand Down Expand Up @@ -518,22 +518,23 @@ private static void IsAutoCloseEnabledChanged(DependencyObject dependencyObject,
{
var flyout = (Flyout)dependencyObject;

Action autoCloseEnabledChangedAction = () => {
if (e.NewValue != e.OldValue)
Action autoCloseEnabledChangedAction = () =>
{
if ((bool)e.NewValue)
if (e.NewValue != e.OldValue)
{
if (flyout.IsOpen)
if ((bool)e.NewValue)
{
flyout.StartAutoCloseTimer();
if (flyout.IsOpen)
{
flyout.StartAutoCloseTimer();
}
}
else
{
flyout.StopAutoCloseTimer();
}
}
else
{
flyout.StopAutoCloseTimer();
}
}
};
};

flyout.Dispatcher.BeginInvoke(DispatcherPriority.Background, autoCloseEnabledChangedAction);
}
Expand All @@ -542,16 +543,17 @@ private static void AutoCloseIntervalChanged(DependencyObject dependencyObject,
{
var flyout = (Flyout)dependencyObject;

Action autoCloseIntervalChangedAction = () => {
if (e.NewValue != e.OldValue)
Action autoCloseIntervalChangedAction = () =>
{
flyout.InitializeAutoCloseTimer();
if (flyout.IsAutoCloseEnabled && flyout.IsOpen)
if (e.NewValue != e.OldValue)
{
flyout.StartAutoCloseTimer();
flyout.InitializeAutoCloseTimer();
if (flyout.IsAutoCloseEnabled && flyout.IsOpen)
{
flyout.StartAutoCloseTimer();
}
}
}
};
};

flyout.Dispatcher.BeginInvoke(DispatcherPriority.Background, autoCloseIntervalChangedAction);
}
Expand Down Expand Up @@ -627,16 +629,14 @@ private void TryFocusElement()
}
}

private static void ThemeChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
private static void OnThemePropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var flyout = (Flyout)dependencyObject;
flyout.UpdateFlyoutTheme();
(dependencyObject as Flyout)?.UpdateFlyoutTheme();
}

private static void AnimateOpacityChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
private static void OnAnimateOpacityPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
var flyout = (Flyout)dependencyObject;
flyout.UpdateOpacityChange();
(dependencyObject as Flyout)?.UpdateOpacityChange();
}

private static void PositionChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
Expand Down
44 changes: 22 additions & 22 deletions src/MahApps.Metro/Controls/MetroWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,31 +1064,31 @@ private void MetroWindow_SizeChanged(object sender, RoutedEventArgs e)

private void ThemeManagerOnIsThemeChanged(object sender, ThemeChangedEventArgs e)
{
if (e.NewTheme != null)
{
var flyouts = this.Flyouts.GetFlyouts().ToList();
// since we disabled the ThemeManager OnThemeChanged part, we must change all children flyouts too
// e.g if the FlyoutsControl is hosted in a UserControl
var allChildFlyouts = (this.Content as DependencyObject).FindChildren<FlyoutsControl>(true).ToList();
if (allChildFlyouts.Any())
this.Invoke(() =>
{
flyouts.AddRange(allChildFlyouts.SelectMany(flyoutsControl => flyoutsControl.GetFlyouts()));
}
var flyouts = this.Flyouts.GetFlyouts().ToList();
// since we disabled the ThemeManager OnThemeChanged part, we must change all children flyouts too
// e.g if the FlyoutsControl is hosted in a UserControl
var allChildFlyouts = (this.Content as DependencyObject).FindChildren<FlyoutsControl>(true).ToList();
if (allChildFlyouts.Any())
{
flyouts.AddRange(allChildFlyouts.SelectMany(flyoutsControl => flyoutsControl.GetFlyouts()));
}
if (!flyouts.Any())
{
// we must update the window command brushes!!!
this.ResetAllWindowCommandsBrush();
return;
}
if (!flyouts.Any())
{
// we must update the window command brushes!!!
this.ResetAllWindowCommandsBrush();
return;
}
foreach (var flyout in flyouts)
{
flyout.ChangeFlyoutTheme(e.NewTheme);
}
this.HandleWindowCommandsForFlyouts(flyouts);
}
foreach (var flyout in flyouts)
{
flyout.ChangeFlyoutTheme(e.NewTheme);
}
this.HandleWindowCommandsForFlyouts(flyouts);
});
}

private void FlyoutsPreviewMouseDown(object sender, MouseButtonEventArgs e)
Expand Down

0 comments on commit eeb66c3

Please sign in to comment.