summaryrefslogtreecommitdiffstats
path: root/mojo/examples/keyboard/keyboard_view.cc
blob: 3a0d71ac48d5458733ab3b71daa72ca101cc4aee (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2014 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 "mojo/examples/keyboard/keyboard_view.h"

#include <algorithm>

#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "mojo/examples/keyboard/keyboard_delegate.h"
#include "mojo/examples/keyboard/keys.h"
#include "ui/events/keycodes/keyboard_code_conversion.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/gfx/canvas.h"
#include "ui/views/background.h"
#include "ui/views/controls/button/label_button.h"
#include "ui/views/controls/button/label_button_border.h"

namespace mojo {
namespace examples {

namespace {

const int kHorizontalPadding = 6;
const int kVerticalPadding = 8;

base::string16 GetDisplayString(int key_code, int flags) {
  return base::string16(1, ui::GetCharacterFromKeyCode(
                            static_cast<ui::KeyboardCode>(key_code), flags));
}

// Returns a font that fits in the space provided. |text| is used as a basis
// for determing the size.
gfx::FontList CalculateFont(int width, int height, const base::string16& text) {
  gfx::FontList font;
  gfx::FontList last_font;
  while (gfx::Canvas::GetStringWidth(text, font) < width &&
         font.GetHeight() < height) {
    last_font = font;
    font = font.DeriveWithSizeDelta(2);
  }
  return last_font;
}

// Returns the total number of keys in |rows|.
int NumKeys(const std::vector<const Row*>& rows) {
  int result = 0;
  for (size_t i = 0; i < rows.size(); ++i)
    result += static_cast<int>(rows[i]->num_keys);
  return result;
}

}  // namespace

KeyboardView::KeyboardView(KeyboardDelegate* delegate)
    : delegate_(delegate),
      max_keys_in_row_(0),
      keyboard_layout_(KEYBOARD_LAYOUT_ALPHA) {
  set_background(views::Background::CreateSolidBackground(SK_ColorBLACK));
  SetRows(GetQWERTYRows());
}

KeyboardView::~KeyboardView() {
}

void KeyboardView::Layout() {
  if (width() == 0 || height() == 0 || rows_.empty() ||
      last_layout_size_ == bounds().size())
    return;

  last_layout_size_ = bounds().size();

  const int button_width =
      (width() - (max_keys_in_row_ - 1) * kHorizontalPadding) /
      max_keys_in_row_;
  const int button_height =
      (height() - (static_cast<int>(rows_.size() - 1) * kVerticalPadding)) /
      static_cast<int>(rows_.size());
  const int initial_x = (width() - button_width * max_keys_in_row_ -
                         kHorizontalPadding * (max_keys_in_row_ - 1)) / 2;
  for (size_t i = 0; i < rows_.size(); ++i) {
    LayoutRow(*(rows_[i]), static_cast<int>(i), initial_x, button_width,
              button_height);
  }

  views::LabelButtonBorder border(views::Button::STYLE_TEXTBUTTON);
  gfx::Insets insets(border.GetInsets());
  gfx::FontList font = CalculateFont(button_width - insets.width(),
                                     button_height - insets.height(),
                                     base::ASCIIToUTF16("W"));
  gfx::FontList special_font = CalculateFont(button_width - insets.width(),
                                             button_height - insets.height(),
                                             base::ASCIIToUTF16("?123"));
  button_font_ = font;
  ResetFonts(font, special_font);
}

void KeyboardView::SetLayout(KeyboardLayout keyboard_layout) {
  if (keyboard_layout_ == keyboard_layout)
    return;

  keyboard_layout_ = keyboard_layout;
  last_layout_size_ = gfx::Size();
  if (keyboard_layout_ == KEYBOARD_LAYOUT_NUMERIC)
    SetRows(GetNumericRows());
  else
    SetRows(GetQWERTYRows());
  Layout();
  SchedulePaint();
}

void KeyboardView::LayoutRow(const Row& row,
                             int row_index,
                             int initial_x,
                             int button_width,
                             int button_height) {
  int x = initial_x + row.padding * (button_width + kHorizontalPadding);
  const int y = row_index * (button_height + kVerticalPadding);
  for (size_t i = 0; i < row.num_keys; ++i) {
    views::View* button = GetButton(row_index, static_cast<int>(i));
    int actual_width = button_width;
    if (row.keys[i].size > 1) {
      actual_width = (button_width + kHorizontalPadding) *
          row.keys[i].size - kHorizontalPadding;
    }
    button->SetBounds(x, y, actual_width, button_height);
    x += actual_width + kHorizontalPadding;
  }
}

void KeyboardView::SetRows(const std::vector<const Row*>& rows) {
  const int num_keys = NumKeys(rows);
  while (child_count() > num_keys)
    delete child_at(child_count() - 1);
  for (int i = child_count(); i < num_keys; ++i)
    AddChildView(CreateButton());

  last_layout_size_ = gfx::Size();

  rows_ = rows;

  max_keys_in_row_ = 0;
  for (size_t i = 0; i < rows_.size(); ++i) {
    max_keys_in_row_ = std::max(max_keys_in_row_,
                                static_cast<int>(rows_[i]->num_keys));
    ConfigureButtonsInRow(static_cast<int>(i), *rows_[i]);
  }
}

void KeyboardView::ConfigureButtonsInRow(int row_index, const Row& row) {
  for (size_t i = 0; i < row.num_keys; ++i) {
    views::LabelButton* button = GetButton(row_index, static_cast<int>(i));
    const Key& key(row.keys[i]);
    switch (key.display_code) {
      case SPECIAL_KEY_SHIFT:
        // TODO: need image.
        button->SetText(base::string16());
        break;
      case SPECIAL_KEY_NUMERIC:
        button->SetText(base::ASCIIToUTF16("?123"));
        break;
      case SPECIAL_KEY_ALPHA:
        button->SetText(base::ASCIIToUTF16("ABC"));
        break;
      default:
        button->SetText(GetDisplayString(key.display_code,
                                         key.event_flags | event_flags()));
        break;
    }
    button->SetState(views::Button::STATE_NORMAL);
  }
}

views::View* KeyboardView::CreateButton() {
  views::LabelButton* button = new views::LabelButton(this, base::string16());
  button->SetTextColor(views::Button::STATE_NORMAL, SK_ColorWHITE);
  button->SetHorizontalAlignment(gfx::ALIGN_CENTER);
  button->set_background(views::Background::CreateSolidBackground(78, 78, 78));
  button->SetFontList(button_font_);
  //  button->SetHaloColor(SK_ColorBLACK);
  // Turn off animations as we reuse buttons in different layouts. If we didn't
  // do this and you click a button to change the layout then the button you
  // clicked on would animate the transition even though it may now represent a
  // different key.
  button->SetAnimationDuration(0);
  return button;
}

views::LabelButton* KeyboardView::GetButton(int row, int column) {
  int offset = column;
  for (int i = 0; i < row; ++i)
    offset += static_cast<int>(rows_[i]->num_keys);
  return static_cast<views::LabelButton*>(child_at(offset));
}

const Key& KeyboardView::GetKeyForButton(views::Button* button) const {
  int index = GetIndexOf(button);
  DCHECK_NE(-1, index);
  int row = 0;
  while (index >= static_cast<int>(rows_[row]->num_keys)) {
    index -= static_cast<int>(rows_[row]->num_keys);
    row++;
  }
  return rows_[row]->keys[index];
}

void KeyboardView::ResetFonts(const gfx::FontList& button_font,
                              const gfx::FontList& special_font) {
  for (size_t i = 0; i < rows_.size(); ++i) {
    for (size_t j = 0; j < rows_[i]->num_keys; ++j) {
      views::LabelButton* button = GetButton(static_cast<int>(i),
                                             static_cast<int>(j));
      const Key& key(GetKeyForButton(button));
      switch (key.display_code) {
        case SPECIAL_KEY_ALPHA:
        case SPECIAL_KEY_NUMERIC:
          button->SetFontList(special_font);
          break;
        default:
          button->SetFontList(button_font);
          break;
      }
    }
  }
}

void KeyboardView::ButtonPressed(views::Button* sender,
                                 const ui::Event& event) {
  const Key& key(GetKeyForButton(sender));
  switch (key.display_code) {
    case SPECIAL_KEY_SHIFT:
      SetLayout((keyboard_layout_ == KEYBOARD_LAYOUT_SHIFT) ?
                KEYBOARD_LAYOUT_ALPHA : KEYBOARD_LAYOUT_SHIFT);
      return;
    case SPECIAL_KEY_ALPHA:
      SetLayout(KEYBOARD_LAYOUT_ALPHA);
      return;
    case SPECIAL_KEY_NUMERIC:
      SetLayout(KEYBOARD_LAYOUT_NUMERIC);
      return;
    default:
      break;
  }

  // Windows isn't happy if we pass in the flags used to get the display string.
#if defined(OS_WIN)
  int key_event_flags = 0;
#else
  int key_event_flags = key.event_flags;
#endif
  delegate_->OnKeyPressed(key.keyboard_code(), key_event_flags | event_flags());
}

}  // namespace examples
}  // namespace mojo