forked from mipstian/catch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CTCMenuController.m
169 lines (132 loc) · 6.04 KB
/
CTCMenuController.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
#import "CTCMenuController.h"
#import "CTCDefaults.h"
#import "CTCScheduler.h"
@interface CTCMenuController ()
@property (strong, nonatomic) IBOutlet NSMenu *menu;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuVersion;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuPauseResume;
@property (strong, nonatomic) IBOutlet NSMenuItem *menuLastUpdate;
@property (strong, nonatomic) NSStatusItem *menuBarItem;
@property (strong, nonatomic) NSDateFormatter *lastUpdateDateFormatter;
@end
@implementation CTCMenuController
- (void)awakeFromNib {
// Skip setup if we're running headless
if (CTCDefaults.shouldRunHeadless) return;
// Create a date formatter for "last update" dates
self.lastUpdateDateFormatter = NSDateFormatter.new;
self.lastUpdateDateFormatter.timeStyle = NSDateFormatterShortStyle;
[self setupMenuItem];
// Update UI with initial values
[self refreshSchedulerStatus];
[self setLastUpdateStatus:YES time:nil];
[self setupObservers];
}
- (void)setupMenuItem {
// Create the NSStatusItem and set its length
self.menuBarItem = [NSStatusBar.systemStatusBar statusItemWithLength:NSSquareStatusItemLength];
// Tell the NSStatusItem what menu to load
self.menuBarItem.menu = self.menu;
// Enable highlighting
self.menuBarItem.highlightMode = YES;
// Set current name and version
self.menuVersion.title = [NSString stringWithFormat:@"%@ %@", CTCDefaults.appName, CTCDefaults.appVersion];
}
- (void)setupObservers {
__weak typeof(self) weakSelf = self;
void (^handleSchedulerStatusChange)(NSNotification *) = ^(NSNotification *notification) {
if (weakSelf) [weakSelf refreshSchedulerStatus];
};
void (^handleSchedulerLastUpdateStatusChange)(NSNotification *) = ^(NSNotification *notification) {
BOOL wasSuccessful = [notification.userInfo[@"successful"] boolValue];
NSDate *lastUpdateDate = notification.userInfo[@"time"];
if (weakSelf) [weakSelf setLastUpdateStatus:wasSuccessful time:lastUpdateDate];
};
[NSNotificationCenter.defaultCenter addObserverForName:kCTCSchedulerStatusNotificationName
object:CTCScheduler.sharedScheduler
queue:nil
usingBlock:handleSchedulerStatusChange];
[NSNotificationCenter.defaultCenter addObserverForName:kCTCSchedulerLastUpdateStatusNotificationName
object:CTCScheduler.sharedScheduler
queue:nil
usingBlock:handleSchedulerLastUpdateStatusChange];
}
- (IBAction)checkNow:(id)sender {
[CTCScheduler.sharedScheduler forceCheck];
}
- (IBAction)togglePause:(id)sender {
[CTCScheduler.sharedScheduler togglePause];
}
- (void)refreshSchedulerStatus {
if (CTCScheduler.sharedScheduler.isChecking) {
[self setRefreshing];
}
else {
if (CTCScheduler.sharedScheduler.isPolling) {
[self setIdle];
}
else {
[self setDisabled];
}
}
}
- (void)setLastUpdateStatus:(BOOL)lastUpdateWasSuccessful time:(NSDate *)time {
// Create something like "Last update: 3:45 AM" and place it in the menu
NSString *lastUpdateStatusFormat = lastUpdateWasSuccessful ?
NSLocalizedString(@"lastupdate", @"Title for the last update time") :
NSLocalizedString(@"lastupdatefailed", @"Title for the last update time if it fails");
NSString *lastUpdateStatus = time ?
[NSString stringWithFormat:lastUpdateStatusFormat,
[self.lastUpdateDateFormatter stringFromDate:time]] :
[NSString stringWithFormat:lastUpdateStatusFormat, NSLocalizedString(@"never", @"Never happened")];
[self.menuLastUpdate setTitle:lastUpdateStatus];
}
- (void)setIdle {
// Sets the images (status: idle)
if (self.shouldUseTemplateMenubarIcons) {
self.menuBarItem.button.image = [NSImage imageNamed:@"Menubar_Idle_Template"];
self.menuBarItem.button.appearsDisabled = NO;
}
else {
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_idle"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_idle-inv"]];
}
// Set pause/resume to "pause"
[self.menuPauseResume setTitle:NSLocalizedString(@"pause", @"Description of pause action")];
}
- (void)setRefreshing {
// Sets the images (status: refreshing)
if (self.shouldUseTemplateMenubarIcons) {
self.menuBarItem.button.image = [NSImage imageNamed:@"Menubar_Refreshing_Template"];
self.menuBarItem.button.appearsDisabled = NO;
}
else {
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_refreshing"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_refreshing-inv"]];
}
// Set pause/resume to "pause"
[self.menuPauseResume setTitle:NSLocalizedString(@"pause", @"Description of pause action")];
// Also overwrite the last update string with "Updating now"
[self.menuLastUpdate setTitle:NSLocalizedString(@"updatingnow", @"An update is in progress")];
}
- (void)setDisabled {
// Sets the images (status: disabled)
if (self.shouldUseTemplateMenubarIcons) {
self.menuBarItem.button.image = [NSImage imageNamed:@"Menubar_Disabled_Template"];
self.menuBarItem.button.appearsDisabled = YES;
}
else {
[self.menuBarItem setImage:[NSImage imageNamed:@"menubar_disabled"]];
[self.menuBarItem setAlternateImage:[NSImage imageNamed:@"menubar_disabled-inv"]];
}
// Set pause/resume to "resume"
[self.menuPauseResume setTitle:NSLocalizedString(@"resume", @"Description of resume action")];
}
- (BOOL)shouldUseTemplateMenubarIcons {
// Use template images in Yosemite and up, plain icons otherwise
return [self.menuBarItem respondsToSelector:@selector(button)];
}
- (void)dealloc {
[NSNotificationCenter.defaultCenter removeObserver:self];
}
@end