src/video/cocoa/SDL_cocoawindow.m
changeset 7087 5639ac726076
parent 7085 152cc7ddfa57
child 7098 f4b2c6fb0258
--- a/src/video/cocoa/SDL_cocoawindow.m	Mon Apr 22 15:24:35 2013 -0700
+++ b/src/video/cocoa/SDL_cocoawindow.m	Mon Apr 22 18:14:26 2013 -0700
@@ -50,6 +50,8 @@
     NSView *view = [window contentView];
 
     _data = data;
+    observingVisible = YES;
+    wasVisible = [window isVisible];
 
     center = [NSNotificationCenter defaultCenter];
 
@@ -90,6 +92,10 @@
                         change:(NSDictionary *)change
                        context:(void *)context
 {
+    if (!observingVisible) {
+        return;
+    }
+
     if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
         int newVisibility = [[change objectForKey:@"new"] intValue];
         if (newVisibility) {
@@ -100,6 +106,27 @@
     }
 }
 
+-(void) pauseVisibleObservation
+{
+    observingVisible = NO;
+    wasVisible = [_data->nswindow isVisible];
+}
+
+-(void) resumeVisibleObservation
+{
+    BOOL isVisible = [_data->nswindow isVisible];
+    observingVisible = YES;
+    if (wasVisible != isVisible) {
+        if (isVisible) {
+            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
+        } else {
+            SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
+        }
+
+        wasVisible = isVisible;
+    }
+}
+
 - (void)close
 {
     NSNotificationCenter *center;
@@ -785,10 +812,13 @@
 Cocoa_ShowWindow(_THIS, SDL_Window * window)
 {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
+    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
+    NSWindow *nswindow = windowData->nswindow;
 
     if (![nswindow isMiniaturized]) {
+        [windowData->listener pauseVisibleObservation];
         [nswindow makeKeyAndOrderFront:nil];
+        [windowData->listener resumeVisibleObservation];
     }
     [pool release];
 }
@@ -807,9 +837,13 @@
 Cocoa_RaiseWindow(_THIS, SDL_Window * window)
 {
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
-    NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
+    SDL_WindowData *windowData = ((SDL_WindowData *) window->driverdata);
+    NSWindow *nswindow = windowData->nswindow;
 
+    [windowData->listener pauseVisibleObservation];
     [nswindow makeKeyAndOrderFront:nil];
+    [windowData->listener resumeVisibleObservation];
+
     [pool release];
 }
 
@@ -960,7 +994,10 @@
         [nswindow setLevel:kCGNormalWindowLevel];
     }
 #endif
+
+    [data->listener pauseVisibleObservation];
     [nswindow makeKeyAndOrderFront:nil];
+    [data->listener resumeVisibleObservation];
 
     if (window == _this->current_glwin) {
         [((NSOpenGLContext *) _this->current_glctx) update];