- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMCCWebScriptWindowController.m
More file actions
Latest commit
314 lines (254 loc) · 9.09 KB
/
Copy pathMCCWebScriptWindowController.m
File metadata and controls
314 lines (254 loc) · 9.09 KB
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"
@interfaceMCC_PREFIXED_NAME(WebScriptWindowController) ()
@property (assign) IBOutletWebView *webView;
@property (strong) NSString *filePath;
@end
@implementationMCC_PREFIXED_NAME(WebScriptWindowController)
#pragma mark - Creation and Setup
- (id)init {
self = [superinit];
if (self){
NSBundle *myBundle = [NSBundlebundleForClass:[selfclass]];
if ([myBundle respondsToSelector:@selector(loadNibNamed:owner:topLevelObjects:)]) {
if (![myBundle loadNibNamed:[selfnibName] owner:selftopLevelObjects:nil]) {
NSLog( @"Warning: Failed to load nib" );
}
}
#if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_8
else {
if (![NSBundleloadNibNamed:[selfnibName] 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 {
[[selfwindow] center];
[self.webView setUIDelegate:self];
[self.webView setResourceLoadDelegate:self];
[self.webView setFrameLoadDelegate:self];
[[self.webView preferences] setJavaScriptEnabled:YES];
[[[self.webView mainFrame] frameView] setAllowsScrolling:NO];
[selfaddObserver:selfforKeyPath:@"pageController"options:0context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPathofObject:(id)objectchange:(NSDictionary *)changecontext:(void *)context {
if ([@"pageController"isEqualToString:keyPath]){
[[self.webView windowScriptObject] setValue:self.pageController forKey:@"pageController"];
}
else{
[superobserveValueForKeyPath: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 *)senderdidReceiveTitle:(NSString *)titleforFrame:(WebFrame *)frame{
if (sender.mainFrame == frame){
[self.window setTitle:title];
}
}
- (void)loadFile:(NSString*)fileNameofType:(NSString*)type {
self.filePath = nil;
NSBundle *thisBundle = [NSBundlebundleForClass:[selfclass]];
NSString *contentPath = [thisBundle resourcePath];
for (NSString *aLocalization in [thisBundle preferredLocalizations]) {
NSString *newPath = [contentPath stringByAppendingFormat:@"/%@.lproj/%@.%@",aLocalization,fileName,type];
if ([[NSFileManagerdefaultManager] fileExistsAtPath:newPath]) {
self.filePath = newPath;
break;
}
}
if (!self.filePath){
self.filePath = [contentPath stringByAppendingFormat:@"/en.lproj/%@.%@",fileName,type];
if (![[NSFileManagerdefaultManager] fileExistsAtPath:self.filePath]) {
self.filePath =[contentPath stringByAppendingFormat:@"/%@.%@",fileName,type];
}
}
NSString * html = [NSStringstringWithContentsOfFile: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:[NSCharacterSetwhitespaceCharacterSet]];
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 = [NSAppmainMenu].menuBarHeight;
CGFloat maxHeight = [NSScreenmainScreen].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:YESanimate:YES];
}
else{
[window setFrame:winFrame display:YESanimate:NO];
}
//NSLog(@"Setting Window size to:%@", NSStringFromSize(winFrame.size));
}
}
elseif ([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];
}
[selfloadFile:[pageName stringByDeletingPathExtension] ofType:extension];
}
#pragma mark - Added JS Methods
- (void)showWindowAndLoadURL:(NSURL*)url {
if (![[selfwindow] isVisible]) {
[[selfwindow] center];
}
[[self.webView mainFrame] loadRequest:[NSURLRequestrequestWithURL:url]];
}
- (void)openURL:(NSString*)urlString {
NSURL* url = [NSURLURLWithString:urlString];
if ([url host]){
[[NSWorkspacesharedWorkspace] openURL:url];
}
else {
[selfloadFile:[[url URLByDeletingPathExtension] absoluteString] ofType:[url pathExtension]];
}
}
- (void)log:(id)logString {
if (logString && [logString isKindOfClass:NSClassFromString(@"DOMNodeList")]){
NSMutableArray *list = [NSMutableArrayarrayWithCapacity:[(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 *)senderdidFinishLoadForFrame:(WebFrame *)frame {
[self.webView setHidden:NO];
if (![[selfwindow] isVisible]) {
[[selfwindow] center];
}
[NSAppactivateIgnoringOtherApps:YES];
[[selfwindow] makeKeyAndOrderFront:self];
}
- (void)webView:(WebView *)senderdidClearWindowObject:(WebScriptObject *)windowScriptObjectforFrame:(WebFrame *)frame {
[windowScriptObject setValue:selfforKey:@"windowController"];
[windowScriptObject setValue:selfforKey:@"console"];
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)sel {
if (sel == @selector(setPageControllerClassName:)) {
returnNO;
}
if (sel == @selector(log:)){
returnNO;
}
if (sel == @selector(openURL:)){
returnNO;
}
if (sel == @selector(closeWindow)){
returnNO;
}
if (sel == @selector(showEmbeddedPage:)){
returnNO;
}
returnYES;
}
+ (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";
}
returnnil;
}
+ (BOOL)isKeyExcludedFromWebScript:(constchar *)property {
returnYES;
}
+ (NSString *)webScriptNameForKey:(constchar *)name {
returnnil;
}
#pragma mark - Methods to Override
- (NSString*)nibName {
NSAssert(NO, @"Override nibName for your WebScriptWindowController");
returnnil;
}
#pragma mark - Class Methods
+ (instancetype)sharedInstance {
staticdispatch_once_t pred;
staticMCC_PREFIXED_NAME(WebScriptWindowController) *_sharedInstance = nil;
dispatch_once(&pred, ^{
_sharedInstance = [[(Class)selfalloc] init];
});
return _sharedInstance;
}
@end