/
AppController.m
160 lines (117 loc) · 4.43 KB
/
AppController.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
//
// AppController.m
// Countdown
//
// Created by Michael Parlee on 9/4/09.
// Copyright __MyCompanyName__ 2009 . All rights reserved.
//
#import "AppController.h"
@interface AppController(PrivateMethods)
- (NSDate*)targetDateFromTime:(NSString *)time;
- (NSTimeInterval)getRemainingTime;
@end
@implementation AppController
@synthesize targetDate;
- (void) awakeFromNib
{
if(![qcView loadCompositionFromFile:[[NSBundle mainBundle] pathForResource:@"QuartzCompTest" ofType:@"qtz"]]) {
NSLog(@"Could not load composition");
}
timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target: self selector:@selector(updateDisplay:) userInfo: nil repeats: YES];
NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
NSLog(@"NSUserDefaults = %@",args);
NSString *targetTime = [args stringForKey:@"t"];
template = [args stringForKey:@"m"];
NSLog(@"templatestr=%@",[template class]);
if (targetTime == nil) {
targetTime = @"0130";
}
if (template == nil) {
template = @"Multi\nLine\ncountdown\n%s";
}
self.targetDate = [self targetDateFromTime:targetTime];
NSLog(@"Got target time of %@ -> %@", targetTime, targetDate);
}
- (void)windowWillClose:(NSNotification *)notification
{
[NSApp terminate:self];
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
int windowLevel;
NSRect screenRect;
// Capture the main display
if (CGDisplayCapture( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( @"Couldn't capture the main display!" );
// Note: you'll probably want to display a proper error dialog here
}
// Get the shielding window level
windowLevel = CGShieldingWindowLevel();
// Get the screen rect of our main display
screenRect = [[NSScreen mainScreen] frame];
// Put up a new window
mainWindow = [[NSWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered
defer:NO screen:[NSScreen mainScreen]];
[mainWindow setLevel:windowLevel];
[mainWindow setBackgroundColor:[NSColor blackColor]];
[mainWindow makeKeyAndOrderFront:nil];
// Strings
displayString = @"";
[qcView setValue:displayString forInputKey:@"arg1"];
// Load our content view
[qcView setFrame:screenRect];
[mainWindow setContentView:qcView];
}
- (void)applicationWillTerminate:(NSNotification *)notification
{
[mainWindow orderOut:self];
// Release the display(s)
if (CGDisplayRelease( kCGDirectMainDisplay ) != kCGErrorSuccess) {
NSLog( @"Couldn't release the display(s)!" );
// Note: if you display an error dialog here, make sure you set
// its window level to the same one as the shield window level,
// or the user won't see anything.
}
}
- (void)updateDisplay: (NSTimer*) timer {
double time = [self getRemainingTime];
int hours = time / 60 / 60;
time -= hours * 60 * 60;
int minutes = time / 60 ;
time -= minutes * 60;
int secs = time;
NSString *message = [template stringByReplacingOccurrencesOfString:@"%s" withString:@"%d:%02d:%02d"];
message = [message stringByReplacingOccurrencesOfString:@"+" withString:@"\n"];
displayString = [NSString stringWithFormat:message, hours,minutes,secs];
[qcView setValue:displayString forInputKey:@"arg1"];
}
- (NSDate*)targetDateFromTime: (NSString*)time {
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
int hour = [[formatter numberFromString:[time substringToIndex:2]] integerValue];
int minute = [[formatter numberFromString:[time substringFromIndex:2]] integerValue];
[formatter release];
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *hourMinute = [[NSDateComponents alloc] init];
[hourMinute setHour:hour];
[hourMinute setMinute:minute];
NSDateComponents *day = [[NSDateComponents alloc] init];
[day setDay:1];
NSDate *date = [NSDate date];
unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *components = [gregorian components:unitFlags fromDate:date];
NSDate *todayMidnight = [gregorian dateFromComponents:components];
NSDate *testDate = [gregorian dateByAddingComponents:hourMinute toDate:todayMidnight options:0];
if ([testDate isLessThan:date]) {
testDate = [gregorian dateByAddingComponents:day toDate:testDate options:0];
}
[hourMinute release];
[day release];
return testDate;
}
-(NSTimeInterval)getRemainingTime {
NSDate *now = [NSDate date];
return [self.targetDate timeIntervalSinceDate:now];
}
@end