Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions Battery Time Remaining/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@
#ifndef _BTR_MENU
#define _BTR_MENU

#define kBTRMenuPowerSourcePercent 1
#define kBTRMenuPowerSourceState 2
#define kBTRMenuPowerSourceAdvanced 3
#define kBTRMenuStartAtLogin 4
#define kBTRMenuNotification 5
#define kBTRMenuSetting 6
#define kBTRMenuAdvanced 7
#define kBTRMenuParenthesis 8
#define kBTRMenuFahrenheit 9
#define kBTRMenuPercentage 10
#define kBTRMenuHideIcon 11
#define kBTRMenuHideTime 12
#define kBTRMenuIconRight 13
#define kBTRMenuEnergySaverSetting 14
#define kBTRMenuUpdater 15
#define kBTRMenuQuitKey 16
#define kBTRMenuPowerSourcePercent 1
#define kBTRMenuPowerSourceState 2
#define kBTRMenuPowerSourceAdvanced 3
#define kBTRMenuStartAtLogin 4
#define kBTRMenuNotification 5
#define kBTRMenuSetting 6
#define kBTRMenuAdvanced 7
#define kBTRMenuParenthesis 8
#define kBTRMenuFahrenheit 9
#define kBTRMenuPercentage 10
#define kBTRMenuHideIcon 11
#define kBTRMenuHideTime 12
#define kBTRMenuIconRight 13
#define kBTRMenuEnergySaverSetting 14
#define kBTRMenuUpdater 15
#define kBTRMenuQuitKey 16
#define kBTRMenuCriticalBatteryAlert 17

#endif

Expand Down
52 changes: 52 additions & 0 deletions Battery Time Remaining/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// exact same look.
#define EXTRA_TOP_OFFSET 2.0f

// This value is used by the critical battery alert.
#define CRITICAL_BATTERY 10

// IOPS notification callback on power source change
static void PowerSourceChanged(void * context)
{
Expand All @@ -48,6 +51,7 @@ @interface AppDelegate ()
BOOL showPercentage;
BOOL hideIcon;
BOOL hideTime;
BOOL enableCriticalBatteryAlert;
BOOL iconRight;
}

Expand Down Expand Up @@ -132,6 +136,13 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
advancedSubmenuItem.target = self;
advancedSubmenuItem.state = ([[NSUserDefaults standardUserDefaults] boolForKey:@"advanced"]) ? NSOnState : NSOffState;
[advancedSubmenuItem setHidden:!self.advancedSupported];

// Critical battery alert item
NSMenuItem *enableCriticalBatteryAlertSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Enable critical battery alert", @"Enable critical battery alert/dialog setting") action:@selector(toggleEnableCriticalBatteryAlert:) keyEquivalent:@""];
[enableCriticalBatteryAlertSubmenuItem setTag:kBTRMenuCriticalBatteryAlert];
enableCriticalBatteryAlertSubmenuItem.target = self;
enableCriticalBatteryAlert = [[NSUserDefaults standardUserDefaults] boolForKey:@"criticalBatteryAlert"];
enableCriticalBatteryAlertSubmenuItem.state = (enableCriticalBatteryAlert) ? NSOnState : NSOffState;

// Time display control menu item
NSMenuItem *parenthesisSubmenuItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Display time with parentheses", @"Display time with parentheses setting") action:@selector(toggleParenthesis:) keyEquivalent:@""];
Expand Down Expand Up @@ -178,6 +189,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
// Build the setting submenu
NSMenu *settingSubmenu = [[NSMenu alloc] initWithTitle:@"Setting Menu"];
[settingSubmenu addItem:advancedSubmenuItem];
[settingSubmenu addItem:enableCriticalBatteryAlertSubmenuItem];
[settingSubmenu addItem:parenthesisSubmenuItem];
[settingSubmenu addItem:fahrenheitSubmenuItem];
[settingSubmenu addItem:percentageSubmenuItem];
Expand Down Expand Up @@ -387,6 +399,11 @@ - (void)updateStatusItem
{
[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]];
}

if (self.currentPercent == CRITICAL_BATTERY) {
[self showCriticalBatteryAlert];
}

self.previousPercent = self.currentPercent;
}
}
Expand Down Expand Up @@ -721,6 +738,28 @@ - (void)toggleAdvanced:(id)sender
[self updateStatusItem];
}

- (void)toggleEnableCriticalBatteryAlert:(id)sender
{
NSMenuItem *item = sender;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

if ([defaults boolForKey:@"criticalBatteryAlert"])
{
item.state = NSOffState;
enableCriticalBatteryAlert = NO;
[defaults setBool:NO forKey:@"criticalBatteryAlert"];
}
else
{
item.state = NSOnState;
enableCriticalBatteryAlert = YES;
[defaults setBool:YES forKey:@"criticalBatteryAlert"];
}
[defaults synchronize];

[self updateStatusItem];
}

- (void)toggleParenthesis:(id)sender
{
NSMenuItem *item = sender;
Expand Down Expand Up @@ -868,6 +907,19 @@ - (void)notify:(NSString *)title message:(NSString *)message
[center scheduleNotification:notification];
}

- (void)showCriticalBatteryAlert
{
if (!enableCriticalBatteryAlert) {
return;
}

NSAlert *alert = [[NSAlert alloc] init];
[alert setAlertStyle:NSWarningAlertStyle];
[alert setMessageText:NSLocalizedString(@"Critical Battery",@"Title of alert")];
[alert setInformativeText:NSLocalizedString(@"Please connect your computer to a charger.","Body of critical battery alert")];
[alert runModal];
}

- (void)loadNotificationSetting
{
// Fetch user settings for notifications
Expand Down