Skip to content

Commit eaef784

Browse files
committed
Fixes #1129
1 parent 5be102d commit eaef784

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
- [#1116](../../issues/1116) - Accessibility: Ribbon Display Options are read as "DropDown Button"
4242
- [#1117](../../issues/1117) - Accessibility: BackButton has low contrast
4343
- [#1125](../../issues/1125) - BackStage Back Button doesn't have an accessibility text.
44+
- [#1129](../../issues/1129) - Popup has been detached by the parent control
4445

4546
- ### Enhancements/Features
4647

Fluent.Ribbon/NativeMethods.txt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,4 @@ SendMessage
1212
SetFocus
1313
ToUnicode
1414

15-
WM_NCACTIVATE
16-
WM_ACTIVATE
17-
WM_NCLBUTTONDOWN
18-
WM_NCXBUTTONDBLCLK
19-
WM_LBUTTONDOWN
20-
WM_MBUTTONDBLCLK
15+
WM_*

Fluent.Ribbon/Services/KeyTipService.cs

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ public KeyTipService(Ribbon ribbon)
110110
/// </summary>
111111
public void Attach()
112112
{
113-
if (this.attached
114-
|| this.ribbon.IsKeyTipHandlingEnabled == false)
113+
if (this.attached)
115114
{
116115
return;
117116
}
@@ -134,7 +133,7 @@ public void Attach()
134133
this.window.KeyUp += this.OnWindowKeyUp;
135134

136135
// Hookup non client area messages
137-
this.attachedHwndSource = (HwndSource)PresentationSource.FromVisual(this.window);
136+
this.attachedHwndSource = (HwndSource?)PresentationSource.FromVisual(this.window);
138137
this.attachedHwndSource?.AddHook(this.WindowProc);
139138
}
140139

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

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

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

195193
return IntPtr.Zero;
194+
195+
static bool ShouldDismissAllPopups(uint message, IntPtr wParam)
196+
{
197+
return message switch
198+
{
199+
PInvoke.WM_ACTIVATE when wParam == IntPtr.Zero => true, // the window is deactivated
200+
PInvoke.WM_SIZE => true, // the window state changed (minimize etc.)
201+
PInvoke.WM_DESTROY => true, // the window is closed
202+
PInvoke.WM_QUIT => true, // the application is exiting
203+
_ => false
204+
};
205+
}
196206
}
197207

198208
private void OnWindowPreviewKeyDown(object? sender, KeyEventArgs e)
199209
{
210+
if (this.ribbon.IsKeyTipHandlingEnabled == false)
211+
{
212+
return;
213+
}
214+
200215
if (this.windowPreviewKeyDownScopeGuard?.IsActive == true)
201216
{
202217
System.Media.SystemSounds.Beep.Play();
@@ -335,6 +350,11 @@ private void OnWindowPreviewKeyDown(object? sender, KeyEventArgs e)
335350

336351
private void OnWindowKeyUp(object sender, KeyEventArgs e)
337352
{
353+
if (this.ribbon.IsKeyTipHandlingEnabled == false)
354+
{
355+
return;
356+
}
357+
338358
if (this.ribbon.IsCollapsed
339359
|| this.ribbon.IsEnabled == false
340360
|| this.window is null

0 commit comments

Comments
 (0)