Skip to content

Commit

Permalink
Some basics of allowing the user to choose to put the app in the dock…
Browse files Browse the repository at this point in the history
…, or leave the window open.
  • Loading branch information
beelsebob committed Nov 17, 2012
1 parent d53ff94 commit 4e88bcd
Show file tree
Hide file tree
Showing 5 changed files with 2,515 additions and 2,218 deletions.
45 changes: 36 additions & 9 deletions Stash/STAAppDelegate.m
Expand Up @@ -22,6 +22,7 @@ @interface STAAppDelegate () <NSWindowDelegate>
@property (strong) NSMutableArray *results;
@property (strong) NSArray *sortedResults;
@property (assign, getter=isFindUIShowing) BOOL findUIShowing;
@property (weak) NSSearchField *selectedSearchField;

- (void)readDocsets;
- (void)showFindUI;
Expand Down Expand Up @@ -50,14 +51,23 @@ @implementation STAAppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
[self setStatusItem:[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]];
[[self statusItem] setMenu:[self statusMenu]];
[[self statusItem] setTitle:@"Stash"];
[[self statusItem] setHighlightMode:YES];

[self setPreferencesController:[[STAPreferencesController alloc] initWithNibNamed:@"STAPreferencesController" bundle:nil]];
[[self preferencesController] setDelegate:self];

STAIconShowingMode mode = [[self preferencesController] iconMode];

if (mode == STAIconShowingModeBoth || mode == STAIconShowingModeMenuBar)
{
[self setStatusItem:[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]];
[[self statusItem] setMenu:[self statusMenu]];
[[self statusItem] setTitle:@"Stash"];
[[self statusItem] setHighlightMode:YES];
}
if (mode == STAIconShowingModeBoth || mode == STAIconShowingModeDock)
{
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
}

unichar c = [[self preferencesController] keyboardShortcutCharacter];
[[self openStashMenuItem] setKeyEquivalent:[NSString stringWithCharacters:&c length:1]];
[[self openStashMenuItem] setKeyEquivalentModifierMask:[[self preferencesController] keyboardShortcutModifierFlags]];
Expand All @@ -76,6 +86,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
};

[NSEvent addGlobalMonitorForEventsMatchingMask:NSKeyUpMask handler:handler];

[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyUpMask handler:^ NSEvent * (NSEvent *e) { handler(e); return e; }];
[NSEvent addLocalMonitorForEventsMatchingMask:NSKeyDownMask handler:^ NSEvent * (NSEvent *e)
{
Expand Down Expand Up @@ -120,7 +131,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
}
return e;
}];

[self readDocsets];
}

Expand All @@ -145,9 +156,12 @@ - (void)showFindUI

- (IBAction)hideSearchBar:(id)sender
{
[[self window] makeFirstResponder:[self searchField]];
if ([self isFindUIShowing])
{
if ([self selectedSearchField] != [self searchField])
{
[[self window] makeFirstResponder:[self searchField]];
}
[self setFindUIShowing:NO];
[NSAnimationContext runAnimationGroup:^ (NSAnimationContext *ctx)
{
Expand Down Expand Up @@ -290,7 +304,10 @@ - (void)cancelOperation:(id)sender

- (void)windowDidResignKey:(NSNotification *)notification
{
[[self window] close];
if ([[self preferencesController] appShouldHideWhenNotActive])
{
[[self window] close];
}
}

- (IBAction)search:(id)sender
Expand All @@ -299,7 +316,8 @@ - (IBAction)search:(id)sender
[self hideSearchBar:self];
[self setCurrentSearchString:searchString];
[self setResults:[NSMutableArray array]];
[[self resultsTable] deselectAll:self];
[self setSortedResults:[NSArray array]];
[[self resultsTable] reloadData];
for (STADocSet *docSet in [[self preferencesController] enabledDocsets])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^()
Expand Down Expand Up @@ -402,6 +420,15 @@ - (void)webView:(WebView *)sender didReceiveTitle:(NSString *)title forFrame:(We
[[self titleView] setStringValue:title];
}

- (BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
{
if ([control isKindOfClass:[NSSearchField class]])
{
[self setSelectedSearchField:(NSSearchField *)control];
}
return YES;
}

@end

NSImage *NSImageFromSTAPlatform(STAPlatform p)
Expand Down
14 changes: 14 additions & 0 deletions Stash/STAPreferencesController.h
Expand Up @@ -10,6 +10,14 @@

#import "STADocSet.h"

typedef enum
{
STAIconShowingModeMenuBar = 0,
STAIconShowingModeDock = 1,
STAIconShowingModeBoth = 2
} STAIconShowingMode;


@class STAPreferencesController;

@protocol STAPreferencesDelegate <NSObject>
Expand All @@ -30,6 +38,10 @@
@property (strong) IBOutlet NSTextField *shortcutText;
@property (strong) IBOutlet NSTableView *docsetTable;
@property (readonly) BOOL isMonitoringForEvents;
@property (weak) IBOutlet NSButton *hideWhenNotActiveCheckbox;
@property (weak) IBOutlet NSPopUpButton *showIconMenuButton;
@property (readonly) BOOL appShouldHideWhenNotActive;
@property (readonly) STAIconShowingMode iconMode;

- (id)initWithNibNamed:(NSString *)nibName bundle:(NSBundle *)bundle;

Expand All @@ -38,5 +50,7 @@
- (void)registerDocset:(STADocSet *)docset;

- (IBAction)changeShortcut:(id)sender;
- (IBAction)showIconChanged:(id)sender;
- (IBAction)hideWhenNotActiveChanged:(id)sender;

@end
72 changes: 69 additions & 3 deletions Stash/STAPreferencesController.m
Expand Up @@ -10,8 +10,12 @@

#import "STASymbolTableViewCell.h"

#import "STAAppDelegate.h"

#define kModifierFlagsKey @"Modifier Flags"
#define kKeyboardShortcutKey @"Keyboard Shortcut"
#define kHidesWhenNotActiveKey @"Hides When Not Active"
#define kShowsIconWhereKey @"Shows Icon Where"
#define kEnabledDocsetsKey @"Enabled Docsets"

NSString *descriptingStringFromChar(unichar c);
Expand Down Expand Up @@ -98,6 +102,9 @@ - (void)showWindow
{
[self setupShortcutText];

[[self hideWhenNotActiveCheckbox] setState:[[NSUserDefaults standardUserDefaults] boolForKey:kHidesWhenNotActiveKey]];
[[self showIconMenuButton] selectItemAtIndex:[[NSUserDefaults standardUserDefaults] integerForKey:kShowsIconWhereKey]];

[[self window] makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
}
Expand All @@ -107,9 +114,9 @@ - (void)setupShortcutText
NSMutableString *keyboardShortcutString = [NSMutableString stringWithString:@""];
NSUInteger modifierFlags = [self keyboardShortcutModifierFlags];
unichar command = 0x2318;
unichar alt = 0x2325;
unichar ctrl = 0x2303;
unichar shift = 0x21E7;
unichar alt = 0x2325;
unichar ctrl = 0x2303;
unichar shift = 0x21E7;
if (modifierFlags & NSControlKeyMask)
{
[keyboardShortcutString appendString:[NSString stringWithCharacters:&ctrl length:1]];
Expand Down Expand Up @@ -142,6 +149,8 @@ - (NSDictionary *)defaultPreferences
return [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInteger:NSCommandKeyMask | NSControlKeyMask], kModifierFlagsKey,
[NSNumber numberWithInt:' '], kKeyboardShortcutKey,
[NSNumber numberWithBool:YES], kHidesWhenNotActiveKey,
[NSNumber numberWithInt:STAIconShowingModeMenuBar], kShowsIconWhereKey,
registeredDocsetNames, kEnabledDocsetsKey,
nil];
}
Expand Down Expand Up @@ -170,6 +179,53 @@ - (IBAction)changeShortcut:(id)sender
}]];
}

- (IBAction)showIconChanged:(id)sender
{
NSInteger selection = [[self showIconMenuButton] indexOfSelectedItem];
NSInteger oldSelection = [[NSUserDefaults standardUserDefaults] integerForKey:kShowsIconWhereKey];
[[NSUserDefaults standardUserDefaults] setInteger:selection
forKey:kShowsIconWhereKey];
[[NSUserDefaults standardUserDefaults] synchronize];

if (selection == STAIconShowingModeBoth || selection == STAIconShowingModeDock)
{
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyRegular];
}
else
{
[[NSApplication sharedApplication] setActivationPolicy:NSApplicationActivationPolicyAccessory];
if (oldSelection != selection)
{
NSAlert *alert = [NSAlert alertWithMessageText:@"Restart Required"
defaultButton:@"Okay"
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@"In order to remove Stash from the dock, you must restart it."];
[alert runModal];
}
}

STAAppDelegate *del = (STAAppDelegate *)[[NSApplication sharedApplication] delegate];
if (selection == STAIconShowingModeMenuBar || selection == STAIconShowingModeBoth)
{
[del setStatusItem:[[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength]];
[[del statusItem] setMenu:[del statusMenu]];
[[del statusItem] setTitle:@"Stash"];
[[del statusItem] setHighlightMode:YES];
}
else
{
[del setStatusItem:nil];
}
}

- (IBAction)hideWhenNotActiveChanged:(id)sender
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:[[self hideWhenNotActiveCheckbox] state] == NSOnState]
forKey:kHidesWhenNotActiveKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}

- (void)removeEventMonitor
{
[NSEvent removeMonitor:[self eventMonitor]];
Expand All @@ -181,6 +237,16 @@ - (BOOL)isMonitoringForEvents
return [self eventMonitor] != nil;
}

- (BOOL)appShouldHideWhenNotActive
{
return [[NSUserDefaults standardUserDefaults] boolForKey:kHidesWhenNotActiveKey];
}

- (STAIconShowingMode)iconMode
{
return (STAIconShowingMode)[[NSUserDefaults standardUserDefaults] integerForKey:kShowsIconWhereKey];
}

- (NSArray *)registeredDocsets
{
return [[self internalRegisteredDocsets] copy];
Expand Down

0 comments on commit 4e88bcd

Please sign in to comment.