Skip to content

Commit

Permalink
Fixes #1: Implement the status item.
Browse files Browse the repository at this point in the history
  • Loading branch information
boredzo committed Jul 23, 2016
1 parent c4f48e8 commit bb6accf
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 2 deletions.
6 changes: 6 additions & 0 deletions SpeedDial/SpeedDial.xcodeproj/project.pbxproj
Expand Up @@ -15,6 +15,7 @@
31454A7B1D440B1500925A2A /* PreferencesWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 31454A791D440B1500925A2A /* PreferencesWindowController.xib */; };
31454A7E1D440FD900925A2A /* VNCBookmark.m in Sources */ = {isa = PBXBuildFile; fileRef = 31454A7D1D440FD900925A2A /* VNCBookmark.m */; };
31454A811D44136D00925A2A /* BookmarksManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 31454A801D44136D00925A2A /* BookmarksManager.m */; };
31454A841D44195F00925A2A /* StatusItemController.m in Sources */ = {isa = PBXBuildFile; fileRef = 31454A831D44195F00925A2A /* StatusItemController.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -32,6 +33,8 @@
31454A7D1D440FD900925A2A /* VNCBookmark.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = VNCBookmark.m; sourceTree = "<group>"; };
31454A7F1D44136D00925A2A /* BookmarksManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BookmarksManager.h; sourceTree = "<group>"; };
31454A801D44136D00925A2A /* BookmarksManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BookmarksManager.m; sourceTree = "<group>"; };
31454A821D44195F00925A2A /* StatusItemController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StatusItemController.h; sourceTree = "<group>"; };
31454A831D44195F00925A2A /* StatusItemController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = StatusItemController.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -75,6 +78,8 @@
31454A7D1D440FD900925A2A /* VNCBookmark.m */,
31454A7F1D44136D00925A2A /* BookmarksManager.h */,
31454A801D44136D00925A2A /* BookmarksManager.m */,
31454A821D44195F00925A2A /* StatusItemController.h */,
31454A831D44195F00925A2A /* StatusItemController.m */,
31454A711D440AA800925A2A /* Info.plist */,
31454A691D440AA800925A2A /* Supporting Files */,
);
Expand Down Expand Up @@ -163,6 +168,7 @@
31454A7E1D440FD900925A2A /* VNCBookmark.m in Sources */,
31454A7A1D440B1500925A2A /* PreferencesWindowController.m in Sources */,
31454A681D440AA800925A2A /* AppDelegate.m in Sources */,
31454A841D44195F00925A2A /* StatusItemController.m in Sources */,
31454A811D44136D00925A2A /* BookmarksManager.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
9 changes: 8 additions & 1 deletion SpeedDial/SpeedDial/AppDelegate.m
Expand Up @@ -8,26 +8,33 @@

#import "AppDelegate.h"

#import "StatusItemController.h"
#import "BookmarksManager.h"
#import "PreferencesWindowController.h"

@implementation AppDelegate
{
StatusItemController *_sic;
BookmarksManager *_mgr;
PreferencesWindowController *_wc;
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification {
_sic = [StatusItemController new];
_mgr = [BookmarksManager new];

_wc = [PreferencesWindowController new];
_wc.bookmarksManager = _mgr;
[_wc showWindow:nil]; //TEMP

_sic.bookmarksManager = _mgr;
_sic.preferencesWindowController = _wc;
}

- (void)applicationWillTerminate:(NSNotification *)sotification {
[_wc close];
_wc = nil;
_mgr = nil;
_sic = nil;
}

@end
2 changes: 1 addition & 1 deletion SpeedDial/SpeedDial/BookmarksManager.h
Expand Up @@ -10,7 +10,7 @@

#import "VNCBookmark.h"

@interface BookmarksManager : NSObject
@interface BookmarksManager : NSObject <NSFastEnumeration>

@property(nonatomic, readonly) NSUInteger count;
- (VNCBookmark *_Nonnull) objectAtIndexedSubscript:(NSUInteger)idx;
Expand Down
9 changes: 9 additions & 0 deletions SpeedDial/SpeedDial/BookmarksManager.m
Expand Up @@ -27,6 +27,15 @@ - (VNCBookmark *_Nonnull) objectAtIndexedSubscript:(NSUInteger)idx {
return [_bookmarks objectAtIndexedSubscript:idx];
}

- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state
objects:(__unsafe_unretained id _Nonnull *)buffer
count:(NSUInteger)len
{
return [_bookmarks countByEnumeratingWithState:state
objects:buffer
count:len];
}

- (VNCBookmark *_Nonnull) addNewEmptyBookmarkAtEnd {
VNCBookmark *_Nonnull const newBookmark = [VNCBookmark new];
[_bookmarks addObject:newBookmark];
Expand Down
2 changes: 2 additions & 0 deletions SpeedDial/SpeedDial/PreferencesWindowController.h
Expand Up @@ -14,4 +14,6 @@

@property(weak) BookmarksManager *bookmarksManager;

- (IBAction) showPreferences:(id)sender;

@end
4 changes: 4 additions & 0 deletions SpeedDial/SpeedDial/PreferencesWindowController.m
Expand Up @@ -30,6 +30,10 @@ - (void)windowDidLoad {
[super windowDidLoad];
}

- (IBAction) showPreferences:(id)sender {
[self showWindow:sender];
}

- (IBAction)addBookmark:(id)sender {
VNCBookmark *_Nonnull const newBookmark = [self.bookmarksManager addNewEmptyBookmarkAtEnd];

Expand Down
19 changes: 19 additions & 0 deletions SpeedDial/SpeedDial/StatusItemController.h
@@ -0,0 +1,19 @@
//
// StatusItemController.h
// SpeedDial
//
// Created by Peter Hosey on 2016-07-23.
// Copyright © 2016 Peter Hosey. All rights reserved.
//

#import <Cocoa/Cocoa.h>

@class BookmarksManager;
@class PreferencesWindowController;

@interface StatusItemController : NSObject

@property(weak) BookmarksManager *bookmarksManager;
@property(weak) PreferencesWindowController *preferencesWindowController;

@end
77 changes: 77 additions & 0 deletions SpeedDial/SpeedDial/StatusItemController.m
@@ -0,0 +1,77 @@
//
// StatusItemController.m
// SpeedDial
//
// Created by Peter Hosey on 2016-07-23.
// Copyright © 2016 Peter Hosey. All rights reserved.
//

#import "StatusItemController.h"

#import "BookmarksManager.h"
#import "PreferencesWindowController.h"

@interface StatusItemController () <NSMenuDelegate>

@end

@implementation StatusItemController
{
NSStatusItem *_statusItem;
}

- (instancetype) init {
if ((self = [super init])) {
_statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
// https://github.com/boredzo/SpeedDial/issues/4
// _statusItem.image = [NSImage imageNamed:@"SpeedDialStatusItemTemplate"];
_statusItem.image = [NSImage imageNamed:NSImageNameHomeTemplate];

NSMenu *_Nonnull const menu = [[NSMenu alloc] initWithTitle:@""];
menu.delegate = self;
_statusItem.menu = menu;
}
return self;
}

#pragma mark NSMenuDelegate conformance

- (void)menuNeedsUpdate:(NSMenu *_Nonnull)menu {
[menu removeAllItems];

for (VNCBookmark *_Nonnull const bookmark in self.bookmarksManager) {
NSMenuItem *_Nonnull const itemForBookmark = [menu addItemWithTitle:bookmark.bookmarkName
action:@selector(openBookmark:)
keyEquivalent:@""];
itemForBookmark.target = self;
itemForBookmark.representedObject = bookmark;
}

if (self.preferencesWindowController != nil) {
[menu addItem:[NSMenuItem separatorItem]];
NSMenuItem *_Nonnull const prefsMenuItem = [menu addItemWithTitle:NSLocalizedString(@"Preferences…", /*menu item in status item menu*/)
action:@selector(showPreferences:)
keyEquivalent:@","];
prefsMenuItem.target = self.preferencesWindowController;
}

[menu addItem:[NSMenuItem separatorItem]];
[menu addItemWithTitle:NSLocalizedString(@"Quit", /*menu item in status item menu*/)
action:@selector(terminate:)
keyEquivalent:@"q"];
}

#pragma mark Actions

- (IBAction) openBookmark:(_Nullable id)sender {
NSAssert([sender isKindOfClass:[NSMenuItem class]], @"Unexpected sender of %s: %@", __func__, sender);
NSMenuItem *_Nullable const menuItem = sender;
VNCBookmark *_Nullable const bookmark = menuItem.representedObject;
NSURL *_Nullable const URL = bookmark.URL;

if (URL != nil) {
[[NSWorkspace sharedWorkspace] openURL:URL];
}
}

@end
3 changes: 3 additions & 0 deletions SpeedDial/SpeedDial/VNCBookmark.h
Expand Up @@ -15,4 +15,7 @@
@property(nullable, copy) NSString *password;
@property(nullable, copy) NSString *hostname;

///Create an NSURL that represents the contents of this bookmark.
@property(nonatomic, readonly, nullable) NSURL *URL;

@end
18 changes: 18 additions & 0 deletions SpeedDial/SpeedDial/VNCBookmark.m
Expand Up @@ -10,4 +10,22 @@

@implementation VNCBookmark

- (NSURL *) URL {
//We have to VNC *somewhere*.
if (self.hostname == nil) {
return nil;
}
//Password but no username isn't valid.
if (self.password != nil && self.username == nil) {
return nil;
}

NSURLComponents *_Nonnull const components = [NSURLComponents new];
components.scheme = @"vnc";
components.user = self.username;
components.password = self.password;
components.host = self.hostname;
return [components URL];
}

@end

0 comments on commit bb6accf

Please sign in to comment.