summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/textfield_with_margin.cc
blob: b8e44a9401ee44f0b16604a16cb5dd60e483e9ad (plain)
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
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/login/textfield_with_margin.h"

#include "chrome/browser/chromeos/login/helper.h"
#include "ui/base/keycodes/keyboard_codes.h"

namespace {

// Holds ratio of the margin to the preferred text height.
const double kTextMarginRate = 0.33;

// Size of each vertical margin (top, bottom).
const int kVerticalMargin = 3;

}  // namespace

namespace chromeos {

TextfieldWithMargin::TextfieldWithMargin() {
  CorrectTextfieldFontSize(this);
}

TextfieldWithMargin::TextfieldWithMargin(views::Textfield::StyleFlags style)
    : Textfield(style) {
  CorrectTextfieldFontSize(this);
}

void TextfieldWithMargin::Layout() {
  int margin = GetPreferredSize().height() * kTextMarginRate;
  SetHorizontalMargins(margin, margin);
  SetVerticalMargins(kVerticalMargin, kVerticalMargin);
  views::Textfield::Layout();
}

bool TextfieldWithMargin::OnKeyPressed(const views::KeyEvent& e) {
  if (e.key_code() == ui::VKEY_ESCAPE && !text().empty()) {
    SetText(string16());
    return true;
  }
  return views::Textfield::OnKeyPressed(e);
}

}  // namespace chromeos