forked from codler/Battery-Time-Remaining
/
AppDelegate.m
771 lines (640 loc) · 32.7 KB
/
AppDelegate.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
//
// AppDelegate.m
// Battery Time Remaining
//
// Created by Han Lin Yap on 2012-08-01.
// Copyright (c) 2012 Han Lin Yap. All rights reserved.
//
#import "AppDelegate.h"
#import "HttpGet.h"
#import "LLManager.h"
#import <IOKit/ps/IOPowerSources.h>
#import <IOKit/ps/IOPSKeys.h>
#import <IOKit/pwr_mgt/IOPM.h>
#import <IOKit/pwr_mgt/IOPMLib.h>
#import <QuartzCore/QuartzCore.h>
//#define DEBUG_BATTERY_PERCENT
#define CHECK_FOR_UPDATE
// In Apple's battery gauge, the battery icon is rendered further down from the
// top than NSStatusItem does it. Hence we add an extra top offset to get the
// exact same look.
#define EXTRA_TOP_OFFSET 2.0f
// IOPS notification callback on power source change
static void PowerSourceChanged(void * context)
{
// Update the time remaining text
AppDelegate *self = (__bridge AppDelegate *)context;
[self updateStatusItem];
}
@interface AppDelegate ()
{
NSDictionary *batteryIcons;
NSTimer *menuUpdateTimer;
NSTimer *optionKeyPressedTimer;
BOOL isOptionKeyPressed;
BOOL isCapacityWarning;
BOOL showParenthesis;
}
- (void)cacheBatteryIcon;
- (NSImage *)loadBatteryIconNamed:(NSString *)iconName;
@end
@implementation AppDelegate
@synthesize statusItem, notifications, previousPercent;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.advancedSupported = ([self getAdvancedBatteryInfo] != nil);
[self cacheBatteryIcon];
isCapacityWarning = NO;
// Init notification
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
[self loadNotificationSetting];
// Set default notification settings if not set
if (![self.notifications objectForKey:@"15"])
{
[self.notifications setValue:[NSNumber numberWithBool:YES] forKey:@"15"];
}
if (![self.notifications objectForKey:@"100"])
{
[self.notifications setValue:[NSNumber numberWithBool:YES] forKey:@"100"];
}
[self saveNotificationSetting];
// Power source menu item
NSMenuItem *psPercentMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Loading…", @"Remaining menuitem") action:nil keyEquivalent:@""];
[psPercentMenu setTag:kBTRMenuPowerSourcePercent];
[psPercentMenu setEnabled:NO];
NSMenuItem *psStateMenu = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Power source: %@", @"Powersource menuitem"), NSLocalizedString(@"Unknown", @"Powersource state")] action:nil keyEquivalent:@""];
[psStateMenu setTag:kBTRMenuPowerSourceState];
[psStateMenu setEnabled:NO];
NSMenuItem *psAdvancedMenu = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
[psAdvancedMenu setTag:kBTRMenuPowerSourceAdvanced];
[psAdvancedMenu setEnabled:NO];
[psAdvancedMenu setHidden:![[NSUserDefaults standardUserDefaults] boolForKey:@"advanced"]];
// Start at login menu item
NSMenuItem *startAtLoginMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Start at login", @"Start at login setting") action:@selector(toggleStartAtLogin:) keyEquivalent:@""];
[startAtLoginMenu setTag:kBTRMenuStartAtLogin];
startAtLoginMenu.target = self;
startAtLoginMenu.state = ([LLManager launchAtLogin]) ? NSOnState : NSOffState;
// Build the notification submenu
NSMenu *notificationSubmenu = [[NSMenu alloc] initWithTitle:@"Notification Menu"];
for (int i = 5; i <= 100; i = i + 5)
{
BOOL state = [[self.notifications valueForKey:[NSString stringWithFormat:@"%d", i]] boolValue];
NSMenuItem *notificationSubmenuItem = [[NSMenuItem alloc] initWithTitle:[NSString stringWithFormat:@"%d%%", i] action:@selector(toggleNotification:) keyEquivalent:@""];
notificationSubmenuItem.tag = i;
notificationSubmenuItem.state = (state) ? NSOnState : NSOffState;
[notificationSubmenu addItem:notificationSubmenuItem];
}
// Notification menu item
NSMenuItem *notificationMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Notifications", @"Notification menuitem") action:nil keyEquivalent:@""];
[notificationMenu setTag:kBTRMenuNotification];
[notificationMenu setSubmenu:notificationSubmenu];
[notificationMenu setHidden:self.advancedSupported && ![[NSUserDefaults standardUserDefaults] boolForKey:@"advanced"]];
// Advanced mode menu item
NSMenuItem *advancedSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Advanced mode", @"Advanced mode setting") action:@selector(toggleAdvanced:) keyEquivalent:@""];
[advancedSubmenuItem setTag:kBTRMenuAdvanced];
advancedSubmenuItem.target = self;
advancedSubmenuItem.state = ([[NSUserDefaults standardUserDefaults] boolForKey:@"advanced"]) ? NSOnState : NSOffState;
[advancedSubmenuItem setHidden:!self.advancedSupported];
// Time display control menu item
NSMenuItem *parenthesisSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Display time with parentheses", @"Display time with parentheses setting") action:@selector(toggleParenthesis:) keyEquivalent:@""];
[parenthesisSubmenuItem setTag:kBTRMenuParenthesis];
parenthesisSubmenuItem.target = self;
showParenthesis = [[NSUserDefaults standardUserDefaults] boolForKey:@"parentheses"];
parenthesisSubmenuItem.state = (showParenthesis) ? NSOnState : NSOffState;
// Build the setting submenu
NSMenu *settingSubmenu = [[NSMenu alloc] initWithTitle:@"Setting Menu"];
[settingSubmenu addItem:advancedSubmenuItem];
[settingSubmenu addItem:parenthesisSubmenuItem];
// Settings menu item
NSMenuItem *settingMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Settings", @"Settings menuitem") action:nil keyEquivalent:@""];
[settingMenu setTag:kBTRMenuSetting];
[settingMenu setSubmenu:settingSubmenu];
#ifdef CHECK_FOR_UPDATE
// Updater menu
NSMenuItem *updaterMenu = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Checking for updates…", @"Update menuitem") action:nil keyEquivalent:@""];
[updaterMenu setTag:kBTRMenuUpdater];
[updaterMenu setEnabled:NO];
#endif
// Build the statusbar menu
NSMenu *statusBarMenu = [[NSMenu alloc] initWithTitle:@"Status Menu"];
[statusBarMenu setDelegate:self];
[statusBarMenu addItem:psPercentMenu];
[statusBarMenu addItem:psStateMenu];
[statusBarMenu addItem:psAdvancedMenu];
[statusBarMenu addItem:[NSMenuItem separatorItem]]; // Separator
[statusBarMenu addItem:startAtLoginMenu];
[statusBarMenu addItem:notificationMenu];
[statusBarMenu addItem:settingMenu];
[statusBarMenu addItem:[NSMenuItem separatorItem]]; // Separator
[statusBarMenu addItemWithTitle:NSLocalizedString(@"Energy Saver Preferences…", @"Open Energy Saver Preferences menuitem") action:@selector(openEnergySaverPreference:) keyEquivalent:@""];
[statusBarMenu addItem:[NSMenuItem separatorItem]]; // Separator
#ifdef CHECK_FOR_UPDATE
[statusBarMenu addItem:updaterMenu];
[statusBarMenu addItem:[NSMenuItem separatorItem]]; // Separator
#endif
[statusBarMenu addItemWithTitle:NSLocalizedString(@"Quit", @"Quit menuitem") action:@selector(terminate:) keyEquivalent:@""];
// Create the status item and set initial text
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusItem.highlightMode = YES;
statusItem.menu = statusBarMenu;
[self updateStatusItem];
// Capture Power Source updates and make sure our callback is called
CFRunLoopSourceRef loop = IOPSNotificationCreateRunLoopSource(PowerSourceChanged, (__bridge void *)self);
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, kCFRunLoopDefaultMode);
CFRelease(loop);
}
- (void)updateStatusItem
{
// reset warning state; new state will be calculated here anyway
isCapacityWarning = NO;
// Get the estimated time remaining
CFTimeInterval timeRemaining = IOPSGetTimeRemainingEstimate();
// Get list of power sources
CFTypeRef psBlob = IOPSCopyPowerSourcesInfo();
CFArrayRef psList = IOPSCopyPowerSourcesList(psBlob);
// Loop through the list of power sources
CFIndex count = CFArrayGetCount(psList);
for (CFIndex i = 0; i < count; i++)
{
CFTypeRef powersource = CFArrayGetValueAtIndex(psList, i);
CFDictionaryRef description = IOPSGetPowerSourceDescription(psBlob, powersource);
// Skip if not present
if (CFDictionaryGetValue(description, CFSTR(kIOPSIsPresentKey)) == kCFBooleanFalse)
{
continue;
}
// Calculate the percent
NSNumber *currentBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey));
NSNumber *maxBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey));
self.currentPercent = (int)[currentBatteryCapacity doubleValue] / [maxBatteryCapacity doubleValue] * 100;
NSString *psState = CFDictionaryGetValue(description, CFSTR(kIOPSPowerSourceStateKey));
NSString *psStateTranslated = ([psState isEqualToString:(NSString *)CFSTR(kIOPSBatteryPowerValue)]) ?
NSLocalizedString(@"Battery Power", @"Powersource state") :
([psState isEqualToString:(NSString *)CFSTR(kIOPSACPowerValue)]) ?
NSLocalizedString(@"AC Power", @"Powersource state") :
NSLocalizedString(@"Off Line", @"Powersource state");
[self.statusItem.menu itemWithTag:kBTRMenuPowerSourceState].title = [NSString stringWithFormat:NSLocalizedString(@"Power source: %@", @"Powersource menuitem"), psStateTranslated];
// Still calculating the estimated time remaining...
// Fixes #22 - state after reboot
if ([psState isEqualToString:(NSString *)CFSTR(kIOPSBatteryPowerValue)] && CFDictionaryGetValue(description, CFSTR(kIOPSIsChargingKey)) == kCFBooleanFalse && (kIOPSTimeRemainingUnknown == timeRemaining || kIOPSTimeRemainingUnlimited == timeRemaining))
{
[self setStatusBarImage:[self getBatteryIconPercent:self.currentPercent] title:[NSString stringWithFormat:@" %@", NSLocalizedString(@"Calculating…", @"Calculating sidetext")]];
}
// We're connected to an unlimited power source (AC adapter probably)
else if (kIOPSTimeRemainingUnlimited == timeRemaining)
{
// Check if the battery is charging atm
if (CFDictionaryGetValue(description, CFSTR(kIOPSIsChargingKey)) == kCFBooleanTrue)
{
CFNumberRef timeToChargeNum = CFDictionaryGetValue(description, CFSTR(kIOPSTimeToFullChargeKey));
int timeTilCharged = [(__bridge NSNumber *)timeToChargeNum intValue];
if (timeTilCharged > 0)
{
// Calculate the hour/minutes
NSInteger hour = timeTilCharged / 60;
NSInteger minute = timeTilCharged % 60;
NSString *title = (showParenthesis) ? @" (%ld:%02ld)" : @" %ld:%02ld";
// Return the time remaining string
[self setStatusBarImage:[self getBatteryIconNamed:@"BatteryCharging"] title:[NSString stringWithFormat:title, hour, minute]];
}
else
{
[self setStatusBarImage:[self getBatteryIconNamed:@"BatteryCharging"] title:[NSString stringWithFormat:@" %@", NSLocalizedString(@"Calculating…", @"Calculating sidetext")]];
}
}
else
{
// Not charging and on a endless powersource
[self setStatusBarImage:[self getBatteryIconNamed:@"BatteryCharged"] title:@""];
NSNumber *currentBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey));
NSNumber *maxBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey));
// Notify user when battery is charged
if ([currentBatteryCapacity intValue] == [maxBatteryCapacity intValue] &&
self.previousPercent != self.currentPercent &&
[[self.notifications valueForKey:@"100"] boolValue])
{
[self notify:NSLocalizedString(@"Charged", @"Charged notification")];
self.previousPercent = self.currentPercent;
}
}
}
// Time is known!
else
{
// Calculate the hour/minutes
NSInteger hour = (int)timeRemaining / 3600;
NSInteger minute = (int)timeRemaining % 3600 / 60;
NSString *title = (showParenthesis) ? @" (%ld:%02ld)" : @" %ld:%02ld";
// Return the time remaining string
[self setStatusBarImage:[self getBatteryIconPercent:self.currentPercent] title:[NSString stringWithFormat:title, hour, minute]];
for (NSString *key in self.notifications)
{
if ([[self.notifications valueForKey:key] boolValue] && [key intValue] == self.currentPercent)
{
// Send notification once
if (self.previousPercent != self.currentPercent)
{
[self notify:NSLocalizedString(@"Battery Time Remaining", "Battery Time Remaining notification") message:[NSString stringWithFormat:NSLocalizedString(@"%1$ld:%2$02ld left (%3$ld%%)", @"Time remaining left notification"), hour, minute, self.currentPercent]];
}
break;
}
}
self.previousPercent = self.currentPercent;
}
}
CFRelease(psList);
CFRelease(psBlob);
}
- (void)updateStatusItemMenu
{
[self updateStatusItem];
// Show power source data in menu
if (self.advancedSupported && ([[self.statusItem.menu itemWithTag:kBTRMenuSetting].submenu itemWithTag:kBTRMenuAdvanced].state == NSOnState || isOptionKeyPressed))
{
NSDictionary *advancedBatteryInfo = [self getAdvancedBatteryInfo];
NSDictionary *moreAdvancedBatteryInfo = [self getMoreAdvancedBatteryInfo];
// Unit mAh
NSNumber *currentBatteryPower = [advancedBatteryInfo objectForKey:@"Current"];
// Unit mAh
NSNumber *maxBatteryPower = [advancedBatteryInfo objectForKey:@"Capacity"];
// Unit mAh
NSNumber *Amperage = [advancedBatteryInfo objectForKey:@"Amperage"];
// Unit mV
NSNumber *Voltage = [advancedBatteryInfo objectForKey:@"Voltage"];
NSNumber *cycleCount = [advancedBatteryInfo objectForKey:@"Cycle Count"];
// Unit Wh
NSNumber *watt = [NSNumber numberWithDouble:[Amperage doubleValue] / 1000 * [Voltage doubleValue] / 1000];
// Unit Celsius
NSNumber *temperature = [NSNumber numberWithDouble:[[moreAdvancedBatteryInfo objectForKey:@"Temperature"] doubleValue] / 100];
[self.statusItem.menu itemWithTag:kBTRMenuPowerSourcePercent].title = [NSString stringWithFormat: NSLocalizedString(@"%ld %% left ( %ld/%ld mAh )", @"Advanced percentage left menuitem"), self.currentPercent, [currentBatteryPower integerValue], [maxBatteryPower integerValue]];
// Each item in array will be a row in menu
NSArray *advancedBatteryInfoTexts = [NSArray arrayWithObjects:
[NSString stringWithFormat:NSLocalizedString(@"Cycle count: %ld", @"Advanced battery info menuitem"), [cycleCount integerValue]],
[NSString stringWithFormat:NSLocalizedString(@"Power usage: %.2f Watt", @"Advanced battery info menuitem"), [watt doubleValue]],
[NSString stringWithFormat:NSLocalizedString(@"Temperature: %.1f°C", @"Advanced battery info menuitem"), [temperature doubleValue]],
nil];
NSDictionary *advancedAttributedStyle = [NSDictionary dictionaryWithObjectsAndKeys:
// Font
[NSFont systemFontOfSize:[NSFont systemFontSize]+1.f],
NSFontAttributeName,
// Text color
[NSColor disabledControlTextColor],
NSForegroundColorAttributeName,
nil];
NSAttributedString *advancedAttributedTitle = [[NSAttributedString alloc] initWithString:[advancedBatteryInfoTexts componentsJoinedByString:@"\n"] attributes:advancedAttributedStyle];
[self.statusItem.menu itemWithTag:kBTRMenuPowerSourceAdvanced].attributedTitle = advancedAttributedTitle;
}
else
{
[self.statusItem.menu itemWithTag:kBTRMenuPowerSourcePercent].title = [NSString stringWithFormat: NSLocalizedString(@"%ld %% left", @"Percentage left menuitem"), self.currentPercent];
}
}
- (void)setStatusBarImage:(NSImage *)image title:(NSString *)title
{
// Image
[image setTemplate:( ! isCapacityWarning)];
[self.statusItem setImage:image];
[self.statusItem setAlternateImage:[self imageInvertColor:image]];
// Title
NSDictionary *attributedStyle = [NSDictionary dictionaryWithObjectsAndKeys:
// Font
[NSFont menuFontOfSize:12.0f],
NSFontAttributeName,
nil];
NSAttributedString *attributedTitle = [[NSAttributedString alloc] initWithString:title attributes:attributedStyle];
self.statusItem.attributedTitle = attributedTitle;
}
- (NSDictionary *)getAdvancedBatteryInfo
{
mach_port_t masterPort;
CFArrayRef batteryInfo;
if (kIOReturnSuccess == IOMasterPort(MACH_PORT_NULL, &masterPort) &&
kIOReturnSuccess == IOPMCopyBatteryInfo(masterPort, &batteryInfo))
{
return [(__bridge NSArray*)batteryInfo objectAtIndex:0];
}
return nil;
}
- (NSDictionary *)getMoreAdvancedBatteryInfo
{
CFMutableDictionaryRef matching, properties = NULL;
io_registry_entry_t entry = 0;
// same as matching = IOServiceMatching("IOPMPowerSource");
matching = IOServiceNameMatching("AppleSmartBattery");
entry = IOServiceGetMatchingService(kIOMasterPortDefault, matching);
IORegistryEntryCreateCFProperties(entry, &properties, NULL, 0);
return (__bridge NSDictionary *)properties;
//IOObjectRelease(entry);
}
- (NSImage *)getBatteryIconPercent:(NSInteger)percent
{
#ifdef DEBUG_BATTERY_PERCENT
percent = arc4random() % 101;
#endif
// Mimic Apple's original battery icon using high resolution artwork
NSImage *batteryOutline = [[self getBatteryIconNamed:@"BatteryEmpty"] copy];
NSImage *batteryLevelLeft = nil;
NSImage *batteryLevelMiddle = nil;
NSImage *batteryLevelRight = nil;
if (percent > 15)
{
isCapacityWarning = NO;
// draw black capacity bar
batteryLevelLeft = [self getBatteryIconNamed:@"BatteryLevelCapB-L"];
batteryLevelMiddle = [self getBatteryIconNamed:@"BatteryLevelCapB-M"];
batteryLevelRight = [self getBatteryIconNamed:@"BatteryLevelCapB-R"];
}
else
{
isCapacityWarning = YES;
// draw red capacity bar
batteryLevelLeft = [self getBatteryIconNamed:@"BatteryLevelCapR-L"];
batteryLevelMiddle = [self getBatteryIconNamed:@"BatteryLevelCapR-M"];
batteryLevelRight = [self getBatteryIconNamed:@"BatteryLevelCapR-R"];
}
const CGFloat drawingUnit = [batteryLevelLeft size].width;
const CGFloat capBarLeftOffset = 3.0f * drawingUnit;
CGFloat capBarHeight = [batteryLevelLeft size].height;
CGFloat capBarTopOffset = (([batteryOutline size].height - (EXTRA_TOP_OFFSET * drawingUnit)) - capBarHeight) / 2.0;
CGFloat capBarLength = ceil(percent / 8.0f) * drawingUnit; // max width is 13 units
if (capBarLength <= (2 * drawingUnit)) { capBarLength = (2 * drawingUnit) + 0.1f; } // must be _greater_than_ the end segments
[batteryOutline lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
NSDrawThreePartImage(NSMakeRect(capBarLeftOffset, capBarTopOffset, capBarLength, capBarHeight),
batteryLevelLeft, batteryLevelMiddle, batteryLevelRight,
NO,
NSCompositeCopy,
0.94f,
NO);
[batteryOutline unlockFocus];
return batteryOutline;
}
- (NSImage *)getBatteryIconNamed:(NSString *)iconName {
return [batteryIcons objectForKey:iconName];
}
- (NSImage *)loadBatteryIconNamed:(NSString *)iconName
{
NSString *fileName = [NSString stringWithFormat:@"/System/Library/CoreServices/Menu Extras/Battery.menu/Contents/Resources/%@.pdf", iconName];
return [[NSImage alloc] initWithContentsOfFile:fileName];
}
- (void)cacheBatteryIcon {
// special treatment for the BatteryCharging, BatteryCharged, and BatteryEmpty images
// they need to be shifted down by 2points to be in the same position as Apple's
NSImage *imgCharging = [self imageOffset:[self loadBatteryIconNamed:@"BatteryCharging"] top:EXTRA_TOP_OFFSET];
NSImage *imgCharged = [self imageOffset:[self loadBatteryIconNamed:@"BatteryCharged"] top:EXTRA_TOP_OFFSET];
NSImage *imgEmpty = [self imageOffset:[self loadBatteryIconNamed:@"BatteryEmpty"] top:EXTRA_TOP_OFFSET];
// Make the image black and white
imgCharged = [self imageBW:[self imageBW:imgCharged]];
// finally construct the dictionary from which we will retrieve the images at runtime
batteryIcons = [NSDictionary dictionaryWithObjectsAndKeys:
imgCharging, @"BatteryCharging",
imgCharged, @"BatteryCharged",
imgEmpty, @"BatteryEmpty",
[self loadBatteryIconNamed:@"BatteryLevelCapB-L"], @"BatteryLevelCapB-L",
[self loadBatteryIconNamed:@"BatteryLevelCapB-M"], @"BatteryLevelCapB-M",
[self loadBatteryIconNamed:@"BatteryLevelCapB-R"], @"BatteryLevelCapB-R",
[self loadBatteryIconNamed:@"BatteryLevelCapR-L"], @"BatteryLevelCapR-L",
[self loadBatteryIconNamed:@"BatteryLevelCapR-M"], @"BatteryLevelCapR-M",
[self loadBatteryIconNamed:@"BatteryLevelCapR-R"], @"BatteryLevelCapR-R",
nil];
}
- (NSImage *)imageBW:(NSImage *)_image
{
NSImage *image = [_image copy];
[image lockFocus];
// http://stackoverflow.com/a/10033772/304894
CIImage *beginImage = [[CIImage alloc] initWithData:[image TIFFRepresentation]];
CIImage *blackAndWhite = [[CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil] valueForKey:@"outputImage"];
CIImage *output = [[CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", [NSNumber numberWithFloat:0.7], nil] valueForKey:@"outputImage"];
[output drawInRect:NSMakeRect(0, 0, [_image size].width, [_image size].height) fromRect:NSRectFromCGRect([output extent]) operation:NSCompositeSourceOver fraction:1.0];
[image unlockFocus];
return image;
}
- (NSImage *)imageOffset:(NSImage *)_image top:(CGFloat)top
{
NSImage *newImage = [[NSImage alloc] initWithSize:NSMakeSize(_image.size.width, _image.size.height + top)];
[newImage lockFocus];
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[_image drawInRect:NSMakeRect(0, 0, _image.size.width, _image.size.height)
fromRect:NSMakeRect(0, 0, _image.size.width, _image.size.height)
operation:NSCompositeSourceOver
fraction:1.0];
[newImage unlockFocus];
return newImage;
}
- (NSImage *)imageInvertColor:(NSImage *)_image
{
NSImage *image = [_image copy];
[image lockFocus];
CIImage *ciImage = [[CIImage alloc] initWithData:[image TIFFRepresentation]];
CIFilter *filter = [CIFilter filterWithName:@"CIColorInvert"];
[filter setDefaults];
[filter setValue:ciImage forKey:@"inputImage"];
CIImage *output = [filter valueForKey:@"outputImage"];
[output drawInRect:NSMakeRect(0, 0, [_image size].width, [_image size].height) fromRect:NSRectFromCGRect([output extent]) operation:NSCompositeSourceOver fraction:1.0];
[image unlockFocus];
return image;
}
- (void)openEnergySaverPreference:(id)sender
{
[[NSWorkspace sharedWorkspace] openFile:@"/System/Library/PreferencePanes/EnergySaver.prefPane"];
}
- (void)openHomeUrl:(id)sender
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"https://github.com/codler/Battery-Time-Remaining/downloads"]];
}
- (void)toggleStartAtLogin:(id)sender
{
if ([LLManager launchAtLogin])
{
[LLManager setLaunchAtLogin:NO];
[self.statusItem.menu itemWithTag:kBTRMenuStartAtLogin].state = NSOffState;
}
else
{
[LLManager setLaunchAtLogin:YES];
[self.statusItem.menu itemWithTag:kBTRMenuStartAtLogin].state = NSOnState;
}
}
- (void)toggleAdvanced:(id)sender
{
NSMenuItem *item = sender;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"advanced"])
{
item.state = NSOffState;
[self showAdvanced:NO];
[defaults setBool:NO forKey:@"advanced"];
}
else
{
item.state = NSOnState;
[self showAdvanced:YES];
[defaults setBool:YES forKey:@"advanced"];
}
[defaults synchronize];
[self updateStatusItem];
}
- (void)toggleParenthesis:(id)sender
{
NSMenuItem *item = sender;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults boolForKey:@"parentheses"])
{
item.state = NSOffState;
showParenthesis = NO;
[defaults setBool:NO forKey:@"parentheses"];
}
else
{
item.state = NSOnState;
showParenthesis = YES;
[defaults setBool:YES forKey:@"parentheses"];
}
[defaults synchronize];
[self updateStatusItem];
}
- (void)notify:(NSString *)message
{
[self notify:@"Battery Time Remaining" message:message];
}
- (void)notify:(NSString *)title message:(NSString *)message
{
NSUserNotification *notification = [[NSUserNotification alloc] init];
[notification setTitle:title];
[notification setInformativeText:message];
[notification setSoundName:NSUserNotificationDefaultSoundName];
NSUserNotificationCenter *center = [NSUserNotificationCenter defaultUserNotificationCenter];
[center scheduleNotification:notification];
}
- (void)loadNotificationSetting
{
// Fetch user settings for notifications
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *immutableNotifications = [defaults dictionaryForKey:@"notifications"];
if (immutableNotifications)
{
self.notifications = [immutableNotifications mutableCopy];
}
else
{
self.notifications = [NSMutableDictionary new];
}
}
- (void)saveNotificationSetting
{
// Save user settings for notifications
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:self.notifications forKey:@"notifications"];
[defaults synchronize];
}
- (void)toggleNotification:(id)sender
{
// Get menu item
NSMenuItem *item = (NSMenuItem *)sender;
// Toggle state
item.state = (item.state==NSOnState) ? NSOffState : NSOnState;
[self.notifications setValue:[NSNumber numberWithBool:(item.state==NSOnState)?YES:NO] forKey:[NSString stringWithFormat:@"%ld", item.tag]];
[self saveNotificationSetting];
}
- (void)showAdvanced:(BOOL)visible
{
[[self.statusItem.menu itemWithTag:kBTRMenuPowerSourceAdvanced] setHidden:!visible];
[[self.statusItem.menu itemWithTag:kBTRMenuNotification] setHidden:!visible];
}
- (void)optionKeyPressed
{
// http://stackoverflow.com/a/12333909/304894
// Get global modifier key flag, [[NSApp currentEvent] modifierFlags] doesn't update
CGEventRef event = CGEventCreate(NULL);
CGEventFlags flags = CGEventGetFlags(event);
CFRelease(event);
BOOL prevIsOptionKeyPressed = isOptionKeyPressed;
isOptionKeyPressed = (flags & kCGEventFlagMaskAlternate) == kCGEventFlagMaskAlternate;
// Option key was pressed or released
if (prevIsOptionKeyPressed != isOptionKeyPressed)
{
[self showAdvanced:self.advancedSupported && ([[self.statusItem.menu itemWithTag:kBTRMenuSetting].submenu itemWithTag:kBTRMenuAdvanced].state == NSOnState || isOptionKeyPressed) ];
[self updateStatusItemMenu];
}
}
#pragma mark - NSUserNotificationCenterDelegate methods
// Force show notification
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
{
return YES;
}
- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
{
// User has clicked on the notification and will open home URL if newer version is available
if ([[notification informativeText] isEqualToString:NSLocalizedString(@"A newer version is available", @"Update menuitem")])
{
[self openHomeUrl:nil];
}
}
#pragma mark - NSMenuDelegate methods
- (void)menuWillOpen:(NSMenu *)menu
{
[self updateStatusItemMenu];
// Detect instant if option key is pressed
optionKeyPressedTimer = [NSTimer timerWithTimeInterval:0.1
target:self
selector:@selector(optionKeyPressed)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:optionKeyPressedTimer forMode:NSRunLoopCommonModes];
// Update menu every 5 seconds
menuUpdateTimer = [NSTimer timerWithTimeInterval:5
target:self
selector:@selector(updateStatusItemMenu)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:menuUpdateTimer forMode:NSRunLoopCommonModes];
#ifdef CHECK_FOR_UPDATE
// Update menu
NSMenuItem *updaterMenu = [self.statusItem.menu itemWithTag:kBTRMenuUpdater];
// Stop checking if newer version is available
if ([updaterMenu isEnabled])
{
return;
}
// Check for newer version
[[HttpGet new] url:@"https://raw.github.com/codler/Battery-Time-Remaining/master/build_version" success:^(NSString *result) {
NSInteger latestBuildVersion = [result integerValue];
NSInteger currentBuildVersion = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"] integerValue];
NSString *currentBuildVersionText = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
// Wrong format build version
if (!latestBuildVersion)
{
updaterMenu.title = NSLocalizedString(@"Could not check for updates", @"Update menuitem");
return;
}
// Newer version available
if (latestBuildVersion > currentBuildVersion)
{
updaterMenu.title = NSLocalizedString(@"A newer version is available", @"Update menuitem");
[updaterMenu setAction:@selector(openHomeUrl:)];
[updaterMenu setEnabled:YES];
[self notify:NSLocalizedString(@"A newer version is available", @"Update notification")];
}
else
{
updaterMenu.title = [NSString stringWithFormat:@"%@ - v%@", NSLocalizedString(@"Up to date", @"Update menuitem"), currentBuildVersionText];
}
} error:^(NSError *error) {
updaterMenu.title = NSLocalizedString(@"Could not check for updates", @"Update menuitem");
}];
#endif
}
- (void)menuDidClose:(NSMenu *)menu
{
if ([[self.statusItem.menu itemWithTag:kBTRMenuSetting].submenu itemWithTag:kBTRMenuAdvanced].state == NSOffState)
{
[self showAdvanced:NO];
}
[menuUpdateTimer invalidate];
menuUpdateTimer = nil;
[optionKeyPressedTimer invalidate];
optionKeyPressedTimer = nil;
}
@end