Improved process setup code design

This commit is contained in:
Marco Antognini 2011-04-16 20:10:47 +02:00
parent ad79328a70
commit 690d4a3eff
2 changed files with 38 additions and 54 deletions

View File

@ -229,6 +229,13 @@ public:
////////////////////////////////////////////////////////////
void ApplyContext(NSOpenGLContextRef context) const;
////////////////////////////////////////////////////////////
/// \brief Change the type of the current process to become a full GUI app.
/// Also ensure NSApp is constructed.
///
////////////////////////////////////////////////////////////
static void SetUpProcess(void);
private:
////////////////////////////////////////////////////////////
/// \brief Process incoming events from the operating system
@ -315,18 +322,6 @@ private:
////////////////////////////////////////////////////////////
virtual void SetIcon(unsigned int width, unsigned int height, const Uint8* pixels);
////////////////////////////////////////////////////////////
/// \brief Construct the pool after ensuring NSApp is valid.
///
////////////////////////////////////////////////////////////
void SetUpPoolAndApplication(void);
////////////////////////////////////////////////////////////
/// \brief Change the type of the current process to become a full GUI app.
///
////////////////////////////////////////////////////////////
static void SetUpProcessAsApplication(void);
////////////////////////////////////////////////////////////
// Member data
////////////////////////////////////////////////////////////

View File

@ -44,7 +44,8 @@ namespace priv
////////////////////////////////////////////////////////////
WindowImplCocoa::WindowImplCocoa(WindowHandle handle)
{
SetUpPoolAndApplication();
// Create the pool.
myPool = [[NSAutoreleasePool alloc] init];
// Treat the handle as it real type
id nsHandle = (id)handle;
@ -90,10 +91,11 @@ WindowImplCocoa::WindowImplCocoa(VideoMode mode,
const std::string& title,
unsigned long style)
{
SetUpPoolAndApplication();
// Transform the app process.
SetUpProcessAsApplication();
SetUpProcess();
// Create the pool.
myPool = [[NSAutoreleasePool alloc] init];
// Don't forget to update our parent (that is, WindowImpl) size :
myWidth = mode.Width;
@ -122,6 +124,30 @@ void WindowImplCocoa::ApplyContext(NSOpenGLContextRef context) const
}
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetUpProcess(void)
{
static bool isTheProcessSetAsApplication = false;
if (!isTheProcessSetAsApplication) {
// Do it only once !
isTheProcessSetAsApplication = true;
// Set the process as a normal application so it can get focus.
ProcessSerialNumber psn;
if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}
// Tell the application to stop bouncing in the Dock.
[[NSApplication sharedApplication] finishLaunching];
// NOTE : This last call won't harm anything even if SFML window was
// created with an external handle.
}
}
#pragma mark
#pragma mark WindowImplCocoa's window-event methods
@ -370,43 +396,6 @@ void WindowImplCocoa::SetIcon(unsigned int width, unsigned int height, const Uin
[myDelegate setIconTo:width by:height with:pixels];
}
#pragma mark
#pragma mark WindowImplCocoa's init methods
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetUpPoolAndApplication(void)
{
// Ensure NSApp exists.
[NSApplication sharedApplication];
// Create the pool.
myPool = [[NSAutoreleasePool alloc] init];
}
////////////////////////////////////////////////////////////
void WindowImplCocoa::SetUpProcessAsApplication(void)
{
static bool isTheProcessSetAsApplication = false;
if (!isTheProcessSetAsApplication) {
// Do it only once !
isTheProcessSetAsApplication = true;
// Set the process as a normal application so it can get focus.
ProcessSerialNumber psn;
if (!GetCurrentProcess(&psn)) {
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetFrontProcess(&psn);
}
// Tell the application to stop bouncing in the Dock.
[[NSApplication sharedApplication] finishLaunching];
// NOTE : This last call won't harm anything even if SFML window was
// created with an external handle.
}
}
} // namespace priv
} // namespace sf