Skip to content

Commit

Permalink
fix Topmost bug: Window going behind other windows on program start #…
Browse files Browse the repository at this point in the history
  • Loading branch information
punker76 committed Mar 1, 2015
1 parent 8c753f6 commit 6b0a8fa
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
39 changes: 29 additions & 10 deletions MahApps.Metro/Behaviours/BorderlessWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public class BorderlessWindowBehavior : Behavior<Window>
private WindowChrome windowChrome;
private PropertyChangeNotifier borderThicknessChangeNotifier;
private Thickness? savedBorderThickness;
private PropertyChangeNotifier topMostChangeNotifier;
private bool savedTopMost;

protected override void OnAttached()
{
Expand Down Expand Up @@ -63,6 +65,10 @@ protected override void OnAttached()
borderThicknessChangeNotifier = new PropertyChangeNotifier(this.AssociatedObject, Window.BorderThicknessProperty);
borderThicknessChangeNotifier.ValueChanged += BorderThicknessChangeNotifierOnValueChanged;

savedTopMost = AssociatedObject.Topmost;
topMostChangeNotifier = new PropertyChangeNotifier(this.AssociatedObject, Window.TopmostProperty);
topMostChangeNotifier.ValueChanged += TopMostChangeNotifierOnValueChanged;

AssociatedObject.Loaded += AssociatedObject_Loaded;
AssociatedObject.Unloaded += AssociatedObject_Unloaded;
AssociatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
Expand All @@ -79,6 +85,11 @@ private void BorderThicknessChangeNotifierOnValueChanged(object sender, EventArg
savedBorderThickness = AssociatedObject.BorderThickness;
}

private void TopMostChangeNotifierOnValueChanged(object sender, EventArgs e)
{
savedTopMost = AssociatedObject.Topmost;
}

private void UseNoneWindowStylePropertyChangedCallback(object sender, EventArgs e)
{
var metroWindow = sender as MetroWindow;
Expand Down Expand Up @@ -201,6 +212,7 @@ private void OnAssociatedObjectHandleMaximize(object sender, EventArgs e)
private void HandleMaximize()
{
borderThicknessChangeNotifier.ValueChanged -= BorderThicknessChangeNotifierOnValueChanged;
topMostChangeNotifier.ValueChanged -= TopMostChangeNotifierOnValueChanged;

var metroWindow = AssociatedObject as MetroWindow;
var enableDWMDropShadow = EnableDWMDropShadow;
Expand Down Expand Up @@ -244,19 +256,26 @@ private void HandleMaximize()
{
AssociatedObject.BorderThickness = savedBorderThickness.GetValueOrDefault(new Thickness(0));
}

// fix nasty TopMost bug
// - set TopMost="True"
// - start mahapps demo
// - TopMost works
// - maximize window and back to normal
// - TopMost is gone
var topMost = AssociatedObject.Topmost;
AssociatedObject.Topmost = false;
AssociatedObject.Topmost = topMost;
}

// fix nasty TopMost bug
// - set TopMost="True"
// - start mahapps demo
// - TopMost works
// - maximize window and back to normal
// - TopMost is gone
//
// Problem with minimize animation when window is maximized #1528
// 1. Activate another application (such as Google Chrome).
// 2. Run the demo and maximize it.
// 3. Minimize the demo by clicking on the taskbar button.
// Note that the minimize animation in this case does actually run, but somehow the other
// application (Google Chrome in this example) is instantly switched to being the top window,
// and so blocking the animation view.
AssociatedObject.Topmost = AssociatedObject.WindowState == WindowState.Minimized || savedTopMost;

borderThicknessChangeNotifier.ValueChanged += BorderThicknessChangeNotifierOnValueChanged;
topMostChangeNotifier.ValueChanged += TopMostChangeNotifierOnValueChanged;
}

private void AssociatedObject_SourceInitialized(object sender, EventArgs e)
Expand Down
10 changes: 0 additions & 10 deletions MahApps.Metro/Behaviours/GlowWindowBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public class GlowWindowBehavior : Behavior<Window>
private static readonly TimeSpan GlowTimerDelay = TimeSpan.FromMilliseconds(200); //200 ms delay, the same as VS2013
private GlowWindow left, right, top, bottom;
private DispatcherTimer makeGlowVisibleTimer;
private bool prevTopmost;

protected override void OnAttached()
{
Expand All @@ -23,15 +22,6 @@ protected override void OnAttached()

void AssociatedObjectStateChanged(object sender, EventArgs e)
{
if (AssociatedObject.WindowState == WindowState.Minimized)
{
prevTopmost = AssociatedObject.Topmost;
AssociatedObject.Topmost = true;
}
else
{
AssociatedObject.Topmost = prevTopmost;
}
if (makeGlowVisibleTimer != null)
{
makeGlowVisibleTimer.Stop();
Expand Down
2 changes: 2 additions & 0 deletions samples/MetroDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
Header="Show CustomDialog Externally" />
</MenuItem>
<MenuItem Header="Window">
<MenuItem IsCheckable="True" Header="Topmost"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}, Path=Topmost}" />
<MenuItem IsCheckable="True" Header="Ignore taskbar on maximize"
IsChecked="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}, Path=IgnoreTaskbarOnMaximize}" />
<MenuItem IsCheckable="True" Header="Toggle FullScreen (no taskbar, window style = none)"
Expand Down

0 comments on commit 6b0a8fa

Please sign in to comment.