app: [macOS] remove support for key bindings

The fix for #616 went to far by attempting to support macOS key bindings
through doCommandBySelector. Issue #625 is a consequence, but more
fundamentally, key bindings does not work with support for key.Release
events.

Remove key binding translation and fix #626, and add a check whether an
IME swallowed a key event, keeping #616 fixed.

While here, replace the KEY_ constants with ASCII codes.

Fixes: https://todo.sr.ht/~eliasnaur/gio/625
References: https://todo.sr.ht/~eliasnaur/gio/616
Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
Elias Naur
2025-01-13 20:41:53 +01:00
parent d2db4f6875
commit 8107ec2206
2 changed files with 53 additions and 102 deletions
+4 -53
View File
@@ -15,7 +15,6 @@ __attribute__ ((visibility ("hidden"))) CALayer *gio_layerFactory(BOOL presentWi
@end
@interface GioView : NSView <CALayerDelegate,NSTextInputClient>
@property NSEvent *lastKeyDown;
@property uintptr_t handle;
@property BOOL presentWithTrans;
@end
@@ -133,12 +132,8 @@ static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFl
handleMouse(self, event, MOUSE_SCROLL, dx, dy);
}
- (void)keyDown:(NSEvent *)event {
// Stash the event for use by doCommandBySelector.
self.lastKeyDown = event;
[self interpretKeyEvents:[NSArray arrayWithObject:event]];
self.lastKeyDown = nil;
NSString *keys = [event charactersIgnoringModifiers];
gio_onKeys(self.handle, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], true);
gio_onKeys(self.handle, (__bridge CFTypeRef)event, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], true);
}
- (void)flagsChanged:(NSEvent *)event {
[self interpretKeyEvents:[NSArray arrayWithObject:event]];
@@ -146,54 +141,14 @@ static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFl
}
- (void)keyUp:(NSEvent *)event {
NSString *keys = [event charactersIgnoringModifiers];
gio_onKeys(self.handle, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], false);
gio_onKeys(self.handle, (__bridge CFTypeRef)event, (__bridge CFTypeRef)keys, [event timestamp], [event modifierFlags], false);
}
- (void)insertText:(id)string {
gio_onText(self.handle, (__bridge CFTypeRef)string);
}
- (void)doCommandBySelector:(SEL)action {
NSEvent *event = self.lastKeyDown;
if (event == nil) {
return;
}
int key = 0;
if (action == @selector(deleteBackward:)) {
key = KEY_DELETE_BACKWARD;
} else if (action == @selector(deleteForward:)) {
key = NSDeleteFunctionKey;
} else if (action == @selector(insertNewline:)) {
NSString *keys = [event charactersIgnoringModifiers];
if ([keys isEqualToString:@"\r"]) {
key = KEY_RETURN;
} else {
key = KEY_ENTER;
}
} else if (action == @selector(moveUp:)) {
key = NSUpArrowFunctionKey;
} else if (action == @selector(moveDown:)) {
key = NSDownArrowFunctionKey;
} else if (action == @selector(moveLeft:)) {
key = NSLeftArrowFunctionKey;
} else if (action == @selector(moveRight:)) {
key = NSRightArrowFunctionKey;
} else if (action == @selector(cancelOperation:)) {
key = KEY_ESCAPE;
} else if (action == @selector(insertTab:)) {
key = KEY_TAB;
} else if (action == @selector(scrollToBeginningOfDocument:)) {
key = NSHomeFunctionKey;
} else if (action == @selector(scrollToEndOfDocument:)) {
key = NSEndFunctionKey;
} else if (action == @selector(scrollPageUp:)) {
key = NSPageUpFunctionKey;
} else if (action == @selector(scrollPageDown:)) {
key = NSPageDownFunctionKey;
} else {
return;
}
gio_onCommandBySelector(self.handle, key, [event timestamp], [event modifierFlags]);
gio_onCommandBySelector(self.handle);
}
- (BOOL)hasMarkedText {
int res = gio_hasMarkedText(self.handle);
return res ? YES : NO;
@@ -229,10 +184,6 @@ static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFl
}
- (void)insertText:(id)string
replacementRange:(NSRange)replaceRange {
NSEvent *event = self.lastKeyDown;
if (event == nil) {
return;
}
NSString *str;
// string is either an NSAttributedString or an NSString.
if ([string isKindOfClass:[NSAttributedString class]]) {
@@ -240,7 +191,7 @@ static void handleMouse(GioView *view, NSEvent *event, int typ, CGFloat dx, CGFl
} else {
str = string;
}
gio_insertText(self.handle, (__bridge CFTypeRef)str, replaceRange, [event timestamp], [event modifierFlags]);
gio_insertText(self.handle, (__bridge CFTypeRef)str, replaceRange);
}
- (NSUInteger)characterIndexForPoint:(NSPoint)p {
return gio_characterIndexForPoint(self.handle, p);