-
Notifications
You must be signed in to change notification settings - Fork 43
/
SPAuthenticationTextField.m
167 lines (121 loc) · 4.84 KB
/
SPAuthenticationTextField.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
//
// SPAuthenticationTextField.m
// Simplenote-OSX
//
// Created by Michael Johnston on 7/24/13.
// Copyright (c) 2013 Simperium. All rights reserved.
//
#import "SPAuthenticationTextField.h"
#import "SPAuthenticationConfiguration.h"
#pragma mark ====================================================================================
#pragma mark Constants
#pragma mark ====================================================================================
static NSString* SPTextFieldDidBecomeFirstResponder = @"SPTextFieldDidBecomeFirstResponder";
#pragma mark ====================================================================================
#pragma mark Private Helper: SPTextField
#pragma mark ====================================================================================
@interface SPTextField : NSTextField
@end
@implementation SPTextField
- (BOOL)becomeFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:SPTextFieldDidBecomeFirstResponder object:self];
return [super becomeFirstResponder];
}
@end
#pragma mark ====================================================================================
#pragma mark Private Helper: SPSecureTextField
#pragma mark ====================================================================================
@interface SPSecureTextField : NSSecureTextField
@end
@implementation SPSecureTextField
- (BOOL)becomeFirstResponder {
[[NSNotificationCenter defaultCenter] postNotificationName:SPTextFieldDidBecomeFirstResponder object:self];
return [super becomeFirstResponder];
}
@end
#pragma mark - SPAuthenticationTextField
@interface SPAuthenticationTextField()
@property (nonatomic, assign) BOOL isWindowFistResponder;
@property (nonatomic, assign) BOOL secure;
@end
@implementation SPAuthenticationTextField
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (instancetype)initWithFrame:(NSRect)frame secure:(BOOL)secure {
self = [super initWithFrame:frame];
if (self) {
[self setupInterface:secure];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[self setupInterface:self.secure];
}
- (void)setupInterface:(BOOL)secure {
// Center the textField vertically
NSRect frame = self.frame;
CGFloat paddingX = 10;
CGFloat fontSize = 20;
CGFloat fieldHeight = [[SPAuthenticationConfiguration sharedInstance] regularFontHeightForSize:fontSize];
CGFloat fieldY = (self.frame.size.height - fieldHeight) / 2;
CGRect textFrame = NSMakeRect(paddingX, fieldY, frame.size.width-paddingX*2, fieldHeight);
Class textFieldClass = secure ? [SPSecureTextField class] : [SPTextField class];
_textField = [[textFieldClass alloc] initWithFrame:textFrame];
NSFont *font = [NSFont fontWithName:[SPAuthenticationConfiguration sharedInstance].regularFontName size:fontSize];
[_textField setFont:font];
[_textField setTextColor:[NSColor colorWithCalibratedWhite:0.1 alpha:1.0]];
[_textField setDrawsBackground:NO];
[_textField setBezeled:NO];
[_textField setBordered:NO];
[_textField setFocusRingType:NSFocusRingTypeNone];
[[_textField cell] setWraps:NO];
[[_textField cell] setScrollable:YES];
[self addSubview:_textField];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:SPTextFieldDidBecomeFirstResponder object:_textField];
[nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSControlTextDidEndEditingNotification object:_textField];
}
- (void)setStringValue:(NSString *)string {
_textField.stringValue = string;
}
- (NSString *)stringValue {
return _textField.stringValue;
}
- (void)setPlaceholderString:(NSString *)string {
[[_textField cell] setPlaceholderString:string];
}
- (void)setDelegate:(id)delegate {
_textField.delegate = delegate;
}
- (id)delegate {
return _textField.delegate;
}
- (void)setEnabled:(BOOL)enabled {
[_textField setEnabled:enabled];
[_textField setEditable:enabled];
}
- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRect:self.bounds];
[betterBounds addClip];
if (self.isWindowFistResponder) {
[[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] setFill];
[betterBounds fill];
} else {
[[NSColor colorWithCalibratedWhite:250.f/255.f alpha:1.0] setFill];
[betterBounds fill];
[[NSColor colorWithCalibratedWhite:218.f/255.f alpha:1.0] setStroke];
[betterBounds stroke];
}
}
#pragma mark - Notification Helpers
- (void)handleTextFieldDidBeginEditing:(NSNotification *)note {
self.isWindowFistResponder = YES;
[self setNeedsDisplay:YES];
}
- (void)handleTextFieldDidFinishEditing:(NSNotification *)note {
self.isWindowFistResponder = NO;
[self setNeedsDisplay:YES];
}
@end