diff options
author | David Robillard <d@drobilla.net> | 2019-07-21 20:54:26 +0200 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2019-07-21 21:30:29 +0200 |
commit | cb897c6f91db4d87fe113e8c5139322a9d2f112b (patch) | |
tree | 8ca2694a070f863afc667daeaf8255d15441ec31 | |
parent | 17af0352387a536c461cf366132b3d6c90ce3c70 (diff) | |
download | pugl-cb897c6f91db4d87fe113e8c5139322a9d2f112b.tar.gz pugl-cb897c6f91db4d87fe113e8c5139322a9d2f112b.tar.bz2 pugl-cb897c6f91db4d87fe113e8c5139322a9d2f112b.zip |
Implement focus on MacOS
-rw-r--r-- | pugl/pugl_osx.m | 63 |
1 files 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<NSWindowDelegate> +{ + 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)]; |