From e10a5b2be9cf7fbc8b4d0539016b21b60fd532b1 Mon Sep 17 00:00:00 2001 From: Joel Barker Date: Mon, 10 Jul 2017 18:23:59 +0100 Subject: [PATCH 1/4] added options to hide the menu bar and also to customise the shortcut key --- Mute Me Now/AppDelegate.h | 3 + Mute Me Now/AppDelegate.m | 51 +++++++++++++---- Mute Me Now/Base.lproj/Main.storyboard | 55 +++++++++++++----- Mute Me Now/ViewController.h | 2 + Mute Me Now/ViewController.m | 79 +++++++++++++++++++++----- 5 files changed, 150 insertions(+), 40 deletions(-) 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..b0a0f51 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..d7584dd 100644 --- a/Mute Me Now/ViewController.m +++ b/Mute Me Now/ViewController.m @@ -1,9 +1,15 @@ #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 +27,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 +121,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]; + + } From fd27e335ddfbe659b25fdd89cb276b159fff2447 Mon Sep 17 00:00:00 2001 From: Joel Barker Date: Fri, 14 Jul 2017 14:07:19 +0100 Subject: [PATCH 2/4] before changing the ui --- Mute Me Now/Base.lproj/Main.storyboard | 36 ++++++++++---------- Mute Me Now/ViewController.m | 5 +++ Pods/MASShortcut/Framework/MASShortcutView.m | 3 ++ 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/Mute Me Now/Base.lproj/Main.storyboard b/Mute Me Now/Base.lproj/Main.storyboard index b0a0f51..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 @@ - + - + - + @@ -841,7 +841,7 @@ - + @@ -857,7 +857,7 @@ - + @@ -870,7 +870,7 @@ - + diff --git a/Mute Me Now/ViewController.m b/Mute Me Now/ViewController.m index d7584dd..af25a15 100644 --- a/Mute Me Now/ViewController.m +++ b/Mute Me Now/ViewController.m @@ -15,6 +15,11 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; + _masShortCutView.style = MASShortcutViewStyleRounded; + + + + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) { // the opposite is used later diff --git a/Pods/MASShortcut/Framework/MASShortcutView.m b/Pods/MASShortcut/Framework/MASShortcutView.m index 8540e61..fe2beca 100644 --- a/Pods/MASShortcut/Framework/MASShortcutView.m +++ b/Pods/MASShortcut/Framework/MASShortcutView.m @@ -96,6 +96,7 @@ - (void)resetShortcutCellStyle switch (_style) { case MASShortcutViewStyleDefault: { _shortcutCell.bezelStyle = NSRoundRectBezelStyle; + break; } case MASShortcutViewStyleTexturedRect: { @@ -113,6 +114,8 @@ - (void)resetShortcutCellStyle break; } } + + _shortcutCell.bezelStyle = NSTexturedSquareBezelStyle; } - (void)setRecording:(BOOL)flag From 19a93135388d2a0af3a668301ccb3f95f6d019cb Mon Sep 17 00:00:00 2001 From: Joel Barker Date: Fri, 14 Jul 2017 16:48:46 +0100 Subject: [PATCH 3/4] looking into better way of modifying masshortcut --- Mute Me Now/ViewController.m | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Mute Me Now/ViewController.m b/Mute Me Now/ViewController.m index af25a15..fef88b1 100644 --- a/Mute Me Now/ViewController.m +++ b/Mute Me Now/ViewController.m @@ -1,7 +1,6 @@ #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/"; @@ -15,10 +14,6 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - _masShortCutView.style = MASShortcutViewStyleRounded; - - - if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) { From 1841147e2c1d6c1b088b535d1d34c3c73ce83cd7 Mon Sep 17 00:00:00 2001 From: Joel Barker Date: Mon, 17 Jul 2017 10:06:18 +0100 Subject: [PATCH 4/4] =?UTF-8?q?don=E2=80=99t=20show=20close=20or=20refresh?= =?UTF-8?q?=20controls=20on=20the=20shortcutview?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Mute Me Now/ViewController.m | 1 - Pods/MASShortcut/Framework/MASShortcutView.m | 20 +++++++++++++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Mute Me Now/ViewController.m b/Mute Me Now/ViewController.m index fef88b1..74c5c1b 100644 --- a/Mute Me Now/ViewController.m +++ b/Mute Me Now/ViewController.m @@ -14,7 +14,6 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) { // the opposite is used later diff --git a/Pods/MASShortcut/Framework/MASShortcutView.m b/Pods/MASShortcut/Framework/MASShortcutView.m index fe2beca..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; @@ -214,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]; @@ -263,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;