Permalink
Browse files

Add active host name in menu bar

Enable the user to choose a user preference
that shows the current host file name in the status bar.
I've also updated this to use the 'button' property
in the NSStatusBarItem

This closes #65
  • Loading branch information...
Trever Shick authored and trevershick committed Oct 17, 2015
1 parent 3a28ae7 commit 0d391347497e4e02621ece3a74cc363618d91408
Showing with 375 additions and 1,136 deletions.
  1. +1 −0 Gas_Mask_Prefix.pch
  2. +295 −1,124 Preferences.xib
  3. BIN Resources/UserDefaults.plist
  4. +2 −1 Source/HostsMainController.m
  5. +70 −11 Source/Menulet.m
  6. +2 −0 Source/Preferences.h
  7. +5 −0 Source/Preferences.m
View
@@ -38,6 +38,7 @@
#define HostsFileSavedNotification @"HostsFileSavedNotification"
// Notifications from hotkeys
+ #define ActivateFileNotification @"activateFileNotification" // sent when any given host file is activated.
#define ActivatePreviousFileNotification @"activatePreviousFileNotification"
#define ActivateNextFileNotification @"activateNextFileNotification"
#define UpdateAndSynchronizeNotification @"updateAndSynchronizeNotification"
View

Large diffs are not rendered by default.

Oops, something went wrong.
Binary file not shown.
@@ -114,8 +114,8 @@ - (void)load
}
[self updateFilesCount];
-
logInfo(@"All hosts files are loaded");
+ [[NSNotificationCenter defaultCenter] postNotificationName:ActivateFileNotification object:NULL];
}
#pragma mark -
@@ -360,6 +360,7 @@ - (void)activateHostsFile:(Hosts*)hosts
if (newActivated) {
[activeHostsFile setActive:NO];
+ [[NSNotificationCenter defaultCenter] postNotificationName:ActivateFileNotification object:NULL];
}
}
View
@@ -22,28 +22,87 @@
#import "Hosts.h"
#import "HostsMainController.h"
#import "HostsMenu.h"
-
+#import "Preferences.h"
@implementation Menulet
- (void)awakeFromNib
{
- statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
- [statusItem setHighlightMode:YES];
- [statusItem setEnabled:YES];
- [statusItem setToolTip:@"Gas Mask"];
- [statusItem setTitle:@""];
- [statusItem setAction:@selector(showMenu:)];
- [statusItem setTarget:self];
-
+
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *path = [bundle pathForResource:@"menuIcon" ofType:@"tiff"];
NSImage *icon = [[NSImage alloc] initWithContentsOfFile:path];
[icon setTemplate:YES];
-
- [statusItem setImage:icon];
+
+ statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
+ [[statusItem button] setEnabled:true];
+ [[statusItem button] setTarget:self];
+ [[statusItem button] setAction:@selector(showMenu:)];
+ [[statusItem button] setImage:icon];
+ [[statusItem button] setTitle:@""];
+ [[statusItem button] setToolTip:@"Gas Mask"];
+
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ [defaults addObserver:self
+ forKeyPath:ShowNameInStatusBarKey
+ options:NSKeyValueObservingOptionNew
+ context:NULL];
+
+ [defaults addObserver:self
+ forKeyPath:ActiveHostsFilePrefKey
+ options:NSKeyValueObservingOptionNew
+ context:NULL];
+
+ [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateName) name:ActivateFileNotification object:NULL];
+
+ if ([Preferences showNameInStatusBar]) {
+ [self initTitleInBar];
+ }
+}
+
+-(void) initTitleInBar {
+ [statusItem setLength:NSVariableStatusItemLength];
+ [[statusItem button] setImagePosition:NSImageLeft];
+}
+
+-(void)updateName {
+ if (![Preferences showNameInStatusBar]) {
+ return;
+ }
+ NSString *name = [[[HostsMainController defaultInstance] activeHostsFile] name];
+ [[statusItem button] setTitle:name];
}
+-(void) removeTitleFromBar {
+ [statusItem setLength:NSSquareStatusItemLength];
+ [[statusItem button] setImagePosition:NSImageOnly];
+ [[statusItem button] setTitle:@""];
+}
+
+-(void)observeValueForKeyPath:(NSString *)keyPath
+ ofObject:(id)object
+ change:(NSDictionary *)change
+ context:(void *)context
+{
+ if ([ShowNameInStatusBarKey isEqualToString:keyPath]) {
+ if ([Preferences showNameInStatusBar]) {
+ [self initTitleInBar];
+ [self updateName];
+ } else {
+ [self removeTitleFromBar];
+ }
+ } else if ([ActiveHostsFilePrefKey isEqualToString:keyPath]) {
+ [self updateName];
+ }
+}
+
+-(void)dealloc {
+ NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
+ [defaults removeObserver:self forKeyPath:ActiveHostsFilePrefKey];
+ [defaults removeObserver:self forKeyPath:ShowNameInStatusBarKey];
+}
+
+
-(IBAction)showMenu:(id)sender
{
HostsMenu *menu = [[HostsMenu alloc] initWithExtras];
View
@@ -24,6 +24,7 @@
#define UpdateAndSynchronizePrefKey @"updateAndSynchronizeHotkey"
#define UseGrowlPrefKey @"Use Growl"
#define OverrideExternalModificationsPrefKey @"overrideExternalModifications"
+#define ShowNameInStatusBarKey @"showNameInStatusBar"
#define ActiveHostsFilePrefKey @"activeHostsFile"
#define ShowEditorWindowPrefKey @"showEditorWindow"
@@ -35,6 +36,7 @@
+ (Preferences *)instance;
+ (BOOL)useGrowl;
+ (BOOL)overrideExternalModifications;
++ (BOOL)showNameInStatusBar;
+ (void)setActiveHostsFile:(NSString *)path;
+ (NSString *)activeHostsFile;
+ (void)setShowEditorWindow:(BOOL)show;
View
@@ -38,6 +38,11 @@ + (BOOL) useGrowl
return [[[self instance] defaults] boolForKey:UseGrowlPrefKey];
}
++(BOOL)showNameInStatusBar
+{
+ return [[[self instance] defaults] boolForKey:ShowNameInStatusBarKey];
+}
+
+ (BOOL)overrideExternalModifications
{
return [[[self instance] defaults] boolForKey:OverrideExternalModificationsPrefKey];

0 comments on commit 0d39134

Please sign in to comment.