Skip to content

Commit

Permalink
Configurable fade timeout.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmhobbs committed Jun 25, 2015
1 parent 1d161ff commit ab51f92
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 18 deletions.
1 change: 1 addition & 0 deletions ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
@interface ConfigurationManager : NSObject

@property float opacity;
@property float fadeTimeout;
@property (strong, nonatomic) NSColor *textColor;

+ (ConfigurationManager *)instance;
Expand Down
8 changes: 6 additions & 2 deletions ConfigurationManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@

#import "ConfigurationManager.h"

static NSString *const kPreferenceOpacity = @"opacity";
static NSString *const kPreferenceTextColor = @"textColor";
static NSString *const kPreferenceOpacity = @"opacity";
static NSString *const kPrefrenceFadeTimeout = @"fade";
static NSString *const kPreferenceTextColor = @"textColor";

@implementation ConfigurationManager

Expand All @@ -31,13 +32,15 @@ - (instancetype)initInstance {

- (void)reset {
_opacity = 0.8;
_fadeTimeout = 1.0;
_textColor = [NSColor whiteColor];
}

- (bool)load {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

_opacity = [prefs floatForKey:kPreferenceOpacity];
_fadeTimeout = [prefs floatForKey:kPrefrenceFadeTimeout];
_textColor = [prefs objectForKey:kPreferenceTextColor];

NSData *data = [prefs objectForKey:kPreferenceTextColor];
Expand All @@ -58,6 +61,7 @@ - (bool)store {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];

[prefs setFloat:_opacity forKey:kPreferenceOpacity];
[prefs setFloat:_fadeTimeout forKey:kPrefrenceFadeTimeout];
NSData *data = [NSArchiver archivedDataWithRootObject:_textColor];
[prefs setObject:data forKey:kPreferenceTextColor];
[prefs synchronize];
Expand Down
10 changes: 9 additions & 1 deletion ShowKeys/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ @implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

[[ConfigurationManager instance] load];
[self.window configure:[ConfigurationManager instance].opacity textColor:[ConfigurationManager instance].textColor];
[self.window configure:[ConfigurationManager instance].opacity
fadeTimeout:[ConfigurationManager instance].fadeTimeout
textColor:[ConfigurationManager instance].textColor];

NSDictionary *options = @{(__bridge id)kAXTrustedCheckOptionPrompt: @YES};
BOOL accessibilityEnabled = AXIsProcessTrustedWithOptions((__bridge CFDictionaryRef)options);
Expand Down Expand Up @@ -116,4 +118,10 @@ - (void)textColorChanged:(NSColor *)color {
[self.window setKeys:@"--TEST--" wipe:YES];
}

- (void)fadeTimeoutChanged:(float)timeout {
[ConfigurationManager instance].fadeTimeout = timeout;
self.window.fadeTimeout = timeout;
[self.window setKeys:@"--TEST--" wipe:YES];
}

@end
4 changes: 2 additions & 2 deletions ShowKeys/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>0.1</string>
<string>0.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>2</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSHumanReadableCopyright</key>
Expand Down
1 change: 1 addition & 0 deletions ShowKeys/PreferencesWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
@protocol PreferencesWindowDelegate <NSObject>

- (void)opacityChanged:(float)opacity;
- (void)fadeTimeoutChanged:(float)timeout;
- (void)textColorChanged:(NSColor *)color;

@end
Expand Down
15 changes: 15 additions & 0 deletions ShowKeys/PreferencesWindowController.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ @interface PreferencesWindowController ()

@property (weak) IBOutlet NSSlider *opacitySlider;
@property (weak) IBOutlet NSColorWell *textColorChooser;
@property (weak) IBOutlet NSSlider *fadeOutSlider;
@property (weak) IBOutlet NSTextField *fadeOutDisplay;

@end

Expand All @@ -21,6 +23,8 @@ @implementation PreferencesWindowController
- (void)windowDidLoad {
[super windowDidLoad];
[_opacitySlider setIntegerValue:(int)([ConfigurationManager instance].opacity * 100.0)];
[_fadeOutSlider setFloatValue:[ConfigurationManager instance].fadeTimeout / 0.25];
[_fadeOutDisplay setStringValue:[NSString stringWithFormat:@"%0.02fs", [ConfigurationManager instance].fadeTimeout]];
[_textColorChooser setColor:[ConfigurationManager instance].textColor];
}

Expand All @@ -32,4 +36,15 @@ - (IBAction)textColorDidChange:(id)sender {
[self.delegate textColorChanged:[sender color]];
}

- (IBAction)fadeOutSliderDidMove:(id)sender {
float seconds = 0.25 * (float)[sender intValue];
if(seconds == 0.0) {
[_fadeOutDisplay setStringValue:@"Never"];
}
else {
[_fadeOutDisplay setStringValue:[NSString stringWithFormat:@"%0.02fs", seconds]];
}
[self.delegate fadeTimeoutChanged:seconds];
}

@end
39 changes: 37 additions & 2 deletions ShowKeys/PreferencesWindowController.xib
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="PreferencesWindowController">
<connections>
<outlet property="fadeOutDisplay" destination="FZ7-mM-BZa" id="LIF-yT-mtp"/>
<outlet property="fadeOutSlider" destination="vD5-Qm-XTx" id="ebk-UJ-fwd"/>
<outlet property="opacitySlider" destination="eQ3-ln-RM1" id="ekM-JT-bKB"/>
<outlet property="textColorChooser" destination="jQ2-I7-sxi" id="X6b-Kn-W9X"/>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
Expand Down Expand Up @@ -38,13 +40,39 @@
</connections>
</slider>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="RE7-ML-my9">
<rect key="frame" x="20" y="139" width="67" height="17"/>
<rect key="frame" x="18" y="139" width="67" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Text Color" id="59P-KH-fRY">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="Ar3-e0-ava">
<rect key="frame" x="18" y="76" width="91" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Text Fade Out" id="Zvq-B2-dc5">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<slider verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="vD5-Qm-XTx">
<rect key="frame" x="18" y="43" width="268" height="27"/>
<sliderCell key="cell" state="on" alignment="left" maxValue="20" doubleValue="4.2105263157894735" tickMarkPosition="below" numberOfTickMarks="20" allowsTickMarkValuesOnly="YES" sliderType="linear" id="106-lk-7Zb"/>
<connections>
<action selector="fadeOutSliderDidMove:" target="-2" id="g8p-Kt-nib"/>
</connections>
</slider>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="FZ7-mM-BZa">
<rect key="frame" x="290" y="49" width="44" height="19"/>
<constraints>
<constraint firstAttribute="width" constant="40" id="6s5-dd-99A"/>
</constraints>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="1.00s" id="HrG-NV-YNN">
<font key="font" size="12" name="HelveticaNeue-Bold"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<colorWell translatesAutoresizingMaskIntoConstraints="NO" id="jQ2-I7-sxi">
<rect key="frame" x="20" y="101" width="312" height="30"/>
<constraints>
Expand All @@ -58,14 +86,21 @@
</subviews>
<constraints>
<constraint firstItem="jQ2-I7-sxi" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="0SO-Tt-0uN"/>
<constraint firstItem="vD5-Qm-XTx" firstAttribute="centerY" secondItem="FZ7-mM-BZa" secondAttribute="centerY" constant="2" id="0dm-52-Q3I"/>
<constraint firstItem="RE7-ML-my9" firstAttribute="top" secondItem="eQ3-ln-RM1" secondAttribute="bottom" constant="8" id="2gy-2G-R83"/>
<constraint firstItem="eQ3-ln-RM1" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="52c-zJ-jvP"/>
<constraint firstItem="eQ3-ln-RM1" firstAttribute="top" secondItem="mvf-Z3-TQa" secondAttribute="bottom" constant="8" id="8eC-xq-FbF"/>
<constraint firstItem="jQ2-I7-sxi" firstAttribute="top" secondItem="RE7-ML-my9" secondAttribute="bottom" constant="8" id="Acr-Md-U1X"/>
<constraint firstItem="mvf-Z3-TQa" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="GTn-zi-Mht"/>
<constraint firstItem="RE7-ML-my9" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="22" id="GY9-38-5DK"/>
<constraint firstItem="RE7-ML-my9" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="GY9-38-5DK"/>
<constraint firstAttribute="trailing" secondItem="eQ3-ln-RM1" secondAttribute="trailing" constant="20" id="Juc-OU-IaQ"/>
<constraint firstItem="Ar3-e0-ava" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="P0V-CK-bRs"/>
<constraint firstItem="FZ7-mM-BZa" firstAttribute="leading" secondItem="vD5-Qm-XTx" secondAttribute="trailing" constant="8" id="PCb-v1-sRX"/>
<constraint firstItem="Ar3-e0-ava" firstAttribute="top" secondItem="jQ2-I7-sxi" secondAttribute="bottom" constant="8" id="TgV-BR-VXg"/>
<constraint firstItem="vD5-Qm-XTx" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" id="dik-7E-XIf"/>
<constraint firstItem="mvf-Z3-TQa" firstAttribute="top" secondItem="se5-gp-TjO" secondAttribute="top" constant="20" id="gSk-N3-JsV"/>
<constraint firstItem="vD5-Qm-XTx" firstAttribute="top" secondItem="Ar3-e0-ava" secondAttribute="bottom" constant="8" id="gw6-uJ-jPE"/>
<constraint firstAttribute="trailing" secondItem="FZ7-mM-BZa" secondAttribute="trailing" constant="20" id="nFF-Yt-DgP"/>
<constraint firstAttribute="trailing" secondItem="jQ2-I7-sxi" secondAttribute="trailing" constant="20" id="yp1-YH-gtd"/>
</constraints>
</view>
Expand Down
3 changes: 2 additions & 1 deletion ShowKeys/ShowKeysWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@

@interface ShowKeysWindow : NSWindow

@property float fadeTimeout;
@property (weak) IBOutlet NSTextField *keysDisplay;

- (void)configure:(float)opacity textColor:(NSColor *)color;
- (void)configure:(float)opacity fadeTimeout:(float)timeout textColor:(NSColor *)color;
- (void)setKeys:(NSString *)keys wipe:(bool)wipe;

@end
21 changes: 11 additions & 10 deletions ShowKeys/ShowKeysWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,18 @@ - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)aStyle backi
}

- (void) configure {
[self configure:0.8 textColor:[NSColor whiteColor]];
[self configure:0.8 fadeTimeout:1.0 textColor:[NSColor whiteColor]];
}

- (void)configure:(float)opacity textColor:(NSColor *)color {
- (void)configure:(float)opacity fadeTimeout:(float)timeout textColor:(NSColor *)color {
[self setBackgroundColor:[NSColor colorWithCalibratedWhite:0.2 alpha:opacity]];
[self.keysDisplay setTextColor:color];
self.fadeTimeout = timeout;
[self setOpaque:NO];
[self setHasShadow:NO];
[self setLevel:NSFloatingWindowLevel];
}



-(void)mouseDown:(NSEvent *)theEvent {
NSRect windowFrame = [self frame];

Expand Down Expand Up @@ -76,7 +75,7 @@ - (void)mouseDragged:(NSEvent *)theEvent {
}

- (void)setKeys:(NSString *)keys wipe:(bool)wipe {
if(wipe || nil == timer || ! timer.valid || [keys isEqualTo:@" "] || [[self.keysDisplay stringValue] isEqualTo:@""]) {
if(wipe || (self.fadeTimeout >= 0.25 && (nil == timer || ! timer.valid)) || [keys isEqualTo:@" "] || [[self.keysDisplay stringValue] isEqualTo:@""]) {
[self.keysDisplay setStringValue:keys];
}
else {
Expand All @@ -89,11 +88,13 @@ - (void)setKeys:(NSString *)keys wipe:(bool)wipe {

[self.keysDisplay setAlphaValue:1.0];

timer = [NSTimer scheduledTimerWithTimeInterval:0.75
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:NO];
if(self.fadeTimeout >= 0.25) {
timer = [NSTimer scheduledTimerWithTimeInterval:self.fadeTimeout - 0.25
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:NO];
}
}

- (void)timeout:(NSTimer *)timer {
Expand Down

0 comments on commit ab51f92

Please sign in to comment.