@@ -110,8 +110,7 @@ public KeyTipService(Ribbon ribbon)
110
110
/// </summary>
111
111
public void Attach ( )
112
112
{
113
- if ( this . attached
114
- || this . ribbon . IsKeyTipHandlingEnabled == false )
113
+ if ( this . attached )
115
114
{
116
115
return ;
117
116
}
@@ -134,7 +133,7 @@ public void Attach()
134
133
this . window . KeyUp += this . OnWindowKeyUp ;
135
134
136
135
// Hookup non client area messages
137
- this . attachedHwndSource = ( HwndSource ) PresentationSource . FromVisual ( this . window ) ;
136
+ this . attachedHwndSource = ( HwndSource ? ) PresentationSource . FromVisual ( this . window ) ;
138
137
this . attachedHwndSource ? . AddHook ( this . WindowProc ) ;
139
138
}
140
139
@@ -172,9 +171,9 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
172
171
173
172
// We must terminate the keytip's adorner chain if:
174
173
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
178
177
{
179
178
if ( this . activeAdornerChain ? . IsAdornerChainAlive == true )
180
179
{
@@ -184,19 +183,35 @@ private IntPtr WindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, re
184
183
185
184
// Fix for #632.
186
185
// 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 ) )
189
187
{
190
188
PopupService . RaiseDismissPopupEvent ( this . ribbon , DismissPopupMode . Always , DismissPopupReason . ApplicationLostFocus ) ;
191
189
PopupService . RaiseDismissPopupEvent ( Mouse . Captured , DismissPopupMode . Always , DismissPopupReason . ApplicationLostFocus ) ;
192
190
PopupService . RaiseDismissPopupEvent ( Keyboard . FocusedElement , DismissPopupMode . Always , DismissPopupReason . ApplicationLostFocus ) ;
193
191
}
194
192
195
193
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
+ }
196
206
}
197
207
198
208
private void OnWindowPreviewKeyDown ( object ? sender , KeyEventArgs e )
199
209
{
210
+ if ( this . ribbon . IsKeyTipHandlingEnabled == false )
211
+ {
212
+ return ;
213
+ }
214
+
200
215
if ( this . windowPreviewKeyDownScopeGuard ? . IsActive == true )
201
216
{
202
217
System . Media . SystemSounds . Beep . Play ( ) ;
@@ -335,6 +350,11 @@ private void OnWindowPreviewKeyDown(object? sender, KeyEventArgs e)
335
350
336
351
private void OnWindowKeyUp ( object sender , KeyEventArgs e )
337
352
{
353
+ if ( this . ribbon . IsKeyTipHandlingEnabled == false )
354
+ {
355
+ return ;
356
+ }
357
+
338
358
if ( this . ribbon . IsCollapsed
339
359
|| this . ribbon . IsEnabled == false
340
360
|| this . window is null
0 commit comments