Skip to content

Commit 5237850

Browse files
committed
[osx] Fix NSApplication initialization
By calling [NSApplication run] and then [NSApplication stop] we are able to initialize the application instance and prevent issues in fullscreen mode (fix aseprite/aseprite#4795)
1 parent b51bee3 commit 5237850

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

os/osx/app.mm

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ void setAppMode(AppMode appMode)
5252

5353
void finishLaunching()
5454
{
55-
[m_app finishLaunching];
55+
// [m_app run] can only be called once.
56+
if (![[NSRunningApplication currentApplication] isFinishedLaunching])
57+
// Note that the [m_app run] call doesn't block because we are calling
58+
// [NSApp stop] from [AppDelegateOSX applicationDidFinishLaunching]. We only
59+
// need the application's initialization done inside run to prevent issues
60+
// such as: https://github.com/aseprite/aseprite/issues/4795
61+
[m_app run];
62+
5663
[m_appDelegate resetCliFiles];
5764
}
5865

os/osx/app_delegate.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)app;
3030
- (void)applicationWillTerminate:(NSNotification*)notification;
3131
- (void)applicationWillResignActive:(NSNotification*)notification;
32+
- (void)applicationDidFinishLaunching:(NSNotification*)notification;
3233
- (void)applicationDidBecomeActive:(NSNotification*)notification;
3334
- (void)applicationDidHide:(NSNotification*)notification;
3435
- (void)applicationDidUnhide:(NSNotification*)notification;

os/osx/app_delegate.mm

+7
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ - (void)applicationWillResignActive:(NSNotification*)notification
106106
[ViewOSX updateKeyFlags:event];
107107
}
108108

109+
- (void)applicationDidFinishLaunching:(NSNotification*)notification
110+
{
111+
// We do not want that the [NSApplication run] call made from
112+
// AppOSX::Impl::finishLaunching() blocks.
113+
[NSApp stop:nil];
114+
}
115+
109116
- (void)applicationDidBecomeActive:(NSNotification*)notification
110117
{
111118
for (id window : [NSApp windows]) {

os/system.h

+9-8
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,16 @@ class System : public RefCount {
9393
// files that were processed from the CLI arguments directly.
9494
virtual void markCliFileAsProcessed(const std::string& cliFile) = 0;
9595

96-
// On macOS it calls [NSApplication finishLaunching] that will
97-
// produce some extra events like [NSApplicationDelegate
98-
// application:openFiles:] which generates os::Event::DropFiles
99-
// events for each file specified in the command line.
96+
// On macOS it calls [NSApplication run], which in turn does two things:
97+
// - It calls [NSApplication finishLaunching], which will produce some extra
98+
// events like [NSApplicationDelegate application:openFiles:] which generates
99+
// os::Event::DropFiles events for each file specified in the command line.
100+
// - It initializes internal state of the NSApplication instance to make it
101+
// behave as expected under some circumstances.
100102
//
101-
// You can ignore those DropFiles events if you've already
102-
// processed through the CLI arguments (app_main(argc, argv)) or
103-
// you can use markCliFileAsProcessed() before calling this
104-
// function.
103+
// About the first point: you can ignore those DropFiles events if you've
104+
// already processed through the CLI arguments (app_main(argc, argv)) or
105+
// you can use markCliFileAsProcessed() before calling this function.
105106
virtual void finishLaunching() = 0;
106107

107108
// We might need to call this function when the app is launched

0 commit comments

Comments
 (0)