@@ -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
0 commit comments