Skip to content

Commit

Permalink
Merge pull request #148 from creamelectricart/master
Browse files Browse the repository at this point in the history
Add display sleep support
  • Loading branch information
newmarcel authored May 1, 2020
2 parents 49a3474 + ecc9fef commit 66ec767
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
8 changes: 7 additions & 1 deletion KeepingYouAwake/Extensions/NSUserDefaults+Keys.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyActivateOnLaunch;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyNotificationsEnabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyDefaultTimeInterval;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyAllowDisplaySleep;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled;
FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyBatteryCapacityThresholdEnabled;
Expand All @@ -37,6 +38,11 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled;
*/
@property (nonatomic) NSTimeInterval kya_defaultTimeInterval;

/**
Returns YES if the app should allow the display to sleep while still keeping the system awake. This exposes the `caffeinate -i` command.
*/
@property (nonatomic, getter = kya_shouldAllowDisplaySleep) BOOL kya_allowDisplaySleep;

/**
Returns YES if the menu bar icon should not be highlighted on left and right click.
*/
Expand All @@ -49,7 +55,7 @@ FOUNDATION_EXPORT NSString * const KYAUserDefaultsKeyPreReleaseUpdatesEnabled;

/**
A battery capacity threshold.
If the user defaults value is below 10.0, 10.0 will be returned.
*/
@property (nonatomic) CGFloat kya_batteryCapacityThreshold;
Expand Down
14 changes: 14 additions & 0 deletions KeepingYouAwake/Extensions/NSUserDefaults+Keys.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
NSString * const KYAUserDefaultsKeyActivateOnLaunch = @"info.marcel-dierkes.KeepingYouAwake.ActivateOnLaunch";
NSString * const KYAUserDefaultsKeyNotificationsEnabled = @"info.marcel-dierkes.KeepingYouAwake.NotificationsEnabled";
NSString * const KYAUserDefaultsKeyDefaultTimeInterval = @"info.marcel-dierkes.KeepingYouAwake.TimeInterval";
NSString * const KYAUserDefaultsKeyAllowDisplaySleep = @"info.marcel-dierkes.KeepingYouAwake.AllowDisplaySleep";
NSString * const KYAUserDefaultsKeyMenuBarIconHighlightDisabled = @"info.marcel-dierkes.KeepingYouAwake.MenuBarIconHighlightDisabled";
NSString * const KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled = @"info.marcel-dierkes.KeepingYouAwake.QuitOnTimerExpirationEnabled";

Expand All @@ -21,6 +22,7 @@

@implementation NSUserDefaults (Keys)
@dynamic kya_activateOnLaunch, kya_defaultTimeInterval, kya_notificationsEnabled;
@dynamic kya_allowDisplaySleep, kya_menuBarIconHighlightDisabled;
@dynamic kya_menuBarIconHighlightDisabled;
@dynamic kya_batteryCapacityThresholdEnabled, kya_batteryCapacityThreshold;
@dynamic kya_preReleaseUpdatesEnabled;
Expand Down Expand Up @@ -62,6 +64,18 @@ - (void)setKya_defaultTimeInterval:(NSTimeInterval)defaultTimeInterval
[self setInteger:(NSInteger)defaultTimeInterval forKey:KYAUserDefaultsKeyDefaultTimeInterval]; // decimal places will be cut-off
}

#pragma mark - Allow Display Sleep

- (BOOL)kya_shouldAllowDisplaySleep
{
return [self boolForKey:KYAUserDefaultsKeyAllowDisplaySleep];
}

- (void)setKya_allowDisplaySleep:(BOOL)allowDisplaySleep
{
[self setBool:allowDisplaySleep forKey:KYAUserDefaultsKeyAllowDisplaySleep];
}

#pragma mark - Menu Bar Icon Highlight Disabled

- (BOOL)kya_isMenuBarIconHighlightDisabled
Expand Down
6 changes: 6 additions & 0 deletions KeepingYouAwake/KYASleepWakeTimer/KYASleepWakeTimer.m
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ - (BOOL)isScheduled
- (void)spawnCaffeinateTaskForTimeInterval:(NSTimeInterval)timeInterval
{
NSMutableArray *arguments = [NSMutableArray new];
if([[NSUserDefaults standardUserDefaults] kya_shouldAllowDisplaySleep])
{
[arguments addObject:@"-i"];
}
else
{
[arguments addObject:@"-di"];

if(timeInterval != KYASleepWakeTimeIntervalIndefinite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ @implementation KYAAdvancedPreferencesViewController
- (void)viewDidLoad
{
[super viewDidLoad];

// Check the battery status
self.batteryStatusAvailable = [[KYABatteryStatus new] isBatteryStatusAvailable];

[self configureAdvancedPreferences];
}

- (void)viewWillAppear
{
[super viewWillAppear];

self.preferredContentSize = self.view.fittingSize;
}

Expand All @@ -44,18 +44,21 @@ - (void)viewWillAppear
- (void)configureAdvancedPreferences
{
KYA_AUTO preferences = [NSMutableArray new];

[preferences addObject:[[KYAPreference alloc] initWithTitle:KYA_L10N_ENABLE_EXPERIMENTAL_NOTIFICATION_CENTER_INTEGRATION
defaultsKey:KYAUserDefaultsKeyNotificationsEnabled
]];

[preferences addObject:[[KYAPreference alloc] initWithTitle:KYA_L10N_DISABLE_MENU_BAR_ICON_HIGHLIGHT_COLOR
defaultsKey:KYAUserDefaultsKeyMenuBarIconHighlightDisabled
]];
[preferences addObject:[[KYAPreference alloc] initWithTitle:KYA_L10N_QUIT_ON_TIMER_EXPIRATION
defaultsKey:KYAUserDefaultsKeyIsQuitOnTimerExpirationEnabled
]];

[preferences addObject:[[KYAPreference alloc] initWithTitle:NSLocalizedString(@"Allow the display to sleep", nil)
defaultsKey:KYAUserDefaultsKeyAllowDisplaySleep
]];

self.preferences = [preferences copy];
}

Expand All @@ -69,7 +72,7 @@ - (IBAction)resetAdvancedPreferences:(id)sender
}
[NSUserDefaults.standardUserDefaults synchronize];
[self.tableView reloadData];

// Disable battery status integration
KYA_AUTO_VAR keyPath = [NSString stringWithFormat:@"values.%@", KYAUserDefaultsKeyBatteryCapacityThresholdEnabled];
[self.defaultsController setValue:@NO forKeyPath:keyPath];
Expand Down

0 comments on commit 66ec767

Please sign in to comment.