Browse Source

Merge pull request #15625 from bruvzg/osx-open-with

[macOS] Adds ability to open files with "Open With" or double-click from Finder
Rémi Verschelde 7 years ago
parent
commit
b90d53ab7c
3 changed files with 33 additions and 1 deletions
  1. 10 1
      platform/osx/godot_main_osx.mm
  2. 2 0
      platform/osx/os_osx.h
  3. 21 0
      platform/osx/os_osx.mm

+ 10 - 1
platform/osx/godot_main_osx.mm

@@ -82,8 +82,17 @@ int main(int argc, char **argv) {
 #endif
 
 	OS_OSX os;
+	Error err;
+
+	if (os.open_with_filename != "") {
+		char *argv_c = (char *)malloc(os.open_with_filename.utf8().size());
+		memcpy(argv_c, os.open_with_filename.utf8().get_data(), os.open_with_filename.utf8().size());
+		err = Main::setup(argv[0], 1, &argv_c);
+		free(argv_c);
+	} else {
+		err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]);
+	}
 
-	Error err = Main::setup(argv[0], argc - first_arg, &argv[first_arg]);
 	if (err != OK)
 		return 255;
 

+ 2 - 0
platform/osx/os_osx.h

@@ -115,6 +115,8 @@ public:
 	Size2 window_size;
 	Rect2 restore_rect;
 
+	String open_with_filename;
+
 	Point2 im_position;
 	ImeCallback im_callback;
 	void *im_target;

+ 21 - 0
platform/osx/os_osx.mm

@@ -144,6 +144,13 @@ static Vector2 get_mouse_pos(NSEvent *event) {
 
 @implementation GodotApplicationDelegate
 
+- (BOOL)application:(NSApplication *)sender openFile:(NSString *)filename {
+	// Note: called before main loop init!
+	char *utfs = strdup([filename UTF8String]);
+	OS_OSX::singleton->open_with_filename.parse_utf8(utfs);
+	return YES;
+}
+
 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
 	if (OS_OSX::singleton->get_main_loop())
 		OS_OSX::singleton->get_main_loop()->notification(MainLoop::NOTIFICATION_WM_QUIT_REQUEST);
@@ -2322,6 +2329,20 @@ OS_OSX::OS_OSX() {
 	Vector<Logger *> loggers;
 	loggers.push_back(memnew(OSXTerminalLogger));
 	_set_logger(memnew(CompositeLogger(loggers)));
+
+	//process application:openFile: event
+	while (true) {
+		NSEvent *event = [NSApp
+				nextEventMatchingMask:NSEventMaskAny
+							untilDate:[NSDate distantPast]
+							   inMode:NSDefaultRunLoopMode
+							  dequeue:YES];
+
+		if (event == nil)
+			break;
+
+		[NSApp sendEvent:event];
+	}
 }
 
 bool OS_OSX::_check_internal_feature_support(const String &p_feature) {