Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing AuthTextField UI Glitch #618

Merged
merged 5 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions Simperium-OSX/SPAuthenticationTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@

@interface SPAuthenticationTextField : NSView

@property (assign) id delegate;
@property (strong) NSTextField *textField;
@property (nonatomic, strong, readonly) NSTextField *textField;
@property (nonatomic, weak, readwrite) id<NSTextFieldDelegate> delegate;
@property (nonatomic, copy, readwrite) NSString *stringValue;
@property (nonatomic, copy, readwrite) NSString *placeholderString;
@property (nonatomic, assign, readwrite, getter=isEnabled) BOOL enabled;

- (instancetype)initWithFrame:(NSRect)frame secure:(BOOL)secure;

- (void)setPlaceholderString:(NSString *)string;
- (NSString *)stringValue;
- (void)setStringValue:(NSString *)string;
- (void)setEnabled:(BOOL)enabled;

@end
74 changes: 22 additions & 52 deletions Simperium-OSX/SPAuthenticationTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,9 @@



#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

Expand All @@ -83,15 +38,14 @@ - (void)awakeFromNib {
}

- (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];
Class textFieldClass = secure ? [NSSecureTextField class] : [NSTextField class];
_textField = [[textFieldClass alloc] initWithFrame:textFrame];
NSFont *font = [NSFont fontWithName:[SPAuthenticationConfiguration sharedInstance].regularFontName size:fontSize];
[_textField setFont:font];
Expand All @@ -105,8 +59,8 @@ - (void)setupInterface:(BOOL)secure {
[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];
[nc addObserver:self selector:@selector(handleTextFieldDidBeginEditing:) name:NSTextDidBeginEditingNotification object:nil];
[nc addObserver:self selector:@selector(handleTextFieldDidFinishEditing:) name:NSTextDidEndEditingNotification object:nil];
}

- (void)setStringValue:(NSString *)string {
Expand All @@ -121,6 +75,10 @@ - (void)setPlaceholderString:(NSString *)string {
[[_textField cell] setPlaceholderString:string];
}

- (NSString *)placeholderString {
return [[_textField cell] placeholderString];
}

- (void)setDelegate:(id)delegate {
_textField.delegate = delegate;
}
Expand All @@ -134,11 +92,15 @@ - (void)setEnabled:(BOOL)enabled {
[_textField setEditable:enabled];
}

- (BOOL)isEnabled {
return _textField.enabled;
}

- (void)drawRect:(NSRect)dirtyRect {
NSBezierPath *betterBounds = [NSBezierPath bezierPathWithRect:self.bounds];
[betterBounds addClip];

if (self.isWindowFistResponder) {
if (self.sp_isFirstResponder) {
[[NSColor colorWithCalibratedWhite:0.9 alpha:1.0] setFill];
[betterBounds fill];

Expand All @@ -151,16 +113,24 @@ - (void)drawRect:(NSRect)dirtyRect {
}
}

- (BOOL)sp_isFirstResponder {
NSResponder *responder = [self.window firstResponder];
if (![responder isKindOfClass:[NSText class]]) {
return responder == self.textField;
}

NSText *fieldEditor = (NSText *)responder;
return fieldEditor.delegate == (id<NSTextDelegate>)self.textField;
}


#pragma mark - Notification Helpers

- (void)handleTextFieldDidBeginEditing:(NSNotification *)note {
self.isWindowFistResponder = YES;
[self setNeedsDisplay:YES];
}

- (void)handleTextFieldDidFinishEditing:(NSNotification *)note {
self.isWindowFistResponder = NO;
[self setNeedsDisplay:YES];
}

Expand Down