Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Properly reflect hidden/shown windows on OSX.
Browse files Browse the repository at this point in the history
This fixes a bug where windows would always be considered to be in the
shown/hidden state they were originally created in.
  • Loading branch information
jorgenpt committed Apr 22, 2013
1 parent bfd5036 commit fa30d21
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/video/cocoa/SDL_cocoawindow.m
Expand Up @@ -65,6 +65,14 @@ - (void)listen:(SDL_WindowData *)data
[window setDelegate:self];
}

// Haven't found a delegate / notification that triggers when the window is
// ordered out (is not visible any more). You can be ordered out without
// minimizing, so DidMiniaturize doesn't work. (e.g. -[NSWindow orderOut:])
[window addObserver:self
forKeyPath:@"visible"
options:NSKeyValueObservingOptionNew
context:NULL];

[window setNextResponder:self];
[window setAcceptsMouseMovedEvents:YES];

Expand All @@ -77,6 +85,21 @@ - (void)listen:(SDL_WindowData *)data
#endif
}

- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if (object == _data->nswindow && [keyPath isEqualToString:@"visible"]) {
int newVisibility = [[change objectForKey:@"new"] intValue];
if (newVisibility) {
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_SHOWN, 0, 0);
} else {
SDL_SendWindowEvent(_data->window, SDL_WINDOWEVENT_HIDDEN, 0, 0);
}
}
}

- (void)close
{
NSNotificationCenter *center;
Expand All @@ -97,6 +120,9 @@ - (void)close
[window setDelegate:nil];
}

[window removeObserver:self
forKeyPath:@"visible"];

if ([window nextResponder] == self) {
[window setNextResponder:nil];
}
Expand Down Expand Up @@ -531,6 +557,7 @@ - (void)rightMouseDown:(NSEvent *)theEvent
} else {
window->flags &= ~SDL_WINDOW_SHOWN;
}

{
unsigned int style = [nswindow styleMask];

Expand All @@ -545,17 +572,20 @@ - (void)rightMouseDown:(NSEvent *)theEvent
window->flags &= ~SDL_WINDOW_RESIZABLE;
}
}

/* isZoomed always returns true if the window is not resizable */
if ((window->flags & SDL_WINDOW_RESIZABLE) && [nswindow isZoomed]) {
window->flags |= SDL_WINDOW_MAXIMIZED;
} else {
window->flags &= ~SDL_WINDOW_MAXIMIZED;
}

if ([nswindow isMiniaturized]) {
window->flags |= SDL_WINDOW_MINIMIZED;
} else {
window->flags &= ~SDL_WINDOW_MINIMIZED;
}

if ([nswindow isKeyWindow]) {
window->flags |= SDL_WINDOW_INPUT_FOCUS;
SDL_SetKeyboardFocus(data->window);
Expand Down

0 comments on commit fa30d21

Please sign in to comment.