Clean-up: Replaces gfx::Font with gfx::FontList in c/b/chromeos.
[chromium-blink-merge.git] / chrome / browser / chromeos / input_method / candidate_window_view.cc
blob35a4c6f26ed0504cb798f2533791cc413164c075
1 // Copyright (c) 2012 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 #include "chrome/browser/chromeos/input_method/candidate_window_view.h"
6 #include <string>
8 #include "ash/shell.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/chromeos/input_method/candidate_view.h"
11 #include "chrome/browser/chromeos/input_method/candidate_window_constants.h"
12 #include "chrome/browser/chromeos/input_method/hidable_area.h"
13 #include "chromeos/ime/candidate_window.h"
14 #include "ui/gfx/color_utils.h"
15 #include "ui/native_theme/native_theme.h"
16 #include "ui/views/background.h"
17 #include "ui/views/border.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/layout/grid_layout.h"
20 #include "ui/views/widget/widget.h"
22 namespace chromeos {
23 namespace input_method {
25 namespace {
26 // VerticalCandidateLabel is used for rendering candidate text in
27 // the vertical candidate window.
28 class VerticalCandidateLabel : public views::Label {
29 public:
30 VerticalCandidateLabel() {}
32 private:
33 virtual ~VerticalCandidateLabel() {}
35 // Returns the preferred size, but guarantees that the width has at
36 // least kMinCandidateLabelWidth pixels.
37 virtual gfx::Size GetPreferredSize() OVERRIDE {
38 gfx::Size size = Label::GetPreferredSize();
39 // Hack. +2 is needed to prevent labels from getting elided like
40 // "abc..." in some cases. TODO(satorux): Figure out why it's
41 // necessary.
42 size.set_width(size.width() + 2);
43 if (size.width() < kMinCandidateLabelWidth) {
44 size.set_width(kMinCandidateLabelWidth);
46 if (size.width() > kMaxCandidateLabelWidth) {
47 size.set_width(kMaxCandidateLabelWidth);
49 return size;
52 DISALLOW_COPY_AND_ASSIGN(VerticalCandidateLabel);
55 // Wraps the given view with some padding, and returns it.
56 views::View* WrapWithPadding(views::View* view, const gfx::Insets& insets) {
57 views::View* wrapper = new views::View;
58 // Use GridLayout to give some insets inside.
59 views::GridLayout* layout = new views::GridLayout(wrapper);
60 wrapper->SetLayoutManager(layout); // |wrapper| owns |layout|.
61 layout->SetInsets(insets);
63 views::ColumnSet* column_set = layout->AddColumnSet(0);
64 column_set->AddColumn(
65 views::GridLayout::FILL, views::GridLayout::FILL,
66 1, views::GridLayout::USE_PREF, 0, 0);
67 layout->StartRow(0, 0);
69 // Add the view contents.
70 layout->AddView(view); // |view| is owned by |wraper|, not |layout|.
71 return wrapper;
74 // Creates shortcut text from the given index and the orientation.
75 base::string16 CreateShortcutText(size_t index,
76 const CandidateWindow& candidate_window) {
77 if (index >= candidate_window.candidates().size())
78 return base::string16();
79 std::string shortcut_text = candidate_window.candidates()[index].label;
80 if (!shortcut_text.empty() &&
81 candidate_window.orientation() != CandidateWindow::VERTICAL)
82 shortcut_text += '.';
83 return base::UTF8ToUTF16(shortcut_text);
86 // Creates the shortcut label, and returns it (never returns NULL).
87 // The label text is not set in this function.
88 views::Label* CreateShortcutLabel(
89 CandidateWindow::Orientation orientation, const ui::NativeTheme& theme) {
90 // Create the shortcut label. The label will be owned by
91 // |wrapped_shortcut_label|, hence it's deleted when
92 // |wrapped_shortcut_label| is deleted.
93 views::Label* shortcut_label = new views::Label;
95 if (orientation == CandidateWindow::VERTICAL) {
96 shortcut_label->SetFontList(
97 shortcut_label->font_list().DeriveFontListWithSizeDeltaAndStyle(
98 kFontSizeDelta, gfx::Font::BOLD));
99 } else {
100 shortcut_label->SetFontList(
101 shortcut_label->font_list().DeriveFontListWithSizeDelta(
102 kFontSizeDelta));
104 // TODO(satorux): Maybe we need to use language specific fonts for
105 // candidate_label, like Chinese font for Chinese input method?
106 shortcut_label->SetEnabledColor(theme.GetSystemColor(
107 ui::NativeTheme::kColorId_LabelEnabledColor));
108 shortcut_label->SetDisabledColor(theme.GetSystemColor(
109 ui::NativeTheme::kColorId_LabelDisabledColor));
111 return shortcut_label;
114 // Wraps the shortcut label, then decorates wrapped shortcut label
115 // and returns it (never returns NULL).
116 // The label text is not set in this function.
117 views::View* CreateWrappedShortcutLabel(
118 views::Label* shortcut_label,
119 CandidateWindow::Orientation orientation,
120 const ui::NativeTheme& theme) {
121 // Wrap it with padding.
122 const gfx::Insets kVerticalShortcutLabelInsets(1, 6, 1, 6);
123 const gfx::Insets kHorizontalShortcutLabelInsets(1, 3, 1, 0);
124 const gfx::Insets insets =
125 (orientation == CandidateWindow::VERTICAL ?
126 kVerticalShortcutLabelInsets :
127 kHorizontalShortcutLabelInsets);
128 views::View* wrapped_shortcut_label =
129 WrapWithPadding(shortcut_label, insets);
131 // Add decoration based on the orientation.
132 if (orientation == CandidateWindow::VERTICAL) {
133 // Set the background color.
134 SkColor blackish = color_utils::AlphaBlend(
135 SK_ColorBLACK,
136 theme.GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
137 0x40);
138 SkColor transparent_blakish = color_utils::AlphaBlend(
139 SK_ColorTRANSPARENT, blackish, 0xE0);
140 wrapped_shortcut_label->set_background(
141 views::Background::CreateSolidBackground(transparent_blakish));
142 shortcut_label->SetBackgroundColor(
143 wrapped_shortcut_label->background()->get_color());
146 return wrapped_shortcut_label;
149 // Creates the candidate label, and returns it (never returns NULL).
150 // The label text is not set in this function.
151 views::Label* CreateCandidateLabel(
152 CandidateWindow::Orientation orientation) {
153 views::Label* candidate_label = NULL;
155 // Create the candidate label. The label will be added to |this| as a
156 // child view, hence it's deleted when |this| is deleted.
157 if (orientation == CandidateWindow::VERTICAL) {
158 candidate_label = new VerticalCandidateLabel;
159 } else {
160 candidate_label = new views::Label;
163 // Change the font size.
164 candidate_label->SetFontList(
165 candidate_label->font_list().DeriveFontListWithSizeDelta(kFontSizeDelta));
166 candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
168 return candidate_label;
171 // Creates the annotation label, and return it (never returns NULL).
172 // The label text is not set in this function.
173 views::Label* CreateAnnotationLabel(
174 CandidateWindow::Orientation orientation, const ui::NativeTheme& theme) {
175 // Create the annotation label.
176 views::Label* annotation_label = new views::Label;
178 // Change the font size and color.
179 annotation_label->SetFontList(
180 annotation_label->font_list().DeriveFontListWithSizeDelta(
181 kFontSizeDelta));
182 annotation_label->SetEnabledColor(theme.GetSystemColor(
183 ui::NativeTheme::kColorId_LabelDisabledColor));
184 annotation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
186 return annotation_label;
189 // Computes shortcut column size.
190 gfx::Size ComputeShortcutColumnSize(
191 const CandidateWindow& candidate_window,
192 const ui::NativeTheme& theme) {
193 int shortcut_column_width = 0;
194 int shortcut_column_height = 0;
195 // Create the shortcut label. The label will be owned by
196 // |wrapped_shortcut_label|, hence it's deleted when
197 // |wrapped_shortcut_label| is deleted.
198 views::Label* shortcut_label = CreateShortcutLabel(
199 candidate_window.orientation(), theme);
200 scoped_ptr<views::View> wrapped_shortcut_label(
201 CreateWrappedShortcutLabel(shortcut_label,
202 candidate_window.orientation(),
203 theme));
205 // Compute the max width and height in shortcut labels.
206 // We'll create temporary shortcut labels, and choose the largest width and
207 // height.
208 for (size_t i = 0; i < candidate_window.page_size(); ++i) {
209 shortcut_label->SetText(CreateShortcutText(i, candidate_window));
210 gfx::Size text_size = wrapped_shortcut_label->GetPreferredSize();
211 shortcut_column_width = std::max(shortcut_column_width, text_size.width());
212 shortcut_column_height = std::max(shortcut_column_height,
213 text_size.height());
216 return gfx::Size(shortcut_column_width, shortcut_column_height);
219 // Computes the page index. For instance, if the page size is 9, and the
220 // cursor is pointing to 13th candidate, the page index will be 1 (2nd
221 // page, as the index is zero-origin). Returns -1 on error.
222 int ComputePageIndex(const CandidateWindow& candidate_window) {
223 if (candidate_window.page_size() > 0)
224 return candidate_window.cursor_position() / candidate_window.page_size();
225 return -1;
228 // Computes candidate column size.
229 gfx::Size ComputeCandidateColumnSize(
230 const CandidateWindow& candidate_window) {
231 int candidate_column_width = 0;
232 int candidate_column_height = 0;
233 scoped_ptr<views::Label> candidate_label(
234 CreateCandidateLabel(candidate_window.orientation()));
236 // Compute the start index of |candidate_window_|.
237 const int current_page_index = ComputePageIndex(candidate_window);
238 if (current_page_index < 0)
239 return gfx::Size(0, 0);
240 const size_t start_from = current_page_index * candidate_window.page_size();
242 // Compute the max width and height in candidate labels.
243 // We'll create temporary candidate labels, and choose the largest width and
244 // height.
245 for (size_t i = 0;
246 i + start_from < candidate_window.candidates().size();
247 ++i) {
248 const size_t index = start_from + i;
250 candidate_label->SetText(
251 base::UTF8ToUTF16(candidate_window.candidates()[index].value));
252 gfx::Size text_size = candidate_label->GetPreferredSize();
253 candidate_column_width = std::max(candidate_column_width,
254 text_size.width());
255 candidate_column_height = std::max(candidate_column_height,
256 text_size.height());
259 return gfx::Size(candidate_column_width, candidate_column_height);
262 // Computes annotation column size.
263 gfx::Size ComputeAnnotationColumnSize(
264 const CandidateWindow& candidate_window, const ui::NativeTheme& theme) {
265 int annotation_column_width = 0;
266 int annotation_column_height = 0;
267 scoped_ptr<views::Label> annotation_label(
268 CreateAnnotationLabel(candidate_window.orientation(), theme));
270 // Compute the start index of |candidate_window_|.
271 const int current_page_index = ComputePageIndex(candidate_window);
272 if (current_page_index < 0)
273 return gfx::Size(0, 0);
274 const size_t start_from = current_page_index * candidate_window.page_size();
276 // Compute max width and height in annotation labels.
277 // We'll create temporary annotation labels, and choose the largest width and
278 // height.
279 for (size_t i = 0;
280 i + start_from < candidate_window.candidates().size();
281 ++i) {
282 const size_t index = start_from + i;
284 annotation_label->SetText(
285 base::UTF8ToUTF16(candidate_window.candidates()[index].annotation));
286 gfx::Size text_size = annotation_label->GetPreferredSize();
287 annotation_column_width = std::max(annotation_column_width,
288 text_size.width());
289 annotation_column_height = std::max(annotation_column_height,
290 text_size.height());
293 return gfx::Size(annotation_column_width, annotation_column_height);
296 } // namespace
298 // InformationTextArea is a HidableArea having a single Label in it.
299 class InformationTextArea : public HidableArea {
300 public:
301 // Specify the alignment and initialize the control.
302 InformationTextArea(gfx::HorizontalAlignment align, int minWidth)
303 : minWidth_(minWidth) {
304 label_ = new views::Label;
305 label_->SetHorizontalAlignment(align);
307 const gfx::Insets kInsets(2, 2, 2, 4);
308 views::View* contents = WrapWithPadding(label_, kInsets);
309 SetContents(contents);
310 contents->set_border(views::Border::CreateSolidBorder(
312 GetNativeTheme()->GetSystemColor(
313 ui::NativeTheme::kColorId_MenuBorderColor)));
314 contents->set_background(views::Background::CreateSolidBackground(
315 color_utils::AlphaBlend(SK_ColorBLACK,
316 GetNativeTheme()->GetSystemColor(
317 ui::NativeTheme::kColorId_WindowBackground),
318 0x10)));
319 label_->SetBackgroundColor(contents->background()->get_color());
322 // Set the displayed text.
323 void SetText(const std::string& utf8_text) {
324 label_->SetText(base::UTF8ToUTF16(utf8_text));
327 protected:
328 virtual gfx::Size GetPreferredSize() OVERRIDE {
329 gfx::Size size = HidableArea::GetPreferredSize();
330 // Hack. +2 is needed as the same reason as in VerticalCandidateLabel
331 size.set_width(size.width() + 2);
332 if (size.width() < minWidth_) {
333 size.set_width(minWidth_);
335 return size;
338 private:
339 views::Label* label_;
340 int minWidth_;
342 DISALLOW_COPY_AND_ASSIGN(InformationTextArea);
345 CandidateView::CandidateView(
346 CandidateWindowView* parent_candidate_window,
347 int index_in_page,
348 CandidateWindow::Orientation orientation)
349 : index_in_page_(index_in_page),
350 orientation_(orientation),
351 parent_candidate_window_(parent_candidate_window),
352 shortcut_label_(NULL),
353 candidate_label_(NULL),
354 annotation_label_(NULL),
355 infolist_icon_(NULL),
356 infolist_icon_enabled_(false) {
359 void CandidateView::Init(int shortcut_column_width,
360 int candidate_column_width,
361 int annotation_column_width,
362 int column_height) {
363 views::GridLayout* layout = new views::GridLayout(this);
364 SetLayoutManager(layout); // |this| owns |layout|.
366 // Create Labels.
367 const ui::NativeTheme& theme = *GetNativeTheme();
368 shortcut_label_ = CreateShortcutLabel(orientation_, theme);
369 views::View* wrapped_shortcut_label =
370 CreateWrappedShortcutLabel(shortcut_label_, orientation_, theme);
371 candidate_label_ = CreateCandidateLabel(orientation_);
372 annotation_label_ = CreateAnnotationLabel(orientation_, theme);
374 // Initialize the column set with three columns.
375 views::ColumnSet* column_set = layout->AddColumnSet(0);
377 // If orientation is vertical, each column width is fixed.
378 // Otherwise the width is resizable.
379 const views::GridLayout::SizeType column_type =
380 orientation_ == CandidateWindow::VERTICAL ?
381 views::GridLayout::FIXED : views::GridLayout::USE_PREF;
383 const int padding_column_width =
384 orientation_ == CandidateWindow::VERTICAL ? 4 : 6;
386 // Set shortcut column type and width.
387 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
388 0, column_type, shortcut_column_width, 0);
389 column_set->AddPaddingColumn(0, padding_column_width);
391 // Set candidate column type and width.
392 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
393 1, views::GridLayout::USE_PREF, 0,
394 orientation_ == CandidateWindow::VERTICAL ?
395 candidate_column_width : 0);
396 column_set->AddPaddingColumn(0, padding_column_width);
398 // Set annotation column type and width.
399 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
400 0, column_type, annotation_column_width, 0);
402 if (orientation_ == CandidateWindow::VERTICAL) {
403 column_set->AddPaddingColumn(0, 1);
404 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 0,
405 views::GridLayout::FIXED, kInfolistIndicatorIconWidth,
407 column_set->AddPaddingColumn(0, 2);
408 } else {
409 column_set->AddPaddingColumn(0, padding_column_width);
412 // Add the shortcut label, the candidate label, and annotation label.
413 layout->StartRow(0, 0);
414 // |wrapped_shortcut_label|, |candidate_label_|, and |annotation_label_|
415 // will be owned by |this|.
416 layout->AddView(wrapped_shortcut_label,
417 1, // Column span.
418 1, // Row span.
419 views::GridLayout::FILL, // Horizontal alignment.
420 views::GridLayout::FILL, // Vertical alignment.
421 -1, // Preferred width, not specified.
422 column_height); // Preferred height.
423 layout->AddView(candidate_label_,
424 1, // Column span.
425 1, // Row span.
426 views::GridLayout::FILL, // Horizontal alignment.
427 views::GridLayout::FILL, // Vertical alignment.
428 -1, // Preferred width, not specified.
429 column_height); // Preferred height.
430 layout->AddView(annotation_label_,
431 1, // Column span.
432 1, // Row span.
433 views::GridLayout::FILL, // Horizontal alignment.
434 views::GridLayout::FILL, // Vertical alignemnt.
435 -1, // Preferred width, not specified.
436 column_height); // Preferred height.
437 if (orientation_ == CandidateWindow::VERTICAL) {
438 infolist_icon_ = new views::View;
439 views::View* infolist_icon_wrapper = new views::View;
440 views::GridLayout* infolist_icon_layout =
441 new views::GridLayout(infolist_icon_wrapper);
442 // |infolist_icon_layout| is owned by |infolist_icon_wrapper|.
443 infolist_icon_wrapper->SetLayoutManager(infolist_icon_layout);
444 infolist_icon_layout->AddColumnSet(0)->AddColumn(
445 views::GridLayout::FILL, views::GridLayout::FILL,
446 0, views::GridLayout::FIXED, kInfolistIndicatorIconWidth, 0);
447 infolist_icon_layout->AddPaddingRow(0, kInfolistIndicatorIconPadding);
448 infolist_icon_layout->StartRow(1.0, 0); // infolist_icon_ is resizable.
449 // |infolist_icon_| is owned by |infolist_icon_wrapper|.
450 infolist_icon_layout->AddView(infolist_icon_);
451 infolist_icon_layout->AddPaddingRow(0, kInfolistIndicatorIconPadding);
452 // |infolist_icon_wrapper| is owned by |this|.
453 layout->AddView(infolist_icon_wrapper);
455 UpdateLabelBackgroundColors();
458 void CandidateView::SetCandidateText(const base::string16& text) {
459 candidate_label_->SetText(text);
462 void CandidateView::SetShortcutText(const base::string16& text) {
463 shortcut_label_->SetText(text);
466 void CandidateView::SetAnnotationText(const base::string16& text) {
467 annotation_label_->SetText(text);
470 void CandidateView::SetInfolistIcon(bool enable) {
471 if (!infolist_icon_ || (infolist_icon_enabled_ == enable))
472 return;
473 infolist_icon_enabled_ = enable;
474 infolist_icon_->set_background(
475 enable ?
476 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
477 ui::NativeTheme::kColorId_FocusedBorderColor)) :
478 NULL);
479 UpdateLabelBackgroundColors();
480 SchedulePaint();
483 void CandidateView::Select() {
484 set_background(
485 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
486 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
487 set_border(views::Border::CreateSolidBorder(
488 1, GetNativeTheme()->GetSystemColor(
489 ui::NativeTheme::kColorId_FocusedBorderColor)));
490 UpdateLabelBackgroundColors();
491 // Need to call SchedulePaint() for background and border color changes.
492 SchedulePaint();
495 void CandidateView::Unselect() {
496 set_background(NULL);
497 set_border(NULL);
498 UpdateLabelBackgroundColors();
499 SchedulePaint(); // See comments at Select().
502 void CandidateView::SetRowEnabled(bool enabled) {
503 shortcut_label_->SetEnabled(enabled);
506 gfx::Point CandidateView::GetCandidateLabelPosition() const {
507 return candidate_label_->GetMirroredPosition();
510 bool CandidateView::OnMousePressed(const ui::MouseEvent& event) {
511 // TODO(kinaba): On Windows and MacOS, candidate windows typically commits a
512 // candidate at OnMouseReleased event. We have chosen OnMousePressed here for
513 // working around several obstacle rising from views implementation over GTK.
514 // See: http://crosbug.com/11423#c11. Since we have moved from GTK to Aura,
515 // the reasoning should have became obsolete. We might want to reconsider
516 // implementing mouse-up selection.
517 SelectCandidateAt(event.location());
518 return false;
521 void CandidateView::OnGestureEvent(ui::GestureEvent* event) {
522 if (event->type() == ui::ET_GESTURE_TAP) {
523 SelectCandidateAt(event->location());
524 event->SetHandled();
525 return;
527 View::OnGestureEvent(event);
530 void CandidateView::SelectCandidateAt(const gfx::Point& location) {
531 gfx::Point location_in_candidate_window = location;
532 views::View::ConvertPointToTarget(this, parent_candidate_window_,
533 &location_in_candidate_window);
534 parent_candidate_window_->OnCandidatePressed(location_in_candidate_window);
535 parent_candidate_window_->CommitCandidate();
538 void CandidateView::UpdateLabelBackgroundColors() {
539 SkColor color = background() ?
540 background()->get_color() :
541 GetNativeTheme()->GetSystemColor(
542 ui::NativeTheme::kColorId_WindowBackground);
543 if (orientation_ != CandidateWindow::VERTICAL)
544 shortcut_label_->SetBackgroundColor(color);
545 candidate_label_->SetBackgroundColor(color);
546 annotation_label_->SetBackgroundColor(color);
549 CandidateWindowView::CandidateWindowView(views::Widget* parent_frame)
550 : selected_candidate_index_in_page_(-1),
551 parent_frame_(parent_frame),
552 preedit_area_(NULL),
553 header_area_(NULL),
554 candidate_area_(NULL),
555 footer_area_(NULL),
556 previous_shortcut_column_size_(0, 0),
557 previous_candidate_column_size_(0, 0),
558 previous_annotation_column_size_(0, 0),
559 should_show_at_composition_head_(false),
560 should_show_upper_side_(false),
561 was_candidate_window_open_(false) {
564 CandidateWindowView::~CandidateWindowView() {
567 void CandidateWindowView::Init() {
568 // Set the background and the border of the view.
569 set_background(
570 views::Background::CreateSolidBackground(GetNativeTheme()->GetSystemColor(
571 ui::NativeTheme::kColorId_WindowBackground)));
572 set_border(views::Border::CreateSolidBorder(
573 1, GetNativeTheme()->GetSystemColor(
574 ui::NativeTheme::kColorId_MenuBorderColor)));
576 // Create areas.
577 preedit_area_ = new InformationTextArea(gfx::ALIGN_LEFT,
578 kMinPreeditAreaWidth);
579 header_area_ = new InformationTextArea(gfx::ALIGN_LEFT, 0);
580 candidate_area_ = new HidableArea;
581 candidate_area_->SetContents(new views::View);
582 footer_area_ = new InformationTextArea(gfx::ALIGN_RIGHT, 0);
584 // Set the window layout of the view
585 views::GridLayout* layout = new views::GridLayout(this);
586 SetLayoutManager(layout); // |this| owns |layout|.
587 views::ColumnSet* column_set = layout->AddColumnSet(0);
588 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL,
589 0, views::GridLayout::USE_PREF, 0, 0);
591 // Add the preedit area
592 layout->StartRow(0, 0);
593 layout->AddView(preedit_area_); // |preedit_area_| is owned by |this|.
595 // Add the header area.
596 layout->StartRow(0, 0);
597 layout->AddView(header_area_); // |header_area_| is owned by |this|.
599 // Add the candidate area.
600 layout->StartRow(0, 0);
601 layout->AddView(candidate_area_); // |candidate_area_| is owned by |this|.
603 // Add the footer area.
604 layout->StartRow(0, 0);
605 layout->AddView(footer_area_); // |footer_area_| is owned by |this|.
608 void CandidateWindowView::HideAll() {
609 parent_frame_->Hide();
610 NotifyIfCandidateWindowOpenedOrClosed();
613 void CandidateWindowView::UpdateParentArea() {
614 if (candidate_area_->IsShown() ||
615 header_area_->IsShown() ||
616 footer_area_->IsShown() ||
617 preedit_area_->IsShown()) {
618 ResizeAndMoveParentFrame();
619 parent_frame_->Show();
620 } else {
621 parent_frame_->Hide();
623 NotifyIfCandidateWindowOpenedOrClosed();
626 void CandidateWindowView::HideLookupTable() {
627 candidate_area_->Hide();
628 UpdateParentArea();
631 void CandidateWindowView::HideAuxiliaryText() {
632 header_area_->Hide();
633 footer_area_->Hide();
634 UpdateParentArea();
637 void CandidateWindowView::ShowAuxiliaryText() {
638 // If candidate_area is not shown, shows auxiliary text at header_area.
639 // We expect both header_area_ and footer_area_ contain same value.
640 if (!candidate_area_->IsShown()) {
641 header_area_->Show();
642 footer_area_->Hide();
643 } else {
644 // If candidate_area is shown, shows auxiliary text with orientation.
645 if (candidate_window_.orientation() == CandidateWindow::HORIZONTAL) {
646 header_area_->Show();
647 footer_area_->Hide();
648 } else {
649 footer_area_->Show();
650 header_area_->Hide();
653 UpdateParentArea();
656 void CandidateWindowView::UpdateAuxiliaryText(const std::string& utf8_text) {
657 header_area_->SetText(utf8_text);
658 footer_area_->SetText(utf8_text);
659 ShowAuxiliaryText();
662 void CandidateWindowView::HidePreeditText() {
663 preedit_area_->Hide();
664 UpdateParentArea();
667 void CandidateWindowView::ShowPreeditText() {
668 preedit_area_->Show();
669 UpdateParentArea();
672 void CandidateWindowView::UpdatePreeditText(const std::string& utf8_text) {
673 preedit_area_->SetText(utf8_text);
676 void CandidateWindowView::ShowLookupTable() {
677 if (!candidate_area_->IsShown())
678 should_show_upper_side_ = false;
679 candidate_area_->Show();
680 UpdateParentArea();
683 void CandidateWindowView::NotifyIfCandidateWindowOpenedOrClosed() {
684 bool is_open = IsCandidateWindowOpen();
685 if (!was_candidate_window_open_ && is_open) {
686 FOR_EACH_OBSERVER(Observer, observers_, OnCandidateWindowOpened());
687 } else if (was_candidate_window_open_ && !is_open) {
688 FOR_EACH_OBSERVER(Observer, observers_, OnCandidateWindowClosed());
690 was_candidate_window_open_ = is_open;
693 bool CandidateWindowView::ShouldUpdateCandidateViews(
694 const CandidateWindow& old_candidate_window,
695 const CandidateWindow& new_candidate_window) {
696 return !old_candidate_window.IsEqual(new_candidate_window);
699 void CandidateWindowView::UpdateCandidates(
700 const CandidateWindow& new_candidate_window) {
701 const bool should_update = ShouldUpdateCandidateViews(candidate_window_,
702 new_candidate_window);
703 // Updating the candidate views is expensive. We'll skip this if possible.
704 if (should_update) {
705 // Initialize candidate views if necessary.
706 MaybeInitializeCandidateViews(new_candidate_window);
708 should_show_at_composition_head_
709 = new_candidate_window.show_window_at_composition();
710 // Compute the index of the current page.
711 const int current_page_index = ComputePageIndex(new_candidate_window);
712 if (current_page_index < 0) {
713 return;
716 // Update the candidates in the current page.
717 const size_t start_from =
718 current_page_index * new_candidate_window.page_size();
720 // In some cases, engines send empty shortcut labels. For instance,
721 // ibus-mozc sends empty labels when they show suggestions. In this
722 // case, we should not show shortcut labels.
723 bool no_shortcut_mode = true;
724 for (size_t i = 0; i < new_candidate_window.candidates().size(); ++i) {
725 if (!new_candidate_window.candidates()[i].label.empty()) {
726 no_shortcut_mode = false;
727 break;
731 for (size_t i = 0; i < candidate_views_.size(); ++i) {
732 const size_t index_in_page = i;
733 const size_t candidate_index = start_from + index_in_page;
734 CandidateView* candidate_view = candidate_views_[index_in_page];
735 // Set the shortcut text.
736 if (no_shortcut_mode) {
737 candidate_view->SetShortcutText(base::string16());
738 } else {
739 // At this moment, we don't use labels sent from engines for UX
740 // reasons. First, we want to show shortcut labels in empty rows
741 // (ex. show 6, 7, 8, ... in empty rows when the number of
742 // candidates is 5). Second, we want to add a period after each
743 // shortcut label when the candidate window is horizontal.
744 candidate_view->SetShortcutText(
745 CreateShortcutText(i, new_candidate_window));
747 // Set the candidate text.
748 if (candidate_index < new_candidate_window.candidates().size()) {
749 const CandidateWindow::Entry& entry =
750 new_candidate_window.candidates()[candidate_index];
751 candidate_view->SetCandidateText(base::UTF8ToUTF16(entry.value));
752 candidate_view->SetAnnotationText(base::UTF8ToUTF16(entry.annotation));
753 candidate_view->SetRowEnabled(true);
754 candidate_view->SetInfolistIcon(!entry.description_title.empty());
755 } else {
756 // Disable the empty row.
757 candidate_view->SetCandidateText(base::string16());
758 candidate_view->SetAnnotationText(base::string16());
759 candidate_view->SetRowEnabled(false);
760 candidate_view->SetInfolistIcon(false);
764 // Update the current candidate window. We'll use candidate_window_ from here.
765 // Note that SelectCandidateAt() uses candidate_window_.
766 candidate_window_.CopyFrom(new_candidate_window);
768 // Select the current candidate in the page.
769 if (candidate_window_.is_cursor_visible()) {
770 if (candidate_window_.page_size()) {
771 const int current_candidate_in_page =
772 candidate_window_.cursor_position() % candidate_window_.page_size();
773 SelectCandidateAt(current_candidate_in_page);
775 } else {
776 // Unselect the currently selected candidate.
777 if (0 <= selected_candidate_index_in_page_ &&
778 static_cast<size_t>(selected_candidate_index_in_page_) <
779 candidate_views_.size()) {
780 candidate_views_[selected_candidate_index_in_page_]->Unselect();
781 selected_candidate_index_in_page_ = -1;
786 void CandidateWindowView::MaybeInitializeCandidateViews(
787 const CandidateWindow& candidate_window) {
788 const CandidateWindow::Orientation orientation =
789 candidate_window.orientation();
790 const int page_size = candidate_window.page_size();
791 views::View* candidate_area_contents = candidate_area_->contents();
793 // Current column width.
794 gfx::Size shortcut_column_size(0, 0);
795 gfx::Size candidate_column_size(0,0);
796 gfx::Size annotation_column_size(0, 0);
798 // If orientation is horizontal, don't need to compute width,
799 // because each label is left aligned.
800 if (orientation == CandidateWindow::VERTICAL) {
801 const ui::NativeTheme& theme = *GetNativeTheme();
802 shortcut_column_size = ComputeShortcutColumnSize(candidate_window, theme);
803 candidate_column_size = ComputeCandidateColumnSize(candidate_window);
804 annotation_column_size = ComputeAnnotationColumnSize(candidate_window,
805 theme);
808 // If the requested number of views matches the number of current views, and
809 // previous and current column width are same, just reuse these.
811 // Note that the early exit logic is not only useful for improving
812 // performance, but also necessary for the horizontal candidate window
813 // to be redrawn properly. If we get rid of the logic, the horizontal
814 // candidate window won't get redrawn properly for some reason when
815 // there is no size change. You can test this by removing "return" here
816 // and type "ni" with Pinyin input method.
817 if (static_cast<int>(candidate_views_.size()) == page_size &&
818 candidate_window_.orientation() == orientation &&
819 previous_shortcut_column_size_ == shortcut_column_size &&
820 previous_candidate_column_size_ == candidate_column_size &&
821 previous_annotation_column_size_ == annotation_column_size) {
822 return;
825 // Update the previous column widths.
826 previous_shortcut_column_size_ = shortcut_column_size;
827 previous_candidate_column_size_ = candidate_column_size;
828 previous_annotation_column_size_ = annotation_column_size;
830 // Clear the existing candidate_views if any.
831 for (size_t i = 0; i < candidate_views_.size(); ++i) {
832 candidate_area_contents->RemoveChildView(candidate_views_[i]);
833 // Delete the view after getting out the current message loop iteration.
834 base::MessageLoop::current()->DeleteSoon(FROM_HERE, candidate_views_[i]);
836 candidate_views_.clear();
837 selected_candidate_index_in_page_ = -1; // Invalidates the index.
839 views::GridLayout* layout = new views::GridLayout(candidate_area_contents);
840 // |candidate_area_contents| owns |layout|.
841 candidate_area_contents->SetLayoutManager(layout);
842 // Initialize the column set.
843 views::ColumnSet* column_set = layout->AddColumnSet(0);
844 if (orientation == CandidateWindow::VERTICAL) {
845 column_set->AddColumn(views::GridLayout::FILL,
846 views::GridLayout::FILL,
847 1, views::GridLayout::USE_PREF, 0, 0);
848 } else {
849 for (int i = 0; i < page_size; ++i) {
850 column_set->AddColumn(views::GridLayout::FILL,
851 views::GridLayout::FILL,
852 0, views::GridLayout::USE_PREF, 0, 0);
856 // Set insets so the border of the selected candidate is drawn inside of
857 // the border of the main candidate window, but we don't have the inset
858 // at the top and the bottom as we have the borders of the header and
859 // footer areas.
860 const gfx::Insets kCandidateAreaInsets(0, 1, 0, 1);
861 layout->SetInsets(kCandidateAreaInsets.top(),
862 kCandidateAreaInsets.left(),
863 kCandidateAreaInsets.bottom(),
864 kCandidateAreaInsets.right());
866 // Use maximum height for all rows in candidate area.
867 const int kColumnHeight = std::max(shortcut_column_size.height(),
868 std::max(candidate_column_size.height(),
869 annotation_column_size.height()));
871 // Add views to the candidate area.
872 if (orientation == CandidateWindow::HORIZONTAL) {
873 layout->StartRow(0, 0);
876 for (int i = 0; i < page_size; ++i) {
877 CandidateView* candidate_row = new CandidateView(this, i, orientation);
878 candidate_row->Init(shortcut_column_size.width(),
879 candidate_column_size.width(),
880 annotation_column_size.width(),
881 kColumnHeight);
882 candidate_views_.push_back(candidate_row);
883 if (orientation == CandidateWindow::VERTICAL) {
884 layout->StartRow(0, 0);
886 // |candidate_row| will be owned by |candidate_area_contents|.
887 layout->AddView(candidate_row,
888 1, // Column span.
889 1, // Row span.
890 // Horizontal alignment.
891 orientation == CandidateWindow::VERTICAL ?
892 views::GridLayout::FILL : views::GridLayout::CENTER,
893 views::GridLayout::CENTER, // Vertical alignment.
894 -1, // Preferred width, not specified.
895 kColumnHeight); // Preferred height.
898 // Compute views size in |layout|.
899 // If we don't call this function, GetHorizontalOffset() often
900 // returns invalid value (returns 0), then candidate window
901 // moves right from the correct position in ResizeAndMoveParentFrame().
902 // TODO(nhiroki): Figure out why it returns invalid value.
903 // It seems that the x-position of the candidate labels is not set.
904 layout->Layout(candidate_area_contents);
907 bool CandidateWindowView::IsCandidateWindowOpen() const {
908 return !should_show_at_composition_head_ &&
909 candidate_area_->visible() && candidate_area_->IsShown();
912 void CandidateWindowView::SelectCandidateAt(int index_in_page) {
913 const int current_page_index = ComputePageIndex(candidate_window_);
914 if (current_page_index < 0) {
915 return;
918 const int cursor_absolute_index =
919 candidate_window_.page_size() * current_page_index + index_in_page;
920 // Ignore click on out of range views.
921 if (cursor_absolute_index < 0 ||
922 candidate_window_.candidates().size() <=
923 static_cast<size_t>(cursor_absolute_index)) {
924 return;
927 // Unselect the currently selected candidate.
928 if (0 <= selected_candidate_index_in_page_ &&
929 static_cast<size_t>(selected_candidate_index_in_page_) <
930 candidate_views_.size()) {
931 candidate_views_[selected_candidate_index_in_page_]->Unselect();
933 // Remember the currently selected candidate index in the current page.
934 selected_candidate_index_in_page_ = index_in_page;
936 // Select the candidate specified by index_in_page.
937 candidate_views_[index_in_page]->Select();
939 // Update the cursor indexes in the model.
940 candidate_window_.set_cursor_position(cursor_absolute_index);
943 void CandidateWindowView::OnCandidatePressed(
944 const gfx::Point& location) {
945 for (size_t i = 0; i < candidate_views_.size(); ++i) {
946 gfx::Point converted_location = location;
947 views::View::ConvertPointToTarget(this, candidate_views_[i],
948 &converted_location);
949 if (candidate_views_[i]->HitTestPoint(converted_location)) {
950 SelectCandidateAt(i);
951 break;
956 void CandidateWindowView::CommitCandidate() {
957 if (!(0 <= selected_candidate_index_in_page_ &&
958 static_cast<size_t>(selected_candidate_index_in_page_) <
959 candidate_views_.size())) {
960 return; // Out of range, do nothing.
963 FOR_EACH_OBSERVER(Observer, observers_,
964 OnCandidateCommitted(selected_candidate_index_in_page_));
967 void CandidateWindowView::ResizeAndMoveParentFrame() {
968 // If rendering operation comes from mozc-engine, uses mozc specific bounds,
969 // otherwise candidate window is shown under the cursor.
970 const int x = should_show_at_composition_head_?
971 composition_head_bounds_.x() : cursor_bounds_.x();
972 // To avoid candidate-window overlapping, uses maximum y-position of mozc
973 // specific bounds and cursor bounds, because mozc-engine does not
974 // consider about multi-line composition.
975 const int y = should_show_at_composition_head_?
976 std::max(composition_head_bounds_.y(), cursor_bounds_.y()) :
977 cursor_bounds_.y();
978 const int height = cursor_bounds_.height();
979 const int horizontal_offset = GetHorizontalOffset();
981 gfx::Rect old_bounds = parent_frame_->GetClientAreaBoundsInScreen();
982 gfx::Rect screen_bounds = ash::Shell::GetScreen()->GetDisplayMatching(
983 cursor_bounds_).work_area();
984 // The size.
985 gfx::Rect frame_bounds = old_bounds;
986 frame_bounds.set_size(GetPreferredSize());
988 // The default position.
989 frame_bounds.set_x(x + horizontal_offset);
990 frame_bounds.set_y(y + height);
992 // Handle overflow at the left and the top.
993 frame_bounds.set_x(std::max(frame_bounds.x(), screen_bounds.x()));
994 frame_bounds.set_y(std::max(frame_bounds.y(), screen_bounds.y()));
996 // Handle overflow at the right.
997 const int right_overflow = frame_bounds.right() - screen_bounds.right();
998 if (right_overflow > 0) {
999 frame_bounds.set_x(frame_bounds.x() - right_overflow);
1002 // Handle overflow at the bottom.
1003 const int bottom_overflow = frame_bounds.bottom() - screen_bounds.bottom();
1005 // To avoid flickering window position, the candidate window should be shown
1006 // on upper side of composition string if it was shown there.
1007 if (should_show_upper_side_ || bottom_overflow > 0) {
1008 frame_bounds.set_y(frame_bounds.y() - height - frame_bounds.height());
1009 should_show_upper_side_ = true;
1012 // TODO(nona): check top_overflow here.
1014 // Move the window per the cursor bounds.
1015 // SetBounds() is not cheap. Only call this when it is really changed.
1016 if (frame_bounds != old_bounds)
1017 parent_frame_->SetBounds(frame_bounds);
1020 int CandidateWindowView::GetHorizontalOffset() {
1021 // Compute the horizontal offset if the candidate window is vertical.
1022 if (!candidate_views_.empty() &&
1023 candidate_window_.orientation() == CandidateWindow::VERTICAL) {
1024 return - candidate_views_[0]->GetCandidateLabelPosition().x();
1026 return 0;
1029 void CandidateWindowView::VisibilityChanged(View* starting_from,
1030 bool is_visible) {
1031 if (is_visible) {
1032 // If the visibility of candidate window is changed,
1033 // we should move the frame to the right position.
1034 ResizeAndMoveParentFrame();
1038 void CandidateWindowView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
1039 // If the bounds(size) of candidate window is changed,
1040 // we should move the frame to the right position.
1041 View::OnBoundsChanged(previous_bounds);
1042 ResizeAndMoveParentFrame();
1045 } // namespace input_method
1046 } // namespace chromeos