Skip to content

Commit e8846c1

Browse files
committed
Display correct charge time and make the battery icon show a bar indicating battery left.
1 parent 2a451d8 commit e8846c1

File tree

3 files changed

+60
-13
lines changed

3 files changed

+60
-13
lines changed

Battery Time Remaining/AppDelegate.m

Lines changed: 55 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ - (void)updateStatusItem
8181
if (timeTilCharged > 0)
8282
{
8383
// Calculate the hour/minutes
84-
NSInteger hour = (int)timeTilCharged / 3600;
85-
NSInteger minute = (int)timeTilCharged % 3600;
84+
NSInteger hour = timeTilCharged / 60;
85+
NSInteger minute = timeTilCharged % 60;
8686

8787
// Return the time remaining string
8888
self.statusItem.image = [self getBatteryIconNamed:@"BatteryCharging"];
@@ -111,13 +111,60 @@ - (void)updateStatusItem
111111
// Time is known!
112112
else
113113
{
114-
// Calculate the hour/minutes
115-
NSInteger hour = (int)timeRemaining / 3600;
116-
NSInteger minute = (int)timeRemaining % 3600 / 60;
114+
// Get list of power sources
115+
CFTypeRef psBlob = IOPSCopyPowerSourcesInfo();
116+
CFArrayRef psList = IOPSCopyPowerSourcesList(psBlob);
117+
118+
// Loop through the list of power sources
119+
CFIndex count = CFArrayGetCount(psList);
120+
for (CFIndex i = 0; i < count; i++)
121+
{
122+
CFTypeRef powersource = CFArrayGetValueAtIndex(psList, i);
123+
CFDictionaryRef description = IOPSGetPowerSourceDescription(psBlob, powersource);
124+
125+
// Calculate the percent
126+
NSNumber *currentBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey));
127+
NSNumber *maxBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey));
128+
129+
NSInteger percent = (int)[currentBatteryCapacity doubleValue] / [maxBatteryCapacity doubleValue] * 100;
130+
131+
// Calculate the hour/minutes
132+
NSInteger hour = (int)timeRemaining / 3600;
133+
NSInteger minute = (int)timeRemaining % 3600 / 60;
134+
135+
// Make dynamic Battery icon
136+
NSImage *batteryDynamic = [self getBatteryIconNamed:@"BatteryEmpty"];
137+
138+
[batteryDynamic lockFocus];
139+
140+
NSRect sourceRect;
141+
sourceRect.origin = NSZeroPoint;
142+
sourceRect.origin.x += [batteryDynamic size].width / 100 * 15;
143+
sourceRect.origin.y += [batteryDynamic size].height / 50 * 10;
144+
sourceRect.size = [batteryDynamic size];
145+
sourceRect.size.width -= [batteryDynamic size].width / 100 * 40;
146+
sourceRect.size.height -= [batteryDynamic size].height / 50 * 20;
147+
148+
sourceRect.size.width -= [batteryDynamic size].width / 100 * (60.0f - (60.0f / 100.0f * percent));
149+
150+
if (percent > 15)
151+
{
152+
[[NSColor blackColor] set];
153+
}
154+
else
155+
{
156+
[[NSColor redColor] set];
157+
}
158+
159+
NSRectFill (sourceRect);
160+
161+
[batteryDynamic unlockFocus];
162+
163+
// Return the time remaining string
164+
self.statusItem.image = batteryDynamic;
165+
self.statusItem.title = [NSString stringWithFormat:@" %ld:%02ld", hour, minute];
166+
}
117167

118-
// Return the time remaining string
119-
self.statusItem.image = [self getBatteryIconNamed:@"BatteryEmpty"];
120-
self.statusItem.title = [NSString stringWithFormat:@" %ld:%02ld", hour, minute];
121168
}
122169
}
123170

Battery Time Remaining/Battery Time Remaining-Info.plist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5-
<key>LSUIElement</key>
6-
<true/>
75
<key>CFBundleDevelopmentRegion</key>
86
<string>en</string>
97
<key>CFBundleExecutable</key>
@@ -19,13 +17,15 @@
1917
<key>CFBundlePackageType</key>
2018
<string>APPL</string>
2119
<key>CFBundleShortVersionString</key>
22-
<string>1.0</string>
20+
<string>1.1</string>
2321
<key>CFBundleSignature</key>
2422
<string>????</string>
2523
<key>CFBundleVersion</key>
26-
<string>1</string>
24+
<string>2</string>
2725
<key>LSMinimumSystemVersion</key>
2826
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
27+
<key>LSUIElement</key>
28+
<true/>
2929
<key>NSHumanReadableCopyright</key>
3030
<string>Copyright © 2012 Han Lin Yap and Wrep. All rights reserved.</string>
3131
<key>NSMainNibFile</key>

Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ How do I install it?
1313

1414
Two options:
1515

16-
- Download [version 0.2](http://cl.ly/1K3M0h1x0Z1b), unzip and run the App
16+
- Download [version 1.1](https://github.com/codler/Battery-Time-Remaining/downloads), run the App
1717
- Download the source here from Github and compile it with XCode
1818

1919
Is it accurate?

0 commit comments

Comments
 (0)