Skip to content

Commit

Permalink
Merge pull request #118 from getlantern/ox/issue109
Browse files Browse the repository at this point in the history
When launching app window on macOS, make application a foreground app…
  • Loading branch information
atavism authored Jan 7, 2020
2 parents d922a91 + d5de106 commit 8503b34
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
example/example
example/main
example/main.exe
*~
*.swp
*.exe
Expand Down
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func onReady() {
mEnabled.SetTitle("Disabled")
mEnabled.Disable()
case <-mUrl.ClickedCh:
systray.ShowAppWindow("https://www.getlantern.org")
systray.ShowAppWindow("https://www.github.com/getlantern/lantern")
case <-mToggle.ClickedCh:
if shown {
mQuitOrig.Hide()
Expand Down
19 changes: 12 additions & 7 deletions systray_browser_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "systray.h"

NSWindowController *windowController = nil;
NSWindow *window = nil;
WKWebView *webView = nil;

void configureAppWindow(char* title, int width, int height)
Expand All @@ -12,15 +13,17 @@ void configureAppWindow(char* title, int width, int height)
return;
}

NSApplication *app = [NSApplication sharedApplication];
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
[app activateIgnoringOtherApps:YES];

NSRect frame = NSMakeRect(0, 0, width, height);
int mask = NSWindowStyleMaskTitled | NSWindowStyleMaskResizable | NSWindowStyleMaskClosable;
NSWindow *window =
[[NSWindow alloc] initWithContentRect:frame
styleMask:mask
backing:NSBackingStoreBuffered
defer:NO];
window = [[NSWindow alloc] initWithContentRect:frame
styleMask:mask
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:[[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil];
[window center];

NSView *contentView = [window contentView];
Expand All @@ -46,6 +49,7 @@ void configureAppWindow(char* title, int width, int height)

// Window controller:
windowController = [[NSWindowController alloc] initWithWindow:window];

free(title);
}

Expand All @@ -55,12 +59,13 @@ void doShowAppWindow(char* url)
// no app window to open
return;
}

id nsURL = [NSURL URLWithString:[[NSString alloc] initWithUTF8String:url]];
id req = [[NSURLRequest alloc] initWithURL: nsURL
cachePolicy: NSURLRequestUseProtocolCachePolicy
timeoutInterval: 5];
[webView loadRequest:req];
[windowController.window orderFrontRegardless];
[windowController showWindow:window];
free(url);
}

Expand Down
2 changes: 0 additions & 2 deletions systray_darwin.m
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ - (void) quit

@end

void configureAppWindow(char* title, int width, int height);

int nativeLoop(char* title, int width, int height) {
AppDelegate *delegate = [[AppDelegate alloc] init];
[[NSApplication sharedApplication] setDelegate:delegate];
Expand Down

0 comments on commit 8503b34

Please sign in to comment.