@@ -124,7 +124,13 @@ - (void)system_theme_changed:(NSNotification *)notification {
124124 }
125125}
126126
127- - (void )applicationDidFinishLaunching : (NSNotification *)notice {
127+ - (void )applicationDidFinishLaunching : (NSNotification *)notification {
128+ static_cast <OS_MacOS *>(OS::get_singleton ())->start_main ();
129+ }
130+
131+ - (void )activate {
132+ [NSApp activateIgnoringOtherApps: YES ];
133+
128134 NSString *nsappname = [[[NSBundle mainBundle ] infoDictionary ] objectForKey: @" CFBundleName" ];
129135 const char *bundled_id = getenv (" __CFBundleIdentifier" );
130136 NSString *nsbundleid_env = [NSString stringWithUTF8String: (bundled_id != nullptr ) ? bundled_id : " " ];
@@ -139,11 +145,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notice {
139145
140146- (id )init {
141147 self = [super init ];
142-
143- NSAppleEventManager *aem = [NSAppleEventManager sharedAppleEventManager ];
144- [aem setEventHandler: self andSelector: @selector (handleAppleEvent:withReplyEvent: ) forEventClass: kInternetEventClass andEventID: kAEGetURL ];
145- [aem setEventHandler: self andSelector: @selector (handleAppleEvent:withReplyEvent: ) forEventClass: kCoreEventClass andEventID: kAEOpenDocuments ];
146-
147148 return self;
148149}
149150
@@ -152,36 +153,45 @@ - (void)dealloc {
152153 [[NSDistributedNotificationCenter defaultCenter ] removeObserver: self name: @" AppleColorPreferencesChangedNotification" object: nil ];
153154}
154155
155- - (void )handleAppleEvent : ( NSAppleEventDescriptor *)event withReplyEvent : ( NSAppleEventDescriptor *) replyEvent {
156+ - (void )application : ( NSApplication *)application openURLs : ( NSArray <NSURL *> *) urls {
156157 OS_MacOS *os = (OS_MacOS *)OS::get_singleton ();
157- if (!event || ! os) {
158+ if (!os) {
158159 return ;
159160 }
160-
161161 List<String> args;
162- if (([event eventClass ] == kInternetEventClass ) && ([event eventID ] == kAEGetURL )) {
163- // Opening URL scheme.
164- NSString *url = [[event paramDescriptorForKeyword: keyDirectObject] stringValue ];
165- args.push_back (vformat (" --uri=\" %s\" " , String::utf8 ([url UTF8String ])));
166- }
167-
168- if (([event eventClass ] == kCoreEventClass ) && ([event eventID ] == kAEOpenDocuments )) {
169- // Opening file association.
170- NSAppleEventDescriptor *files = [event paramDescriptorForKeyword: keyDirectObject];
171- if (files) {
172- NSInteger count = [files numberOfItems ];
173- for (NSInteger i = 1 ; i <= count; i++) {
174- NSURL *url = [NSURL URLWithString: [[files descriptorAtIndex: i] stringValue ]];
175- args.push_back (String::utf8 ([url.path UTF8String ]));
176- }
162+ for (NSURL *url in urls) {
163+ if ([url isFileURL ]) {
164+ args.push_back (String::utf8 ([url.path UTF8String ]));
165+ } else {
166+ args.push_back (vformat (" --uri=\" %s\" " , String::utf8 ([url.absoluteString UTF8String ])));
177167 }
178168 }
169+ if (!args.is_empty ()) {
170+ if (os->get_main_loop ()) {
171+ // Application is already running, open a new instance with the URL/files as command line arguments.
172+ os->create_instance (args);
173+ } else if (os->get_cmd_argc () == 0 ) {
174+ // Application is just started, add to the list of command line arguments and continue.
175+ os->set_cmdline_platform_args (args);
176+ }
177+ }
178+ }
179179
180+ - (void )application : (NSApplication *)sender openFiles : (NSArray <NSString *> *)filenames {
181+ OS_MacOS *os = (OS_MacOS *)OS::get_singleton ();
182+ if (!os) {
183+ return ;
184+ }
185+ List<String> args;
186+ for (NSString *filename in filenames) {
187+ NSURL *url = [NSURL URLWithString: filename];
188+ args.push_back (String::utf8 ([url.path UTF8String ]));
189+ }
180190 if (!args.is_empty ()) {
181191 if (os->get_main_loop ()) {
182192 // Application is already running, open a new instance with the URL/files as command line arguments.
183193 os->create_instance (args);
184- } else {
194+ } else if (os-> get_cmd_argc () == 0 ) {
185195 // Application is just started, add to the list of command line arguments and continue.
186196 os->set_cmdline_platform_args (args);
187197 }
@@ -220,11 +230,23 @@ - (NSMenu *)applicationDockMenu:(NSApplication *)sender {
220230 }
221231}
222232
233+ - (void )applicationWillTerminate : (NSNotification *)notification {
234+ OS_MacOS *os = (OS_MacOS *)OS::get_singleton ();
235+ if (os) {
236+ os->cleanup ();
237+ exit (os->get_exit_code ());
238+ }
239+ }
240+
223241- (NSApplicationTerminateReply )applicationShouldTerminate : (NSApplication *)sender {
224242 DisplayServerMacOS *ds = (DisplayServerMacOS *)DisplayServer::get_singleton ();
225243 if (ds) {
226244 ds->send_window_event (ds->get_window (DisplayServerMacOS::MAIN_WINDOW_ID), DisplayServerMacOS::WINDOW_EVENT_CLOSE_REQUEST);
227245 }
246+ OS_MacOS *os = (OS_MacOS *)OS::get_singleton ();
247+ if (!os || os->os_should_terminate ()) {
248+ return NSTerminateNow;
249+ }
228250 return NSTerminateCancel;
229251}
230252
0 commit comments