Commit 270228dc authored by wesleylancel's avatar wesleylancel Committed by Commit bot

Match profile name textfield behaviour to Windows

- Submit on tab
- Do nothing when field is empty

BUG=457585

Review URL: https://codereview.chromium.org/960863002

Cr-Commit-Position: refs/heads/master@{#318945}
parent 5d62c70e
...@@ -618,7 +618,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -618,7 +618,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
@end @end
// A custom text control that turns into a textfield for editing when clicked. // A custom text control that turns into a textfield for editing when clicked.
@interface EditableProfileNameButton : HoverImageButton { @interface EditableProfileNameButton : HoverImageButton<NSTextFieldDelegate> {
@private @private
base::scoped_nsobject<NSTextField> profileNameTextField_; base::scoped_nsobject<NSTextField> profileNameTextField_;
Profile* profile_; // Weak. Profile* profile_; // Weak.
...@@ -635,7 +635,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -635,7 +635,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
- (void)showEditableView:(id)sender; - (void)showEditableView:(id)sender;
// Called when enter is pressed in the text field. // Called when enter is pressed in the text field.
- (void)saveProfileName:(id)sender; - (void)saveProfileName;
@end @end
...@@ -691,8 +691,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -691,8 +691,7 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
NSLineBreakByTruncatingTail]; NSLineBreakByTruncatingTail];
[[profileNameTextField_ cell] setUsesSingleLineMode:YES]; [[profileNameTextField_ cell] setUsesSingleLineMode:YES];
[self addSubview:profileNameTextField_]; [self addSubview:profileNameTextField_];
[profileNameTextField_ setTarget:self]; [profileNameTextField_ setDelegate:self];
[profileNameTextField_ setAction:@selector(saveProfileName:)];
// Hide the textfield until the user clicks on the button. // Hide the textfield until the user clicks on the button.
[profileNameTextField_ setHidden:YES]; [profileNameTextField_ setHidden:YES];
...@@ -726,21 +725,23 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -726,21 +725,23 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
return self; return self;
} }
- (void)saveProfileName:(id)sender { - (void)saveProfileName {
base::string16 newProfileName = base::string16 newProfileName =
base::SysNSStringToUTF16([profileNameTextField_ stringValue]); base::SysNSStringToUTF16([profileNameTextField_ stringValue]);
// Empty profile names are not allowed, and are treated as a cancel. // Empty profile names are not allowed, and do nothing.
base::TrimWhitespace(newProfileName, base::TRIM_ALL, &newProfileName); base::TrimWhitespace(newProfileName, base::TRIM_ALL, &newProfileName);
if (!newProfileName.empty()) { if (!newProfileName.empty()) {
profiles::UpdateProfileName(profile_, newProfileName); profiles::UpdateProfileName(profile_, newProfileName);
[controller_ [controller_
postActionPerformed:ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_NAME]; postActionPerformed:ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_NAME];
} else { [profileNameTextField_ setHidden:YES];
// Since the text is empty and not allowed, revert it from the textbox. // This needs to be called async as the firstResponder is reset
[profileNameTextField_ setStringValue:[self title]]; // at the same time that controlTextDidEndEditing happens.
dispatch_async(dispatch_get_main_queue(), ^{
[[self window] makeFirstResponder:nil];
});
} }
[profileNameTextField_ setHidden:YES];
} }
- (void)showEditableView:(id)sender { - (void)showEditableView:(id)sender {
...@@ -752,6 +753,10 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver, ...@@ -752,6 +753,10 @@ class ActiveProfileObserverBridge : public AvatarMenuObserver,
return false; return false;
} }
- (void)controlTextDidEndEditing:(NSNotification*)notification {
[self saveProfileName];
}
@end @end
// A custom button that allows for setting a background color when hovered over. // A custom button that allows for setting a background color when hovered over.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment