-
-
Notifications
You must be signed in to change notification settings - Fork 134
/
TPKeyComboView.m
137 lines (111 loc) · 2.95 KB
/
TPKeyComboView.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
126
127
128
129
130
131
132
133
134
135
136
137
//
// TPKeyComboView.m
// teleport
//
// Created by Julien Robert on 08/04/09.
// Copyright 2009 Apple. All rights reserved.
//
#import "TPKeyComboView.h"
#import "PTKeyBroadcaster.h"
@implementation TPKeyComboView
- (void)drawRect:(NSRect)rect
{
BOOL hasFocus = ([[self window] firstResponder] == self);
[[NSColor whiteColor] set];
NSRectFill(rect);
if(hasFocus) {
[[NSColor keyboardFocusIndicatorColor] set];
NSFrameRectWithWidthUsingOperation([self bounds], 2.0, NSCompositeSourceOver);
}
else {
[[NSColor lightGrayColor] set];
NSFrameRect([self bounds]);
}
NSString * string = nil;
NSColor * color = nil;
if(_keyCombo == nil) {
color = [NSColor grayColor];
if(hasFocus) {
string = NSLocalizedStringFromTableInBundle(@"Type key combo", nil, [NSBundle bundleForClass:[self class]], @"Displayed in the key combo field, should be short");
}
else {
string = NSLocalizedStringFromTableInBundle(@"Click to set", nil, [NSBundle bundleForClass:[self class]], @"Displayed in the key combo field, should be short");
}
}
else {
color = [NSColor blackColor];
string = [_keyCombo description];
}
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSCenterTextAlignment];
NSFont * font = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSMiniControlSize]];
NSDictionary * attributes = [[NSDictionary alloc] initWithObjectsAndKeys:
paragraphStyle, NSParagraphStyleAttributeName,
font, NSFontAttributeName,
color, NSForegroundColorAttributeName,
nil];
[string drawInRect:NSInsetRect([self bounds], 2.0, 2.0) withAttributes:attributes];
}
- (void)setDelegate:(id)delegate
{
_delegate = delegate;
}
- (id)delegate
{
return _delegate;
}
- (void)setKeyCombo:(PTKeyCombo*)keyCombo
{
if(keyCombo != _keyCombo) {
_keyCombo = keyCombo;
[self setNeedsDisplay:YES];
}
}
- (PTKeyCombo*)keyCombo
{
return _keyCombo;
}
- (void)_updateKeyComboWithEvent:(NSEvent*)event
{
BOOL updateKeyCombo = YES;
PTKeyCombo * keyCombo = nil;
unsigned short keyCode = [event keyCode];
switch(keyCode) {
case 51: // backspace: clear
break;
case 53: // escape: exit
updateKeyCombo = NO;
break;
default:
keyCombo = [PTKeyCombo keyComboWithKeyCode:keyCode modifiers:[PTKeyBroadcaster cocoaModifiersAsCarbonModifiers:[event modifierFlags]]];
if(![keyCombo isValidHotKeyCombo]) {
NSBeep();
return;
}
}
if(updateKeyCombo) {
[self setKeyCombo:keyCombo];
if(_delegate != nil && [_delegate respondsToSelector:@selector(keyComboDidChange:)])
[_delegate keyComboDidChange:keyCombo];
}
[[self window] makeFirstResponder:[self superview]];
}
- (void)keyDown:(NSEvent*)event
{
[self _updateKeyComboWithEvent:event];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (BOOL)becomeFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
- (BOOL)resignFirstResponder
{
[self setNeedsDisplay:YES];
return YES;
}
@end