Permalink
Browse files

A smattering of ReactiveCocoa to get rid of the obnoxious manual KVO,…

… a bunch of localization of previously not localized strings, tons of constants, etc.
  • Loading branch information...
1 parent 2314e7a commit 4ace3149f3aeca570a3a086ee9590f821cfb7187 @codykrieger committed Jun 17, 2012
@@ -8,7 +8,7 @@
#import <Cocoa/Cocoa.h>
#import "GSPreferencesModule.h"
-#import "PrefsController.h"
+#import "GSPreferences.h"
@interface AdvancedPreferencesViewController : NSViewController <GSPreferencesModule>
@@ -18,6 +18,6 @@
@property (strong) IBOutlet NSTextField *onBatteryTextField;
@property (strong) IBOutlet NSTextField *pluggedInTextField;
-@property (strong) PrefsController *prefs;
+@property (strong) GSPreferences *prefs;
@end
@@ -9,6 +9,9 @@
#import "AdvancedPreferencesViewController.h"
#import "GSGPU.h"
+#define kAdvancedPreferencesName @"Advanced"
+#define kAdvancedPreferencesIdentifier @"advanced"
+
@implementation AdvancedPreferencesViewController
@synthesize prefChkPowerSourceBasedSwitching;
@@ -24,7 +27,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:@"AdvancedPreferencesView" bundle:nil];
if (self) {
- prefs = [PrefsController sharedInstance];
+ prefs = [GSPreferences sharedInstance];
}
return self;
@@ -68,12 +71,12 @@ - (void)loadView
- (NSString *)title
{
- return Str(@"Advanced");
+ return Str(kAdvancedPreferencesName);
}
- (NSString *)identifier
{
- return @"advanced";
+ return kAdvancedPreferencesIdentifier;
}
- (NSImage *)image
@@ -8,7 +8,7 @@
#import <Cocoa/Cocoa.h>
#import "PreferencesWindowController.h"
-#import "PrefsController.h"
+#import "GSPreferences.h"
@protocol GSMenuControllerDelegate <NSObject>
- (void)something;
@@ -17,7 +17,7 @@
@interface GSMenuController : NSObject <NSMenuDelegate> {
NSStatusItem *_statusItem;
- PrefsController *_prefs;
+ GSPreferences *_prefs;
PreferencesWindowController *_preferencesWindowController;
}
@@ -44,6 +44,8 @@
@property (strong) IBOutlet NSMenuItem *dependentProcesses;
@property (strong) IBOutlet NSMenuItem *processList;
+@property BOOL menuIsOpen;
+
- (void)setupMenu;
- (void)updateMenu;
View
@@ -15,8 +15,13 @@
#import "GSProcess.h"
#import "GSGPU.h"
-@interface GSMenuController ()
-@property BOOL menuIsOpen;
+#define kImageIconIntegratedName @"integrated"
+#define kImageIconDiscreteName @"discrete"
+#define kImageIconOpenSuffix @"-white"
+
+#define kShouldUseSmartMenuBarIconsKeyPath @"prefsDict.shouldUseSmartMenuBarIcons"
+
+@interface GSMenuController (Internal)
- (void)_localizeMenu;
- (void)_updateProcessList;
@end
@@ -49,11 +54,10 @@ - (id)init
{
self = [super init];
if (self) {
- _prefs = [PrefsController sharedInstance];
+ _prefs = [GSPreferences sharedInstance];
- // FIXME: Test out ReactiveCocoa for this use case.
- [RACAble(_prefs, shouldUseSmartMenuBarIcons) subscribeNext:^(id smartIcons) {
- GTMLoggerDebug(@"shouldUseSmartMenuBarIcons: %d", [smartIcons boolValue]);
+ [[_prefs rac_subscribableForKeyPath:kShouldUseSmartMenuBarIconsKeyPath onObject:self] subscribeNext:^(id x) {
+ GTMLoggerDebug(@"Use smart menu bar icons value changed: %@", x);
[self updateMenu];
}];
}
@@ -77,9 +81,21 @@ - (void)setupMenu
[discreteOnly setHidden:isLegacyMachine];
[dynamicSwitching setHidden:isLegacyMachine];
- // FIXME: Test out ReactiveCocoa for this use case.
- [RACAbleSelf(self.menuIsOpen) subscribeNext:^(id open) {
- GTMLoggerDebug(@"open: %d", [open boolValue]);
+ // Listen for when the menu opens and change the icons appropriately if the
+ // user is using images.
+ [RACAbleSelf(self.menuIsOpen) subscribeNext:^(id x) {
+ GTMLoggerDebug(@"Menu open: %@", x);
+
+ if (_prefs.shouldUseImageIcons) {
+ NSString *imageName = _statusItem.image.name;
+
+ if ([x boolValue])
+ imageName = [imageName stringByAppendingString:kImageIconOpenSuffix];
+ else
+ imageName = [imageName stringByReplacingOccurrencesOfString:kImageIconOpenSuffix withString:@""];
+
+ [_statusItem setImage:[NSImage imageNamed:imageName]];
+ }
}];
[self _localizeMenu];
@@ -99,7 +115,7 @@ - (void)updateMenu
// set menu bar icon
if ([_prefs shouldUseImageIcons]) {
- [_statusItem setImage:[NSImage imageNamed:(isUsingIntegrated ? @"integrated.png" : @"discrete.png")]];
+ [_statusItem setImage:[NSImage imageNamed:(isUsingIntegrated ? kImageIconIntegratedName : kImageIconDiscreteName)]];
} else {
// grab first character of GPU string for the menu bar icon
unichar firstLetter;
@@ -173,9 +189,6 @@ - (IBAction)openPreferences:(id)sender
[_preferencesWindowController setModules:modules];
}
- // FIXME this sucks, the menu controller shouldn't know anything about prefs
- _preferencesWindowController.window.delegate = _prefs;
-
[_preferencesWindowController.window center];
[_preferencesWindowController.window makeKeyAndOrderFront:self];
[_preferencesWindowController.window setOrderedIndex:0];
@@ -184,7 +197,7 @@ - (IBAction)openPreferences:(id)sender
- (IBAction)openApplicationURL:(id)sender
{
- [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://codykrieger.com/gfxCardStatus"]];
+ [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kApplicationWebsiteURL]];
}
- (IBAction)quit:(id)sender
@@ -235,25 +248,17 @@ - (void)menuNeedsUpdate:(NSMenu *)menu
- (void)menuWillOpen:(NSMenu *)menu
{
- // FIXME: Do this in a shorter/more succinct way.
-
- // white image when menu is open
- if ([_prefs shouldUseImageIcons]) {
- [_statusItem setImage:[NSImage imageNamed:[[[_statusItem image] name] stringByAppendingString:@"-white.png"]]];
- }
+ self.menuIsOpen = YES;
}
- (void)menuDidClose:(NSMenu *)menu
{
- // FIXME: Do this in a shorter/more succinct way.
-
- // black image when menu is closed
- if ([_prefs shouldUseImageIcons]) {
- [_statusItem setImage:[NSImage imageNamed:[[[_statusItem image] name] stringByReplacingOccurrencesOfString:@"-white" withString:@".png"]]];
- }
+ self.menuIsOpen = NO;
}
-#pragma mark - Private helpers
+@end
+
+@implementation GSMenuController (Internal)
- (void)_localizeMenu
{
View
@@ -55,25 +55,23 @@ + (void)queueNotification:(GSNotificationType)type
+ (void)showOneTimeNotification
{
- // FIXME: Localize these strings
NSAlert *versionInfo = [[NSAlert alloc] init];
- [versionInfo setMessageText:@"Thanks for downloading gfxCardStatus!"];
- [versionInfo setInformativeText:@"If you find it useful, please consider donating to support development and hosting costs. You can find the donate link, and the FAQ page (which you should REALLY read) at the gfxCardStatus website:"];
+ [versionInfo setMessageText:Str(@"ThanksForDownloading")];
+ [versionInfo setInformativeText:Str(@"PleaseConsiderDonating")];
NSTextView *accessory = [[NSTextView alloc] initWithFrame:NSMakeRect(0,0,300,15)];
[accessory insertText:[NSAttributedString hyperlinkFromString:@"http://codykrieger.com/gfxCardStatus"
withURL:[NSURL URLWithString:@"http://codykrieger.com/gfxCardStatus"]]];
[accessory setEditable:NO];
[accessory setDrawsBackground:NO];
[versionInfo setAccessoryView:accessory];
- [versionInfo addButtonWithTitle:@"Don't show this again!"];
+ [versionInfo addButtonWithTitle:Str(@"DontShowAgain")];
[versionInfo runModal];
}
+ (void)showUnsupportedMachineMessage
{
- // FIXME: Localize these strings
- NSAlert *alert = [NSAlert alertWithMessageText:@"You are using a system that gfxCardStatus does not support. Please ensure that you are using a MacBook Pro with dual GPUs."
- defaultButton:@"Oh, I see."
+ NSAlert *alert = [NSAlert alertWithMessageText:Str(@"UnsupportedMachine")
+ defaultButton:Str(@"OhISee")
alternateButton:nil
otherButton:nil
informativeTextWithFormat:@""];
@@ -1,5 +1,5 @@
//
-// PrefsController.h
+// GSPreferences.h
// gfxCardStatus
//
// Created by Cody Krieger on 9/26/10.
@@ -8,14 +8,15 @@
#import <Cocoa/Cocoa.h>
-@interface PrefsController : NSObject <NSWindowDelegate> {
- NSString *prefsPath;
- NSMutableDictionary *prefs;
+@interface GSPreferences : NSObject <NSWindowDelegate> {
+ NSMutableDictionary *_prefsDict;
NSNumber *yesNumber;
NSNumber *noNumber;
}
+@property (strong) NSMutableDictionary *prefsDict;
+
- (void)setUpPreferences;
- (void)setDefaults;
- (void)savePreferences;
@@ -32,6 +33,6 @@
- (void)savePreferences;
-+ (PrefsController *)sharedInstance;
++ (GSPreferences *)sharedInstance;
@end
Oops, something went wrong.

0 comments on commit 4ace314

Please sign in to comment.