ec158accef633ae05c83588e9ec1d04436029a4a
[platform/framework/web/crosswalk.git] / src / ash / ime / candidate_window_view.cc
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
3 // found in the LICENSE file.
4
5 #include "ash/ime/candidate_window_view.h"
6
7 #include <string>
8
9 #include "ash/ime/candidate_view.h"
10 #include "ash/ime/candidate_window_constants.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "ui/gfx/color_utils.h"
13 #include "ui/gfx/screen.h"
14 #include "ui/native_theme/native_theme.h"
15 #include "ui/views/background.h"
16 #include "ui/views/border.h"
17 #include "ui/views/bubble/bubble_frame_view.h"
18 #include "ui/views/controls/label.h"
19 #include "ui/views/corewm/window_animations.h"
20 #include "ui/views/layout/box_layout.h"
21 #include "ui/views/layout/fill_layout.h"
22
23 namespace ash {
24 namespace ime {
25
26 namespace {
27
28 class CandidateWindowBorder : public views::BubbleBorder {
29  public:
30   explicit CandidateWindowBorder(gfx::NativeView parent)
31       : views::BubbleBorder(views::BubbleBorder::TOP_CENTER,
32                             views::BubbleBorder::NO_SHADOW,
33                             SK_ColorTRANSPARENT),
34         parent_(parent),
35         offset_(0) {
36     set_paint_arrow(views::BubbleBorder::PAINT_NONE);
37   }
38   virtual ~CandidateWindowBorder() {}
39
40   void set_offset(int offset) { offset_ = offset; }
41
42  private:
43   // Overridden from views::BubbleBorder:
44   virtual gfx::Rect GetBounds(const gfx::Rect& anchor_rect,
45                               const gfx::Size& content_size) const OVERRIDE {
46     gfx::Rect bounds(content_size);
47     bounds.set_origin(gfx::Point(
48         anchor_rect.x() - offset_,
49         is_arrow_on_top(arrow()) ?
50         anchor_rect.bottom() : anchor_rect.y() - content_size.height()));
51
52     // It cannot use the normal logic of arrow offset for horizontal offscreen,
53     // because the arrow must be in the content's edge. But CandidateWindow has
54     // to be visible even when |anchor_rect| is out of the screen.
55     gfx::Rect work_area = gfx::Screen::GetNativeScreen()->
56         GetDisplayNearestWindow(parent_).work_area();
57     if (bounds.right() > work_area.right())
58       bounds.set_x(work_area.right() - bounds.width());
59     if (bounds.x() < work_area.x())
60       bounds.set_x(work_area.x());
61
62     return bounds;
63   }
64
65   virtual gfx::Insets GetInsets() const OVERRIDE {
66     return gfx::Insets();
67   }
68
69   gfx::NativeView parent_;
70   int offset_;
71
72   DISALLOW_COPY_AND_ASSIGN(CandidateWindowBorder);
73 };
74
75 // Computes the page index. For instance, if the page size is 9, and the
76 // cursor is pointing to 13th candidate, the page index will be 1 (2nd
77 // page, as the index is zero-origin). Returns -1 on error.
78 int ComputePageIndex(const ui::CandidateWindow& candidate_window) {
79   if (candidate_window.page_size() > 0)
80     return candidate_window.cursor_position() / candidate_window.page_size();
81   return -1;
82 }
83
84 }  // namespace
85
86 class InformationTextArea : public views::View {
87  public:
88   // InformationTextArea's border is drawn as a separator, it should appear
89   // at either top or bottom.
90   enum BorderPosition {
91     TOP,
92     BOTTOM
93   };
94
95   // Specify the alignment and initialize the control.
96   InformationTextArea(gfx::HorizontalAlignment align, int min_width)
97       : min_width_(min_width) {
98     label_ = new views::Label;
99     label_->SetHorizontalAlignment(align);
100     label_->SetBorder(views::Border::CreateEmptyBorder(2, 2, 2, 4));
101
102     SetLayoutManager(new views::FillLayout());
103     AddChildView(label_);
104     set_background(views::Background::CreateSolidBackground(
105         color_utils::AlphaBlend(SK_ColorBLACK,
106                                 GetNativeTheme()->GetSystemColor(
107                                     ui::NativeTheme::kColorId_WindowBackground),
108                                 0x10)));
109   }
110
111   // Sets the text alignment.
112   void SetAlignment(gfx::HorizontalAlignment alignment) {
113     label_->SetHorizontalAlignment(alignment);
114   }
115
116   // Sets the displayed text.
117   void SetText(const base::string16& text) {
118     label_->SetText(text);
119   }
120
121   // Sets the border thickness for top/bottom.
122   void SetBorderFromPosition(BorderPosition position) {
123     SetBorder(views::Border::CreateSolidSidedBorder(
124         (position == TOP) ? 1 : 0,
125         0,
126         (position == BOTTOM) ? 1 : 0,
127         0,
128         GetNativeTheme()->GetSystemColor(
129             ui::NativeTheme::kColorId_MenuBorderColor)));
130   }
131
132  protected:
133   virtual gfx::Size GetPreferredSize() OVERRIDE {
134     gfx::Size size = views::View::GetPreferredSize();
135     size.SetToMax(gfx::Size(min_width_, 0));
136     return size;
137   }
138
139  private:
140   views::Label* label_;
141   int min_width_;
142
143   DISALLOW_COPY_AND_ASSIGN(InformationTextArea);
144 };
145
146 CandidateWindowView::CandidateWindowView(gfx::NativeView parent)
147     : selected_candidate_index_in_page_(-1),
148       should_show_at_composition_head_(false),
149       should_show_upper_side_(false),
150       was_candidate_window_open_(false) {
151   set_parent_window(parent);
152   set_margins(gfx::Insets());
153
154   // Set the background and the border of the view.
155   ui::NativeTheme* theme = GetNativeTheme();
156   set_background(
157       views::Background::CreateSolidBackground(theme->GetSystemColor(
158           ui::NativeTheme::kColorId_WindowBackground)));
159   SetBorder(views::Border::CreateSolidBorder(
160       1, theme->GetSystemColor(ui::NativeTheme::kColorId_MenuBorderColor)));
161
162   SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0));
163   auxiliary_text_ = new InformationTextArea(gfx::ALIGN_RIGHT, 0);
164   preedit_ = new InformationTextArea(gfx::ALIGN_LEFT, kMinPreeditAreaWidth);
165   candidate_area_ = new views::View;
166   auxiliary_text_->SetVisible(false);
167   preedit_->SetVisible(false);
168   candidate_area_->SetVisible(false);
169   preedit_->SetBorderFromPosition(InformationTextArea::BOTTOM);
170   if (candidate_window_.orientation() == ui::CandidateWindow::VERTICAL) {
171     AddChildView(preedit_);
172     AddChildView(candidate_area_);
173     AddChildView(auxiliary_text_);
174     auxiliary_text_->SetBorderFromPosition(InformationTextArea::TOP);
175     candidate_area_->SetLayoutManager(new views::BoxLayout(
176         views::BoxLayout::kVertical, 0, 0, 0));
177   } else {
178     AddChildView(preedit_);
179     AddChildView(auxiliary_text_);
180     AddChildView(candidate_area_);
181     auxiliary_text_->SetAlignment(gfx::ALIGN_LEFT);
182     auxiliary_text_->SetBorderFromPosition(InformationTextArea::BOTTOM);
183     candidate_area_->SetLayoutManager(new views::BoxLayout(
184         views::BoxLayout::kHorizontal, 0, 0, 0));
185   }
186 }
187
188 CandidateWindowView::~CandidateWindowView() {
189 }
190
191 views::Widget* CandidateWindowView::InitWidget() {
192   views::Widget* widget = BubbleDelegateView::CreateBubble(this);
193
194   views::corewm::SetWindowVisibilityAnimationType(
195       widget->GetNativeView(),
196       views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE);
197
198   GetBubbleFrameView()->SetBubbleBorder(scoped_ptr<views::BubbleBorder>(
199       new CandidateWindowBorder(parent_window())));
200   return widget;
201 }
202
203 void CandidateWindowView::UpdateVisibility() {
204   if (candidate_area_->visible() || auxiliary_text_->visible() ||
205       preedit_->visible()) {
206     SizeToContents();
207   } else {
208     GetWidget()->Close();
209   }
210 }
211
212 void CandidateWindowView::HideLookupTable() {
213   candidate_area_->SetVisible(false);
214   auxiliary_text_->SetVisible(false);
215   UpdateVisibility();
216 }
217
218 void CandidateWindowView::HidePreeditText() {
219   preedit_->SetVisible(false);
220   UpdateVisibility();
221 }
222
223 void CandidateWindowView::ShowPreeditText() {
224   preedit_->SetVisible(true);
225   UpdateVisibility();
226 }
227
228 void CandidateWindowView::UpdatePreeditText(const base::string16& text) {
229   preedit_->SetText(text);
230 }
231
232 void CandidateWindowView::ShowLookupTable() {
233   candidate_area_->SetVisible(true);
234   auxiliary_text_->SetVisible(candidate_window_.is_auxiliary_text_visible());
235   UpdateVisibility();
236 }
237
238 void CandidateWindowView::UpdateCandidates(
239     const ui::CandidateWindow& new_candidate_window) {
240   // Updating the candidate views is expensive. We'll skip this if possible.
241   if (!candidate_window_.IsEqual(new_candidate_window)) {
242     if (candidate_window_.orientation() != new_candidate_window.orientation()) {
243       // If the new layout is vertical, the aux text should appear at the
244       // bottom. If horizontal, it should appear between preedit and candidates.
245       if (new_candidate_window.orientation() == ui::CandidateWindow::VERTICAL) {
246         ReorderChildView(auxiliary_text_, -1);
247         auxiliary_text_->SetAlignment(gfx::ALIGN_RIGHT);
248         auxiliary_text_->SetBorderFromPosition(InformationTextArea::TOP);
249         candidate_area_->SetLayoutManager(new views::BoxLayout(
250             views::BoxLayout::kVertical, 0, 0, 0));
251       } else {
252         ReorderChildView(auxiliary_text_, 1);
253         auxiliary_text_->SetAlignment(gfx::ALIGN_LEFT);
254         auxiliary_text_->SetBorderFromPosition(InformationTextArea::BOTTOM);
255         candidate_area_->SetLayoutManager(new views::BoxLayout(
256             views::BoxLayout::kHorizontal, 0, 0, 0));
257       }
258     }
259
260     // Initialize candidate views if necessary.
261     MaybeInitializeCandidateViews(new_candidate_window);
262
263     should_show_at_composition_head_
264         = new_candidate_window.show_window_at_composition();
265     // Compute the index of the current page.
266     const int current_page_index = ComputePageIndex(new_candidate_window);
267     if (current_page_index < 0)
268       return;
269
270     // Update the candidates in the current page.
271     const size_t start_from =
272         current_page_index * new_candidate_window.page_size();
273
274     int max_shortcut_width = 0;
275     int max_candidate_width = 0;
276     for (size_t i = 0; i < candidate_views_.size(); ++i) {
277       const size_t index_in_page = i;
278       const size_t candidate_index = start_from + index_in_page;
279       CandidateView* candidate_view = candidate_views_[index_in_page];
280       // Set the candidate text.
281       if (candidate_index < new_candidate_window.candidates().size()) {
282         const ui::CandidateWindow::Entry& entry =
283             new_candidate_window.candidates()[candidate_index];
284         candidate_view->SetEntry(entry);
285         candidate_view->SetState(views::Button::STATE_NORMAL);
286         candidate_view->SetInfolistIcon(!entry.description_title.empty());
287       } else {
288         // Disable the empty row.
289         candidate_view->SetEntry(ui::CandidateWindow::Entry());
290         candidate_view->SetState(views::Button::STATE_DISABLED);
291         candidate_view->SetInfolistIcon(false);
292       }
293       if (new_candidate_window.orientation() == ui::CandidateWindow::VERTICAL) {
294         int shortcut_width = 0;
295         int candidate_width = 0;
296         candidate_views_[i]->GetPreferredWidths(
297             &shortcut_width, &candidate_width);
298         max_shortcut_width = std::max(max_shortcut_width, shortcut_width);
299         max_candidate_width = std::max(max_candidate_width, candidate_width);
300       }
301     }
302     if (new_candidate_window.orientation() == ui::CandidateWindow::VERTICAL) {
303       for (size_t i = 0; i < candidate_views_.size(); ++i)
304         candidate_views_[i]->SetWidths(max_shortcut_width, max_candidate_width);
305     }
306
307     CandidateWindowBorder* border = static_cast<CandidateWindowBorder*>(
308         GetBubbleFrameView()->bubble_border());
309     if (new_candidate_window.orientation() == ui::CandidateWindow::VERTICAL)
310       border->set_offset(max_shortcut_width);
311     else
312       border->set_offset(0);
313   }
314   // Update the current candidate window. We'll use candidate_window_ from here.
315   // Note that SelectCandidateAt() uses candidate_window_.
316   candidate_window_.CopyFrom(new_candidate_window);
317
318   // Select the current candidate in the page.
319   if (candidate_window_.is_cursor_visible()) {
320     if (candidate_window_.page_size()) {
321       const int current_candidate_in_page =
322           candidate_window_.cursor_position() % candidate_window_.page_size();
323       SelectCandidateAt(current_candidate_in_page);
324     }
325   } else {
326     // Unselect the currently selected candidate.
327     if (0 <= selected_candidate_index_in_page_ &&
328         static_cast<size_t>(selected_candidate_index_in_page_) <
329         candidate_views_.size()) {
330       candidate_views_[selected_candidate_index_in_page_]->SetState(
331           views::Button::STATE_NORMAL);
332       selected_candidate_index_in_page_ = -1;
333     }
334   }
335
336   // Updates auxiliary text
337   auxiliary_text_->SetVisible(candidate_window_.is_auxiliary_text_visible());
338   auxiliary_text_->SetText(base::UTF8ToUTF16(
339       candidate_window_.auxiliary_text()));
340 }
341
342 void CandidateWindowView::SetCursorBounds(const gfx::Rect& cursor_bounds,
343                                           const gfx::Rect& composition_head) {
344   if (candidate_window_.show_window_at_composition())
345     SetAnchorRect(composition_head);
346   else
347     SetAnchorRect(cursor_bounds);
348 }
349
350 void CandidateWindowView::MaybeInitializeCandidateViews(
351     const ui::CandidateWindow& candidate_window) {
352   const ui::CandidateWindow::Orientation orientation =
353       candidate_window.orientation();
354   const size_t page_size = candidate_window.page_size();
355
356   // Reset all candidate_views_ when orientation changes.
357   if (orientation != candidate_window_.orientation())
358     STLDeleteElements(&candidate_views_);
359
360   while (page_size < candidate_views_.size()) {
361     delete candidate_views_.back();
362     candidate_views_.pop_back();
363   }
364   while (page_size > candidate_views_.size()) {
365     CandidateView* new_candidate = new CandidateView(this, orientation);
366     candidate_area_->AddChildView(new_candidate);
367     candidate_views_.push_back(new_candidate);
368   }
369 }
370
371 void CandidateWindowView::SelectCandidateAt(int index_in_page) {
372   const int current_page_index = ComputePageIndex(candidate_window_);
373   if (current_page_index < 0) {
374     return;
375   }
376
377   const int cursor_absolute_index =
378       candidate_window_.page_size() * current_page_index + index_in_page;
379   // Ignore click on out of range views.
380   if (cursor_absolute_index < 0 ||
381       candidate_window_.candidates().size() <=
382       static_cast<size_t>(cursor_absolute_index)) {
383     return;
384   }
385
386   // Remember the currently selected candidate index in the current page.
387   selected_candidate_index_in_page_ = index_in_page;
388
389   // Select the candidate specified by index_in_page.
390   candidate_views_[index_in_page]->SetState(views::Button::STATE_PRESSED);
391
392   // Update the cursor indexes in the model.
393   candidate_window_.set_cursor_position(cursor_absolute_index);
394 }
395
396 void CandidateWindowView::ButtonPressed(views::Button* sender,
397                                         const ui::Event& event) {
398   for (size_t i = 0; i < candidate_views_.size(); ++i) {
399     if (sender == candidate_views_[i]) {
400       FOR_EACH_OBSERVER(Observer, observers_, OnCandidateCommitted(i));
401       return;
402     }
403   }
404 }
405
406 }  // namespace ime
407 }  // namespace ash