From 8cc07f5fcc112b47471741976c944ae7ad82e413 Mon Sep 17 00:00:00 2001 From: Alex Carver Date: Thu, 15 Aug 2024 23:10:26 +0100 Subject: [PATCH] Mac: Added missing close, focus and resize events Added an NSWindowDelegate to the NSWindow to get more information. Emits ApplicationDefined NSEvents back to the NSApplication to be picked up in the existing EventQueue. --- src/CrossWindow/Cocoa/CocoaEventQueue.mm | 18 ++++++- src/CrossWindow/Cocoa/CocoaWindow.h | 3 ++ src/CrossWindow/Cocoa/CocoaWindow.mm | 61 ++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 1 deletion(-) diff --git a/src/CrossWindow/Cocoa/CocoaEventQueue.mm b/src/CrossWindow/Cocoa/CocoaEventQueue.mm index 3ce2faa..4a900b7 100644 --- a/src/CrossWindow/Cocoa/CocoaEventQueue.mm +++ b/src/CrossWindow/Cocoa/CocoaEventQueue.mm @@ -107,7 +107,23 @@ break; case NSEventTypeScrollWheel: [nsEvent deltaY]; - + break; + + case NSEventTypeApplicationDefined: + switch((EventType) nsEvent.subtype) { + case EventType::Close: { + curEvent = xwin::Event(EventType::Close); + } break; + case EventType::Resize: { + curEvent = xwin::Event(xwin::ResizeData(nsEvent.data1, nsEvent.data2, true)); + } break; + case EventType::Focus: { + curEvent = xwin::Event(xwin::FocusData(nsEvent.data1 ? true : false)); + } break; + default: { + + } break; + } break; default: break; diff --git a/src/CrossWindow/Cocoa/CocoaWindow.h b/src/CrossWindow/Cocoa/CocoaWindow.h index c9e63dc..203c1a8 100644 --- a/src/CrossWindow/Cocoa/CocoaWindow.h +++ b/src/CrossWindow/Cocoa/CocoaWindow.h @@ -54,6 +54,9 @@ class Window // XWinWindow* void* window; + // XWinWindowDelegate* + void* windowDelegate; + // XWinView* void* view; diff --git a/src/CrossWindow/Cocoa/CocoaWindow.mm b/src/CrossWindow/Cocoa/CocoaWindow.mm index 8bd9ab7..0ac61dd 100644 --- a/src/CrossWindow/Cocoa/CocoaWindow.mm +++ b/src/CrossWindow/Cocoa/CocoaWindow.mm @@ -12,6 +12,60 @@ @implementation XWinWindow @end +@interface XWinWindowDelegate : NSObject +{ +} + +@property (assign) XWinWindow* window; + +-(void) postEvent: (xwin::EventType)type data1:(NSInteger)data1 data2:(NSInteger)data2; + +@end + +@implementation XWinWindowDelegate + +- (void) postEvent:(xwin::EventType)type data1:(NSInteger)data1 data2:(NSInteger)data2 +{ + NSPoint point; + NSTimeInterval timestamp; + NSEvent* event = [NSEvent + otherEventWithType:NSEventTypeApplicationDefined + location:point + modifierFlags:0 + timestamp:timestamp + windowNumber:self.window.windowNumber + context:nil + subtype:(short) type + data1:data1 + data2:data2 + ]; + [self.window postEvent:event atStart:false]; +} + +- (void)windowDidBecomeMain:(NSNotification *)notification +{ + [self postEvent: xwin::EventType::Focus data1: 1 data2: 0]; +} + +- (void)windowDidResignMain:(NSNotification *)notification +{ + [self postEvent: xwin::EventType::Focus data1: 0 data2: 0]; +} + +- (void)windowWillClose:(NSNotification *)notification +{ + [self postEvent: xwin::EventType::Close data1: 0 data2: 0]; +} + +- (NSSize)windowWillResize:(NSWindow *)sender toSize:(NSSize)frameSize +{ + CGSize contentSize = [[self.window contentView] frame].size; + [self postEvent: xwin::EventType::Resize data1: contentSize.width data2: contentSize.height]; + return frameSize; +} + +@end + @interface XWinView : NSView - (BOOL) acceptsFirstResponder; - (BOOL) isOpaque; @@ -118,6 +172,13 @@ - (BOOL)isOpaque mTitle = [NSString stringWithCString:desc.title.c_str() encoding:[NSString defaultCStringEncoding]]; XWinWindow* w = ((XWinWindow*)window); + + // Setup NSWindowDelegate + windowDelegate = [XWinWindowDelegate alloc]; + XWinWindowDelegate* wd = ((XWinWindowDelegate*)windowDelegate); + [w setDelegate: wd]; + [wd setWindow: w]; + if(!desc.title.empty()) { [w setTitle: (NSString*)mTitle]; } if(desc.centered)