Properly reflect hidden/shown windows on OSX.
This fixes a bug where windows would always be considered to be in the
shown/hidden state they were originally created in.
1.1 --- a/src/video/cocoa/SDL_cocoawindow.m Mon Apr 22 11:18:45 2013 -0300
1.2 +++ b/src/video/cocoa/SDL_cocoawindow.m Mon Apr 22 12:07:13 2013 -0700
1.3 @@ -65,6 +65,14 @@
1.4 [window setDelegate:self];
1.5 }
1.6
1.7 + // Haven't found a delegate / notification that triggers when the window is
1.8 + // ordered out (is not visible any more). You can be ordered out without
1.9 + // minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
1.10 + [window addObserver:self
1.11 + forKeyPath:@"visible"
1.12 + options:NSKeyValueObservingOptionNew
1.13 + context:NULL];
1.14 +
1.15 [window setNextResponder:self];
1.16 [window setAcceptsMouseMovedEvents:YES];
1.17
1.18 @@ -77,6 +85,21 @@
1.19 #endif
1.20 }
1.21
1.22 +- (void)observeValueForKeyPath:(NSString *)keyPath
1.23 + ofObject:(id)object
1.24 + change:(NSDictionary *)change
1.25 + context:(void *)context
1.26 +{
1.27 + if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
1.28 + int newVisibility = [[change objectForKey:@"new"] intValue];
1.29 + if (newVisibility) {
1.30 + SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
1.31 + } else {
1.32 + SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
1.33 + }
1.34 + }
1.35 +}
1.36 +
1.37 - (void)close
1.38 {
1.39 NSNotificationCenter *center;
1.40 @@ -97,6 +120,9 @@
1.41 [window setDelegate:nil];
1.42 }
1.43
1.44 + [window removeObserver:self
1.45 + forKeyPath:@"visible"];
1.46 +
1.47 if ([window nextResponder] == self) {
1.48 [window setNextResponder:nil];
1.49 }
1.50 @@ -531,6 +557,7 @@
1.51 } else {
1.52 window->flags &= ~SDL_WINDOW_SHOWN;
1.53 }
1.54 +
1.55 {
1.56 unsigned int style = [nswindow styleMask];
1.57
1.58 @@ -545,17 +572,20 @@
1.59 window->flags &= ~SDL_WINDOW_RESIZABLE;
1.60 }
1.61 }
1.62 +
1.63 /* isZoomed always returns true if the window is not resizable */
1.64 if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
1.65 window->flags |= SDL_WINDOW_MAXIMIZED;
1.66 } else {
1.67 window->flags &= ~SDL_WINDOW_MAXIMIZED;
1.68 }
1.69 +
1.70 if ([nswindow isMiniaturized]) {
1.71 window->flags |= SDL_WINDOW_MINIMIZED;
1.72 } else {
1.73 window->flags &= ~SDL_WINDOW_MINIMIZED;
1.74 }
1.75 +
1.76 if ([nswindow isKeyWindow]) {
1.77 window->flags |= SDL_WINDOW_INPUT_FOCUS;
1.78 SDL_SetKeyboardFocus(data->window);