Skip to content

Commit

Permalink
Removed special chars in statusbar and cleaned up the code a bit
Browse files Browse the repository at this point in the history
- Removed special chars around the message in the menubar
- Call IOPSGetTimeRemainingEstimate() only once
- Added comments
- Using more properties to improve readability
  • Loading branch information
mac-cain13 authored and codler committed Aug 5, 2012
1 parent 21e5139 commit 6e6c78f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Battery Time Remaining/AppDelegate.h
Expand Up @@ -10,7 +10,7 @@

@interface AppDelegate : NSObject <NSApplicationDelegate>

- (NSString*)GetTimeRemainingText;
- (NSString*)getTimeRemainingText;

@property (assign) IBOutlet NSWindow *window;
@property (strong) NSStatusItem *statusItem;
Expand Down
63 changes: 38 additions & 25 deletions Battery Time Remaining/AppDelegate.m
Expand Up @@ -9,10 +9,12 @@
#import "AppDelegate.h"
#import <IOKit/ps/IOPowerSources.h>

static void PowerSourceChanged(void * context) {

AppDelegate * self = (__bridge AppDelegate *)context;
[self.statusItem setTitle:[self GetTimeRemainingText]];
// IOPS notification callback on power source change
static void PowerSourceChanged(void * context)
{
// Update the time remaining text
AppDelegate *self = (__bridge AppDelegate *)context;
self.statusItem.title = [self getTimeRemainingText];
}

@implementation AppDelegate
Expand All @@ -21,35 +23,46 @@ @implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSMenu *stackMenu = [[NSMenu alloc] initWithTitle:@"Status Menu"];
[stackMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""];

statusItem = [[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength];
[statusItem setHighlightMode:YES];
[statusItem setMenu:stackMenu];

NSString *title = [self GetTimeRemainingText];
// Build the status menu
NSMenu *statusMenu = [[NSMenu alloc] initWithTitle:@"Status Menu"];
[statusMenu addItemWithTitle:@"Quit" action:@selector(terminate:) keyEquivalent:@""];

[statusItem setTitle:title];
// Create the status item and set initial text
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
statusItem.highlightMode = YES;
statusItem.menu = statusMenu;
statusItem.title = [self getTimeRemainingText];

// Update Power Source
// Capture Power Source updates and make sure our callback is called
CFRunLoopSourceRef loop = IOPSNotificationCreateRunLoopSource(PowerSourceChanged, (__bridge void *)self);
CFRunLoopAddSource(CFRunLoopGetCurrent(), loop, kCFRunLoopDefaultMode);
CFRelease(loop);
}

- (NSString*)GetTimeRemainingText
- (NSString *)getTimeRemainingText
{
if (kIOPSTimeRemainingUnlimited == IOPSGetTimeRemainingEstimate()) {
return @"Ω Unlimited Ω";
} else if (kIOPSTimeRemainingUnknown == IOPSGetTimeRemainingEstimate()) {
return @"Ω Calculating Ω";
} else {
CFTimeInterval time = IOPSGetTimeRemainingEstimate();
NSInteger hour = (int)time / 3600;
NSInteger minut = (int)time % 3600 / 60;
return [NSString stringWithFormat:@"<%ld:%02ld>", hour, minut];
// Get the estimated time remaining
CFTimeInterval timeRemaining = IOPSGetTimeRemainingEstimate();

// We're connected to an unlimited power source (AC adapter probably)
if (kIOPSTimeRemainingUnlimited == timeRemaining)
{
return @"Unlimited";
}
// Still calculating the estimated time remaining...
else if (kIOPSTimeRemainingUnknown == timeRemaining)
{
return @"Calculating";
}
// Time is known!
else
{
// Calculate the hour/minutes
NSInteger hour = (int)timeRemaining / 3600;
NSInteger minute = (int)timeRemaining % 3600 / 60;

// Return the time remaining string
return [NSString stringWithFormat:@"%ld:%02ld", hour, minute];
}
}

Expand Down

0 comments on commit 6e6c78f

Please sign in to comment.