Added documentation to web_view.js/web_view_experimental.js regarding the webview...
[chromium-blink-merge.git] / ash / ime / candidate_view.cc
blob652998a8403e4fb548e2f762a4d7f8514129dc28
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.
5 #include "ash/ime/candidate_view.h"
7 #include "ash/ime/candidate_window_constants.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "ui/base/ime/candidate_window.h"
10 #include "ui/gfx/color_utils.h"
11 #include "ui/native_theme/native_theme.h"
12 #include "ui/views/background.h"
13 #include "ui/views/border.h"
14 #include "ui/views/controls/label.h"
15 #include "ui/views/widget/widget.h"
17 namespace ash {
18 namespace ime {
20 namespace {
22 // VerticalCandidateLabel is used for rendering candidate text in
23 // the vertical candidate window.
24 class VerticalCandidateLabel : public views::Label {
25 public:
26 VerticalCandidateLabel() {}
28 private:
29 virtual ~VerticalCandidateLabel() {}
31 // Returns the preferred size, but guarantees that the width has at
32 // least kMinCandidateLabelWidth pixels.
33 virtual gfx::Size GetPreferredSize() OVERRIDE {
34 gfx::Size size = Label::GetPreferredSize();
35 size.SetToMax(gfx::Size(kMinCandidateLabelWidth, 0));
36 size.SetToMin(gfx::Size(kMaxCandidateLabelWidth, size.height()));
37 return size;
40 DISALLOW_COPY_AND_ASSIGN(VerticalCandidateLabel);
43 // Creates the shortcut label, and returns it (never returns NULL).
44 // The label text is not set in this function.
45 views::Label* CreateShortcutLabel(
46 ui::CandidateWindow::Orientation orientation,
47 const ui::NativeTheme& theme) {
48 // Create the shortcut label. The label will be owned by
49 // |wrapped_shortcut_label|, hence it's deleted when
50 // |wrapped_shortcut_label| is deleted.
51 views::Label* shortcut_label = new views::Label;
53 if (orientation == ui::CandidateWindow::VERTICAL) {
54 shortcut_label->SetFontList(
55 shortcut_label->font_list().DeriveFontListWithSizeDeltaAndStyle(
56 kFontSizeDelta, gfx::Font::BOLD));
57 } else {
58 shortcut_label->SetFontList(
59 shortcut_label->font_list().DeriveFontListWithSizeDelta(
60 kFontSizeDelta));
62 // TODO(satorux): Maybe we need to use language specific fonts for
63 // candidate_label, like Chinese font for Chinese input method?
64 shortcut_label->SetEnabledColor(theme.GetSystemColor(
65 ui::NativeTheme::kColorId_LabelEnabledColor));
66 shortcut_label->SetDisabledColor(theme.GetSystemColor(
67 ui::NativeTheme::kColorId_LabelDisabledColor));
69 // Setup paddings.
70 const gfx::Insets kVerticalShortcutLabelInsets(1, 6, 1, 6);
71 const gfx::Insets kHorizontalShortcutLabelInsets(1, 3, 1, 0);
72 const gfx::Insets insets =
73 (orientation == ui::CandidateWindow::VERTICAL ?
74 kVerticalShortcutLabelInsets :
75 kHorizontalShortcutLabelInsets);
76 shortcut_label->set_border(views::Border::CreateEmptyBorder(
77 insets.top(), insets.left(), insets.bottom(), insets.right()));
79 // Add decoration based on the orientation.
80 if (orientation == ui::CandidateWindow::VERTICAL) {
81 // Set the background color.
82 SkColor blackish = color_utils::AlphaBlend(
83 SK_ColorBLACK,
84 theme.GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
85 0x40);
86 SkColor transparent_blakish = color_utils::AlphaBlend(
87 SK_ColorTRANSPARENT, blackish, 0xE0);
88 shortcut_label->set_background(
89 views::Background::CreateSolidBackground(transparent_blakish));
92 return shortcut_label;
95 // Creates the candidate label, and returns it (never returns NULL).
96 // The label text is not set in this function.
97 views::Label* CreateCandidateLabel(
98 ui::CandidateWindow::Orientation orientation) {
99 views::Label* candidate_label = NULL;
101 // Create the candidate label. The label will be added to |this| as a
102 // child view, hence it's deleted when |this| is deleted.
103 if (orientation == ui::CandidateWindow::VERTICAL) {
104 candidate_label = new VerticalCandidateLabel;
105 } else {
106 candidate_label = new views::Label;
109 // Change the font size.
110 candidate_label->SetFontList(
111 candidate_label->font_list().DeriveFontListWithSizeDelta(kFontSizeDelta));
112 candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
114 return candidate_label;
117 // Creates the annotation label, and return it (never returns NULL).
118 // The label text is not set in this function.
119 views::Label* CreateAnnotationLabel(
120 ui::CandidateWindow::Orientation orientation,
121 const ui::NativeTheme& theme) {
122 // Create the annotation label.
123 views::Label* annotation_label = new views::Label;
125 // Change the font size and color.
126 annotation_label->SetFontList(
127 annotation_label->font_list().DeriveFontListWithSizeDelta(
128 kFontSizeDelta));
129 annotation_label->SetEnabledColor(theme.GetSystemColor(
130 ui::NativeTheme::kColorId_LabelDisabledColor));
131 annotation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
133 return annotation_label;
136 } // namespace
138 CandidateView::CandidateView(
139 views::ButtonListener* listener,
140 ui::CandidateWindow::Orientation orientation)
141 : views::CustomButton(listener),
142 orientation_(orientation),
143 shortcut_label_(NULL),
144 candidate_label_(NULL),
145 annotation_label_(NULL),
146 infolist_icon_(NULL) {
147 set_border(views::Border::CreateEmptyBorder(1, 1, 1, 1));
149 const ui::NativeTheme& theme = *GetNativeTheme();
150 shortcut_label_ = CreateShortcutLabel(orientation, theme);
151 candidate_label_ = CreateCandidateLabel(orientation);
152 annotation_label_ = CreateAnnotationLabel(orientation, theme);
154 AddChildView(shortcut_label_);
155 AddChildView(candidate_label_);
156 AddChildView(annotation_label_);
158 if (orientation == ui::CandidateWindow::VERTICAL) {
159 infolist_icon_ = new views::View;
160 infolist_icon_->set_background(
161 views::Background::CreateSolidBackground(theme.GetSystemColor(
162 ui::NativeTheme::kColorId_FocusedBorderColor)));
163 AddChildView(infolist_icon_);
167 void CandidateView::GetPreferredWidths(int* shortcut_width,
168 int* candidate_width) {
169 *shortcut_width = shortcut_label_->GetPreferredSize().width();
170 *candidate_width = candidate_label_->GetPreferredSize().width();
173 void CandidateView::SetWidths(int shortcut_width, int candidate_width) {
174 shortcut_width_ = shortcut_width;
175 shortcut_label_->SetVisible(shortcut_width_ != 0);
176 candidate_width_ = candidate_width;
179 void CandidateView::SetEntry(const ui::CandidateWindow::Entry& entry) {
180 std::string label = entry.label;
181 if (!label.empty() && orientation_ != ui::CandidateWindow::VERTICAL)
182 label += '.';
183 shortcut_label_->SetText(base::UTF8ToUTF16(label));
184 candidate_label_->SetText(base::UTF8ToUTF16(entry.value));
185 annotation_label_->SetText(base::UTF8ToUTF16(entry.annotation));
188 void CandidateView::SetInfolistIcon(bool enable) {
189 if (infolist_icon_)
190 infolist_icon_->SetVisible(enable);
191 SchedulePaint();
194 void CandidateView::StateChanged() {
195 shortcut_label_->SetEnabled(state() != STATE_DISABLED);
196 if (state() == STATE_PRESSED) {
197 ui::NativeTheme* theme = GetNativeTheme();
198 set_background(
199 views::Background::CreateSolidBackground(theme->GetSystemColor(
200 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
201 set_border(views::Border::CreateSolidBorder(
202 1, theme->GetSystemColor(
203 ui::NativeTheme::kColorId_FocusedBorderColor)));
205 // Cancel currently focused one.
206 for (int i = 0; i < parent()->child_count(); ++i) {
207 CandidateView* view =
208 static_cast<CandidateView*>((parent()->child_at(i)));
209 if (view != this && view->state() == STATE_PRESSED)
210 view->SetState(STATE_NORMAL);
212 } else {
213 set_background(NULL);
214 set_border(views::Border::CreateEmptyBorder(1, 1, 1, 1));
218 bool CandidateView::OnMouseDragged(const ui::MouseEvent& event) {
219 if (!HitTestPoint(event.location())) {
220 // Moves the drag target to the sibling view.
221 gfx::Point location_in_widget(event.location());
222 ConvertPointToWidget(this, &location_in_widget);
223 for (int i = 0; i < parent()->child_count(); ++i) {
224 views::View* sibling = parent()->child_at(i);
225 if (sibling == this)
226 continue;
227 gfx::Point location_in_sibling(location_in_widget);
228 ConvertPointFromWidget(sibling, &location_in_sibling);
229 if (sibling->HitTestPoint(location_in_sibling)) {
230 GetWidget()->GetRootView()->SetMouseHandler(sibling);
231 return sibling->OnMouseDragged(event);
235 return false;
238 return views::CustomButton::OnMouseDragged(event);
241 void CandidateView::Layout() {
242 const int padding_width =
243 orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
244 int x = 0;
245 shortcut_label_->SetBounds(x, 0, shortcut_width_, height());
246 if (shortcut_width_ > 0)
247 x += shortcut_width_ + padding_width;
248 candidate_label_->SetBounds(x, 0, candidate_width_, height());
249 x += candidate_width_ + padding_width;
251 int right = bounds().right();
252 if (infolist_icon_ && infolist_icon_->visible()) {
253 infolist_icon_->SetBounds(
254 right - kInfolistIndicatorIconWidth - kInfolistIndicatorIconPadding,
255 kInfolistIndicatorIconPadding,
256 kInfolistIndicatorIconWidth,
257 height() - kInfolistIndicatorIconPadding * 2);
258 right -= kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2;
260 annotation_label_->SetBounds(x, 0, right - x, height());
263 gfx::Size CandidateView::GetPreferredSize() {
264 const int padding_width =
265 orientation_ == ui::CandidateWindow::VERTICAL ? 4 : 6;
266 gfx::Size size;
267 if (shortcut_label_->visible()) {
268 size = shortcut_label_->GetPreferredSize();
269 size.SetToMax(gfx::Size(shortcut_width_, 0));
270 size.Enlarge(padding_width, 0);
272 gfx::Size candidate_size = candidate_label_->GetPreferredSize();
273 candidate_size.SetToMax(gfx::Size(candidate_width_, 0));
274 size.Enlarge(candidate_size.width() + padding_width, 0);
275 size.SetToMax(candidate_size);
276 if (annotation_label_->visible()) {
277 gfx::Size annotation_size = annotation_label_->GetPreferredSize();
278 size.Enlarge(annotation_size.width() + padding_width, 0);
279 size.SetToMax(annotation_size);
282 // Reserves the margin for infolist_icon even if it's not visible.
283 size.Enlarge(
284 kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2, 0);
285 return size;
288 } // namespace ime
289 } // namespace ash