-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathwebview_darwin.m
204 lines (153 loc) · 6.16 KB
/
webview_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
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
#define WEBVIEW_HINT_NONE 0 // Width and height are default size
#define WEBVIEW_HINT_MIN 1 // Width and height are minimum bounds
#define WEBVIEW_HINT_MAX 2 // Width and height are maximum bounds
#define WEBVIEW_HINT_FIXED 3 // Window size can not be changed by a user
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
#import <WebKit/WebKit.h>
@interface AppWebView : WKWebView
@end
@implementation AppWebView
- (BOOL)performKeyEquivalent:(NSEvent *)event
{
if ([event modifierFlags] & NSEventModifierFlagCommand) {
// The command key is the ONLY modifier key being pressed.
if ([[event charactersIgnoringModifiers] isEqualToString:@"x"]) {
return [NSApp sendAction:@selector(cut:) to:[[self window] firstResponder] from:self];
} else if ([[event charactersIgnoringModifiers] isEqualToString:@"c"]) {
return [NSApp sendAction:@selector(copy:) to:[[self window] firstResponder] from:self];
} else if ([[event charactersIgnoringModifiers] isEqualToString:@"v"]) {
return [NSApp sendAction:@selector(paste:) to:[[self window] firstResponder] from:self];
} else if ([[event charactersIgnoringModifiers] isEqualToString:@"a"]) {
return [NSApp sendAction:@selector(selectAll:) to:[[self window] firstResponder] from:self];
}
}
return [super performKeyEquivalent:event];
}
@end
@protocol MessageDelegate <NSObject>
-(void)onMessage:(id)message;
@end
@interface WebViewApp : NSObject<NSApplicationDelegate,WKScriptMessageHandler> {
NSWindow *_window;
NSWindowController *_controller;
AppWebView *_webview;
WKUserContentController *_manager;
BOOL _hide;
id<MessageDelegate> _delegate;
}
-(id) initApp:(NSInteger)width height:(NSInteger)height hide:(BOOL)hide debug:(BOOL)debug;
-(void) close;
-(void) hide;
-(void) show;
-(void) run;
-(void) terminate;
-(void)setDelegate:(id<MessageDelegate>)delegate;
-(void)setTitle:(NSString*)title;
-(void) setSize:(NSInteger)width height:(NSInteger)height hints:(NSInteger)hints;
-(void) initJS:(NSString*)js;
-(void) evalJS:(NSString*)js;
-(void) navigate:(NSString *)url;
-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender;
-(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message;
@end
@implementation WebViewApp {
}
-(id) initApp:(NSInteger)width height:(NSInteger)height hide:(BOOL)hide debug:(BOOL)debug {
id app = [NSApplication sharedApplication];
[app setActivationPolicy:YES];
_hide = hide;
[app setDelegate:self];
_window = [[NSWindow alloc] initWithContentRect:CGRectMake(0, 0, width, height) styleMask:0 backing:NSBackingStoreBuffered defer:NO];
_controller = [[NSWindowController alloc]initWithWindow:_window];
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
WKPreferences *preference = [[WKPreferences alloc]init];
if (debug) {
[preference setValue:@YES forKey:@"developerExtrasEnabled"];
}
[preference setValue:@YES forKey:@"fullScreenEnabled"];
[preference setValue:@YES forKey:@"javaScriptCanAccessClipboard"];
[preference setValue:@YES forKey:@"DOMPasteAllowed"];
config.preferences = preference;
_manager = [[WKUserContentController alloc] init];
[_manager addScriptMessageHandler:self name:@"external"];
config.userContentController = _manager;
_webview = [[AppWebView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) configuration:config];
[self initJS:@"window.external = { invoke: function(s) {window.webkit.messageHandlers.external.postMessage(s)}}"];
[_window setContentView:_webview];
[_window makeKeyAndOrderFront:nil];
return self;
// Application
};
-(void) initJS:(NSString *)js {
dispatch_async(dispatch_get_main_queue(),^{
[self->_manager addUserScript:[[WKUserScript alloc] initWithSource:js injectionTime:WKUserScriptInjectionTimeAtDocumentStart forMainFrameOnly:YES]];
});
}
-(void) evalJS:(NSString *)js{
dispatch_async(dispatch_get_main_queue(),^{
[self->_webview evaluateJavaScript:js completionHandler:nil];
});
}
-(void) setDelegate:(id<MessageDelegate>)delegate{
_delegate = delegate;
}
-(void) setTitle:(NSString *)title{
[_window setTitle:title];
}
-(void) setSize:(NSInteger)width height:(NSInteger)height hints:(NSInteger)hints{
NSUInteger style = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable;
if (hints != WEBVIEW_HINT_FIXED) {
style = style | NSWindowStyleMaskResizable;
}
[_window setStyleMask:style];
if (hints == WEBVIEW_HINT_MIN) {
[_window setContentMinSize:CGSizeMake(width, height)];
}else if (hints == WEBVIEW_HINT_MAX){
[_window setContentMaxSize:CGSizeMake(width, height)];
}else{
[_window setFrame:CGRectMake(0, 0, width, height) display:YES animate:NO];
}
[_window center];
}
-(void) close {
dispatch_async(dispatch_get_main_queue(),^{
[self->_window close];
});
}
-(void) hide {
dispatch_async(dispatch_get_main_queue(),^{
[self->_window orderOut:self->_window];
});
}
-(void) show{
dispatch_async(dispatch_get_main_queue(),^{
id app = [NSApplication sharedApplication];
[app activateIgnoringOtherApps:YES];
[self->_controller showWindow:self->_window];
[self->_window makeKeyAndOrderFront:nil];
});
}
-(void) navigate:(NSString *)url{
id request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
[_webview loadRequest:request];
}
-(void) terminate {
id app = [NSApplication sharedApplication];
[self close];
[app terminate:nil];
}
-(void) run {
id app = [NSApplication sharedApplication];
[app activateIgnoringOtherApps:YES];
[app run];
}
-(BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender{
return !_hide;
}
- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
if (_delegate != nil){
[_delegate onMessage:message];
}
}
@end;