From 78fe25407ddbfe20a34e71dbdb2111bb028907fd Mon Sep 17 00:00:00 2001 From: Julien Lebosquain Date: Fri, 23 Aug 2024 16:47:44 +0200 Subject: [PATCH] Fixes stuck modifier keys on focus lost on macOS --- native/Avalonia.Native/src/OSX/AvnView.h | 1 + native/Avalonia.Native/src/OSX/AvnView.mm | 5 +++++ native/Avalonia.Native/src/OSX/AvnWindow.mm | 5 ++++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/native/Avalonia.Native/src/OSX/AvnView.h b/native/Avalonia.Native/src/OSX/AvnView.h index 75a5e98ed46..030330c9081 100644 --- a/native/Avalonia.Native/src/OSX/AvnView.h +++ b/native/Avalonia.Native/src/OSX/AvnView.h @@ -18,6 +18,7 @@ -(NSEvent* _Nonnull) lastMouseDownEvent; -(AvnPoint) translateLocalPoint:(AvnPoint)pt; -(void) onClosed; +-(void) setModifiers:(NSEventModifierFlags)modifierFlags; -(AvnPlatformResizeReason) getResizeReason; -(void) setResizeReason:(AvnPlatformResizeReason)reason; diff --git a/native/Avalonia.Native/src/OSX/AvnView.mm b/native/Avalonia.Native/src/OSX/AvnView.mm index 8c38b1cf2d9..664ac81e2f3 100644 --- a/native/Avalonia.Native/src/OSX/AvnView.mm +++ b/native/Avalonia.Native/src/OSX/AvnView.mm @@ -480,6 +480,11 @@ - (void) keyboardEvent: (NSEvent *) event withType: (AvnRawKeyEventType)type _parent->TopLevelEvents->RawKeyEvent(type, timestamp, modifiers, key, physicalKey, keySymbolUtf8); } +- (void)setModifiers:(NSEventModifierFlags)modifierFlags +{ + _modifierState = [self getModifiers:modifierFlags]; +} + - (void)flagsChanged:(NSEvent *)event { auto newModifierState = [self getModifiers:[event modifierFlags]]; diff --git a/native/Avalonia.Native/src/OSX/AvnWindow.mm b/native/Avalonia.Native/src/OSX/AvnWindow.mm index 9b92dba5239..2f603f0cade 100644 --- a/native/Avalonia.Native/src/OSX/AvnWindow.mm +++ b/native/Avalonia.Native/src/OSX/AvnWindow.mm @@ -298,7 +298,10 @@ - (void)windowDidBecomeKey:(NSNotification *_Nonnull)notification { if (_parent == nullptr) return; - + + if (_parent->View != nullptr) + [_parent->View setModifiers:NSEvent.modifierFlags]; + _parent->BringToFront(); dispatch_async(dispatch_get_main_queue(), ^{