Skip to content

Commit

Permalink
Merge pull request #13 from fernando-berrios/optional-status-bar-enha…
Browse files Browse the repository at this point in the history
…ncements

Optional enhancements for status / menu bar
  • Loading branch information
lnikell committed Oct 17, 2017
2 parents 41e8f02 + 74f5af2 commit 83d0751
Show file tree
Hide file tree
Showing 15 changed files with 288 additions and 78 deletions.
1 change: 1 addition & 0 deletions Mute Me Now/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- (IBAction)menuMenuItemAction:(id)sender;

- (void) hideMenuBar:(BOOL)enableState;
- (void) updateMenuItemIcon;

- (void) shortCutKeyPressed;

Expand Down
124 changes: 104 additions & 20 deletions Mute Me Now/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,61 @@ @implementation AppDelegate
NSString *STATUS_ICON_RED = @"tray-active";
NSString *STATUS_ICON_WHITE = @"tray-unactive-white";

NSString *STATUS_ICON_OFF = @"micOff";
NSString *STATUS_ICON_ON = @"micOn";


- (void) awakeFromNib {

BOOL hideStatusBar = NO;
BOOL statusBarButtonToggle = NO;
BOOL useAlternateStatusBarIcons = NO;

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hide_status_bar"] != nil) {
hideStatusBar = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"];
}

if (!hideStatusBar) {
[self setupStatusBarItem];
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_button_toggle"] != nil) {
statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"];
}

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateStatusBarIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}

[[NSUserDefaults standardUserDefaults] setBool:hideStatusBar forKey:@"hide_status_bar"];
[[NSUserDefaults standardUserDefaults] setBool:statusBarButtonToggle forKey:@"status_bar_button_toggle"];
[[NSUserDefaults standardUserDefaults] setBool:useAlternateStatusBarIcons forKey:@"status_bar_alternate_icons"];

if (!hideStatusBar) {
[self setupStatusBarItem];
}

// masshortcut
[self setShortcutKey];
}

- (void) setupStatusBarItem {

self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
BOOL statusBarButtonToggle = NO;

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_button_toggle"] != nil) {
statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"];
}

if (statusBarButtonToggle) {
NSStatusItem *statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
NSStatusBarButton *statusButton = statusItem.button;

statusButton.target = self;
statusButton.action = @selector(handleStatusButtonAction);

[statusButton sendActionOn:NSEventMaskLeftMouseUp|NSEventMaskRightMouseUp];

self.statusBar = statusItem;
} else {
self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
self.statusBar.menu = self.statusMenu;
}

NSImage* statusImage = [self getStatusBarImage];

Expand All @@ -55,12 +88,11 @@ - (void) setupStatusBarItem {
// allows cocoa to change the background of the icon
[statusImage setTemplate:YES];

self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
self.statusBar.image = statusImage;
self.statusBar.highlightMode = YES;
self.statusBar.enabled = YES;
self.statusBar.menu = self.statusMenu;


[self updateMenuItemIcon];
}

- (void) setShortcutKey {
Expand Down Expand Up @@ -98,6 +130,12 @@ - (void) shortCutKeyPressed {

}

- (void) showMenu {

[self.statusBar popUpStatusItemMenu:self.statusMenu];

}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[[[[NSApplication sharedApplication] windows] lastObject] close];

Expand Down Expand Up @@ -144,16 +182,28 @@ -(void)darkModeChanged:(NSNotification *)notif

- (NSImage*) getStatusBarImage {

NSImage* statusImage = [NSImage imageNamed:STATUS_ICON_BLACK];

// see https://stackoverflow.com/questions/25379525/how-to-detect-dark-mode-in-yosemite-to-change-the-status-bar-menu-icon
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];

BOOL darkModeOn = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );

if (darkModeOn) {
statusImage = [NSImage imageNamed:STATUS_ICON_WHITE];
BOOL useAlternateIcons = NO;

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}

NSImage* statusImage;

if (useAlternateIcons) {
statusImage = [NSImage imageNamed:STATUS_ICON_ON];
} else {
statusImage = [NSImage imageNamed:STATUS_ICON_BLACK];

// see https://stackoverflow.com/questions/25379525/how-to-detect-dark-mode-in-yosemite-to-change-the-status-bar-menu-icon
NSDictionary *dict = [[NSUserDefaults standardUserDefaults] persistentDomainForName:NSGlobalDomain];
id style = [dict objectForKey:@"AppleInterfaceStyle"];

BOOL darkModeOn = ( style && [style isKindOfClass:[NSString class]] && NSOrderedSame == [style caseInsensitiveCompare:@"dark"] );

if (darkModeOn) {
statusImage = [NSImage imageNamed:STATUS_ICON_WHITE];
}
}

return statusImage;
Expand All @@ -166,13 +216,27 @@ - (void) setStatusBarImgRed:(BOOL) shouldBeRed {

if (shouldBeRed) {
NSLog (@"using red");
statusImage = [NSImage imageNamed:STATUS_ICON_RED];

BOOL useAlternateIcons = NO;

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) {
useAlternateIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"];
}

if (useAlternateIcons) {
statusImage = [NSImage imageNamed:STATUS_ICON_OFF];
[statusImage setTemplate:YES];
} else {
statusImage = [NSImage imageNamed:STATUS_ICON_RED];
[statusImage setTemplate:!shouldBeRed];
}

}

statusImage.size = NSMakeSize(18, 18);
[statusImage setTemplate:!shouldBeRed];


self.statusBar.image = statusImage;

}


Expand Down Expand Up @@ -279,6 +343,14 @@ - (IBAction)menuMenuItemAction:(id)sender {
[self updateMenuItem];
}

- (void) updateMenuItemIcon {
if (self.muteMenuItem.state == NSOnState) {
[self setStatusBarImgRed:YES];
} else {
[self setStatusBarImgRed:NO];
}
}

- (void) updateMenuItem {

if (self.muteMenuItem.state == NSOffState) {
Expand All @@ -296,6 +368,18 @@ - (void) updateMenuItem {

}

- (void) handleStatusButtonAction {
NSEvent *event = [[NSApplication sharedApplication] currentEvent];

if ((event.modifierFlags & NSEventModifierFlagControl) || (event.modifierFlags & NSEventModifierFlagOption) || (event.type == NSEventTypeRightMouseUp)) {

[self showMenu];

return;
}

[self updateMenuItem];
}


@end
23 changes: 23 additions & 0 deletions Mute Me Now/Assets.xcassets/micOff.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "mic-off@1x.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "mic-off@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "mic-off@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions Mute Me Now/Assets.xcassets/micOn.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"images" : [
{
"idiom" : "universal",
"filename" : "mic-on@1x.png",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "mic-on@2x.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"filename" : "mic-on@3x.png",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 83d0751

Please sign in to comment.