Compare commits

...

1 Commits

Author SHA1 Message Date
Lea Anthony 0394bcaea7 mac keypress WIP 2023-09-20 20:35:35 +10:00
6 changed files with 89 additions and 1 deletions
+26
View File
@@ -187,6 +187,13 @@ type webViewAssetRequest struct {
windowName string
}
var windowKeyEvents = make(chan *windowKeyEvent)
type windowKeyEvent struct {
windowId uint
acceleratorString string
}
func (r *webViewAssetRequest) Header() (http.Header, error) {
h, err := r.Request.Header()
if err != nil {
@@ -430,6 +437,12 @@ func (a *App) Run() error {
a.handleWindowMessage(event)
}
}()
go func() {
for {
event := <-windowKeyEvents
a.handleWindowKeyEvent(event)
}
}()
go func() {
for {
dragAndDropMessage := <-windowDragAndDropBuffer
@@ -742,6 +755,19 @@ func (a *App) processKeyBinding(acceleratorString string, window *WebviewWindow)
return true
}
func (a *App) handleWindowKeyEvent(event *windowKeyEvent) {
// Get window from window map
a.windowsLock.Lock()
window, ok := a.windows[event.windowId]
a.windowsLock.Unlock()
if !ok {
log.Printf("WebviewWindow #%d not found", event.windowId)
return
}
// Get callback from window
window.handleKeyEvent(event.acceleratorString)
}
func invokeSync(fn func()) {
var wg sync.WaitGroup
wg.Add(1)
+8
View File
@@ -272,6 +272,14 @@ func processURLRequest(windowID C.uint, wkUrlSchemeTask unsafe.Pointer) {
}
}
//export processWindowKeyDownEvent
func processWindowKeyDownEvent(windowID C.uint, acceleratorString *C.char) {
windowKeyEvents <- &windowKeyEvent{
windowId: uint(windowID),
acceleratorString: C.GoString(acceleratorString),
}
}
//export processDragItems
func processDragItems(windowID C.uint, arr **C.char, length C.int) {
var filenames []string
+10
View File
@@ -69,6 +69,7 @@ type (
absolutePosition() (int, int)
setAbsolutePosition(x int, y int)
flash(enabled bool)
handleKeyEvent(acceleratorString string)
}
)
@@ -1065,3 +1066,12 @@ func (w *WebviewWindow) processKeyBinding(acceleratorString string) bool {
return true
}
func (w *WebviewWindow) handleKeyEvent(acceleratorString string) {
if w.impl == nil {
return
}
invokeSync(func() {
w.impl.handleKeyEvent(acceleratorString)
})
}
@@ -720,6 +720,16 @@ type macosWebviewWindow struct {
parent *WebviewWindow
}
func (w *macosWebviewWindow) handleKeyEvent(acceleratorString string) {
// Parse acceleratorString
accelerator, err := parseAccelerator(acceleratorString)
if err != nil {
globalApplication.error("unable to parse accelerator: %s", err.Error())
return
}
w.parent.processKeyBinding(accelerator.String())
}
func (w *macosWebviewWindow) isFocused() bool {
return bool(C.windowIsFocused(w.nsWindow))
}
@@ -5,6 +5,7 @@
#import "../events/events.h"
extern void processMessage(unsigned int, const char*);
extern void processURLRequest(unsigned int, void *);
extern void processWindowKeyDownEvent(unsigned int, const char*);
extern bool hasListeners(unsigned int);
@implementation WebviewWindow
- (WebviewWindow*) initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)windowStyle backing:(NSBackingStoreType)bufferingType defer:(BOOL)deferCreation;
@@ -16,8 +17,41 @@ extern bool hasListeners(unsigned int);
[self setMovableByWindowBackground:YES];
return self;
}
- (void)keyDown:(NSEvent *)event {
NSUInteger modifierFlags = event.modifierFlags;
// Create an array to hold the modifier strings
NSMutableArray *modifierStrings = [NSMutableArray array];
// Check for modifier flags and add corresponding strings to the array
if (modifierFlags & NSEventModifierFlagShift) {
[modifierStrings addObject:@"Shift"];
}
if (modifierFlags & NSEventModifierFlagControl) {
[modifierStrings addObject:@"Ctrl"];
}
if (modifierFlags & NSEventModifierFlagOption) {
[modifierStrings addObject:@"Option"];
}
if (modifierFlags & NSEventModifierFlagCommand) {
[modifierStrings addObject:@"Cmd"];
}
NSString *keyString = [NSEvent stringWithKeyCode:event.keyCode];
[modifierStrings addObject:keyString];
// Combine the modifier strings with the key character
NSString *keyEventString = [modifierStrings componentsJoinedByString:@"+"];
const char* utf8String = [keyEventString UTF8String];
WebviewWindowDelegate *delegate = (WebviewWindowDelegate*)self.delegate;
processWindowKeyDownEvent(delegate.windowId, utf8String);
NSLog(@"Key pressed: %@", keyEventString);
}
- (BOOL)canBecomeKeyWindow {
return YES;
}
+1 -1
View File
@@ -1550,7 +1550,7 @@ func (w *windowsWebviewWindow) processKeyBinding(vkey uint) bool {
if len(w.parent.keyBindings) == 0 {
return false
}
// Get the keyboard state and convert to an accellerator
// Get the keyboard state and convert to an accelerator
var keyState [256]byte
if !w32.GetKeyboardState(keyState[:]) {
globalApplication.error("Error getting keyboard state")