Skip to content

Commit

Permalink
Fixes #1129
Browse files Browse the repository at this point in the history
  • Loading branch information
batzen committed May 6, 2023
1 parent 5be102d commit eaef784
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
- [#1116](../../issues/1116) - Accessibility: Ribbon Display Options are read as "DropDown Button"
- [#1117](../../issues/1117) - Accessibility: BackButton has low contrast
- [#1125](../../issues/1125) - BackStage Back Button doesn't have an accessibility text.
- [#1129](../../issues/1129) - Popup has been detached by the parent control

- ### Enhancements/Features

Expand Down
7 changes: 1 addition & 6 deletions Fluent.Ribbon/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,4 @@ SendMessage
SetFocus
ToUnicode

WM_NCACTIVATE
WM_ACTIVATE
WM_NCLBUTTONDOWN
WM_NCXBUTTONDBLCLK
WM_LBUTTONDOWN
WM_MBUTTONDBLCLK
WM_*
36 changes: 28 additions & 8 deletions Fluent.Ribbon/Services/KeyTipService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ public KeyTipService(Ribbon ribbon)
/// </summary>
public void Attach()
{
if (this.attached
|| this.ribbon.IsKeyTipHandlingEnabled == false)
if (this.attached)
{
return;
}
Expand All @@ -134,7 +133,7 @@ public void Attach()
this.window.KeyUp += this.OnWindowKeyUp;

// Hookup non client area messages
this.attachedHwndSource = (HwndSource)PresentationSource.FromVisual(this.window);
this.attachedHwndSource = (HwndSource?)PresentationSource.FromVisual(this.window);
this.attachedHwndSource?.AddHook(this.WindowProc);
}

Expand Down Expand Up @@ -172,9 +171,9 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re

// We must terminate the keytip's adorner chain if:
if (message == PInvoke.WM_NCACTIVATE // mouse clicks in non client area
|| (message == PInvoke.WM_ACTIVATE && wParam == IntPtr.Zero) // the window is deactivated
|| (message >= PInvoke.WM_NCLBUTTONDOWN && message <= PInvoke.WM_NCXBUTTONDBLCLK) // mouse click (non client area)
|| (message >= PInvoke.WM_LBUTTONDOWN && message <= PInvoke.WM_MBUTTONDBLCLK)) // mouse click
|| (message is PInvoke.WM_ACTIVATE && wParam == IntPtr.Zero) // the window is deactivated
|| message is >= PInvoke.WM_NCLBUTTONDOWN and <= PInvoke.WM_NCXBUTTONDBLCLK // mouse click (non client area)
|| message is >= PInvoke.WM_LBUTTONDOWN and <= PInvoke.WM_MBUTTONDBLCLK) // mouse click
{
if (this.activeAdornerChain?.IsAdornerChainAlive == true)
{
Expand All @@ -184,19 +183,35 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re

// Fix for #632.
// Yes this looks awkward, calling the PopupService here, but the alternative would be to let the PopupService know about windows.
if (message == PInvoke.WM_ACTIVATE
&& wParam == IntPtr.Zero) // the window is deactivated
if (ShouldDismissAllPopups(message, wParam))
{
PopupService.RaiseDismissPopupEvent(this.ribbon, DismissPopupMode.Always, DismissPopupReason.ApplicationLostFocus);
PopupService.RaiseDismissPopupEvent(Mouse.Captured, DismissPopupMode.Always, DismissPopupReason.ApplicationLostFocus);
PopupService.RaiseDismissPopupEvent(Keyboard.FocusedElement, DismissPopupMode.Always, DismissPopupReason.ApplicationLostFocus);
}

return IntPtr.Zero;

static bool ShouldDismissAllPopups(uint message, IntPtr wParam)
{
return message switch
{
PInvoke.WM_ACTIVATE when wParam == IntPtr.Zero => true, // the window is deactivated
PInvoke.WM_SIZE => true, // the window state changed (minimize etc.)
PInvoke.WM_DESTROY => true, // the window is closed
PInvoke.WM_QUIT => true, // the application is exiting
_ => false
};
}
}

private void OnWindowPreviewKeyDown(object? sender, KeyEventArgs e)
{
if (this.ribbon.IsKeyTipHandlingEnabled == false)
{
return;
}

if (this.windowPreviewKeyDownScopeGuard?.IsActive == true)
{
System.Media.SystemSounds.Beep.Play();
Expand Down Expand Up @@ -335,6 +350,11 @@ private void OnWindowPreviewKeyDown(object? sender, KeyEventArgs e)

private void OnWindowKeyUp(object sender, KeyEventArgs e)
{
if (this.ribbon.IsKeyTipHandlingEnabled == false)
{
return;
}

if (this.ribbon.IsCollapsed
|| this.ribbon.IsEnabled == false
|| this.window is null
Expand Down

0 comments on commit eaef784

Please sign in to comment.