-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScheduleView.m
97 lines (88 loc) · 3.23 KB
/
ScheduleView.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
#import "ScheduleView.h"
#import "MiniScheduleAppDelegate.h"
#import <CalendarStore/CalendarStore.h>
#define XOFFSET 21
#define DEFAULT_TEXT @"No Events"
@implementation ScheduleView
- (void)awakeFromNib {
[[MiniScheduleAppDelegate shared] addObserver:self forKeyPath:@"events" options:0 context:nil];
[self observeValueForKeyPath:@"events" ofObject:[MiniScheduleAppDelegate shared] change:nil context:nil];
}
- (void)dealloc {
[[MiniScheduleAppDelegate shared] removeObserver:self forKeyPath:@"events"];
[super dealloc];
}
- (NSString *)descriptionForEvent:(CalEvent *)event {
static NSDateFormatter *formatter = nil;
if(!formatter) {
formatter = [[NSDateFormatter alloc] init];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateStyle:NSDateFormatterNoStyle];
}
NSString *str;
if(event.isAllDay) str = event.title;
else str = [NSString stringWithFormat:@"%@: %@",[formatter stringFromDate:event.startDate],event.title];
if(event.location) str = [str stringByAppendingFormat:@" -- %@",event.location];
return str;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if([keyPath isEqualToString:@"events"]) {
NSArray *events = [MiniScheduleAppDelegate shared].events;
NSUInteger count = [events count];
NSSize size = {self.frame.size.width,[self itemHeight]};
if(count) {
if([self autosizesWidth]) {
NSDictionary *textOpts = [NSDictionary dictionaryWithObjectsAndKeys:[self titleFont],NSFontAttributeName,[self titleColor],NSForegroundColorAttributeName,nil];
size.width = 0;
for(CalEvent *event in events) {
NSSize eventSize = [[self descriptionForEvent:event] sizeWithAttributes:textOpts];
if(eventSize.width > size.width) size.width = eventSize.width;
}
size.width += 2*XOFFSET;
if(size.width > 512) size.width = 512;
}
size.height *= count;
} else {
if([self autosizesWidth]) {
NSDictionary *textOpts = [NSDictionary dictionaryWithObjectsAndKeys:[self titleFont],NSFontAttributeName,[self titleColor],NSForegroundColorAttributeName,nil];
size.width = [DEFAULT_TEXT sizeWithAttributes:textOpts].width + 2*XOFFSET;
}
}
[self setFrameSize:size];
[self setNeedsDisplay:YES];
} else [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
- (BOOL)isFlipped {
return YES;
}
- (NSFont *)titleFont {
return [NSFont systemFontOfSize:14];
}
- (NSColor *)titleColor {
return [NSColor whiteColor];
}
- (float)itemHeight {
return 20;
}
- (BOOL)autosizesWidth {
return NO;
}
- (void)drawRect:(NSRect)dirtyRect {
NSArray *events = [[MiniScheduleAppDelegate shared] events];
NSDictionary *textOpts = [NSDictionary dictionaryWithObjectsAndKeys:[self titleFont],NSFontAttributeName,[self titleColor],NSForegroundColorAttributeName,nil];
if([events count] == 0) {
[DEFAULT_TEXT drawAtPoint:NSMakePoint(XOFFSET,0) withAttributes:textOpts];
return;
}
NSRect rect = self.bounds;
rect.size.width -= XOFFSET*2;
rect.origin.x += XOFFSET;
NSRectClip(rect);
NSPoint point = (NSPoint){XOFFSET,0};
float height = [self itemHeight];
for(CalEvent *event in events) {
[[self descriptionForEvent:event] drawAtPoint:point withAttributes:textOpts];
point.y += height;
}
}
@end