-
Notifications
You must be signed in to change notification settings - Fork 23
/
EpochFlipClock.m
executable file
·124 lines (96 loc) · 4.17 KB
/
EpochFlipClock.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
#import "EpochFlipClock.h"
#import <WebKit/WebKit.h>
@implementation EpochFlipClock
static NSString * const epochFlipClockModule = @"com.epochflipclock";
- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview {
if (!(self = [super initWithFrame:frame isPreview:isPreview])) return nil;
// Preference Defaults
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:epochFlipClockModule];
[defaults registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:
@"0", @"screenDisplayOption", // Default to show only on primary display
nil]];
// Webview
NSURL* indexHTMLDocumentURL = [NSURL URLWithString:[[[NSURL fileURLWithPath:[[NSBundle bundleForClass:self.class].resourcePath stringByAppendingString:@"/Webview/index.html"] 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:epochFlipClockModule];
if (!configSheet)
{
if (![NSBundle loadNibNamed:@"ConfigureSheet" owner:self])
{
NSLog( @"Failed to load configure sheet." );
}
}
[screenDisplayOption selectItemAtIndex:[defaults integerForKey:@"screenDisplayOption"]];
return configSheet;
}
- (IBAction)cancelClick:(id)sender
{
[[NSApplication sharedApplication] endSheet:configSheet];
}
- (IBAction) okClick: (id)sender
{
ScreenSaverDefaults *defaults;
defaults = [ScreenSaverDefaults defaultsForModuleWithName:epochFlipClockModule];
// Update our defaults
[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