Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/chromeos/login/existing_user_view.h"
      6 
      7 #include "base/utf_string_conversions.h"
      8 #include "chrome/browser/chromeos/login/textfield_with_margin.h"
      9 #include "chrome/browser/chromeos/login/user_controller.h"
     10 #include "chrome/browser/chromeos/login/wizard_accessibility_helper.h"
     11 #include "grit/generated_resources.h"
     12 #include "ui/base/l10n/l10n_util.h"
     13 #include "ui/base/resource/resource_bundle.h"
     14 #include "views/background.h"
     15 #include "views/controls/textfield/textfield.h"
     16 #include "views/focus/focus_manager.h"
     17 #include "views/layout/fill_layout.h"
     18 
     19 namespace chromeos {
     20 
     21 // Colors for gradient background. These should be consistent with border
     22 // window background so textfield border is not visible to the user.
     23 // The background is needed for password textfield to imitate its borders
     24 // transparency correctly.
     25 const SkColor kBackgroundColorTop = SkColorSetRGB(209, 213, 216);
     26 const SkColor kBackgroundColorBottom = SkColorSetRGB(205, 210, 213);
     27 
     28 // Textfield with custom processing for Tab/Shift+Tab to select entries.
     29 class UserEntryTextfield : public TextfieldWithMargin {
     30  public:
     31   UserEntryTextfield(UserController* controller,
     32                      views::Textfield::StyleFlags style)
     33       : TextfieldWithMargin(style),
     34         controller_(controller) {}
     35 
     36   // Overridden from views::View:
     37   virtual bool OnKeyPressed(const views::KeyEvent& e) {
     38     if (e.key_code() == ui::VKEY_TAB) {
     39       controller_->SelectUserRelative(e.IsShiftDown() ? -1 : 1);
     40       return true;
     41     } else if (e.key_code() == ui::VKEY_LEFT) {
     42       controller_->SelectUserRelative(-1);
     43       return true;
     44     } else if (e.key_code() == ui::VKEY_RIGHT) {
     45       controller_->SelectUserRelative(1);
     46       return true;
     47     } else {
     48       return TextfieldWithMargin::OnKeyPressed(e);
     49     }
     50   }
     51 
     52   virtual bool SkipDefaultKeyEventProcessing(const views::KeyEvent& e) {
     53     if (e.key_code() == ui::VKEY_TAB)
     54       return true;
     55     else
     56       return views::Textfield::SkipDefaultKeyEventProcessing(e);
     57   }
     58 
     59  private:
     60   UserController* controller_;
     61 
     62   DISALLOW_COPY_AND_ASSIGN(UserEntryTextfield);
     63 };
     64 
     65 
     66 ExistingUserView::ExistingUserView(UserController* user_controller)
     67     : user_controller_(user_controller),
     68       password_field_(NULL),
     69       accel_enterprise_enrollment_(
     70           views::Accelerator(ui::VKEY_E, false, true, true)),
     71       accel_login_off_the_record_(
     72           views::Accelerator(ui::VKEY_B, false, false, true)),
     73       accel_toggle_accessibility_(
     74           WizardAccessibilityHelper::GetAccelerator()) {
     75   AddAccelerator(accel_enterprise_enrollment_);
     76   AddAccelerator(accel_login_off_the_record_);
     77   AddAccelerator(accel_toggle_accessibility_);
     78 }
     79 
     80 void ExistingUserView::RecreateFields() {
     81   if (password_field_ == NULL) {
     82     SetLayoutManager(new views::FillLayout);
     83     password_field_ = new UserEntryTextfield(user_controller_,
     84                                              views::Textfield::STYLE_PASSWORD);
     85     password_field_->set_background(
     86         views::Background::CreateVerticalGradientBackground(
     87             kBackgroundColorTop, kBackgroundColorBottom));
     88     password_field_->SetFocusable(true);
     89     password_field_->SetController(this);
     90     AddChildView(password_field_);
     91   }
     92   const gfx::Font& base_font = ResourceBundle::GetSharedInstance().GetFont(
     93       ResourceBundle::BaseFont);
     94   SetAndCorrectTextfieldFont(password_field_, base_font);
     95   password_field_->set_text_to_display_when_empty(
     96       l10n_util::GetStringUTF16(IDS_LOGIN_POD_EMPTY_PASSWORD_TEXT));
     97   Layout();
     98   SchedulePaint();
     99 }
    100 
    101 bool ExistingUserView::AcceleratorPressed(
    102     const views::Accelerator& accelerator) {
    103   if (accelerator == accel_enterprise_enrollment_) {
    104     user_controller_->OnStartEnterpriseEnrollment();
    105     return true;
    106   } else if (accelerator == accel_login_off_the_record_) {
    107     user_controller_->OnLoginAsGuest();
    108     return true;
    109   } else if (accelerator == accel_toggle_accessibility_) {
    110     WizardAccessibilityHelper::GetInstance()->ToggleAccessibility();
    111     return true;
    112   }
    113   return false;
    114 }
    115 
    116 bool ExistingUserView::HandleKeyEvent(views::Textfield* sender,
    117                                       const views::KeyEvent& key_event) {
    118   if (key_event.key_code() == ui::VKEY_RETURN) {
    119     if (!password_field_->text().empty())
    120       user_controller_->OnLogin("", UTF16ToUTF8(password_field_->text()));
    121   } else {
    122     user_controller_->ClearErrors();
    123     return false;
    124   }
    125   return true;
    126 }
    127 
    128 void ExistingUserView::RequestFocus() {
    129   password_field_->RequestFocus();
    130 }
    131 
    132 void ExistingUserView::ContentsChanged(views::Textfield* sender,
    133                                        const string16& new_contents) {
    134   if (!new_contents.empty())
    135     user_controller_->ClearErrors();
    136 }
    137 
    138 void ExistingUserView::EnableInputControls(bool enabled) {
    139   password_field_->SetEnabled(enabled);
    140 }
    141 
    142 void ExistingUserView::ClearAndFocusControls() {
    143   ClearAndFocusPassword();
    144 }
    145 
    146 void ExistingUserView::ClearAndFocusPassword() {
    147   password_field_->SetText(string16());
    148   FocusPasswordField();
    149 }
    150 
    151 void ExistingUserView::FocusPasswordField() {
    152   password_field_->RequestFocus();
    153 }
    154 
    155 gfx::Rect ExistingUserView::GetMainInputScreenBounds() const {
    156   return password_field_->GetScreenBounds();
    157 }
    158 
    159 void ExistingUserView::OnLocaleChanged() {
    160   RecreateFields();
    161 }
    162 
    163 }  // namespace chromeos
    164