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"
22 // VerticalCandidateLabel is used for rendering candidate text in
23 // the vertical candidate window.
24 class VerticalCandidateLabel
: public views::Label
{
26 VerticalCandidateLabel() {}
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()));
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
));
58 shortcut_label
->SetFontList(
59 shortcut_label
->font_list().DeriveFontListWithSizeDelta(
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
));
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(
84 theme
.GetSystemColor(ui::NativeTheme::kColorId_WindowBackground
),
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
;
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(
129 annotation_label
->SetEnabledColor(theme
.GetSystemColor(
130 ui::NativeTheme::kColorId_LabelDisabledColor
));
131 annotation_label
->SetHorizontalAlignment(gfx::ALIGN_LEFT
);
133 return annotation_label
;
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
)
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
) {
190 infolist_icon_
->SetVisible(enable
);
194 void CandidateView::StateChanged() {
195 shortcut_label_
->SetEnabled(state() != STATE_DISABLED
);
196 if (state() == STATE_PRESSED
) {
197 ui::NativeTheme
* theme
= GetNativeTheme();
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
);
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
);
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
);
238 return views::CustomButton::OnMouseDragged(event
);
241 void CandidateView::Layout() {
242 const int padding_width
=
243 orientation_
== ui::CandidateWindow::VERTICAL
? 4 : 6;
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;
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.
284 kInfolistIndicatorIconWidth
+ kInfolistIndicatorIconPadding
* 2, 0);