-
Notifications
You must be signed in to change notification settings - Fork 8
/
ShowKeysWindowController.m
125 lines (103 loc) · 3.87 KB
/
ShowKeysWindowController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// ShowKeysWindowController.m
// ShowKeys
//
// Created by John Hobbs on 7/1/15.
// Copyright (c) 2015 John Hobbs. All rights reserved.
//
#import <QuartzCore/QuartzCore.h>
#import "ShowKeysWindowController.h"
#import "ShowKeysWindow.h"
#import "ConfigurationManager.h"
@implementation ShowKeysWindowController {
NSTimer *timer;
float fadeTimeout;
NSInteger maxChars;
ShowKeysWindow *skwindow;
NSMutableArray *keys;
}
- (id)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:window];
keys = [[NSMutableArray alloc] init];
// I'm lazy.
skwindow = (ShowKeysWindow *) self.window;
[self.window setOpaque:NO];
[self.window setHasShadow:NO];
[self.window setLevel:NSFloatingWindowLevel];
[self configurationChanged:nil];
[skwindow.keysDisplay setNeedsLayout:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(configurationChanged:)
name:kConfigurationChangedNotification
object:nil];
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void) configurationChanged:(NSNotification *) notification {
[self.window setBackgroundColor:[NSColor colorWithCalibratedWhite:0.2 alpha:[ConfigurationManager instance].opacity]];
[skwindow.keysDisplay setTextColor:[ConfigurationManager instance].textColor];
fadeTimeout = [ConfigurationManager instance].fadeTimeout;
NSFont *font = [NSFont fontWithName:skwindow.keysDisplay.font.fontName size:(float)[ConfigurationManager instance].fontSize];
[skwindow.keysDisplay setFont:font];
maxChars = [ConfigurationManager instance].maxChars;
[keys removeAllObjects];
[keys addObject:[[Key alloc] initWithTest]];
[self updateKeysDisplay];
}
- (void)resetWindowWidth {
NSRect window = [self.window frame];
window.size.width = 320; // TODO: Make this width configurable
[self.window setFrame:window display:YES animate:YES];
}
- (void)appendKey:(Key *)key {
// Let's see you grok this, sucker.
if(key.wipeBefore || ([keys count] > 0 && ((Key *)[keys objectAtIndex:[keys count]-1]).wipeAfter)) {
[keys removeAllObjects];
}
[keys addObject:key];
NSInteger length = 0;
for (Key *key in keys) {
length += [key length];
}
while(length > maxChars && [keys count] > 1) {
length -= [keys[0] length];
[keys removeObjectAtIndex:0];
}
[self updateKeysDisplay];
}
- (NSString *)keysAsString {
NSMutableString *out = [[NSMutableString alloc] init];
for (Key *key in keys) {
[out appendString:[key stringValue]];
}
return out;
}
- (void)updateKeysDisplay {
[skwindow.keysDisplay setStringValue:[self keysAsString]];
[self resetWindowWidth];
if(timer) {
[timer invalidate];
}
[skwindow.keysDisplay setAlphaValue:1.0];
if(fadeTimeout >= 0.25) {
timer = [NSTimer scheduledTimerWithTimeInterval:fadeTimeout - 0.25
target:self
selector:@selector(timeout:)
userInfo:nil
repeats:NO];
}
}
- (void)timeout:(NSTimer *)timer {
[keys removeAllObjects];
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
[[NSAnimationContext currentContext] setDuration:.25];
[[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[skwindow.keysDisplay.animator setAlphaValue:0.0];
}
completionHandler:^{
[self resetWindowWidth];
}];
}
@end