forked from getlantern/systray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystray_browser_darwin.m
72 lines (65 loc) · 2.19 KB
/
systray_browser_darwin.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#include "systray.h"
NSWindowController *windowController = nil;
WKWebView *webView = nil;
void configureAppWindow(char* title, int width, int height)
{
if (windowController != nil) {
// already configured, ignore
return;
}
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 setTitle:[[NSString alloc] initWithUTF8String:title]];
[window makeKeyAndOrderFront:nil];
[window center];
NSView *contentView = [window contentView];
webView = [[WKWebView alloc] initWithFrame:[contentView bounds]];
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
[contentView addSubview:webView];
[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:webView
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeWidth
multiplier:1
constant:0]];
[contentView addConstraint:
[NSLayoutConstraint constraintWithItem:webView
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:contentView
attribute:NSLayoutAttributeHeight
multiplier:1
constant:0]];
// Window controller:
windowController = [[NSWindowController alloc] initWithWindow:window];
free(title);
}
void doShowAppWindow(char* url)
{
if (windowController == nil) {
// 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];
free(url);
}
void showAppWindow(char* url)
{
dispatch_async(dispatch_get_main_queue(), ^{
doShowAppWindow(url);
});
}