Skip to content

Commit

Permalink
Fix QL-Win#644 step 1: correctly bring window back to top when clicked
Browse files Browse the repository at this point in the history
  • Loading branch information
xupefei committed May 9, 2020
1 parent 472ccc1 commit af608dc
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions QuickLook/ViewerWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media.Animation;
using QuickLook.Common.ExtensionMethods;
using QuickLook.Common.Helpers;
Expand Down Expand Up @@ -55,16 +56,15 @@ internal ViewerWindow()

StateChanged += (sender, e) => _ignoreNextWindowSizeChange = true;

// bring the window to top when users click in the client area.
// the non-client area is handled by the WndProc inside OnSourceInitialized().
PreviewMouseDown += (sender, e) => this.BringToFront(false);

windowFrameContainer.PreviewMouseMove += ShowWindowCaptionContainer;

Topmost = SettingHelper.Get("Topmost", false);
buttonTop.Tag = Topmost ? "Top" : "Auto";

SourceInitialized += (sender, e) => this.SetNoactivate();

// bring the window to top. use together with SetNoactivate()
PreviewMouseDown += (sender, e) => this.BringToFront(false);

buttonTop.Click += (sender, e) =>
{
Topmost = !Topmost;
Expand Down Expand Up @@ -103,6 +103,27 @@ internal ViewerWindow()
buttonOpenWith.Click += (sender, e) => ShareHelper.Share(_path, this, true);
}

// bring the window to top when users click in the non-client area.
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);

this.SetNoactivate();

HwndSource.FromHwnd(new WindowInteropHelper(this).Handle)?.AddHook(
(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) =>
{
switch (msg)
{
case 0x0112: // WM_SYSCOMMAND
this.BringToFront(false);
break;
}

return IntPtr.Zero;
});
}

public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Expand Down

0 comments on commit af608dc

Please sign in to comment.