-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTransparentWebViewAppDelegate.m
363 lines (279 loc) · 11.1 KB
/
TransparentWebViewAppDelegate.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
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
361
362
363
//
// TransparentWebViewAppDelegate.m
// TransparentWebView
//
// Created by Dirk van Oosterbosch on 22-12-10.
// Copyright 2010 IR labs. All rights reserved.
//
#import "TransparentWebViewAppDelegate.h"
#import "WebViewWindow.h"
#import "PreferenceController.h"
NSString *const TWVLocationUrlKey = @"WebViewLocationUrl";
NSString *const TWVBorderlessWindowKey = @"OpenBorderlessWindow";
NSString *const TWVDrawCroppedUnderTitleBarKey = @"DrawCroppedUnderTitleBar";
NSString *const TWVMainTransparantWindowFrameKey = @"MainTransparentWindow";
CGFloat const titleBarHeight = 22.0f;
@implementation TransparentWebViewAppDelegate
@synthesize window, theWebView;
@synthesize borderlessWindowMenuItem, cropUnderTitleBarMenuItem;
@synthesize locationSheet, urlString;
@synthesize preferenceController;
- (id) init {
[super init];
// Register the Defaults in the Preferences
NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
[defaultValues setObject:@"http://localhost/" forKey:TWVLocationUrlKey];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:TWVBorderlessWindowKey];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:TWVDrawCroppedUnderTitleBarKey];
[defaultValues setObject:[NSNumber numberWithBool:NO] forKey:TWVShouldAutomaticReloadKey];
[defaultValues setObject:[NSNumber numberWithInt:15] forKey:TWVAutomaticReloadIntervalKey];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
// Set the url from the Preferences file
self.urlString = [[NSUserDefaults standardUserDefaults] objectForKey:TWVLocationUrlKey];
// Register for Preference Changes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAutomaticReloadChange:)
name:TWVAutomaticReloadChangedNotification
object:nil];
return self;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
NSLog(@"TransparentWebView app got launched ...");
[self loadUrlString:self.urlString IntoWebView:self.theWebView];
// Deal with the borderless and crop under title bar settings
BOOL borderlessState = [[[NSUserDefaults standardUserDefaults] objectForKey:TWVBorderlessWindowKey] boolValue];
BOOL cropUnderTitleState = [[[NSUserDefaults standardUserDefaults] objectForKey:TWVDrawCroppedUnderTitleBarKey] boolValue];
// Set the state of the menu items
[self setBorderlessWindowMenuItemState:borderlessState];
[self setCropUnderTitleBarMenuItemState:cropUnderTitleState];
// Make us the delegate of the Main Window
[window setDelegate:self];
// Set the window type and the content frame
if (borderlessState) {
NSRect borderlessContentRect = [[window contentView] frame];
if (cropUnderTitleState) {
borderlessContentRect = [window frame];
}
[self replaceWindowWithBorderlessWindow:YES WithContentRect:borderlessContentRect];
} else {
if (cropUnderTitleState) {
[self cropContentUnderTitleBar:YES];
}
}
// Start a timer if the Transparent Web View is set to reload with a given interval
[self resetAutomaticReloadTimer];
}
#pragma mark -
#pragma mark Location Sheet
- (IBAction)showLocationSheet:(id)sender {
//
[NSApp beginSheet:locationSheet
modalForWindow:window
modalDelegate:nil
didEndSelector:NULL
contextInfo:NULL];
}
- (IBAction)endLocationSheet:(id)sender {
// Return to normal event handling and hide the sheet
[NSApp endSheet:locationSheet];
[locationSheet orderOut:sender];
// Save the location url in the Preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.urlString forKey:TWVLocationUrlKey];
NSLog(@"Load the url: %@", urlString);
[self loadUrlString:self.urlString IntoWebView:self.theWebView];
}
- (IBAction)cancelLocationSheet:(id)sender {
// Return to normal event handling and hide the sheet
[NSApp endSheet:locationSheet];
[locationSheet orderOut:sender];
}
/*
* The method to load any url string into a web view of choice
*/
- (void)loadUrlString:(NSString *)anUrlString IntoWebView:(WebView *)aWebView {
// Make an URL from the String, and then a Request from the URL
NSURL *url = [NSURL URLWithString:anUrlString];
NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
// Get the webFrame and load the request
WebFrame* webFrame = [aWebView mainFrame];
[webFrame loadRequest: urlReq];
}
#pragma mark -
#pragma mark Preferences Panel
- (IBAction)showPreferencePanel:(id)sender {
// Lazy loading
if (self.preferenceController == nil) {
PreferenceController *prefController = [[PreferenceController alloc] init];
self.preferenceController = prefController;
[prefController release];
}
NSLog(@"showing %@", preferenceController);
[preferenceController showWindow:self];
}
- (void)handleAutomaticReloadChange:(NSNotification *)notification {
//NSLog(@"Received Notification %@", notification);
[self resetAutomaticReloadTimer];
}
- (void)resetAutomaticReloadTimer {
// Invalidate the previousTimer
if (automaticReloadTimer != nil) {
[automaticReloadTimer invalidate];
[automaticReloadTimer release];
automaticReloadTimer = nil;
}
// Do we need a timer?
if ( [[NSUserDefaults standardUserDefaults] boolForKey:TWVShouldAutomaticReloadKey] ) {
// Create a new timer
int reloadInterval = [[[NSUserDefaults standardUserDefaults] objectForKey:TWVAutomaticReloadIntervalKey] intValue];
automaticReloadTimer = [[NSTimer scheduledTimerWithTimeInterval:reloadInterval
target:self
selector:@selector(reloadWebView:)
userInfo:nil
repeats:YES] retain];
}
}
- (void)reloadWebView:(NSTimer *)timer {
// Reload the web view
NSLog(@"Reload the web view");
[self.theWebView reload:self];
}
#pragma mark -
#pragma mark Borderless Window
- (IBAction)toggleBorderlessWindow:(id)sender {
// Toggle the borderless Window state:
BOOL newState = ([borderlessWindowMenuItem state] == NSOffState);
// Set the MenuItemState
[self setBorderlessWindowMenuItemState:newState];
// Save the borderless Window state in the Preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:newState] forKey:TWVBorderlessWindowKey];
// Create a new window and reload the content
NSLog(@"Create a new %@ window", newState ? @"BORDERLESS" : @"BORDERED");
BOOL cropUnderTitleState = [[[NSUserDefaults standardUserDefaults] objectForKey:TWVDrawCroppedUnderTitleBarKey] boolValue];
NSRect newContentRect = [window frame];
if (newState) {
if (cropUnderTitleState) {
[self cropContentUnderTitleBar:NO];
} else {
newContentRect.size.height = newContentRect.size.height - titleBarHeight;
}
} else {
if (cropUnderTitleState) {
[self cropContentUnderTitleBar:YES];
} else {
// Fix the window frame (not the content frame)
NSRect theWindowFrame = [window frame];
theWindowFrame.size.height = theWindowFrame.size.height + titleBarHeight;
[window setFrame:theWindowFrame display:NO];
}
}
[self replaceWindowWithBorderlessWindow:newState WithContentRect:newContentRect];
}
- (IBAction)toggleCropUnderTitleBar:(id)sender {
// Toggle the Crop Under Title Bar state:
BOOL newState = ([cropUnderTitleBarMenuItem state] == NSOffState);
// Set the MenuItemState
[self setCropUnderTitleBarMenuItemState:newState];
// Save the Crop Under Title Bar state in the Preferences
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:[NSNumber numberWithBool:newState] forKey:TWVDrawCroppedUnderTitleBarKey];
// Perform the content cropping change
[self cropContentUnderTitleBar:newState];
}
/*
* Methods sets the UI properties according to the state of the Borderless Window
*/
- (void)setBorderlessWindowMenuItemState:(BOOL)booleanState {
if (booleanState) {
// YES BorderlessWindow
NSLog(@"Set borderless!");
[borderlessWindowMenuItem setState:NSOnState];
[borderlessWindowMenuItem setTitle:@"Hide Borderless"];
[cropUnderTitleBarMenuItem setEnabled:NO];
} else {
// NO BorderlessWindow
NSLog(@"Set NOT borderless!");
[borderlessWindowMenuItem setState:NSOffState];
[borderlessWindowMenuItem setTitle:@"Show Borderless"];
[cropUnderTitleBarMenuItem setEnabled:YES];
}
}
- (void)setCropUnderTitleBarMenuItemState:(BOOL)booleanState {
if (booleanState) {
// YES CropUnderTitleBar
[cropUnderTitleBarMenuItem setState:NSOnState];
} else {
// NO CropUnderTitleBar
[cropUnderTitleBarMenuItem setState:NSOffState];
}
}
- (void)replaceWindowWithBorderlessWindow:(BOOL)borderlessFlag WithContentRect:(NSRect)contentRect {
// Save the previous frame (to file and to string)
[window saveFrameUsingName:TWVMainTransparantWindowFrameKey];
NSString *savedFrameString = [window stringWithSavedFrame];
// Get a pointer to the old window
NSWindow *oldWindow = window;
// Make the windowstyle
NSUInteger newStyle;
if (borderlessFlag) {
newStyle = NSBorderlessWindowMask;
} else {
newStyle = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
}
// Create the new window
self.window = [[WebViewWindow alloc] initWithContentRect:contentRect
styleMask:newStyle
backing:NSBackingStoreBuffered
defer:NO];
// Set the properties (as also set in Interface Builder)
[window setContentView:[oldWindow contentView]];
[window setTitle:@"Transparent Web View"];
[window setFrameAutosaveName:TWVMainTransparantWindowFrameKey];
// Restore the frame from the one save above
[window setFrameFromString:savedFrameString];
// Set us as the delegate
[window setDelegate:self];
// Order front (Show the Window)
[window makeKeyAndOrderFront:self];
// Call the same window as awakeFromNib would have
[(WebViewWindow *)window setDrawsBackgroundSettings];
// Close the old window
[oldWindow close];
}
- (void)cropContentUnderTitleBar:(BOOL)cropUnderTitleFlag {
// Set the new frame of the web view
// The origin.y is measured from the bottom, so we only have to set the height
// newFrame.origin.y = newFrame.origin.y + titleBarHeight;
// Get the current frame of the WebView
NSRect newFrame = theWebView.frame;
// Change the frame
if (cropUnderTitleFlag) {
newFrame.size.height = newFrame.size.height + titleBarHeight;
} else {
newFrame.size.height = newFrame.size.height - titleBarHeight;
}
// Set the frame back to the web view
[theWebView setFrame:newFrame];
}
#pragma mark -
#pragma mark NSWindow Delegate Methods
- (void)windowDidResize:(NSNotification *)notification {
// Save the frame (since it wasn't working out of the box on our own created windows)
[window saveFrameUsingName:TWVMainTransparantWindowFrameKey];
}
- (void)windowDidMove:(NSNotification *)notification {
// Save the frame (since it wasn't working out of the box on our own created windows)
[window saveFrameUsingName:TWVMainTransparantWindowFrameKey];
}
#pragma mark -
- (void)dealloc {
[urlString release];
[preferenceController release];
[automaticReloadTimer invalidate];
[automaticReloadTimer release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
@end