-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCCWebScriptWindowController.m
314 lines (254 loc) · 9.09 KB
/
MCCWebScriptWindowController.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
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
//
// MCCWebScriptWindowController.m
// MailCommon
//
// Created by smorr on 2013-09-24.
// Copyright (c) 2013 Indev Software. All rights reserved.
//
#import "MCCWebScriptWindowController.h"
#import "MCCWebScriptPageController.h"
@interface MCC_PREFIXED_NAME(WebScriptWindowController) ()
@property (assign) IBOutlet WebView *webView;
@property (strong) NSString *filePath;
@end
@implementation MCC_PREFIXED_NAME(WebScriptWindowController)
#pragma mark - Creation and Setup
- (id)init {
self = [super init];
if (self){
NSBundle *myBundle = [NSBundle bundleForClass:[self class]];
if ([myBundle respondsToSelector:@selector(loadNibNamed:owner:topLevelObjects:)]) {
if (![myBundle loadNibNamed:[self nibName] owner:self topLevelObjects:nil]) {
NSLog( @"Warning: Failed to load nib" );
}
}
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8
else {
if (![NSBundle loadNibNamed:[self nibName] owner:self]) {
NSLog( @"Warning: Failed to load nib" );
}
}
#endif
}
return self;
}
#if !__has_feature(objc_arc)
- (void)dealloc {
self.filePath = nil;
self.pageController = nil;
MCC_DEALLOC();
}
#endif
- (void)awakeFromNib {
[[self window] center];
[self.webView setUIDelegate:self];
[self.webView setResourceLoadDelegate:self];
[self.webView setFrameLoadDelegate:self];
[[self.webView preferences] setJavaScriptEnabled:YES];
[[[self.webView mainFrame] frameView] setAllowsScrolling:NO];
[self addObserver:self forKeyPath:@"pageController" options:0 context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([@"pageController" isEqualToString:keyPath]){
[[self.webView windowScriptObject] setValue:self.pageController forKey:@"pageController"];
}
else{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)setPageControllerClassName:(NSString*)controllerClassName {
Class aClassName = NSClassFromString(controllerClassName);
if (aClassName){
MCC_PREFIXED_NAME(WebScriptPageController) *controllerObject= [[aClassName alloc] init];
if (controllerObject && [controllerObject isKindOfClass:[MCC_PREFIXED_NAME(WebScriptPageController) class]]){
self.pageController = controllerObject;
controllerObject.webView = self.webView;
[controllerObject initPage];
}
else{
NSLog(@"ERROR Class %@ does not inherit from %@!", controllerClassName, [MCC_PREFIXED_NAME(WebScriptPageController) class]);
}
MCC_RELEASE(controllerObject);
}
else{
NSLog(@"ERROR cannot find Class %@!",controllerClassName);
}
}
- (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(WebFrame *)frame{
if (sender.mainFrame == frame){
[self.window setTitle:title];
}
}
- (void)loadFile:(NSString*)fileName ofType:(NSString*)type {
self.filePath = nil;
NSBundle *thisBundle = [NSBundle bundleForClass:[self class]];
NSString *contentPath = [thisBundle resourcePath];
for (NSString *aLocalization in [thisBundle preferredLocalizations]) {
NSString *newPath = [contentPath stringByAppendingFormat:@"/%@.lproj/%@.%@",aLocalization,fileName,type];
if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
self.filePath = newPath;
break;
}
}
if (!self.filePath){
self.filePath = [contentPath stringByAppendingFormat:@"/en.lproj/%@.%@",fileName,type];
if (![[NSFileManager defaultManager] fileExistsAtPath:self.filePath]) {
self.filePath =[contentPath stringByAppendingFormat:@"/%@.%@",fileName,type];
}
}
NSString * html = [NSString stringWithContentsOfFile:self.filePath encoding:NSUTF8StringEncoding error:nil];
NSArray *lines = [html componentsSeparatedByString:@"\n"];
for (NSString *aLine in lines) {
NSRange configRange = [aLine rangeOfString:@"<!--config--"];
if (configRange.location != NSNotFound) {
NSString *configString = [[aLine substringFromIndex:(configRange.location + configRange.length)] stringByReplacingOccurrencesOfString:@"-->" withString:@""];
if ([configString hasPrefix:@"size:"]) {
NSWindow *window = self.window;
configString = [[configString stringByReplacingOccurrencesOfString:@"size:" withString:@""] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSSize size = NSSizeFromString(configString);
NSRect winFrame = [window frame];
NSSize winContentSize = [[self.window contentView] frame].size;
CGFloat chromeHeightDifference = winFrame.size.height - winContentSize.height;
CGFloat newWindowHeight = size.height + chromeHeightDifference;
// Don't get taller than the main screen
CGFloat menuBarHeight = [NSApp mainMenu].menuBarHeight;
CGFloat maxHeight = [NSScreen mainScreen].frame.size.height - menuBarHeight;
if (newWindowHeight > maxHeight) {
newWindowHeight = maxHeight;
}
winFrame.origin.x -= floorf((size.width - winFrame.size.width) / 2.0f);
winFrame.size.width = size.width;
winFrame.origin.y -= newWindowHeight - winFrame.size.height;
winFrame.size.height = newWindowHeight;
// Stay below the menu
if (winFrame.origin.y < menuBarHeight) {
winFrame.origin.y = menuBarHeight;
}
if ([window isVisible]){
[window setFrame:winFrame display:YES animate:YES];
}
else{
[window setFrame:winFrame display:YES animate:NO];
}
//NSLog(@"Setting Window size to:%@", NSStringFromSize(winFrame.size));
}
}
else if ([aLine rangeOfString:@"</head>"].location != NSNotFound) {
break;
}
}
if (self.filePath) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self.webView setMainFrameURL:self.filePath];
self.pageController = nil;
});
}
//NSLog(@"Window size is:%@", NSStringFromSize([self.window frame].size));
}
- (void)closeWindow {
[[self.webView window] close];
}
- (void)showEmbeddedPage:(NSString *)pageName {
NSString *extension = @"html";
if ([[pageName pathExtension] length] > 0) {
extension = [pageName pathExtension];
}
[self loadFile:[pageName stringByDeletingPathExtension] ofType:extension];
}
#pragma mark - Added JS Methods
- (void)showWindowAndLoadURL:(NSURL*)url {
if (![[self window] isVisible]) {
[[self window] center];
}
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url]];
}
- (void)openURL:(NSString*)urlString {
NSURL* url = [NSURL URLWithString:urlString];
if ([url host]){
[[NSWorkspace sharedWorkspace] openURL:url];
}
else {
[self loadFile:[[url URLByDeletingPathExtension] absoluteString] ofType:[url pathExtension]];
}
}
- (void)log:(id)logString {
if (logString && [logString isKindOfClass:NSClassFromString(@"DOMNodeList")]){
NSMutableArray *list = [NSMutableArray arrayWithCapacity:[(DOMNodeList*)logString length]];
for (NSInteger i = 0; i<[(DOMNodeList*)logString length]; i++){
[list addObject:[logString item:(int)i]];
}
NSLog(@"Javascript Log: %@", list);
}
else
NSLog(@"Javascript Log: %@", logString);
}
#pragma mark - WebView Methods
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
[self.webView setHidden:NO];
if (![[self window] isVisible]) {
[[self window] center];
}
[NSApp activateIgnoringOtherApps:YES];
[[self window] makeKeyAndOrderFront:self];
}
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowScriptObject forFrame:(WebFrame *)frame {
[windowScriptObject setValue:self forKey:@"windowController"];
[windowScriptObject setValue:self forKey:@"console"];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
if (sel == @selector(setPageControllerClassName:)) {
return NO;
}
if (sel == @selector(log:)){
return NO;
}
if (sel == @selector(openURL:)){
return NO;
}
if (sel == @selector(closeWindow)){
return NO;
}
if (sel == @selector(showEmbeddedPage:)){
return NO;
}
return YES;
}
+ (NSString *)webScriptNameForSelector:(SEL)sel {
if (sel == @selector(setPageControllerClassName:)) {
return @"setPageControllerClassName";
}
if (sel == @selector(log:)){
return @"log";
}
if (sel == @selector(openURL:)){
return @"openURL";
}
if (sel == @selector(closeWindow)){
return @"closeWindow";
}
if (sel == @selector(showEmbeddedPage:)){
return @"showEmbeddedPage";
}
return nil;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)property {
return YES;
}
+ (NSString *)webScriptNameForKey:(const char *)name {
return nil;
}
#pragma mark - Methods to Override
- (NSString*)nibName {
NSAssert(NO, @"Override nibName for your WebScriptWindowController");
return nil;
}
#pragma mark - Class Methods
+ (instancetype)sharedInstance {
static dispatch_once_t pred;
static MCC_PREFIXED_NAME(WebScriptWindowController) *_sharedInstance = nil;
dispatch_once(&pred, ^{
_sharedInstance = [[(Class)self alloc] init];
});
return _sharedInstance;
}
@end