-
Notifications
You must be signed in to change notification settings - Fork 3
/
provider_macos.go
360 lines (302 loc) · 10.4 KB
/
provider_macos.go
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// +build darwin
// +build amd64,!sim
package goui
/*
#cgo darwin CFLAGS: -x objective-c
#cgo darwin LDFLAGS: -framework Cocoa -framework WebKit
#include <stdlib.h>
#include <string.h>
#include <Cocoa/Cocoa.h>
#include <mach-o/dyld.h>
#include <WebKit/WebKit.h>
#include <objc/runtime.h>
#include "provider.h"
extern void menuClicked(_GoString_ s);
extern void handleClientReq(const char* s);
//extern void goLog(_GoString_ s);
@interface GoUIMessageHandler : NSObject <WKScriptMessageHandler> {
}
@end
@implementation GoUIMessageHandler
- (void)userContentController:(WKUserContentController *)userContentController
didReceiveScriptMessage:(WKScriptMessage *)message {
goUILog("didReceiveScriptMessage: %s\n",[message.name UTF8String]);
if ([message.name isEqualToString:@"goui"]) {
const char* str = [message.body UTF8String];
goUILog("Received event %s\n", str);
handleClientReq(str);
//(_GoString_){str, strlen(str)}
}
}
@end
//menu
@interface CustomAction : NSObject
@end
@implementation CustomAction
- (void)action:(id)sender {
//NSLog(@"click menu:%@",[sender representedObject]);
const char* str = [[sender representedObject] UTF8String];
//menuClicked((_GoString_){str, strlen(str)});
menuClicked((_GoString_){str, strlen(str)});
goUILog("click menu: %s\n",str);
}
@end
@interface GoUIMenu : NSObject
@end
@implementation GoUIMenu
static CustomAction* customAction;
static NSDictionary* actionMap;
+ (void)initialize {
customAction = [[CustomAction alloc] init];
actionMap = [NSDictionary dictionaryWithObjectsAndKeys:
//app menu
[NSValue valueWithPointer:@selector(orderFrontStandardAboutPanel:)], @"about",
[NSValue valueWithPointer:@selector(hide:)], @"hide",
[NSValue valueWithPointer:@selector(hideOtherApplications:)], @"hideothers",
[NSValue valueWithPointer:@selector(unhideAllApplications:)], @"unhide",
[NSValue valueWithPointer:@selector(terminate:)], @"quit",
nil];
}
NSString * utf8(const char* cs) {
NSString *ns = @"";
if(cs) {
ns = [NSString stringWithUTF8String:cs];
}
return ns;
}
NSMenuItem* createMenuItem(MenuDef def) {
NSString *title = utf8(def.title);
NSString *key = utf8(def.key);
SEL act = NULL;
if(def.menuType == standard) {
id pointer = [actionMap objectForKey:[NSString stringWithUTF8String:def.action]];
if(pointer) {
act = [pointer pointerValue];
}
} else if(def.menuType == custom) {
act = @selector(action:);
}
NSMenuItem *item = [[[NSMenuItem alloc] initWithTitle:title
action: act
keyEquivalent:key]
autorelease];
if(def.menuType == custom) {
[item setRepresentedObject:[NSString stringWithUTF8String:def.action]];
[item setTarget:customAction];
}
return item;
}
NSMenuItem* createMenu(MenuDef def) {
NSString *title = utf8(def.title);
NSMenu *menu = [[[NSMenu alloc] initWithTitle:title] autorelease];
NSMenuItem *item = createMenuItem(def);
[item setSubmenu:menu];
for(int i=0;i<def.childrenCount;i++) {
buildSubMenu(menu,def.children[i]);
}
return item;
}
void buildSubMenu(NSMenu* parent,MenuDef def) {
goUILog("build menu, type: %d\n",def.menuType);
if(def.menuType == container) {
[parent addItem:createMenu(def)];
} else if(def.menuType == separator) {
[parent addItem:[NSMenuItem separatorItem]];
} else {
[parent addItem:createMenuItem(def)];
}
}
+(void)buildMenu:(MenuDef[])defs count: (int)count {
NSMenu *menubar = [[[NSMenu alloc] initWithTitle:@"menu bar"] autorelease];
[NSApp setMainMenu:menubar];
goUILog("buildMenu count: %d\n",count);
for(int i=0; i<count;i++) {
@autoreleasepool {
buildSubMenu(menubar,defs[i]);
}
}
}
@end
//window
@interface WindowDelegate : NSObject <NSWindowDelegate> {
@private
NSView* _view;
}
@property (nonatomic, assign) NSView* view;
@end
@implementation WindowDelegate
@synthesize view = _view;
- (void)windowDidResize:(NSNotification *)notification {
goUILog("windowDidResize\n");
}
- (void)windowDidMiniaturize:(NSNotification *)notification{
goUILog("windowDidMiniaturize\n");
}
- (void)windowDidEnterFullScreen:(NSNotification *)notification {
goUILog("windowDidEnterFullScreen\n");
}
- (void)windowDidExitFullScreen:(NSNotification *)notification {
goUILog("windowDidExitFullScreen\n");
}
- (void)windowDidBecomeKey:(NSNotification *)notification {
goUILog("Window: become key\n");
}
- (void)windowDidBecomeMain:(NSNotification *)notification {
goUILog("Window: become main\n");
}
- (void)windowDidResignKey:(NSNotification *)notification {
goUILog("Window: resign key\n");
}
- (void)windowDidResignMain:(NSNotification *)notification {
goUILog("Window: resign main\n");
}
- (void)windowWillClose:(NSNotification *)notification {
[NSAutoreleasePool new];
goUILog("NSWindowDelegate::windowWillClose\n");
[NSApp terminate:NSApp];
}
@end
@interface GoUIWindow : NSObject {
@private
WKWebView* webView;
}
@end
@implementation GoUIWindow
//@synthesize webView = webView_;
//static WKWebView* webView;
- (void)create:(struct WindowSettings)settings {
@autoreleasepool {
WindowDelegate* delegate = [[WindowDelegate alloc] init];
NSRect rect = NSMakeRect(0, 0, settings.width, settings.height);
id window = [[NSWindow alloc]
initWithContentRect:rect
styleMask:(NSWindowStyleMaskTitled |
NSWindowStyleMaskClosable |
NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskResizable |
NSWindowStyleMaskUnifiedTitleAndToolbar )
backing:NSBackingStoreBuffered
defer:NO];
delegate.view = [window contentView];
[window setDelegate:delegate];
[window cascadeTopLeftFromPoint:NSMakePoint(settings.left,settings.top)];
[window setTitle:[NSString stringWithUTF8String:settings.title]];
GoUIMessageHandler* handler = [[GoUIMessageHandler alloc] init];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addScriptMessageHandler:handler name:@"goui"];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
webView = [[WKWebView alloc] initWithFrame:rect configuration:configuration];
[webView.configuration.preferences setValue:@YES forKey:@"developerExtrasEnabled"];
NSString *index = [NSString stringWithUTF8String:settings.index];
if([index hasPrefix:@"http"]) {
NSURL *nsURL = [NSURL URLWithString:index];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:nsURL];
[webView loadRequest:requestObj];
} else {
NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
NSString *dir = [bundlePath stringByAppendingPathComponent:[NSString stringWithUTF8String:settings.webDir]];
index = [dir stringByAppendingPathComponent:index];
goUILog("bundlePath:%s",[bundlePath UTF8String]);
goUILog("dir:%s",[dir UTF8String]);
goUILog("index:%s",[index UTF8String]);
NSURL *nsURL = [NSURL fileURLWithPath:index isDirectory:false];
NSURL *nsDir = [NSURL fileURLWithPath:dir isDirectory:true];
[webView loadFileURL:nsURL allowingReadAccessToURL:nsDir];
}
[[window contentView] addSubview:webView];
[window makeKeyAndOrderFront:nil];
}
}
-(void) evaluateJS:(NSString*)script {
goUILog("evalue:%s",[script UTF8String]);
[webView evaluateJavaScript:script completionHandler:^(id _Nullable response, NSError * _Nullable error) {
//goUILog("response:%s,error:%s",[response UTF8String],[error UTF8String]);
}];
}
@end
//app
@interface ApplicationDelegate : NSObject <NSApplicationDelegate> {
@private
MenuDef* _menuDefs;
int _menuCount;
}
@property (nonatomic, assign) struct MenuDef* menuDefs;
@property (assign) int menuCount;
@end
@implementation ApplicationDelegate
-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
{
goUILog("applicationWillFinishLaunching\n");
[NSApplication sharedApplication];
goUILog("_menuCount: %d\n",_menuCount);
if(_menuCount!=0) {
[GoUIMenu buildMenu:_menuDefs count:_menuCount ];
}
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
}
-(void)applicationDidFinishLaunching:(NSNotification *)notification
{
goUILog("applicationDidFinishLaunching\n");
[NSApplication sharedApplication];
[NSApp activateIgnoringOtherApps:YES];
}
@end
@interface GoUIApp : NSObject
@end
@implementation GoUIApp
static GoUIWindow* window;
+(void)initialize {}
+(void)start:(WindowSettings)settings menuDefs:(struct MenuDef[])menuDefs menuCount: (int)menuCount {
@autoreleasepool {
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
ApplicationDelegate* appDelegate = [[ApplicationDelegate alloc] init];
appDelegate.menuDefs = menuDefs;
appDelegate.menuCount = menuCount;
goUILog("menuCount: %d\n",menuCount);
[NSApp setDelegate:appDelegate];
window = [[GoUIWindow alloc] init];
[window create:settings];
goUILog("run loop");
[NSApp run];
//run();
}
}
+(void)evaluateJS:(NSString*)js {
[window evaluateJS:js];
}
+(void)exit {
[NSApp terminate:NSApp];
}
@end
void create(WindowSettings settings, MenuDef* menuDefs, int menuCount) {
[GoUIApp start:settings menuDefs:menuDefs menuCount:menuCount];
}
void invokeJS(const char *js,int fromMainThread) {
goUILog("invokeJS async:%s",js);
NSString* script = [NSString stringWithUTF8String:js];
if(fromMainThread) {
[GoUIApp evaluateJS:script];
} else {
dispatch_async_and_wait(dispatch_get_main_queue(),^{
[GoUIApp evaluateJS:script];
});
}
}
void exitApp() {
[GoUIApp exit];
}
*/
import "C"
func cCreate(cs C.WindowSettings, cMenuDefs *C.MenuDef, count C.int) {
C.create(cs, cMenuDefs, count)
}
func cActivate() {
}
func cInvokeJS(js *C.char, fromMainThread C.int) {
C.invokeJS(js, fromMainThread)
}
func cExit() {
C.exitApp()
}