OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 #include "chrome/browser/chromeos/input_method/candidate_window_view.h" | 4 #include "chrome/browser/chromeos/input_method/candidate_window_view.h" |
5 | 5 |
6 #include <string> | 6 #include <string> |
7 | 7 |
8 #include "ash/shell.h" | |
9 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
10 #include "chrome/browser/chromeos/input_method/candidate_view.h" | 9 #include "chrome/browser/chromeos/input_method/candidate_view.h" |
11 #include "chrome/browser/chromeos/input_method/candidate_window_constants.h" | 10 #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" | 11 #include "chromeos/ime/candidate_window.h" |
14 #include "ui/gfx/color_utils.h" | 12 #include "ui/gfx/color_utils.h" |
| 13 #include "ui/gfx/screen.h" |
15 #include "ui/native_theme/native_theme.h" | 14 #include "ui/native_theme/native_theme.h" |
16 #include "ui/views/background.h" | 15 #include "ui/views/background.h" |
17 #include "ui/views/border.h" | 16 #include "ui/views/border.h" |
| 17 #include "ui/views/bubble/bubble_frame_view.h" |
18 #include "ui/views/controls/label.h" | 18 #include "ui/views/controls/label.h" |
19 #include "ui/views/layout/grid_layout.h" | 19 #include "ui/views/corewm/window_animations.h" |
20 #include "ui/views/widget/widget.h" | 20 #include "ui/views/layout/box_layout.h" |
| 21 #include "ui/views/layout/fill_layout.h" |
21 | 22 |
22 namespace chromeos { | 23 namespace chromeos { |
23 namespace input_method { | 24 namespace input_method { |
24 | 25 |
25 namespace { | 26 namespace { |
26 // VerticalCandidateLabel is used for rendering candidate text in | 27 |
27 // the vertical candidate window. | 28 class CandidateWindowBorder : public views::BubbleBorder { |
28 class VerticalCandidateLabel : public views::Label { | |
29 public: | 29 public: |
30 VerticalCandidateLabel() {} | 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; } |
31 | 41 |
32 private: | 42 private: |
33 virtual ~VerticalCandidateLabel() {} | 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())); |
34 | 51 |
35 // Returns the preferred size, but guarantees that the width has at | 52 // It cannot use the normal logic of arrow offset for horizontal offscreen, |
36 // least kMinCandidateLabelWidth pixels. | 53 // because the arrow must be in the content's edge. But CandidateWindow has |
37 virtual gfx::Size GetPreferredSize() OVERRIDE { | 54 // to be visible even when |anchor_rect| is out of the screen. |
38 gfx::Size size = Label::GetPreferredSize(); | 55 gfx::Rect work_area = gfx::Screen::GetNativeScreen()-> |
39 // Hack. +2 is needed to prevent labels from getting elided like | 56 GetDisplayNearestWindow(parent_).work_area(); |
40 // "abc..." in some cases. TODO(satorux): Figure out why it's | 57 if (bounds.right() > work_area.right()) |
41 // necessary. | 58 bounds.set_x(work_area.right() - bounds.width()); |
42 size.set_width(size.width() + 2); | 59 if (bounds.x() < work_area.x()) |
43 if (size.width() < kMinCandidateLabelWidth) { | 60 bounds.set_x(work_area.x()); |
44 size.set_width(kMinCandidateLabelWidth); | 61 |
45 } | 62 return bounds; |
46 if (size.width() > kMaxCandidateLabelWidth) { | |
47 size.set_width(kMaxCandidateLabelWidth); | |
48 } | |
49 return size; | |
50 } | 63 } |
51 | 64 |
52 DISALLOW_COPY_AND_ASSIGN(VerticalCandidateLabel); | 65 virtual gfx::Insets GetInsets() const OVERRIDE { |
53 }; | 66 return gfx::Insets(); |
54 | |
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); | |
62 | |
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); | |
68 | |
69 // Add the view contents. | |
70 layout->AddView(view); // |view| is owned by |wraper|, not |layout|. | |
71 return wrapper; | |
72 } | |
73 | |
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); | |
84 } | |
85 | |
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; | |
94 | |
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)); | |
103 } | |
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)); | |
110 | |
111 return shortcut_label; | |
112 } | |
113 | |
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); | |
130 | |
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()); | |
144 } | 67 } |
145 | 68 |
146 return wrapped_shortcut_label; | 69 gfx::NativeView parent_; |
147 } | 70 int offset_; |
148 | 71 |
149 // Creates the candidate label, and returns it (never returns NULL). | 72 DISALLOW_COPY_AND_ASSIGN(CandidateWindowBorder); |
150 // The label text is not set in this function. | 73 }; |
151 views::Label* CreateCandidateLabel( | |
152 CandidateWindow::Orientation orientation) { | |
153 views::Label* candidate_label = NULL; | |
154 | |
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; | |
161 } | |
162 | |
163 // Change the font size. | |
164 candidate_label->SetFontList( | |
165 candidate_label->font_list().DeriveFontListWithSizeDelta(kFontSizeDelta)); | |
166 candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
167 | |
168 return candidate_label; | |
169 } | |
170 | |
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; | |
177 | |
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); | |
185 | |
186 return annotation_label; | |
187 } | |
188 | |
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)); | |
204 | |
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()); | |
214 } | |
215 | |
216 return gfx::Size(shortcut_column_width, shortcut_column_height); | |
217 } | |
218 | 74 |
219 // Computes the page index. For instance, if the page size is 9, and the | 75 // 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 | 76 // 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. | 77 // page, as the index is zero-origin). Returns -1 on error. |
222 int ComputePageIndex(const CandidateWindow& candidate_window) { | 78 int ComputePageIndex(const CandidateWindow& candidate_window) { |
223 if (candidate_window.page_size() > 0) | 79 if (candidate_window.page_size() > 0) |
224 return candidate_window.cursor_position() / candidate_window.page_size(); | 80 return candidate_window.cursor_position() / candidate_window.page_size(); |
225 return -1; | 81 return -1; |
226 } | 82 } |
227 | 83 |
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())); | |
235 | |
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(); | |
241 | |
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; | |
249 | |
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()); | |
257 } | |
258 | |
259 return gfx::Size(candidate_column_width, candidate_column_height); | |
260 } | |
261 | |
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)); | |
269 | |
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(); | |
275 | |
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; | |
283 | |
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()); | |
291 } | |
292 | |
293 return gfx::Size(annotation_column_width, annotation_column_height); | |
294 } | |
295 | |
296 } // namespace | 84 } // namespace |
297 | 85 |
298 // InformationTextArea is a HidableArea having a single Label in it. | 86 class InformationTextArea : public views::View { |
299 class InformationTextArea : public HidableArea { | |
300 public: | 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 |
301 // Specify the alignment and initialize the control. | 95 // Specify the alignment and initialize the control. |
302 InformationTextArea(gfx::HorizontalAlignment align, int minWidth) | 96 InformationTextArea(gfx::HorizontalAlignment align, int min_width) |
303 : minWidth_(minWidth) { | 97 : min_width_(min_width) { |
304 label_ = new views::Label; | 98 label_ = new views::Label; |
305 label_->SetHorizontalAlignment(align); | 99 label_->SetHorizontalAlignment(align); |
| 100 label_->set_border(views::Border::CreateEmptyBorder(2, 2, 2, 4)); |
306 | 101 |
307 const gfx::Insets kInsets(2, 2, 2, 4); | 102 SetLayoutManager(new views::FillLayout()); |
308 views::View* contents = WrapWithPadding(label_, kInsets); | 103 AddChildView(label_); |
309 SetContents(contents); | 104 set_background(views::Background::CreateSolidBackground( |
310 contents->set_border(views::Border::CreateSolidBorder( | |
311 1, | |
312 GetNativeTheme()->GetSystemColor( | |
313 ui::NativeTheme::kColorId_MenuBorderColor))); | |
314 contents->set_background(views::Background::CreateSolidBackground( | |
315 color_utils::AlphaBlend(SK_ColorBLACK, | 105 color_utils::AlphaBlend(SK_ColorBLACK, |
316 GetNativeTheme()->GetSystemColor( | 106 GetNativeTheme()->GetSystemColor( |
317 ui::NativeTheme::kColorId_WindowBackground), | 107 ui::NativeTheme::kColorId_WindowBackground), |
318 0x10))); | 108 0x10))); |
319 label_->SetBackgroundColor(contents->background()->get_color()); | |
320 } | 109 } |
321 | 110 |
322 // Set the displayed text. | 111 // Sets the text alignment. |
| 112 void SetAlignment(gfx::HorizontalAlignment alignment) { |
| 113 label_->SetHorizontalAlignment(alignment); |
| 114 } |
| 115 |
| 116 // Sets the displayed text. |
323 void SetText(const std::string& utf8_text) { | 117 void SetText(const std::string& utf8_text) { |
324 label_->SetText(base::UTF8ToUTF16(utf8_text)); | 118 label_->SetText(base::UTF8ToUTF16(utf8_text)); |
325 } | 119 } |
326 | 120 |
| 121 // Sets the border thickness for top/bottom. |
| 122 void SetBorder(BorderPosition position) { |
| 123 set_border(views::Border::CreateSolidSidedBorder( |
| 124 (position == TOP) ? 1 : 0, 0, (position == BOTTOM) ? 1 : 0, 0, |
| 125 GetNativeTheme()->GetSystemColor( |
| 126 ui::NativeTheme::kColorId_MenuBorderColor))); |
| 127 } |
| 128 |
327 protected: | 129 protected: |
328 virtual gfx::Size GetPreferredSize() OVERRIDE { | 130 virtual gfx::Size GetPreferredSize() OVERRIDE { |
329 gfx::Size size = HidableArea::GetPreferredSize(); | 131 gfx::Size size = views::View::GetPreferredSize(); |
330 // Hack. +2 is needed as the same reason as in VerticalCandidateLabel | 132 size.SetToMax(gfx::Size(min_width_, 0)); |
331 size.set_width(size.width() + 2); | |
332 if (size.width() < minWidth_) { | |
333 size.set_width(minWidth_); | |
334 } | |
335 return size; | 133 return size; |
336 } | 134 } |
337 | 135 |
338 private: | 136 private: |
339 views::Label* label_; | 137 views::Label* label_; |
340 int minWidth_; | 138 int min_width_; |
341 | 139 |
342 DISALLOW_COPY_AND_ASSIGN(InformationTextArea); | 140 DISALLOW_COPY_AND_ASSIGN(InformationTextArea); |
343 }; | 141 }; |
344 | 142 |
345 CandidateView::CandidateView( | 143 CandidateWindowView::CandidateWindowView(gfx::NativeView parent) |
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) { | |
357 } | |
358 | |
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|. | |
365 | |
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); | |
373 | |
374 // Initialize the column set with three columns. | |
375 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
376 | |
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; | |
382 | |
383 const int padding_column_width = | |
384 orientation_ == CandidateWindow::VERTICAL ? 4 : 6; | |
385 | |
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); | |
390 | |
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); | |
397 | |
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); | |
401 | |
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, | |
406 0); | |
407 column_set->AddPaddingColumn(0, 2); | |
408 } else { | |
409 column_set->AddPaddingColumn(0, padding_column_width); | |
410 } | |
411 | |
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); | |
454 } | |
455 UpdateLabelBackgroundColors(); | |
456 } | |
457 | |
458 void CandidateView::SetCandidateText(const base::string16& text) { | |
459 candidate_label_->SetText(text); | |
460 } | |
461 | |
462 void CandidateView::SetShortcutText(const base::string16& text) { | |
463 shortcut_label_->SetText(text); | |
464 } | |
465 | |
466 void CandidateView::SetAnnotationText(const base::string16& text) { | |
467 annotation_label_->SetText(text); | |
468 } | |
469 | |
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(); | |
481 } | |
482 | |
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(); | |
493 } | |
494 | |
495 void CandidateView::Unselect() { | |
496 set_background(NULL); | |
497 set_border(NULL); | |
498 UpdateLabelBackgroundColors(); | |
499 SchedulePaint(); // See comments at Select(). | |
500 } | |
501 | |
502 void CandidateView::SetRowEnabled(bool enabled) { | |
503 shortcut_label_->SetEnabled(enabled); | |
504 } | |
505 | |
506 gfx::Point CandidateView::GetCandidateLabelPosition() const { | |
507 return candidate_label_->GetMirroredPosition(); | |
508 } | |
509 | |
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; | |
519 } | |
520 | |
521 void CandidateView::OnGestureEvent(ui::GestureEvent* event) { | |
522 if (event->type() == ui::ET_GESTURE_TAP) { | |
523 SelectCandidateAt(event->location()); | |
524 event->SetHandled(); | |
525 return; | |
526 } | |
527 View::OnGestureEvent(event); | |
528 } | |
529 | |
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(); | |
536 } | |
537 | |
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); | |
547 } | |
548 | |
549 CandidateWindowView::CandidateWindowView(views::Widget* parent_frame) | |
550 : selected_candidate_index_in_page_(-1), | 144 : 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), | 145 should_show_at_composition_head_(false), |
560 should_show_upper_side_(false), | 146 should_show_upper_side_(false), |
561 was_candidate_window_open_(false) { | 147 was_candidate_window_open_(false) { |
| 148 set_parent_window(parent); |
| 149 set_margins(gfx::Insets()); |
| 150 |
| 151 // Set the background and the border of the view. |
| 152 ui::NativeTheme* theme = GetNativeTheme(); |
| 153 set_background( |
| 154 views::Background::CreateSolidBackground(theme->GetSystemColor( |
| 155 ui::NativeTheme::kColorId_WindowBackground))); |
| 156 set_border(views::Border::CreateSolidBorder( |
| 157 1, theme->GetSystemColor(ui::NativeTheme::kColorId_MenuBorderColor))); |
| 158 |
| 159 SetLayoutManager(new views::BoxLayout(views::BoxLayout::kVertical, 0, 0, 0)); |
| 160 auxiliary_text_ = new InformationTextArea(gfx::ALIGN_RIGHT, 0); |
| 161 preedit_ = new InformationTextArea(gfx::ALIGN_LEFT, kMinPreeditAreaWidth); |
| 162 candidate_area_ = new views::View; |
| 163 auxiliary_text_->SetVisible(false); |
| 164 preedit_->SetVisible(false); |
| 165 candidate_area_->SetVisible(false); |
| 166 preedit_->SetBorder(InformationTextArea::BOTTOM); |
| 167 if (candidate_window_.orientation() == CandidateWindow::VERTICAL) { |
| 168 AddChildView(preedit_); |
| 169 AddChildView(candidate_area_); |
| 170 AddChildView(auxiliary_text_); |
| 171 auxiliary_text_->SetBorder(InformationTextArea::TOP); |
| 172 candidate_area_->SetLayoutManager(new views::BoxLayout( |
| 173 views::BoxLayout::kVertical, 0, 0, 0)); |
| 174 } else { |
| 175 AddChildView(preedit_); |
| 176 AddChildView(auxiliary_text_); |
| 177 AddChildView(candidate_area_); |
| 178 auxiliary_text_->SetAlignment(gfx::ALIGN_LEFT); |
| 179 auxiliary_text_->SetBorder(InformationTextArea::BOTTOM); |
| 180 candidate_area_->SetLayoutManager(new views::BoxLayout( |
| 181 views::BoxLayout::kHorizontal, 0, 0, 0)); |
| 182 } |
562 } | 183 } |
563 | 184 |
564 CandidateWindowView::~CandidateWindowView() { | 185 CandidateWindowView::~CandidateWindowView() { |
565 } | 186 } |
566 | 187 |
567 void CandidateWindowView::Init() { | 188 views::Widget* CandidateWindowView::InitWidget() { |
568 // Set the background and the border of the view. | 189 views::Widget* widget = BubbleDelegateView::CreateBubble(this); |
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))); | |
575 | 190 |
576 // Create areas. | 191 views::corewm::SetWindowVisibilityAnimationType( |
577 preedit_area_ = new InformationTextArea(gfx::ALIGN_LEFT, | 192 widget->GetNativeView(), |
578 kMinPreeditAreaWidth); | 193 views::corewm::WINDOW_VISIBILITY_ANIMATION_TYPE_FADE); |
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); | |
583 | 194 |
584 // Set the window layout of the view | 195 GetBubbleFrameView()->SetBubbleBorder( |
585 views::GridLayout* layout = new views::GridLayout(this); | 196 new CandidateWindowBorder(parent_window())); |
586 SetLayoutManager(layout); // |this| owns |layout|. | 197 return widget; |
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); | |
590 | |
591 // Add the preedit area | |
592 layout->StartRow(0, 0); | |
593 layout->AddView(preedit_area_); // |preedit_area_| is owned by |this|. | |
594 | |
595 // Add the header area. | |
596 layout->StartRow(0, 0); | |
597 layout->AddView(header_area_); // |header_area_| is owned by |this|. | |
598 | |
599 // Add the candidate area. | |
600 layout->StartRow(0, 0); | |
601 layout->AddView(candidate_area_); // |candidate_area_| is owned by |this|. | |
602 | |
603 // Add the footer area. | |
604 layout->StartRow(0, 0); | |
605 layout->AddView(footer_area_); // |footer_area_| is owned by |this|. | |
606 } | 198 } |
607 | 199 |
608 void CandidateWindowView::HideAll() { | 200 void CandidateWindowView::UpdateVisibility() { |
609 parent_frame_->Hide(); | 201 if (candidate_area_->visible() || auxiliary_text_->visible() || |
610 NotifyIfCandidateWindowOpenedOrClosed(); | 202 preedit_->visible()) { |
611 } | 203 SizeToContents(); |
612 | |
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 { | 204 } else { |
621 parent_frame_->Hide(); | 205 GetWidget()->Close(); |
622 } | 206 } |
623 NotifyIfCandidateWindowOpenedOrClosed(); | |
624 } | 207 } |
625 | 208 |
626 void CandidateWindowView::HideLookupTable() { | 209 void CandidateWindowView::HideLookupTable() { |
627 candidate_area_->Hide(); | 210 candidate_area_->SetVisible(false); |
628 header_area_->Hide(); | 211 UpdateVisibility(); |
629 footer_area_->Hide(); | |
630 UpdateParentArea(); | |
631 } | 212 } |
632 | 213 |
633 void CandidateWindowView::HidePreeditText() { | 214 void CandidateWindowView::HidePreeditText() { |
634 preedit_area_->Hide(); | 215 preedit_->SetVisible(false); |
635 UpdateParentArea(); | 216 UpdateVisibility(); |
636 } | 217 } |
637 | 218 |
638 void CandidateWindowView::ShowPreeditText() { | 219 void CandidateWindowView::ShowPreeditText() { |
639 preedit_area_->Show(); | 220 preedit_->SetVisible(true); |
640 UpdateParentArea(); | 221 UpdateVisibility(); |
641 } | 222 } |
642 | 223 |
643 void CandidateWindowView::UpdatePreeditText(const std::string& utf8_text) { | 224 void CandidateWindowView::UpdatePreeditText(const std::string& utf8_text) { |
644 preedit_area_->SetText(utf8_text); | 225 preedit_->SetText(utf8_text); |
645 } | 226 } |
646 | 227 |
647 void CandidateWindowView::ShowLookupTable() { | 228 void CandidateWindowView::ShowLookupTable() { |
648 if (!candidate_area_->IsShown()) | 229 candidate_area_->SetVisible(true); |
649 should_show_upper_side_ = false; | 230 auxiliary_text_->SetVisible(candidate_window_.is_auxiliary_text_visible()); |
650 candidate_area_->Show(); | 231 UpdateVisibility(); |
651 | |
652 // Show auxiliary text. | |
653 if (!candidate_window_.is_auxiliary_text_visible()) { | |
654 header_area_->Hide(); | |
655 footer_area_->Hide(); | |
656 } else if (candidate_window_.orientation() == CandidateWindow::HORIZONTAL) { | |
657 header_area_->Show(); | |
658 footer_area_->Hide(); | |
659 } else { | |
660 header_area_->Hide(); | |
661 footer_area_->Show(); | |
662 } | |
663 | |
664 UpdateParentArea(); | |
665 } | |
666 | |
667 void CandidateWindowView::NotifyIfCandidateWindowOpenedOrClosed() { | |
668 bool is_open = IsCandidateWindowOpen(); | |
669 if (!was_candidate_window_open_ && is_open) { | |
670 FOR_EACH_OBSERVER(Observer, observers_, OnCandidateWindowOpened()); | |
671 } else if (was_candidate_window_open_ && !is_open) { | |
672 FOR_EACH_OBSERVER(Observer, observers_, OnCandidateWindowClosed()); | |
673 } | |
674 was_candidate_window_open_ = is_open; | |
675 } | |
676 | |
677 bool CandidateWindowView::ShouldUpdateCandidateViews( | |
678 const CandidateWindow& old_candidate_window, | |
679 const CandidateWindow& new_candidate_window) { | |
680 return !old_candidate_window.IsEqual(new_candidate_window); | |
681 } | 232 } |
682 | 233 |
683 void CandidateWindowView::UpdateCandidates( | 234 void CandidateWindowView::UpdateCandidates( |
684 const CandidateWindow& new_candidate_window) { | 235 const CandidateWindow& new_candidate_window) { |
685 const bool should_update = ShouldUpdateCandidateViews(candidate_window_, | |
686 new_candidate_window); | |
687 // Updating the candidate views is expensive. We'll skip this if possible. | 236 // Updating the candidate views is expensive. We'll skip this if possible. |
688 if (should_update) { | 237 if (!candidate_window_.IsEqual(new_candidate_window)) { |
| 238 if (candidate_window_.orientation() != new_candidate_window.orientation()) { |
| 239 // If the new layout is vertical, the aux text should appear at the |
| 240 // bottom. If horizontal, it should appear between preedit and candidates. |
| 241 if (new_candidate_window.orientation() == CandidateWindow::VERTICAL) { |
| 242 ReorderChildView(auxiliary_text_, -1); |
| 243 auxiliary_text_->SetAlignment(gfx::ALIGN_RIGHT); |
| 244 auxiliary_text_->SetBorder(InformationTextArea::TOP); |
| 245 candidate_area_->SetLayoutManager(new views::BoxLayout( |
| 246 views::BoxLayout::kVertical, 0, 0, 0)); |
| 247 } else { |
| 248 ReorderChildView(auxiliary_text_, 1); |
| 249 auxiliary_text_->SetAlignment(gfx::ALIGN_LEFT); |
| 250 auxiliary_text_->SetBorder(InformationTextArea::BOTTOM); |
| 251 candidate_area_->SetLayoutManager(new views::BoxLayout( |
| 252 views::BoxLayout::kHorizontal, 0, 0, 0)); |
| 253 } |
| 254 } |
| 255 |
689 // Initialize candidate views if necessary. | 256 // Initialize candidate views if necessary. |
690 MaybeInitializeCandidateViews(new_candidate_window); | 257 MaybeInitializeCandidateViews(new_candidate_window); |
691 | 258 |
692 should_show_at_composition_head_ | 259 should_show_at_composition_head_ |
693 = new_candidate_window.show_window_at_composition(); | 260 = new_candidate_window.show_window_at_composition(); |
694 // Compute the index of the current page. | 261 // Compute the index of the current page. |
695 const int current_page_index = ComputePageIndex(new_candidate_window); | 262 const int current_page_index = ComputePageIndex(new_candidate_window); |
696 if (current_page_index < 0) { | 263 if (current_page_index < 0) |
697 return; | 264 return; |
698 } | |
699 | 265 |
700 // Update the candidates in the current page. | 266 // Update the candidates in the current page. |
701 const size_t start_from = | 267 const size_t start_from = |
702 current_page_index * new_candidate_window.page_size(); | 268 current_page_index * new_candidate_window.page_size(); |
703 | 269 |
704 // In some cases, engines send empty shortcut labels. For instance, | 270 int max_shortcut_width = 0; |
705 // ibus-mozc sends empty labels when they show suggestions. In this | 271 int max_candidate_width = 0; |
706 // case, we should not show shortcut labels. | |
707 bool no_shortcut_mode = true; | |
708 for (size_t i = 0; i < new_candidate_window.candidates().size(); ++i) { | |
709 if (!new_candidate_window.candidates()[i].label.empty()) { | |
710 no_shortcut_mode = false; | |
711 break; | |
712 } | |
713 } | |
714 | |
715 for (size_t i = 0; i < candidate_views_.size(); ++i) { | 272 for (size_t i = 0; i < candidate_views_.size(); ++i) { |
716 const size_t index_in_page = i; | 273 const size_t index_in_page = i; |
717 const size_t candidate_index = start_from + index_in_page; | 274 const size_t candidate_index = start_from + index_in_page; |
718 CandidateView* candidate_view = candidate_views_[index_in_page]; | 275 CandidateView* candidate_view = candidate_views_[index_in_page]; |
719 // Set the shortcut text. | |
720 if (no_shortcut_mode) { | |
721 candidate_view->SetShortcutText(base::string16()); | |
722 } else { | |
723 // At this moment, we don't use labels sent from engines for UX | |
724 // reasons. First, we want to show shortcut labels in empty rows | |
725 // (ex. show 6, 7, 8, ... in empty rows when the number of | |
726 // candidates is 5). Second, we want to add a period after each | |
727 // shortcut label when the candidate window is horizontal. | |
728 candidate_view->SetShortcutText( | |
729 CreateShortcutText(i, new_candidate_window)); | |
730 } | |
731 // Set the candidate text. | 276 // Set the candidate text. |
732 if (candidate_index < new_candidate_window.candidates().size()) { | 277 if (candidate_index < new_candidate_window.candidates().size()) { |
733 const CandidateWindow::Entry& entry = | 278 const CandidateWindow::Entry& entry = |
734 new_candidate_window.candidates()[candidate_index]; | 279 new_candidate_window.candidates()[candidate_index]; |
735 candidate_view->SetCandidateText(base::UTF8ToUTF16(entry.value)); | 280 candidate_view->SetEntry(entry); |
736 candidate_view->SetAnnotationText(base::UTF8ToUTF16(entry.annotation)); | 281 candidate_view->SetState(views::Button::STATE_NORMAL); |
737 candidate_view->SetRowEnabled(true); | 282 candidate_view->SetInfolistIcon(!entry.description_title.empty()); |
738 candidate_view->SetInfolistIcon(!entry.description_title.empty()); | |
739 } else { | 283 } else { |
740 // Disable the empty row. | 284 // Disable the empty row. |
741 candidate_view->SetCandidateText(base::string16()); | 285 candidate_view->SetEntry(CandidateWindow::Entry()); |
742 candidate_view->SetAnnotationText(base::string16()); | 286 candidate_view->SetState(views::Button::STATE_DISABLED); |
743 candidate_view->SetRowEnabled(false); | |
744 candidate_view->SetInfolistIcon(false); | 287 candidate_view->SetInfolistIcon(false); |
745 } | 288 } |
| 289 if (new_candidate_window.orientation() == CandidateWindow::VERTICAL) { |
| 290 int shortcut_width = 0; |
| 291 int candidate_width = 0; |
| 292 candidate_views_[i]->GetPreferredWidths( |
| 293 &shortcut_width, &candidate_width); |
| 294 max_shortcut_width = std::max(max_shortcut_width, shortcut_width); |
| 295 max_candidate_width = std::max(max_candidate_width, candidate_width); |
| 296 } |
746 } | 297 } |
| 298 if (new_candidate_window.orientation() == CandidateWindow::VERTICAL) { |
| 299 for (size_t i = 0; i < candidate_views_.size(); ++i) |
| 300 candidate_views_[i]->SetWidths(max_shortcut_width, max_candidate_width); |
| 301 } |
| 302 |
| 303 CandidateWindowBorder* border = static_cast<CandidateWindowBorder*>( |
| 304 GetBubbleFrameView()->bubble_border()); |
| 305 if (new_candidate_window.orientation() == CandidateWindow::VERTICAL) |
| 306 border->set_offset(max_shortcut_width); |
| 307 else |
| 308 border->set_offset(0); |
747 } | 309 } |
748 // Update the current candidate window. We'll use candidate_window_ from here. | 310 // Update the current candidate window. We'll use candidate_window_ from here. |
749 // Note that SelectCandidateAt() uses candidate_window_. | 311 // Note that SelectCandidateAt() uses candidate_window_. |
750 candidate_window_.CopyFrom(new_candidate_window); | 312 candidate_window_.CopyFrom(new_candidate_window); |
751 | 313 |
752 // Select the current candidate in the page. | 314 // Select the current candidate in the page. |
753 if (candidate_window_.is_cursor_visible()) { | 315 if (candidate_window_.is_cursor_visible()) { |
754 if (candidate_window_.page_size()) { | 316 if (candidate_window_.page_size()) { |
755 const int current_candidate_in_page = | 317 const int current_candidate_in_page = |
756 candidate_window_.cursor_position() % candidate_window_.page_size(); | 318 candidate_window_.cursor_position() % candidate_window_.page_size(); |
757 SelectCandidateAt(current_candidate_in_page); | 319 SelectCandidateAt(current_candidate_in_page); |
758 } | 320 } |
759 } else { | 321 } else { |
760 // Unselect the currently selected candidate. | 322 // Unselect the currently selected candidate. |
761 if (0 <= selected_candidate_index_in_page_ && | 323 if (0 <= selected_candidate_index_in_page_ && |
762 static_cast<size_t>(selected_candidate_index_in_page_) < | 324 static_cast<size_t>(selected_candidate_index_in_page_) < |
763 candidate_views_.size()) { | 325 candidate_views_.size()) { |
764 candidate_views_[selected_candidate_index_in_page_]->Unselect(); | 326 candidate_views_[selected_candidate_index_in_page_]->SetState( |
| 327 views::Button::STATE_NORMAL); |
765 selected_candidate_index_in_page_ = -1; | 328 selected_candidate_index_in_page_ = -1; |
766 } | 329 } |
767 } | 330 } |
768 | 331 |
769 // Updates auxiliary text | 332 // Updates auxiliary text |
770 if (candidate_window_.is_auxiliary_text_visible()) { | 333 auxiliary_text_->SetVisible(candidate_window_.is_auxiliary_text_visible()); |
771 header_area_->SetText(candidate_window_.auxiliary_text()); | 334 auxiliary_text_->SetText(candidate_window_.auxiliary_text()); |
772 footer_area_->SetText(candidate_window_.auxiliary_text()); | 335 } |
773 } | 336 |
| 337 void CandidateWindowView::SetCursorBounds(const gfx::Rect& cursor_bounds, |
| 338 const gfx::Rect& composition_head) { |
| 339 if (candidate_window_.show_window_at_composition()) |
| 340 SetAnchorRect(composition_head); |
| 341 else |
| 342 SetAnchorRect(cursor_bounds); |
774 } | 343 } |
775 | 344 |
776 void CandidateWindowView::MaybeInitializeCandidateViews( | 345 void CandidateWindowView::MaybeInitializeCandidateViews( |
777 const CandidateWindow& candidate_window) { | 346 const CandidateWindow& candidate_window) { |
778 const CandidateWindow::Orientation orientation = | 347 const CandidateWindow::Orientation orientation = |
779 candidate_window.orientation(); | 348 candidate_window.orientation(); |
780 const int page_size = candidate_window.page_size(); | 349 const size_t page_size = candidate_window.page_size(); |
781 views::View* candidate_area_contents = candidate_area_->contents(); | |
782 | 350 |
783 // Current column width. | 351 // Reset all candidate_views_ when orientation changes. |
784 gfx::Size shortcut_column_size(0, 0); | 352 if (orientation != candidate_window_.orientation()) |
785 gfx::Size candidate_column_size(0,0); | 353 STLDeleteElements(&candidate_views_); |
786 gfx::Size annotation_column_size(0, 0); | |
787 | 354 |
788 // If orientation is horizontal, don't need to compute width, | 355 while (page_size < candidate_views_.size()) { |
789 // because each label is left aligned. | 356 delete candidate_views_.back(); |
790 if (orientation == CandidateWindow::VERTICAL) { | 357 candidate_views_.pop_back(); |
791 const ui::NativeTheme& theme = *GetNativeTheme(); | |
792 shortcut_column_size = ComputeShortcutColumnSize(candidate_window, theme); | |
793 candidate_column_size = ComputeCandidateColumnSize(candidate_window); | |
794 annotation_column_size = ComputeAnnotationColumnSize(candidate_window, | |
795 theme); | |
796 } | 358 } |
797 | 359 while (page_size > candidate_views_.size()) { |
798 // If the requested number of views matches the number of current views, and | 360 CandidateView* new_candidate = new CandidateView(this, orientation); |
799 // previous and current column width are same, just reuse these. | 361 candidate_area_->AddChildView(new_candidate); |
800 // | 362 candidate_views_.push_back(new_candidate); |
801 // Note that the early exit logic is not only useful for improving | |
802 // performance, but also necessary for the horizontal candidate window | |
803 // to be redrawn properly. If we get rid of the logic, the horizontal | |
804 // candidate window won't get redrawn properly for some reason when | |
805 // there is no size change. You can test this by removing "return" here | |
806 // and type "ni" with Pinyin input method. | |
807 if (static_cast<int>(candidate_views_.size()) == page_size && | |
808 candidate_window_.orientation() == orientation && | |
809 previous_shortcut_column_size_ == shortcut_column_size && | |
810 previous_candidate_column_size_ == candidate_column_size && | |
811 previous_annotation_column_size_ == annotation_column_size) { | |
812 return; | |
813 } | 363 } |
814 | |
815 // Update the previous column widths. | |
816 previous_shortcut_column_size_ = shortcut_column_size; | |
817 previous_candidate_column_size_ = candidate_column_size; | |
818 previous_annotation_column_size_ = annotation_column_size; | |
819 | |
820 // Clear the existing candidate_views if any. | |
821 for (size_t i = 0; i < candidate_views_.size(); ++i) { | |
822 candidate_area_contents->RemoveChildView(candidate_views_[i]); | |
823 // Delete the view after getting out the current message loop iteration. | |
824 base::MessageLoop::current()->DeleteSoon(FROM_HERE, candidate_views_[i]); | |
825 } | |
826 candidate_views_.clear(); | |
827 selected_candidate_index_in_page_ = -1; // Invalidates the index. | |
828 | |
829 views::GridLayout* layout = new views::GridLayout(candidate_area_contents); | |
830 // |candidate_area_contents| owns |layout|. | |
831 candidate_area_contents->SetLayoutManager(layout); | |
832 // Initialize the column set. | |
833 views::ColumnSet* column_set = layout->AddColumnSet(0); | |
834 if (orientation == CandidateWindow::VERTICAL) { | |
835 column_set->AddColumn(views::GridLayout::FILL, | |
836 views::GridLayout::FILL, | |
837 1, views::GridLayout::USE_PREF, 0, 0); | |
838 } else { | |
839 for (int i = 0; i < page_size; ++i) { | |
840 column_set->AddColumn(views::GridLayout::FILL, | |
841 views::GridLayout::FILL, | |
842 0, views::GridLayout::USE_PREF, 0, 0); | |
843 } | |
844 } | |
845 | |
846 // Set insets so the border of the selected candidate is drawn inside of | |
847 // the border of the main candidate window, but we don't have the inset | |
848 // at the top and the bottom as we have the borders of the header and | |
849 // footer areas. | |
850 const gfx::Insets kCandidateAreaInsets(0, 1, 0, 1); | |
851 layout->SetInsets(kCandidateAreaInsets.top(), | |
852 kCandidateAreaInsets.left(), | |
853 kCandidateAreaInsets.bottom(), | |
854 kCandidateAreaInsets.right()); | |
855 | |
856 // Use maximum height for all rows in candidate area. | |
857 const int kColumnHeight = std::max(shortcut_column_size.height(), | |
858 std::max(candidate_column_size.height(), | |
859 annotation_column_size.height())); | |
860 | |
861 // Add views to the candidate area. | |
862 if (orientation == CandidateWindow::HORIZONTAL) { | |
863 layout->StartRow(0, 0); | |
864 } | |
865 | |
866 for (int i = 0; i < page_size; ++i) { | |
867 CandidateView* candidate_row = new CandidateView(this, i, orientation); | |
868 candidate_row->Init(shortcut_column_size.width(), | |
869 candidate_column_size.width(), | |
870 annotation_column_size.width(), | |
871 kColumnHeight); | |
872 candidate_views_.push_back(candidate_row); | |
873 if (orientation == CandidateWindow::VERTICAL) { | |
874 layout->StartRow(0, 0); | |
875 } | |
876 // |candidate_row| will be owned by |candidate_area_contents|. | |
877 layout->AddView(candidate_row, | |
878 1, // Column span. | |
879 1, // Row span. | |
880 // Horizontal alignment. | |
881 orientation == CandidateWindow::VERTICAL ? | |
882 views::GridLayout::FILL : views::GridLayout::CENTER, | |
883 views::GridLayout::CENTER, // Vertical alignment. | |
884 -1, // Preferred width, not specified. | |
885 kColumnHeight); // Preferred height. | |
886 } | |
887 | |
888 // Compute views size in |layout|. | |
889 // If we don't call this function, GetHorizontalOffset() often | |
890 // returns invalid value (returns 0), then candidate window | |
891 // moves right from the correct position in ResizeAndMoveParentFrame(). | |
892 // TODO(nhiroki): Figure out why it returns invalid value. | |
893 // It seems that the x-position of the candidate labels is not set. | |
894 layout->Layout(candidate_area_contents); | |
895 } | |
896 | |
897 bool CandidateWindowView::IsCandidateWindowOpen() const { | |
898 return !should_show_at_composition_head_ && | |
899 candidate_area_->visible() && candidate_area_->IsShown(); | |
900 } | 364 } |
901 | 365 |
902 void CandidateWindowView::SelectCandidateAt(int index_in_page) { | 366 void CandidateWindowView::SelectCandidateAt(int index_in_page) { |
903 const int current_page_index = ComputePageIndex(candidate_window_); | 367 const int current_page_index = ComputePageIndex(candidate_window_); |
904 if (current_page_index < 0) { | 368 if (current_page_index < 0) { |
905 return; | 369 return; |
906 } | 370 } |
907 | 371 |
908 const int cursor_absolute_index = | 372 const int cursor_absolute_index = |
909 candidate_window_.page_size() * current_page_index + index_in_page; | 373 candidate_window_.page_size() * current_page_index + index_in_page; |
910 // Ignore click on out of range views. | 374 // Ignore click on out of range views. |
911 if (cursor_absolute_index < 0 || | 375 if (cursor_absolute_index < 0 || |
912 candidate_window_.candidates().size() <= | 376 candidate_window_.candidates().size() <= |
913 static_cast<size_t>(cursor_absolute_index)) { | 377 static_cast<size_t>(cursor_absolute_index)) { |
914 return; | 378 return; |
915 } | 379 } |
916 | 380 |
917 // Unselect the currently selected candidate. | |
918 if (0 <= selected_candidate_index_in_page_ && | |
919 static_cast<size_t>(selected_candidate_index_in_page_) < | |
920 candidate_views_.size()) { | |
921 candidate_views_[selected_candidate_index_in_page_]->Unselect(); | |
922 } | |
923 // Remember the currently selected candidate index in the current page. | 381 // Remember the currently selected candidate index in the current page. |
924 selected_candidate_index_in_page_ = index_in_page; | 382 selected_candidate_index_in_page_ = index_in_page; |
925 | 383 |
926 // Select the candidate specified by index_in_page. | 384 // Select the candidate specified by index_in_page. |
927 candidate_views_[index_in_page]->Select(); | 385 candidate_views_[index_in_page]->SetState(views::Button::STATE_PRESSED); |
928 | 386 |
929 // Update the cursor indexes in the model. | 387 // Update the cursor indexes in the model. |
930 candidate_window_.set_cursor_position(cursor_absolute_index); | 388 candidate_window_.set_cursor_position(cursor_absolute_index); |
931 } | 389 } |
932 | 390 |
933 void CandidateWindowView::OnCandidatePressed( | 391 void CandidateWindowView::ButtonPressed(views::Button* sender, |
934 const gfx::Point& location) { | 392 const ui::Event& event) { |
935 for (size_t i = 0; i < candidate_views_.size(); ++i) { | 393 for (size_t i = 0; i < candidate_views_.size(); ++i) { |
936 gfx::Point converted_location = location; | 394 if (sender == candidate_views_[i]) { |
937 views::View::ConvertPointToTarget(this, candidate_views_[i], | 395 FOR_EACH_OBSERVER(Observer, observers_, OnCandidateCommitted(i)); |
938 &converted_location); | 396 return; |
939 if (candidate_views_[i]->HitTestPoint(converted_location)) { | |
940 SelectCandidateAt(i); | |
941 break; | |
942 } | 397 } |
943 } | 398 } |
944 } | 399 } |
945 | 400 |
946 void CandidateWindowView::CommitCandidate() { | |
947 if (!(0 <= selected_candidate_index_in_page_ && | |
948 static_cast<size_t>(selected_candidate_index_in_page_) < | |
949 candidate_views_.size())) { | |
950 return; // Out of range, do nothing. | |
951 } | |
952 | |
953 FOR_EACH_OBSERVER(Observer, observers_, | |
954 OnCandidateCommitted(selected_candidate_index_in_page_)); | |
955 } | |
956 | |
957 void CandidateWindowView::ResizeAndMoveParentFrame() { | |
958 // If rendering operation comes from mozc-engine, uses mozc specific bounds, | |
959 // otherwise candidate window is shown under the cursor. | |
960 const int x = should_show_at_composition_head_? | |
961 composition_head_bounds_.x() : cursor_bounds_.x(); | |
962 // To avoid candidate-window overlapping, uses maximum y-position of mozc | |
963 // specific bounds and cursor bounds, because mozc-engine does not | |
964 // consider about multi-line composition. | |
965 const int y = should_show_at_composition_head_? | |
966 std::max(composition_head_bounds_.y(), cursor_bounds_.y()) : | |
967 cursor_bounds_.y(); | |
968 const int height = cursor_bounds_.height(); | |
969 const int horizontal_offset = GetHorizontalOffset(); | |
970 | |
971 gfx::Rect old_bounds = parent_frame_->GetClientAreaBoundsInScreen(); | |
972 gfx::Rect screen_bounds = ash::Shell::GetScreen()->GetDisplayMatching( | |
973 cursor_bounds_).work_area(); | |
974 // The size. | |
975 gfx::Rect frame_bounds = old_bounds; | |
976 frame_bounds.set_size(GetPreferredSize()); | |
977 | |
978 // The default position. | |
979 frame_bounds.set_x(x + horizontal_offset); | |
980 frame_bounds.set_y(y + height); | |
981 | |
982 // Handle overflow at the left and the top. | |
983 frame_bounds.set_x(std::max(frame_bounds.x(), screen_bounds.x())); | |
984 frame_bounds.set_y(std::max(frame_bounds.y(), screen_bounds.y())); | |
985 | |
986 // Handle overflow at the right. | |
987 const int right_overflow = frame_bounds.right() - screen_bounds.right(); | |
988 if (right_overflow > 0) { | |
989 frame_bounds.set_x(frame_bounds.x() - right_overflow); | |
990 } | |
991 | |
992 // Handle overflow at the bottom. | |
993 const int bottom_overflow = frame_bounds.bottom() - screen_bounds.bottom(); | |
994 | |
995 // To avoid flickering window position, the candidate window should be shown | |
996 // on upper side of composition string if it was shown there. | |
997 if (should_show_upper_side_ || bottom_overflow > 0) { | |
998 frame_bounds.set_y(frame_bounds.y() - height - frame_bounds.height()); | |
999 should_show_upper_side_ = true; | |
1000 } | |
1001 | |
1002 // TODO(nona): check top_overflow here. | |
1003 | |
1004 // Move the window per the cursor bounds. | |
1005 // SetBounds() is not cheap. Only call this when it is really changed. | |
1006 if (frame_bounds != old_bounds) | |
1007 parent_frame_->SetBounds(frame_bounds); | |
1008 } | |
1009 | |
1010 int CandidateWindowView::GetHorizontalOffset() { | |
1011 // Compute the horizontal offset if the candidate window is vertical. | |
1012 if (!candidate_views_.empty() && | |
1013 candidate_window_.orientation() == CandidateWindow::VERTICAL) { | |
1014 return - candidate_views_[0]->GetCandidateLabelPosition().x(); | |
1015 } | |
1016 return 0; | |
1017 } | |
1018 | |
1019 void CandidateWindowView::VisibilityChanged(View* starting_from, | |
1020 bool is_visible) { | |
1021 if (is_visible) { | |
1022 // If the visibility of candidate window is changed, | |
1023 // we should move the frame to the right position. | |
1024 ResizeAndMoveParentFrame(); | |
1025 } | |
1026 } | |
1027 | |
1028 void CandidateWindowView::OnBoundsChanged(const gfx::Rect& previous_bounds) { | |
1029 // If the bounds(size) of candidate window is changed, | |
1030 // we should move the frame to the right position. | |
1031 View::OnBoundsChanged(previous_bounds); | |
1032 ResizeAndMoveParentFrame(); | |
1033 } | |
1034 | |
1035 } // namespace input_method | 401 } // namespace input_method |
1036 } // namespace chromeos | 402 } // namespace chromeos |
OLD | NEW |