Improved OS X implementation

It makes sure the notifications sent to SFOpenGLView are only from its window.
This commit is contained in:
Marco Antognini 2014-05-26 15:31:45 +02:00
parent 7d4235a38f
commit 3f61214571
4 changed files with 50 additions and 25 deletions

View File

@ -69,6 +69,8 @@ namespace sf {
/// NB: -initWithFrame: is also implemented to default isFullscreen to NO /// NB: -initWithFrame: is also implemented to default isFullscreen to NO
/// in case SFOpenGLView is created with the standard message. /// in case SFOpenGLView is created with the standard message.
/// ///
/// To finish the initialization -finishInit should be called too.
///
/// \param frameRect dimension of the view /// \param frameRect dimension of the view
/// \param isFullscreen fullscreen flag /// \param isFullscreen fullscreen flag
/// ///
@ -77,6 +79,14 @@ namespace sf {
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
-(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen; -(id)initWithFrame:(NSRect)frameRect fullscreen:(BOOL)isFullscreen;
////////////////////////////////////////////////////////////
/// \brief Finish the creation of the SFML OpenGL view
///
/// This method should be called after the view was added to a window
///
////////////////////////////////////////////////////////////
-(void)finishInit;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Apply the given requester to the view /// \brief Apply the given requester to the view
/// ///

View File

@ -160,31 +160,7 @@ BOOL isValidTextUnicode(NSEvent* event);
[self addTrackingArea:m_trackingArea]; [self addTrackingArea:m_trackingArea];
m_fullscreen = isFullscreen; m_fullscreen = isFullscreen;
[self updateScaleFactor]; m_scaleFactor = 1.0; // Default value; it will be updated in finishInit
// Register for window focus events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeKey:)
name:NSWindowDidBecomeKeyNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResignKey:)
name:NSWindowDidResignKeyNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResignKey:)
name:NSWindowWillCloseNotification
object:[self window]];
// Register for changed screen and changed screen's profile events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateScaleFactor)
name:NSWindowDidChangeScreenNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateScaleFactor)
name:NSWindowDidChangeScreenProfileNotification
object:[self window]];
// Create a hidden text view for parsing key down event properly // Create a hidden text view for parsing key down event properly
m_silentResponder = [[SFSilentResponder alloc] init]; m_silentResponder = [[SFSilentResponder alloc] init];
@ -193,12 +169,47 @@ BOOL isValidTextUnicode(NSEvent* event);
// Request high resolution on high DPI displays // Request high resolution on high DPI displays
[self setWantsBestResolutionOpenGLSurface:YES]; [self setWantsBestResolutionOpenGLSurface:YES];
// At that point, the view isn't attached to a window. We defer the rest of
// the initialization process to later.
} }
return self; return self;
} }
////////////////////////////////////////////////////////
-(void)finishInit
{
// Register for window focus events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidBecomeKey:)
name:NSWindowDidBecomeKeyNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResignKey:)
name:NSWindowDidResignKeyNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResignKey:)
name:NSWindowWillCloseNotification
object:[self window]];
// Register for changed screen and changed screen's profile events
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateScaleFactor)
name:NSWindowDidChangeScreenNotification
object:[self window]];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(updateScaleFactor)
name:NSWindowDidChangeScreenProfileNotification
object:[self window]];
// Now that we have a window, set up correctly the scale factor
[self updateScaleFactor];
}
//////////////////////////////////////////////////////// ////////////////////////////////////////////////////////
-(void)setRequesterTo:(sf::priv::WindowImplCocoa*)requester -(void)setRequesterTo:(sf::priv::WindowImplCocoa*)requester
{ {

View File

@ -71,6 +71,8 @@
[m_view addSubview:m_oglView]; [m_view addSubview:m_oglView];
[m_oglView setAutoresizingMask:[m_view autoresizingMask]]; [m_oglView setAutoresizingMask:[m_view autoresizingMask]];
[m_oglView finishInit];
} }
return self; return self;

View File

@ -149,6 +149,8 @@
[self setupFullscreenViewWithMode:mode]; [self setupFullscreenViewWithMode:mode];
else else
[self setupWindowWithMode:mode andStyle:style]; [self setupWindowWithMode:mode andStyle:style];
[m_oglView finishInit];
} }
return self; return self;
} }