Skip to content

Commit

Permalink
add mute-on-lock trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
yonilevy committed Nov 24, 2020
1 parent a24c229 commit ca98c2e
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 11 deletions.
54 changes: 46 additions & 8 deletions automute/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ @interface AppDelegate () <MJMenuBarControllerDelegate, MJDisableMuteManagerDele
@property(nonatomic, strong) StartAtLoginController *startAtLoginController;
@property(nonatomic) BOOL isMutingDisabled;
@property(nonatomic) BOOL didMuteOnLastSleep;
@property(nonatomic) BOOL didMuteOnLastLock;
@end

@implementation AppDelegate
Expand Down Expand Up @@ -43,15 +44,29 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
[self.menuBarController showLaunchAtLoginPopup];
}

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(willSleep)
name:NSWorkspaceWillSleepNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(willSleep)
name:NSWorkspaceWillSleepNotification
object:nil];

[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
selector:@selector(didWake)
name:NSWorkspaceDidWakeNotification
object:nil];
[[[NSWorkspace sharedWorkspace] notificationCenter]
addObserver:self
selector:@selector(didWake)
name:NSWorkspaceDidWakeNotification
object:nil];

[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didLock)
name:@"com.apple.screenIsLocked"
object:nil];

[[NSDistributedNotificationCenter defaultCenter]
addObserver:self
selector:@selector(didUnlock)
name:@"com.apple.screenIsUnlocked"
object:nil];
}

- (void)willSleep
Expand All @@ -67,6 +82,19 @@ - (void)didWake
}
}

- (void)didLock
{
self.didMuteOnLastLock =
[self menuBarController_isSetToMuteOnLock] && !self.headphoneDetector->areHeadphonesConnected() && [self mute];
}

- (void)didUnlock
{
if (self.didMuteOnLastLock) {
[self.menuBarController showLockMuteNotification];
}
}

- (void)onHeadphoneStateChangedTo:(bool)connected
{
[self.menuBarController updateMenuIcon:connected];
Expand Down Expand Up @@ -129,6 +157,16 @@ - (void)menuBarController_setMuteOnSleep:(BOOL)muteOnSleep
[self.userDefaults setMuteOnSleep:muteOnSleep];
}

- (BOOL)menuBarController_isSetToMuteOnLock
{
return self.userDefaults.isSetToMuteOnLock;
}

- (void)menuBarController_setMuteOnLock:(BOOL)muteOnLock
{
[self.userDefaults setMuteOnLock:muteOnLock];
}

- (BOOL)menuBarController_isSetToMuteOnHeadphones
{
return self.userDefaults.isSetToMuteOnHeadphones;
Expand Down
3 changes: 3 additions & 0 deletions automute/MJMenuBarController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- (void)menuBarController_setLaunchAtLogin:(BOOL)launchAtLogin;
- (BOOL)menuBarController_isSetToMuteOnSleep;
- (void)menuBarController_setMuteOnSleep:(BOOL)muteOnSleep;
- (BOOL)menuBarController_isSetToMuteOnLock;
- (void)menuBarController_setMuteOnLock:(BOOL)muteOnLock;
- (BOOL)menuBarController_isSetToMuteOnHeadphones;
- (void)menuBarController_setMuteOnHeadphones:(BOOL)muteOnHeadphones;
- (void)menuBarController_toggleMuteNotifications;
Expand All @@ -26,6 +28,7 @@
- (void)showLaunchAtLoginPopup;
- (void)showHeadphonesDisconnectedMuteNotification;
- (void)showSleepMuteNotification;
- (void)showLockMuteNotification;
- (void)updateMenuIcon:(BOOL)headphonesConnected;
- (void)terminate;
@end
19 changes: 17 additions & 2 deletions automute/MJMenuBarController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
static const NSInteger MENU_ITEM_MUTE_ON_SLEEP = 104;
static const NSInteger MENU_ITEM_MUTE_ON_HEADPHONES = 105;
static const NSInteger MENU_ITEM_MUTE_NOTIFICATIONS = 106;
static const NSInteger MENU_ITEM_MUTE_ON_LOCK = 107;

static const NSInteger MENU_ITEM_DISABLE_1H = 201;
static const NSInteger MENU_ITEM_DISABLE_6H = 202;
Expand Down Expand Up @@ -56,9 +57,10 @@ - (NSMenu *)createMenuWithItems
[menu addItem:[self buildMenuItemWithTitle:@"Welcome" action:@selector(showWelcomePopup)]];
[menu addItem:[NSMenuItem separatorItem]];
[menu addItem:[NSMenuItem separatorItem]];
NSMenuItem *triggersItem = [self buildMenuItemWithTitle:@"Triggers" action:nil];
NSMenuItem *triggersItem = [self buildMenuItemWithTitle:@"Mute Triggers" action:nil];
NSMenu *muteOnSubmenu = [[NSMenu alloc] init];
[muteOnSubmenu addItem:[self buildMenuItemWithTitle:@"Mac Awakes from Sleep" action:@selector(muteOnSleepToggled) tag:MENU_ITEM_MUTE_ON_SLEEP]];
[muteOnSubmenu addItem:[self buildMenuItemWithTitle:@"Mac Goes to Sleep" action:@selector(muteOnSleepToggled) tag:MENU_ITEM_MUTE_ON_SLEEP]];
[muteOnSubmenu addItem:[self buildMenuItemWithTitle:@"Mac Gets Locked / Enters Screen Saver" action:@selector(muteOnLockToggled) tag:MENU_ITEM_MUTE_ON_LOCK]];
[muteOnSubmenu addItem:[self buildMenuItemWithTitle:@"Headphones Disconnected" action:@selector(muteOnHeadphonesToggled) tag:MENU_ITEM_MUTE_ON_HEADPHONES]];
triggersItem.submenu = muteOnSubmenu;
[menu addItem:triggersItem];
Expand Down Expand Up @@ -146,6 +148,11 @@ - (void)muteOnSleepToggled
[self.delegate menuBarController_setMuteOnSleep:![self.delegate menuBarController_isSetToMuteOnSleep]];
}

- (void)muteOnLockToggled
{
[self.delegate menuBarController_setMuteOnLock:![self.delegate menuBarController_isSetToMuteOnLock]];
}

- (void)muteOnHeadphonesToggled
{
[self.delegate menuBarController_setMuteOnHeadphones:![self.delegate menuBarController_isSetToMuteOnHeadphones]];
Expand Down Expand Up @@ -218,6 +225,11 @@ - (void)showSleepMuteNotification
[self showNotificationWithTitle:@"Woke up from sleep" body:@"Sound Muted."];
}

- (void)showLockMuteNotification
{
[self showNotificationWithTitle:@"Unlocked" body:@"Sound Muted."];
}

- (void)showNotificationWithTitle:(NSString *)title
body:(NSString *)body
{
Expand Down Expand Up @@ -265,6 +277,9 @@ - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
if (menuItem.tag == MENU_ITEM_MUTE_ON_SLEEP) {
menuItem.state = [self.delegate menuBarController_isSetToMuteOnSleep] ? NSOnState : NSOffState;
}
if (menuItem.tag == MENU_ITEM_MUTE_ON_LOCK) {
menuItem.state = [self.delegate menuBarController_isSetToMuteOnLock] ? NSOnState : NSOffState;
}
if (menuItem.tag == MENU_ITEM_MUTE_ON_HEADPHONES) {
menuItem.state = [self.delegate menuBarController_isSetToMuteOnHeadphones] ? NSOnState : NSOffState;
}
Expand Down
5 changes: 4 additions & 1 deletion automute/MJUserDefaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
- (NSTimeInterval)getScheduledTimeToEnableMuting;

- (BOOL)isSetToMuteOnSleep;
- (void)setMuteOnSleep:(BOOL)sleep;
- (void)setMuteOnSleep:(BOOL)muteOnSleep;

- (BOOL)isSetToMuteOnLock;
- (void)setMuteOnLock:(BOOL)muteOnLock;

- (BOOL)isSetToMuteOnHeadphones;
- (void)setMuteOnHeadphones:(BOOL)muteOnHeadphones;
Expand Down
13 changes: 13 additions & 0 deletions automute/MJUserDefaults.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
static NSString *const MUTING_DISABLED = @"MUTING_DISABLED";
static NSString *const SCHEDULED_TIME_TO_ENABLE_MUTING = @"SCHEDULED_TIME_TO_ENABLE_MUTING";
static NSString *const MUTE_ON_SLEEP = @"MUTE_ON_SLEEP";
static NSString *const MUTE_ON_LOCK = @"MUTE_ON_LOCK";
static NSString *const MUTE_ON_HEADPHONES = @"MUTE_ON_HEADPHONES";
static NSString *const MUTE_NOTIFICATIONS_ENABLED = @"MUTE_NOTIFICATIONS_ENABLED";

Expand All @@ -22,6 +23,7 @@ - (instancetype)init {
self.defaults = [NSUserDefaults standardUserDefaults];
[self.defaults registerDefaults:@{
MUTE_ON_SLEEP: @(YES),
MUTE_ON_LOCK: @(YES),
MUTE_ON_HEADPHONES: @(YES),
MUTE_NOTIFICATIONS_ENABLED: @(YES)
}];
Expand Down Expand Up @@ -85,6 +87,17 @@ - (void)setMuteOnSleep:(BOOL)muteOnSleep
[self.defaults synchronize];
}

- (BOOL)isSetToMuteOnLock
{
return [self.defaults boolForKey:MUTE_ON_LOCK];
}

- (void)setMuteOnLock:(BOOL)muteOnLock
{
[self.defaults setBool:muteOnLock forKey:MUTE_ON_LOCK];
[self.defaults synchronize];
}

- (BOOL)isSetToMuteOnHeadphones
{
return [self.defaults boolForKey:MUTE_ON_HEADPHONES];
Expand Down

0 comments on commit ca98c2e

Please sign in to comment.