diff --git a/Mute Me Now/AppDelegate.h b/Mute Me Now/AppDelegate.h index a7546a5..73d4465 100644 --- a/Mute Me Now/AppDelegate.h +++ b/Mute Me Now/AppDelegate.h @@ -13,6 +13,9 @@ - (IBAction)menuMenuItemAction:(id)sender; +- (void) hideMenuBar:(BOOL)enableState; + +- (void) shortCutKeyPressed; @end diff --git a/Mute Me Now/AppDelegate.m b/Mute Me Now/AppDelegate.m index eff0f69..a057256 100644 --- a/Mute Me Now/AppDelegate.m +++ b/Mute Me Now/AppDelegate.m @@ -7,6 +7,7 @@ #import static const NSTouchBarItemIdentifier muteIdentifier = @"pp.mute"; +static NSString *const MASCustomShortcutKey = @"customShortcut"; @interface AppDelegate () @@ -27,6 +28,24 @@ @implementation AppDelegate - (void) awakeFromNib { + BOOL hideStatusBar = NO; + + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hide_status_bar"] != nil) { + hideStatusBar = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"]; + } + + if (!hideStatusBar) { + [self setupStatusBarItem]; + } + + [[NSUserDefaults standardUserDefaults] setBool:hideStatusBar forKey:@"hide_status_bar"]; + + // masshortcut + [self setShortcutKey]; +} + +- (void) setupStatusBarItem { + self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]; NSImage* statusImage = [self getStatusBarImage]; @@ -41,25 +60,37 @@ - (void) awakeFromNib { self.statusBar.highlightMode = YES; self.statusBar.enabled = YES; self.statusBar.menu = self.statusMenu; - - // masshortcut + +} + +- (void) setShortcutKey { // default shortcut is "Shift Command 0" MASShortcut *firstLaunchShortcut = [MASShortcut shortcutWithKeyCode:kVK_ANSI_0 modifierFlags:NSEventModifierFlagCommand | NSEventModifierFlagShift]; NSData *firstLaunchShortcutData = [NSKeyedArchiver archivedDataWithRootObject:firstLaunchShortcut]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults registerDefaults:@{ + MASCustomShortcutKey : firstLaunchShortcutData + }]; - [[MASShortcutMonitor sharedMonitor] registerShortcut:firstLaunchShortcut withAction:^{ - [self shortCutKeyPressed]; - }]; + [defaults synchronize]; - // Register default values to be used for the first app start - [defaults registerDefaults:@{ - @"customShortcut" : firstLaunchShortcutData + + [[MASShortcutMonitor sharedMonitor] registerShortcut:firstLaunchShortcut withAction:^{ + [self shortCutKeyPressed]; }]; + +} +- (void) hideMenuBar: (BOOL) enableState { + + if (!enableState) { + [self setupStatusBarItem]; + } else { + self.statusBar = nil; } +} - (void) shortCutKeyPressed { @@ -102,10 +133,6 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // fires if we enter / exit dark mode [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil]; - - - - } -(void)darkModeChanged:(NSNotification *)notif diff --git a/Mute Me Now/Base.lproj/Main.storyboard b/Mute Me Now/Base.lproj/Main.storyboard index f4f356b..a3772b6 100644 --- a/Mute Me Now/Base.lproj/Main.storyboard +++ b/Mute Me Now/Base.lproj/Main.storyboard @@ -712,11 +712,11 @@ - + - + @@ -724,7 +724,7 @@ - + @@ -732,7 +732,7 @@ - + @@ -740,7 +740,7 @@ - + @@ -758,7 +758,7 @@ - + @@ -766,7 +766,7 @@ + + + + + + + + + + + + + @@ -819,29 +841,36 @@ + + + + + - + + + - + diff --git a/Mute Me Now/ViewController.h b/Mute Me Now/ViewController.h index bb6fff8..2055d0f 100644 --- a/Mute Me Now/ViewController.h +++ b/Mute Me Now/ViewController.h @@ -8,6 +8,8 @@ @property (weak) IBOutlet NSButton *autoLoginState; @property (weak) IBOutlet NSButton *showInMenuBarState; +@property (strong) IBOutlet MASShortcutView *masShortCutView; + - (IBAction)showMenuBarChanged:(id)sender; @end diff --git a/Mute Me Now/ViewController.m b/Mute Me Now/ViewController.m index 8566022..74c5c1b 100644 --- a/Mute Me Now/ViewController.m +++ b/Mute Me Now/ViewController.m @@ -1,9 +1,14 @@ #import "ViewController.h" +#import "AppDelegate.h" static NSString *githubURL = @"https://github.com/pixel-point/mute-me"; static NSString *projectURL = @"https://muteme.pixelpoint.io/"; static NSString *companyURL = @"https://pixelpoint.io/"; +static NSString *const MASCustomShortcutKey = @"customShortcut"; + +static void *MASObservingContext = &MASObservingContext; + @implementation ViewController - (void)viewDidLoad { @@ -21,23 +26,61 @@ - (void)viewDidLoad { [self.autoLoginState setState: !state]; - BOOL statusBarState = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar"]; - [self.showInMenuBarState setState: statusBarState]; - + BOOL hideStatusBarState = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"]; + [self.showInMenuBarState setState: hideStatusBarState]; + + // enable to nil out preferences - //[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"status_bar"]; + //[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"hide_status_bar"]; //[[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"auto_login"]; //[[NSUserDefaults standardUserDefaults] synchronize]; + // Make a global context reference + void *kGlobalShortcutContext = &kGlobalShortcutContext; + + // this sets the existing shortcut and allows it to save + [self.masShortCutView setAssociatedUserDefaultsKey:MASCustomShortcutKey]; + + + // Implement when loading view + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + [defaults addObserver:self forKeyPath:MASCustomShortcutKey + options:NSKeyValueObservingOptionInitial + context:MASObservingContext]; + + + + } +- (void) observeValueForKeyPath: (NSString*) keyPath ofObject: (id) object change: (NSDictionary*) change context: (void*) context +{ + + if (context != MASObservingContext) { + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; + return; + } + + if ([keyPath isEqualToString:MASCustomShortcutKey]) { + + NSLog (@"change key"); + + [[MASShortcutBinder sharedBinder] bindShortcutWithDefaultsKey:MASCustomShortcutKey toAction:^{ + + AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate]; + [appDelegate shortCutKeyPressed]; + }]; + + } + +} + + -(void)viewDidAppear { [super viewDidAppear]; [[self.view window] setTitle:@"Mute me"]; [[self.view window] center]; - - } @@ -77,20 +120,25 @@ - (IBAction)showMenuBarChanged:(id)sender { enableState = YES; } - [[NSUserDefaults standardUserDefaults] setBool:!enableState forKey:@"status_bar"]; - + [[NSUserDefaults standardUserDefaults] setBool:enableState forKey:@"hide_status_bar"]; [[NSUserDefaults standardUserDefaults] synchronize]; - NSString *msgText = @"You will need to restart the App for this change to be applied."; + AppDelegate *appDelegate = (AppDelegate *) [[NSApplication sharedApplication] delegate]; + [appDelegate hideMenuBar:enableState]; + + + if (enableState == YES) { - if (enableState == NO) { - msgText = [NSString stringWithFormat:@"%@ Long press on the Touch Bar Mute Button to show Preferences when the Menu Item is disabled.", msgText]; + NSString *msgText = @"Long press on the Touch Bar Mute Button to show Preferences when the Menu Item is disabled."; + + NSAlert* msgBox = [[NSAlert alloc] init] ; + [msgBox setMessageText:msgText]; + [msgBox addButtonWithTitle: @"OK"]; + [msgBox runModal]; } - NSAlert* msgBox = [[NSAlert alloc] init] ; - [msgBox setMessageText:msgText]; - [msgBox addButtonWithTitle: @"OK"]; - [msgBox runModal]; + + } diff --git a/Pods/MASShortcut/Framework/MASShortcutView.m b/Pods/MASShortcut/Framework/MASShortcutView.m index 8540e61..62f4894 100644 --- a/Pods/MASShortcut/Framework/MASShortcutView.m +++ b/Pods/MASShortcut/Framework/MASShortcutView.m @@ -31,6 +31,8 @@ @implementation MASShortcutView { + (Class)shortcutCellClass { + + return [NSButtonCell class]; } @@ -56,6 +58,7 @@ - (void)commonInit { _shortcutCell = [[[self.class shortcutCellClass] alloc] init]; _shortcutCell.buttonType = NSPushOnPushOffButton; + _shortcutCell.font = [[NSFontManager sharedFontManager] convertFont:_shortcutCell.font toSize:MASButtonFontSize]; _shortcutValidator = [MASShortcutValidator sharedValidator]; _enabled = YES; @@ -96,6 +99,7 @@ - (void)resetShortcutCellStyle switch (_style) { case MASShortcutViewStyleDefault: { _shortcutCell.bezelStyle = NSRoundRectBezelStyle; + break; } case MASShortcutViewStyleTexturedRect: { @@ -113,6 +117,8 @@ - (void)resetShortcutCellStyle break; } } + + _shortcutCell.bezelStyle = NSTexturedSquareBezelStyle; } - (void)setRecording:(BOOL)flag @@ -211,12 +217,17 @@ - (void)drawInRect:(CGRect)frame withTitle:(NSString *)title alignment:(NSTextAl - (void)drawRect:(CGRect)dirtyRect { + if (self.shortcutValue) { NSString *buttonTitle; if (self.recording) { - buttonTitle = NSStringFromMASKeyCode(kMASShortcutGlyphEscape); + //buttonTitle = NSStringFromMASKeyCode(kMASShortcutGlyphEscape); + buttonTitle = @""; } else if (self.showsDeleteButton) { - buttonTitle = NSStringFromMASKeyCode(kMASShortcutGlyphClear); + //buttonTitle = NSStringFromMASKeyCode(kMASShortcutGlyphClear); + buttonTitle = @""; + + NSLog (@"buttonTitle : %@", buttonTitle); } if (buttonTitle != nil) { [self drawInRect:self.bounds withTitle:buttonTitle alignment:NSRightTextAlignment state:NSOffState]; @@ -260,12 +271,18 @@ - (void)getShortcutRect:(CGRect *)shortcutRectRef hintRect:(CGRect *)hintRectRef { CGRect shortcutRect, hintRect; CGFloat hintButtonWidth = MASHintButtonWidth; + + NSLog (@"hintButtonWidth : %f", hintButtonWidth); + switch (self.style) { case MASShortcutViewStyleTexturedRect: hintButtonWidth += 2.0; break; case MASShortcutViewStyleRounded: hintButtonWidth += 3.0; break; case MASShortcutViewStyleFlat: hintButtonWidth -= 8.0 - (_shortcutCell.font.pointSize - MASButtonFontSize); break; - default: break; + default: hintButtonWidth -= 8.0 - (_shortcutCell.font.pointSize - MASButtonFontSize); hintButtonWidth = hintButtonWidth; break; } + + hintButtonWidth = 0; + CGRectDivide(self.bounds, &hintRect, &shortcutRect, hintButtonWidth, CGRectMaxXEdge); if (shortcutRectRef) *shortcutRectRef = shortcutRect; if (hintRectRef) *hintRectRef = hintRect;