-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgoui_osx.m.txt
261 lines (217 loc) · 7.64 KB
/
goui_osx.m.txt
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#import <IOKit/IOKitLib.h>
#import <IOKit/graphics/IOGraphicsLib.h>
#include <stdio.h>
extern void guiReadyCallback();
extern void guiWindowCloseCallback(int);
typedef struct ScreenList ScreenList;
struct ScreenList {
int left, top;
int width, height;
int usableLeft, usableTop;
int usableWidth, usableHeight;
int isPrimary;
ScreenList* next;
};
//
// Map of Window handle -> NSWindows.
//
NSMutableDictionary* windowLookup;
//
// Browser Delegate: Catches load notifications, and sets the window title based on document title.
//
@interface BrowserDelegate : WebView {
}
@end
@implementation BrowserDelegate
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
// Get the HTML page title and use it as the window title.
NSString *title = [sender stringByEvaluatingJavaScriptFromString:@"document.title"];
[[sender window] setTitle: title];
}
@end
BrowserDelegate* browserDelegate;
//
// Window Delegate: Catches window events.
//
@interface WindowDelegate : NSObject <NSWindowDelegate> {
}
@end
@implementation WindowDelegate
-(void)windowWillClose:(NSNotification*)notification {
NSWindow* window = (NSWindow*)[notification object];
NSArray *windowIds = [windowLookup allKeysForObject: window];
if ([windowIds count] > 0) {
int windowId = [[windowIds objectAtIndex:0] intValue];
guiWindowCloseCallback(windowId);
}
[[NSApplication sharedApplication] stopModal];
}
@end
WindowDelegate* windowDelegate;
//
// App Delegate: Catches app ready notification, informs Go program that the GUI is ready to rock. Opens new Windows.
//
@interface AppDelegate : NSObject <NSApplicationDelegate> {
}
@end
@implementation AppDelegate
-(void)applicationDidFinishLaunching:(NSNotification *)aNotification {
guiReadyCallback();
}
-(void)openWindow:(const char*)url withId:(int)id withFlags:(int)flags asModal:(bool)modal {
NSString* nsurl = [NSString stringWithCString:url encoding:NSUTF8StringEncoding];
// Create a window.
NSWindow* window = [[NSWindow alloc]
initWithContentRect: NSMakeRect(0, 0, 500, 400)
styleMask: flags
backing:NSBackingStoreBuffered
defer:NO
];
[window makeKeyAndOrderFront:nil];
[window setDelegate:windowDelegate];
if (modal)
[window setLevel:NSModalPanelWindowLevel];
// Determine an autosave name based on the URL.
NSError* error = nil;
NSString* basename = [nsurl lastPathComponent];
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@"[^a-z0-9]" options:NSRegularExpressionCaseInsensitive error:&error];
NSString* autosaveName = [regex stringByReplacingMatchesInString:basename options:0 range:NSMakeRange(0, [basename length]) withTemplate:@""];
[window center];
[window setFrameAutosaveName:autosaveName];
// Create a WebView, and load the specified URL.
NSView *superview = [window contentView];
NSRect frame = NSMakeRect(0, 0, 500, 400);
WebView* webView = [[WebView alloc] initWithFrame:frame];
[superview addSubview:webView];
[webView setMainFrameURL:nsurl];
[webView setFrameLoadDelegate: browserDelegate];
// Constrain the WebView to 100% fill the surrounding window.
[webView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSDictionary *viewBindings = NSDictionaryOfVariableBindings(webView);
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[webView]|" options:0 metrics:nil views:viewBindings]];
[superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[webView]|" options:0 metrics:nil views:viewBindings]];
// Store a reference to this window for later lookups.
windowLookup[@(id)] = window;
NSString* wkey = [NSString stringWithFormat:@"w%p", window];
windowLookup[wkey] = @(id);
}
-(void)closeWindow:(NSWindow*)window {
[window close];
}
@end
AppDelegate* appDelegate;
//
// OpenWindow; exported method, callable from Go code. Calls App Delegate's open window function in the main NSApp thread. Returns an int window handle.
//
void OpenWindow(int windowId, const char* url, int flags, bool modal) {
dispatch_sync(dispatch_get_main_queue(), ^{
[appDelegate openWindow:url withId:windowId withFlags:flags asModal:modal];
});
}
//
// CloseWindow; exported method, closes the specified window by id.
//
void CloseWindow(int id) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
[appDelegate performSelectorOnMainThread:@selector(closeWindow:) withObject:window waitUntilDone:YES];
[windowLookup removeObjectForKey:@(id)];
}
}
//
// StartApp; exported method, prepares an NSApp to manage all Cocoa windows. Never returns.
//
int StartApp() {
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
appDelegate = [[AppDelegate new] autorelease];
[NSApp setDelegate: appDelegate];
browserDelegate = [[BrowserDelegate new] autorelease];
windowDelegate = [[WindowDelegate new] autorelease];
windowLookup = [NSMutableDictionary dictionary];
[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"WebKitDeveloperExtras",
[NSNumber numberWithBool:YES], @"WebKitScriptDebuggerEnabled",
nil
]];
// Create a menu bar.
id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc]
initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"]
autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];
[NSApp activateIgnoringOtherApps:YES];
[NSApp run];
return 0;
}
//
// StopApp; exported method, used to stop running the nsapp.
//
void StopApp() {
[NSApp terminate:0];
}
void SetWindowTitle(int id, const char* title) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[window setTitle:[NSString stringWithCString:title encoding:NSUTF8StringEncoding]];
});
}
}
void SetWindowSize(int id, int width, int height) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
NSRect frame = [window frame];
frame.size.width = width;
frame.size.height = height;
[window setFrame:frame display:YES animate:NO];
});
}
}
void SetWindowPosition(int id, int left, int top) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
// Cocoa is weird. Coords are upside-down. Compensate for it.
NSScreen* mainScreen = [NSScreen mainScreen];
NSRect frame = [window frame];
frame.origin.x = left;
frame.origin.y = mainScreen.visibleFrame.size.height - top - frame.size.height;
[window setFrame:frame display:YES animate:NO];
});
}
}
void GetScreenSize(int* width, int* height) {
NSScreen* main = [NSScreen mainScreen];
*width = main.visibleFrame.size.width;
*height = main.visibleFrame.size.height;
}
void RememberWindowGeometry(int id, const char* key) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[window setFrameAutosaveName:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
});
}
}
void RunModal(int id) {
NSWindow* window = (NSWindow*)[windowLookup objectForKey:@(id)];
if (window != nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[NSApp runModalForWindow: window];
});
}
}