-
Notifications
You must be signed in to change notification settings - Fork 12
/
CBTextFieldView.m
218 lines (182 loc) · 6.25 KB
/
CBTextFieldView.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// CBTextFieldView.m
// Podcasts
//
// Created by Chris Brakebill on 5/23/14.
// Copyright (c) 2014 Chris Brakebill. All rights reserved.
//
#import "CBTextFieldView.h"
#import "CBLoginFieldCell.h"
#import "CBSecureTextFieldCell.h"
#import <POP/POP.h>
@implementation CBTextFieldView
NSTimer *theTimer;
bool hasFocus(id theField) {
return [[[theField window] firstResponder] isKindOfClass:[NSTextView class]]
&& [[theField window] fieldEditor:NO forObject:nil]!=nil
&& ( (id) [[theField window] firstResponder] ==theField
|| [(id) [[theField window] firstResponder] delegate]==theField); }
-(id)initWithFrame:(NSRect)frameRect secure:(BOOL)secure
{
self = [super initWithFrame:frameRect];
if (self) {
_secure = secure;
[self setWantsLayer:YES];
//self.layer.backgroundColor = [NSColor whiteColor].CGColor;
[self _initializeTextField];
_selectedColor = [NSColor colorWithRed:0.98 green:0.3 blue:0.98 alpha:1.0];
_selectedLayer = [CALayer layer];
_selectedLayer.backgroundColor = _selectedColor.CGColor;
_selectedLayer.frame = CGRectMake(_textField.frame.origin.x, _textField.frame.origin.y, 0, _textField.frame.size.height);
_selectedLayer.zPosition = 1.0;
[self.layer addSublayer:_selectedLayer];
if (hasFocus(_textField)) {
[self selectedTextField:_textField];
} else {
[self deselectedTextField:_textField];
}
theTimer= [NSTimer scheduledTimerWithTimeInterval:.1
target:self
selector:@selector(_checkFocusChange)
userInfo:nil
repeats:YES];
}
return self;
}
- (id)initWithFrame:(NSRect)frame
{
return [self initWithFrame:frame secure:NO];
}
- (void)drawRect:(NSRect)dirtyRect
{
[super drawRect:dirtyRect];
// Drawing code here.
}
-(void)setSecure:(BOOL)secure
{
_secure = secure;
[self _initializeTextField];
// Redraw the textfield with a secure textfield
}
-(void)setPlaceholderString:(NSString *)placeholderString
{
_placeholderString = placeholderString;
if (_placeholderLayer == nil && _placeholderString != nil && _placeholderString.length > 0) {
_placeholderLayer = [CATextLayer layer];
[_placeholderLayer setFrame:[self _placeholderFrame]];
_placeholderLayer.string = _placeholderString;
[_placeholderLayer setFontSize:11.0];
_placeholderLayer.foregroundColor = [NSColor colorWithWhite:0.4 alpha:1.0].CGColor;
_placeholderLayer.zPosition = 2.0;
_placeholderLayer.shouldRasterize = NO;
_placeholderLayer.contentsScale = [[NSScreen mainScreen] backingScaleFactor];
[self.layer addSublayer:_placeholderLayer];
}
}
-(void)_checkFocusChange
{
BOOL focusedNow = hasFocus(_textField);
if (focusedNow != _selected) {
_selected = focusedNow;
if (focusedNow) {
[self selectedTextField:_textField];
} else {
[self deselectedTextField:_textField];
}
[self _updatePlaceHolderFrame];
}
}
-(void)_initializeTextField
{
NSTextField *old = _textField;
NSRect textRect = NSMakeRect(0, 0, self.frame.size.width, self.frame.size.height-20);
if (_secure) {
_textField = [[NSSecureTextField alloc] initWithFrame:textRect];
CBSecureTextFieldCell *cell = [[CBSecureTextFieldCell alloc] initTextCell:@""];
[cell setEditable:YES];
[cell setEnabled:YES];
[_textField setCell:cell];
} else {
[NSTextField setCellClass:[CBLoginFieldCell class]];
_textField = [[NSTextField alloc] initWithFrame:textRect];
}
_textField.delegate = self;
_textField.backgroundColor = [NSColor clearColor];
[_textField setBordered:NO];
[_textField.cell setFocusRingType:NSFocusRingTypeNone];
[_textField setFont:[NSFont fontWithName:@"AvenirNext-Regular" size:12.0]];
[_textField setTextColor:[NSColor colorWithWhite:0.37 alpha:1.0]];
[_textField setWantsLayer:YES];
_textField.layer.backgroundColor = [NSColor whiteColor].CGColor;
[self addSubview:_textField];
[old removeFromSuperview];
}
-(BOOL)control:(NSControl *)control textShouldBeginEditing:(NSText *)fieldEditor
{
NSLog(@"Begin Editing");
return YES;
}
-(BOOL)control:(NSControl *)control textShouldEndEditing:(NSText *)fieldEditor
{
NSLog(@"End Editing");
return YES;
}
-(CGFloat)_placeholderTranslation
{
if ((_textField.stringValue != nil && _textField.stringValue.length > 0) || _selected) {
return 25;
} else {
return 0;
}
}
-(CGRect)_placeholderFrame
{
CGRect ret = CGRectInset(NSRectToCGRect(_textField.frame), 10, 8);
return ret;
}
-(void)_updatePlaceHolderFrame
{
CGRect newFrame = [self _placeholderFrame];
POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerTranslationY];
positionAnimation.toValue = @([self _placeholderTranslation]);
positionAnimation.springBounciness = 12.0f;
positionAnimation.springSpeed = 12.0f;
[_placeholderLayer pop_addAnimation:positionAnimation forKey:@"translation"];
}
-(CGFloat)_selectedColorWidth
{
if (_selected) {
return 10.0f;
} else {
return 0.0f;
}
}
-(void)_updateSelectedColorFrame
{
POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerBounds];
positionAnimation.toValue = [NSValue valueWithCGRect:NSMakeRect(0, 0, [self _selectedColorWidth], _selectedLayer.bounds.size.height)];
positionAnimation.springBounciness = 12.0f;
positionAnimation.springSpeed = 12.0f;
[_selectedLayer pop_addAnimation:positionAnimation forKey:@"width"];
}
-(void)selectedTextField:(NSTextField *)textField
{
_selected = YES;
[self _updatePlaceHolderFrame];
[self _updateSelectedColorFrame];
}
-(void)deselectedTextField:(NSTextField *)textField
{
_selected = NO;
[self _updatePlaceHolderFrame];
[self _updateSelectedColorFrame];
}
-(BOOL)acceptsFirstResponder
{
return NO;
}
-(void)dealloc
{
[theTimer invalidate];
}
@end