Skip to content

Commit

Permalink
Display correct charge time and make the battery icon show a bar indi…
Browse files Browse the repository at this point in the history
…cating battery left.
  • Loading branch information
codler committed Aug 5, 2012
1 parent 2a451d8 commit e8846c1
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 13 deletions.
63 changes: 55 additions & 8 deletions Battery Time Remaining/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ - (void)updateStatusItem
if (timeTilCharged > 0)
{
// Calculate the hour/minutes
NSInteger hour = (int)timeTilCharged / 3600;
NSInteger minute = (int)timeTilCharged % 3600;
NSInteger hour = timeTilCharged / 60;
NSInteger minute = timeTilCharged % 60;

// Return the time remaining string
self.statusItem.image = [self getBatteryIconNamed:@"BatteryCharging"];
Expand Down Expand Up @@ -111,13 +111,60 @@ - (void)updateStatusItem
// Time is known!
else
{
// Calculate the hour/minutes
NSInteger hour = (int)timeRemaining / 3600;
NSInteger minute = (int)timeRemaining % 3600 / 60;
// 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);

// Calculate the percent
NSNumber *currentBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSCurrentCapacityKey));
NSNumber *maxBatteryCapacity = CFDictionaryGetValue(description, CFSTR(kIOPSMaxCapacityKey));

NSInteger percent = (int)[currentBatteryCapacity doubleValue] / [maxBatteryCapacity doubleValue] * 100;

// Calculate the hour/minutes
NSInteger hour = (int)timeRemaining / 3600;
NSInteger minute = (int)timeRemaining % 3600 / 60;

// Make dynamic Battery icon
NSImage *batteryDynamic = [self getBatteryIconNamed:@"BatteryEmpty"];

[batteryDynamic lockFocus];

NSRect sourceRect;
sourceRect.origin = NSZeroPoint;
sourceRect.origin.x += [batteryDynamic size].width / 100 * 15;
sourceRect.origin.y += [batteryDynamic size].height / 50 * 10;
sourceRect.size = [batteryDynamic size];
sourceRect.size.width -= [batteryDynamic size].width / 100 * 40;
sourceRect.size.height -= [batteryDynamic size].height / 50 * 20;

sourceRect.size.width -= [batteryDynamic size].width / 100 * (60.0f - (60.0f / 100.0f * percent));

if (percent > 15)
{
[[NSColor blackColor] set];
}
else
{
[[NSColor redColor] set];
}

NSRectFill (sourceRect);

[batteryDynamic unlockFocus];

// Return the time remaining string
self.statusItem.image = batteryDynamic;
self.statusItem.title = [NSString stringWithFormat:@" %ld:%02ld", hour, minute];
}

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

Expand Down
8 changes: 4 additions & 4 deletions Battery Time Remaining/Battery Time Remaining-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>LSUIElement</key>
<true/>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand All @@ -19,13 +17,15 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>LSMinimumSystemVersion</key>
<string>${MACOSX_DEPLOYMENT_TARGET}</string>
<key>LSUIElement</key>
<true/>
<key>NSHumanReadableCopyright</key>
<string>Copyright © 2012 Han Lin Yap and Wrep. All rights reserved.</string>
<key>NSMainNibFile</key>
Expand Down
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ How do I install it?

Two options:

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

Is it accurate?
Expand Down

0 comments on commit e8846c1

Please sign in to comment.