summaryrefslogtreecommitdiff
path: root/src/macos.m
diff options
context:
space:
mode:
authorJaakko Keränen <jaakko.keranen@iki.fi>2020-09-11 14:25:03 +0300
committerJaakko Keränen <jaakko.keranen@iki.fi>2020-09-11 14:25:03 +0300
commit25456ce008c7fe78378ddaaf784c74209a2d8c14 (patch)
treefbb86324f468ab984ca1be1c25f92872499ed948 /src/macos.m
parent2fc802367acd63b86fecda92461d63d029d7378c (diff)
macOS: Handling launch URLs and drop'n'drop
Improved drop and drop event handling: multiple dropped files/URLs open in new tabs. The application registers gemini: as a handled URL scheme.
Diffstat (limited to 'src/macos.m')
-rw-r--r--src/macos.m53
1 files changed, 47 insertions, 6 deletions
diff --git a/src/macos.m b/src/macos.m
index d44be7d0..bbbaaa19 100644
--- a/src/macos.m
+++ b/src/macos.m
@@ -121,8 +121,9 @@ enum iTouchBarVariant {
121} 121}
122- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl; 122- (id)initWithSDLDelegate:(NSObject<NSApplicationDelegate> *)sdl;
123//- (NSTouchBar *)makeTouchBar; 123//- (NSTouchBar *)makeTouchBar;
124/* SDL needs to do its own thing. */ 124- (BOOL)application:(NSApplication *)app openFile:(NSString *)filename;
125- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename; 125- (void)application:(NSApplication *)app openFiles:(NSArray<NSString *> *)filenames;
126- (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls;
126- (void)applicationDidFinishLaunching:(NSNotification *)notifications; 127- (void)applicationDidFinishLaunching:(NSNotification *)notifications;
127@end 128@end
128 129
@@ -151,7 +152,7 @@ enum iTouchBarVariant {
151static void appearanceChanged_MacOS_(NSString *name) { 152static void appearanceChanged_MacOS_(NSString *name) {
152 const iBool isDark = [name containsString:@"Dark"]; 153 const iBool isDark = [name containsString:@"Dark"];
153 const iBool isHighContrast = [name containsString:@"HighContrast"]; 154 const iBool isHighContrast = [name containsString:@"HighContrast"];
154 postCommandf_App("os.theme.changed dark:%d contrast:%d", isDark ? 1 : 0, isHighContrast ? 1 : 0); 155 postCommandf_App("~os.theme.changed dark:%d contrast:%d", isDark ? 1 : 0, isHighContrast ? 1 : 0);
155// printf("Effective appearance changed: %s\n", [name cStringUsingEncoding:NSUTF8StringEncoding]); 156// printf("Effective appearance changed: %s\n", [name cStringUsingEncoding:NSUTF8StringEncoding]);
156// fflush(stdout); 157// fflush(stdout);
157} 158}
@@ -170,8 +171,23 @@ static void appearanceChanged_MacOS_(NSString *name) {
170 [menuCommands setObject:command forKey:[menuItem title]]; 171 [menuCommands setObject:command forKey:[menuItem title]];
171} 172}
172 173
173- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename { 174- (BOOL)application:(NSApplication *)app openFile:(NSString *)filename {
174 return [sdlDelegate application:theApplication openFile:filename]; 175 return [sdlDelegate application:app openFile:filename];
176}
177
178- (void)application:(NSApplication *)app openFiles:(NSArray<NSString *> *)filenames {
179 /* TODO: According to AppKit docs, this method won't be called when openURLs is defined. */
180 for (NSString *fn in filenames) {
181 NSLog(@"openFiles: %@", fn);
182 [self application:app openFile:fn];
183 }
184}
185
186- (void)application:(NSApplication *)app openURLs:(NSArray<NSURL *> *)urls {
187 for (NSURL *url in urls) {
188 NSLog(@"openURLs: %@", [url absoluteString]);
189 [sdlDelegate application:app openFile:[url absoluteString]];
190 }
175} 191}
176 192
177- (void)applicationDidFinishLaunching:(NSNotification *)notification { 193- (void)applicationDidFinishLaunching:(NSNotification *)notification {
@@ -397,9 +413,30 @@ void enableMomentumScroll_MacOS(void) {
397 forKey: @"AppleMomentumScrollSupported"]; 413 forKey: @"AppleMomentumScrollSupported"];
398} 414}
399 415
416@interface UrlHandler : NSObject
417- (void)handleURLEvent:(NSAppleEventDescriptor*)event
418 withReplyEvent:(NSAppleEventDescriptor*)replyEvent;
419@end
420
421@implementation UrlHandler
422- (void)handleURLEvent:(NSAppleEventDescriptor*)event
423 withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
424 NSString *url = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
425 postCommandf_App("~open url:%s", [url cStringUsingEncoding:NSUTF8StringEncoding]);
426}
427@end
428
429void registerURLHandler_MacOS(void) {
430 UrlHandler *handler = [[UrlHandler alloc] init];
431 [[NSAppleEventManager sharedAppleEventManager]
432 setEventHandler:handler
433 andSelector:@selector(handleURLEvent:withReplyEvent:)
434 forEventClass:kInternetEventClass
435 andEventID:kAEGetURL];
436}
437
400void setupApplication_MacOS(void) { 438void setupApplication_MacOS(void) {
401 NSApplication *app = [NSApplication sharedApplication]; 439 NSApplication *app = [NSApplication sharedApplication];
402 //appearanceChanged_MacOS_([[app effectiveAppearance] name]);
403 /* Our delegate will override SDL's delegate. */ 440 /* Our delegate will override SDL's delegate. */
404 MyDelegate *myDel = [[MyDelegate alloc] initWithSDLDelegate:app.delegate]; 441 MyDelegate *myDel = [[MyDelegate alloc] initWithSDLDelegate:app.delegate];
405 [myDel setAppearance:[[app effectiveAppearance] name]]; 442 [myDel setAppearance:[[app effectiveAppearance] name]];
@@ -502,3 +539,7 @@ void handleCommand_MacOS(const char *cmd) {
502 } 539 }
503#endif 540#endif
504} 541}
542
543void log_MacOS(const char *msg) {
544 NSLog(@"%s", msg);
545}