-
Notifications
You must be signed in to change notification settings - Fork 31
/
EvangelionClock.m
executable file
·147 lines (119 loc) · 4.97 KB
/
EvangelionClock.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
#import "EvangelionClock.h"
#import <WebKit/WebKit.h>
@implementation EvangelionClock
static NSString * const evangelionClockModule = @"de.pascal-wagler.evangelion-clock";
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
if (!(self = [super initWithFrame:frame isPreview:isPreview])) return nil;
// Preference Defaults
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:evangelionClockModule];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
@"0", @"screenDisplayOption", // Default to show only on primary display
nil]];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
@"0", @"styleOption", // Default to use normal style
nil]];
NSString *style = @"/Webview/style-normal-3d.html";
switch ([defaults integerForKey:@"styleOption"]) {
case 0:
style = @"/Webview/style-normal-3d.html";
break;
case 1:
style = @"/Webview/style-red-3d.html";
break;
case 2:
style = @"/Webview/style-normal.html";
break;
case 3:
style = @"/Webview/style-red.html";
break;
default:
style = @"/Webview/style-normal-3d.html";
break;
}
// Webview
NSURL* indexHTMLDocumentURL = [NSURL URLWithString:[[[NSURL fileURLWithPath:[[NSBundle bundleForClass:self.class].resourcePath stringByAppendingString:style] isDirectory:NO] description] stringByAppendingFormat:@"?screensaver=1%@", self.isPreview ? @"&is_preview=1" : @""]];
WebView* webView = [[WebView alloc] initWithFrame:NSMakeRect(0, 0, frame.size.width, frame.size.height)];
webView.drawsBackground = NO; // Avoids a "white flash" just before the index.html file has loaded
[webView.mainFrame loadRequest:[NSURLRequest requestWithURL:indexHTMLDocumentURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0]];
// Show on screens based on preferences
NSArray* screens = [NSScreen screens];
NSScreen* primaryScreen = [screens objectAtIndex:0];
switch ([defaults integerForKey:@"screenDisplayOption"]) {
// Primary screen (System Preferences > Displays).
// The screen the menubar is shown on under 'arrangement'
case 0:
if ((primaryScreen.frame.origin.x == frame.origin.x) || isPreview) {
[self addSubview:webView];
}
break;
// Last Focussed Screen
// This _sometimes_ results in nothing being shown when previewing in system prefs.
case 1:
if (([NSScreen mainScreen].frame.origin.x == frame.origin.x) || isPreview) {
[self addSubview:webView];
}
break;
// All Screens
case 2:
[self addSubview:webView];
break;
default:
[self addSubview:webView];
break;
}
return self;
}
#pragma mark - ScreenSaverView
- (void)animateOneFrame { [self stopAnimation]; }
#pragma mark - Config
// http://cocoadevcentral.com/articles/000088.php
- (BOOL)hasConfigureSheet { return YES; }
- (NSWindow *)configureSheet
{
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:evangelionClockModule];
if (!configSheet)
{
if (![NSBundle loadNibNamed:@"ConfigureSheet" owner:self])
{
NSLog( @"Failed to load configure sheet." );
}
}
[styleOption selectItemAtIndex:[defaults integerForKey:@"styleOption"]];
[screenDisplayOption selectItemAtIndex:[defaults integerForKey:@"screenDisplayOption"]];
return configSheet;
}
- (IBAction)cancelClick:(id)sender
{
[[NSApplication sharedApplication] endSheet:configSheet];
}
- (IBAction) okClick: (id)sender
{
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:evangelionClockModule];
// Update our defaults
[defaults setInteger:[styleOption indexOfSelectedItem] forKey:@"styleOption"];
[defaults setInteger:[screenDisplayOption indexOfSelectedItem]
forKey:@"screenDisplayOption"];
// Save the settings to disk
[defaults synchronize];
// Close the sheet
[[NSApplication sharedApplication] endSheet:configSheet];
}
#pragma mark - WebFrameLoadDelegate
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
NSLog(@"%@ error=%@", NSStringFromSelector(_cmd), error);
}
#pragma mark Focus Overrides
- (NSView *)hitTest:(NSPoint)aPoint {return self;}
//- (void)keyDown:(NSEvent *)theEvent {return;}
//- (void)keyUp:(NSEvent *)theEvent {return;}
- (void)mouseDown:(NSEvent *)theEvent {return;}
- (void)mouseUp:(NSEvent *)theEvent {return;}
- (void)mouseDragged:(NSEvent *)theEvent {return;}
- (void)mouseEntered:(NSEvent *)theEvent {return;}
- (void)mouseExited:(NSEvent *)theEvent {return;}
- (BOOL)acceptsFirstResponder {return YES;}
- (BOOL)resignFirstResponder {return NO;}
@end