From cb897c6f91db4d87fe113e8c5139322a9d2f112b Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 21 Jul 2019 20:54:26 +0200 Subject: Implement focus on MacOS --- pugl/pugl_osx.m | 63 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/pugl/pugl_osx.m b/pugl/pugl_osx.m index 28ea313..73fe654 100644 --- a/pugl/pugl_osx.m +++ b/pugl/pugl_osx.m @@ -59,8 +59,6 @@ struct PuglInternalsImpl { backing:(NSBackingStoreType)bufferingType defer:(BOOL)flag; - (void) setPuglview:(PuglView*)view; -- (BOOL) windowShouldClose:(id)sender; -- (BOOL) canBecomeKeyWindow:(id)sender; @end @implementation PuglWindow @@ -85,18 +83,6 @@ struct PuglInternalsImpl { [self setContentSize:NSMakeSize(view->width, view->height)]; } -- (BOOL)windowShouldClose:(id)sender -{ - const PuglEventClose ev = { - PUGL_CLOSE, - puglview, - 0 - }; - puglDispatchEvent(puglview, (const PuglEvent*)&ev); - - return YES; -} - - (BOOL) canBecomeKeyWindow { return YES; @@ -107,11 +93,6 @@ struct PuglInternalsImpl { return YES; } -- (BOOL) canBecomeKeyWindow:(id)sender -{ - return NO; -} - @end @interface PuglOpenGLView : NSOpenGLView @@ -572,6 +553,47 @@ handleCrossing(PuglOpenGLView* view, NSEvent* event, const PuglEventType type) @end +@interface PuglWindowDelegate : NSObject +{ + PuglWindow* window; +} + +- (instancetype) initWithPuglWindow:(PuglWindow*)window; + +@end + +@implementation PuglWindowDelegate + +- (instancetype) initWithPuglWindow:(PuglWindow*)puglWindow +{ + if ((self = [super init])) { + window = puglWindow; + } + + return self; +} + +- (BOOL) windowShouldClose:(id)sender +{ + const PuglEventClose ev = { PUGL_CLOSE, window->puglview, 0 }; + puglDispatchEvent(window->puglview, (const PuglEvent*)&ev); + return YES; +} + +- (void) windowDidBecomeKey:(NSNotification*)notification +{ + const PuglEventFocus ev = { PUGL_FOCUS_IN, window->puglview, 0, false }; + puglDispatchEvent(window->puglview, (const PuglEvent*)&ev); +} + +- (void) windowDidResignKey:(NSNotification*)notification +{ + const PuglEventFocus ev = { PUGL_FOCUS_OUT, window->puglview, 0, false }; + puglDispatchEvent(window->puglview, (const PuglEvent*)&ev); +} + +@end + PuglInternals* puglInitInternals(void) { @@ -661,6 +683,9 @@ puglCreateWindow(PuglView* view, const char* title) } impl->window = window; + ((NSWindow*)window).delegate = [[PuglWindowDelegate alloc] + initWithPuglWindow:window]; + if (view->min_aspect_x && view->min_aspect_y) { [window setContentAspectRatio:NSMakeSize(view->min_aspect_x, view->min_aspect_y)]; -- cgit v1.2.1