OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import <Cocoa/Cocoa.h> | 5 #import <Cocoa/Cocoa.h> |
6 #import <Carbon/Carbon.h> // kVK_Return. | 6 #import <Carbon/Carbon.h> // kVK_Return. |
7 | 7 |
8 #import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h" | 8 #import "chrome/browser/ui/cocoa/profiles/profile_chooser_controller.h" |
9 | 9 |
10 #include "base/mac/bundle_locations.h" | 10 #include "base/mac/bundle_locations.h" |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
611 if ([action isEqualToString:NSAccessibilityPressAction]) { | 611 if ([action isEqualToString:NSAccessibilityPressAction]) { |
612 avatarMenu_->EditProfile(avatarMenu_->GetActiveProfileIndex()); | 612 avatarMenu_->EditProfile(avatarMenu_->GetActiveProfileIndex()); |
613 } | 613 } |
614 | 614 |
615 [super accessibilityPerformAction:action]; | 615 [super accessibilityPerformAction:action]; |
616 } | 616 } |
617 | 617 |
618 @end | 618 @end |
619 | 619 |
620 // A custom text control that turns into a textfield for editing when clicked. | 620 // A custom text control that turns into a textfield for editing when clicked. |
621 @interface EditableProfileNameButton : HoverImageButton { | 621 @interface EditableProfileNameButton : HoverImageButton<NSTextFieldDelegate> { |
622 @private | 622 @private |
623 base::scoped_nsobject<NSTextField> profileNameTextField_; | 623 base::scoped_nsobject<NSTextField> profileNameTextField_; |
624 Profile* profile_; // Weak. | 624 Profile* profile_; // Weak. |
625 ProfileChooserController* controller_; | 625 ProfileChooserController* controller_; |
626 } | 626 } |
627 | 627 |
628 - (id)initWithFrame:(NSRect)frameRect | 628 - (id)initWithFrame:(NSRect)frameRect |
629 profile:(Profile*)profile | 629 profile:(Profile*)profile |
630 profileName:(NSString*)profileName | 630 profileName:(NSString*)profileName |
631 editingAllowed:(BOOL)editingAllowed | 631 editingAllowed:(BOOL)editingAllowed |
632 withController:(ProfileChooserController*)controller; | 632 withController:(ProfileChooserController*)controller; |
633 | 633 |
634 // Called when the button is clicked. | 634 // Called when the button is clicked. |
635 - (void)showEditableView:(id)sender; | 635 - (void)showEditableView:(id)sender; |
636 | 636 |
637 // Called when enter is pressed in the text field. | 637 // Called when enter is pressed in the text field. |
638 - (void)saveProfileName:(id)sender; | 638 - (void)saveProfileName; |
639 | 639 |
640 @end | 640 @end |
641 | 641 |
642 @implementation EditableProfileNameButton | 642 @implementation EditableProfileNameButton |
643 - (id)initWithFrame:(NSRect)frameRect | 643 - (id)initWithFrame:(NSRect)frameRect |
644 profile:(Profile*)profile | 644 profile:(Profile*)profile |
645 profileName:(NSString*)profileName | 645 profileName:(NSString*)profileName |
646 editingAllowed:(BOOL)editingAllowed | 646 editingAllowed:(BOOL)editingAllowed |
647 withController:(ProfileChooserController*)controller { | 647 withController:(ProfileChooserController*)controller { |
648 if ((self = [super initWithFrame:frameRect])) { | 648 if ((self = [super initWithFrame:frameRect])) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
684 [profileNameTextField_ setFont:[NSFont labelFontOfSize:kTitleFontSize]]; | 684 [profileNameTextField_ setFont:[NSFont labelFontOfSize:kTitleFontSize]]; |
685 [profileNameTextField_ setEditable:YES]; | 685 [profileNameTextField_ setEditable:YES]; |
686 [profileNameTextField_ setDrawsBackground:YES]; | 686 [profileNameTextField_ setDrawsBackground:YES]; |
687 [profileNameTextField_ setBezeled:YES]; | 687 [profileNameTextField_ setBezeled:YES]; |
688 [profileNameTextField_ setAlignment:NSCenterTextAlignment]; | 688 [profileNameTextField_ setAlignment:NSCenterTextAlignment]; |
689 [[profileNameTextField_ cell] setWraps:NO]; | 689 [[profileNameTextField_ cell] setWraps:NO]; |
690 [[profileNameTextField_ cell] setLineBreakMode: | 690 [[profileNameTextField_ cell] setLineBreakMode: |
691 NSLineBreakByTruncatingTail]; | 691 NSLineBreakByTruncatingTail]; |
692 [[profileNameTextField_ cell] setUsesSingleLineMode:YES]; | 692 [[profileNameTextField_ cell] setUsesSingleLineMode:YES]; |
693 [self addSubview:profileNameTextField_]; | 693 [self addSubview:profileNameTextField_]; |
694 [profileNameTextField_ setTarget:self]; | 694 [profileNameTextField_ setDelegate:self]; |
695 [profileNameTextField_ setAction:@selector(saveProfileName:)]; | |
696 | 695 |
697 // Hide the textfield until the user clicks on the button. | 696 // Hide the textfield until the user clicks on the button. |
698 [profileNameTextField_ setHidden:YES]; | 697 [profileNameTextField_ setHidden:YES]; |
699 | 698 |
700 [[self cell] accessibilitySetOverrideValue:l10n_util::GetNSStringF( | 699 [[self cell] accessibilitySetOverrideValue:l10n_util::GetNSStringF( |
701 IDS_PROFILES_NEW_AVATAR_MENU_EDIT_NAME_ACCESSIBLE_NAME, | 700 IDS_PROFILES_NEW_AVATAR_MENU_EDIT_NAME_ACCESSIBLE_NAME, |
702 base::SysNSStringToUTF16(profileName)) | 701 base::SysNSStringToUTF16(profileName)) |
703 forAttribute:NSAccessibilityTitleAttribute]; | 702 forAttribute:NSAccessibilityTitleAttribute]; |
704 } | 703 } |
705 | 704 |
706 [[self cell] accessibilitySetOverrideValue:NSAccessibilityButtonRole | 705 [[self cell] accessibilitySetOverrideValue:NSAccessibilityButtonRole |
707 forAttribute:NSAccessibilityRoleAttribute]; | 706 forAttribute:NSAccessibilityRoleAttribute]; |
708 [[self cell] | 707 [[self cell] |
709 accessibilitySetOverrideValue:NSAccessibilityRoleDescription( | 708 accessibilitySetOverrideValue:NSAccessibilityRoleDescription( |
710 NSAccessibilityButtonRole, nil) | 709 NSAccessibilityButtonRole, nil) |
711 forAttribute:NSAccessibilityRoleDescriptionAttribute]; | 710 forAttribute:NSAccessibilityRoleDescriptionAttribute]; |
712 | 711 |
713 [self setBordered:NO]; | 712 [self setBordered:NO]; |
714 [self setFont:[NSFont labelFontOfSize:kTitleFontSize]]; | 713 [self setFont:[NSFont labelFontOfSize:kTitleFontSize]]; |
715 [self setAlignment:NSCenterTextAlignment]; | 714 [self setAlignment:NSCenterTextAlignment]; |
716 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; | 715 [[self cell] setLineBreakMode:NSLineBreakByTruncatingTail]; |
717 [self setTitle:profileName]; | 716 [self setTitle:profileName]; |
718 } | 717 } |
719 return self; | 718 return self; |
720 } | 719 } |
721 | 720 |
722 - (void)saveProfileName:(id)sender { | 721 - (void)saveProfileName { |
723 base::string16 newProfileName = | 722 base::string16 newProfileName = |
724 base::SysNSStringToUTF16([profileNameTextField_ stringValue]); | 723 base::SysNSStringToUTF16([profileNameTextField_ stringValue]); |
725 | 724 |
726 // Empty profile names are not allowed, and are treated as a cancel. | 725 // Empty profile names are not allowed, and do nothing. |
727 base::TrimWhitespace(newProfileName, base::TRIM_ALL, &newProfileName); | 726 base::TrimWhitespace(newProfileName, base::TRIM_ALL, &newProfileName); |
728 if (!newProfileName.empty()) { | 727 if (!newProfileName.empty()) { |
729 profiles::UpdateProfileName(profile_, newProfileName); | 728 profiles::UpdateProfileName(profile_, newProfileName); |
730 [controller_ | 729 [controller_ |
731 postActionPerformed:ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_NAME]; | 730 postActionPerformed:ProfileMetrics::PROFILE_DESKTOP_MENU_EDIT_NAME]; |
732 } else { | 731 [profileNameTextField_ setHidden:YES]; |
733 // Since the text is empty and not allowed, revert it from the textbox. | 732 // Somehow this doesn't work if it's not async. Might need improving. |
groby-ooo-7-16
2015/02/27 23:32:37
I don't think I'm OK with code that is based on "s
| |
734 [profileNameTextField_ setStringValue:[self title]]; | 733 dispatch_async(dispatch_get_main_queue(), ^{ |
734 [[self window] makeFirstResponder:nil]; | |
735 }); | |
735 } | 736 } |
736 [profileNameTextField_ setHidden:YES]; | |
737 } | 737 } |
738 | 738 |
739 - (void)showEditableView:(id)sender { | 739 - (void)showEditableView:(id)sender { |
740 [profileNameTextField_ setHidden:NO]; | 740 [profileNameTextField_ setHidden:NO]; |
741 [[self window] makeFirstResponder:profileNameTextField_]; | 741 [[self window] makeFirstResponder:profileNameTextField_]; |
742 } | 742 } |
743 | 743 |
744 - (BOOL)canBecomeKeyView { | 744 - (BOOL)canBecomeKeyView { |
745 return false; | 745 return false; |
746 } | 746 } |
747 | 747 |
748 - (void)controlTextDidEndEditing:(NSNotification*)notification { | |
749 [self saveProfileName]; | |
750 } | |
751 | |
748 @end | 752 @end |
749 | 753 |
750 // A custom button that allows for setting a background color when hovered over. | 754 // A custom button that allows for setting a background color when hovered over. |
751 @interface BackgroundColorHoverButton : HoverImageButton { | 755 @interface BackgroundColorHoverButton : HoverImageButton { |
752 @private | 756 @private |
753 base::scoped_nsobject<NSColor> backgroundColor_; | 757 base::scoped_nsobject<NSColor> backgroundColor_; |
754 base::scoped_nsobject<NSColor> hoverColor_; | 758 base::scoped_nsobject<NSColor> hoverColor_; |
755 } | 759 } |
756 @end | 760 @end |
757 | 761 |
(...skipping 1526 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
2284 } | 2288 } |
2285 | 2289 |
2286 - (bool)shouldShowGoIncognito { | 2290 - (bool)shouldShowGoIncognito { |
2287 bool incognitoAvailable = | 2291 bool incognitoAvailable = |
2288 IncognitoModePrefs::GetAvailability(browser_->profile()->GetPrefs()) != | 2292 IncognitoModePrefs::GetAvailability(browser_->profile()->GetPrefs()) != |
2289 IncognitoModePrefs::DISABLED; | 2293 IncognitoModePrefs::DISABLED; |
2290 return incognitoAvailable && !browser_->profile()->IsGuestSession(); | 2294 return incognitoAvailable && !browser_->profile()->IsGuestSession(); |
2291 } | 2295 } |
2292 | 2296 |
2293 @end | 2297 @end |
OLD | NEW |