diff --git a/Battery Time Remaining/AppDelegate.h b/Battery Time Remaining/AppDelegate.h index de62aab..fb7f1e6 100644 --- a/Battery Time Remaining/AppDelegate.h +++ b/Battery Time Remaining/AppDelegate.h @@ -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 diff --git a/Battery Time Remaining/AppDelegate.m b/Battery Time Remaining/AppDelegate.m index db13fd8..077145c 100644 --- a/Battery Time Remaining/AppDelegate.m +++ b/Battery Time Remaining/AppDelegate.m @@ -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) { @@ -48,6 +51,7 @@ @interface AppDelegate () BOOL showPercentage; BOOL hideIcon; BOOL hideTime; + BOOL enableCriticalBatteryAlert; BOOL iconRight; } @@ -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:@""]; @@ -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]; @@ -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; } } @@ -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; @@ -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