Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(153)

Side by Side Diff: chrome/browser/chromeos/input_method/candidate_view.cc

Issue 121163003: Cleanup CandidateWindowView. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: fix Created 6 years, 12 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 "chrome/browser/chromeos/input_method/candidate_view.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/chromeos/input_method/candidate_window_constants.h"
9 #include "chromeos/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"
16
17 namespace chromeos {
18 namespace input_method {
19
20 namespace {
21
22 // VerticalCandidateLabel is used for rendering candidate text in
23 // the vertical candidate window.
24 class VerticalCandidateLabel : public views::Label {
25 public:
26 VerticalCandidateLabel() {}
27
28 private:
29 virtual ~VerticalCandidateLabel() {}
30
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;
38 }
39
40 DISALLOW_COPY_AND_ASSIGN(VerticalCandidateLabel);
41 };
42
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 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;
52
53 if (orientation == CandidateWindow::VERTICAL) {
54 shortcut_label->SetFont(
55 shortcut_label->font().DeriveFont(kFontSizeDelta, gfx::Font::BOLD));
56 } else {
57 shortcut_label->SetFont(
58 shortcut_label->font().DeriveFont(kFontSizeDelta));
59 }
60 // TODO(satorux): Maybe we need to use language specific fonts for
61 // candidate_label, like Chinese font for Chinese input method?
62 shortcut_label->SetEnabledColor(theme.GetSystemColor(
63 ui::NativeTheme::kColorId_LabelEnabledColor));
64 shortcut_label->SetDisabledColor(theme.GetSystemColor(
65 ui::NativeTheme::kColorId_LabelDisabledColor));
66
67 // Setup paddings.
68 const gfx::Insets kVerticalShortcutLabelInsets(1, 6, 1, 6);
69 const gfx::Insets kHorizontalShortcutLabelInsets(1, 3, 1, 0);
70 const gfx::Insets insets =
71 (orientation == CandidateWindow::VERTICAL ?
72 kVerticalShortcutLabelInsets :
73 kHorizontalShortcutLabelInsets);
74 shortcut_label->set_border(views::Border::CreateEmptyBorder(
75 insets.top(), insets.left(), insets.bottom(), insets.right()));
76
77 // Add decoration based on the orientation.
78 if (orientation == CandidateWindow::VERTICAL) {
79 // Set the background color.
80 SkColor blackish = color_utils::AlphaBlend(
81 SK_ColorBLACK,
82 theme.GetSystemColor(ui::NativeTheme::kColorId_WindowBackground),
83 0x40);
84 SkColor transparent_blakish = color_utils::AlphaBlend(
85 SK_ColorTRANSPARENT, blackish, 0xE0);
86 shortcut_label->set_background(
87 views::Background::CreateSolidBackground(transparent_blakish));
88 }
89
90 return shortcut_label;
91 }
92
93 // Creates the candidate label, and returns it (never returns NULL).
94 // The label text is not set in this function.
95 views::Label* CreateCandidateLabel(
96 CandidateWindow::Orientation orientation) {
97 views::Label* candidate_label = NULL;
98
99 // Create the candidate label. The label will be added to |this| as a
100 // child view, hence it's deleted when |this| is deleted.
101 if (orientation == CandidateWindow::VERTICAL) {
102 candidate_label = new VerticalCandidateLabel;
103 } else {
104 candidate_label = new views::Label;
105 }
106
107 // Change the font size.
108 candidate_label->SetFont(
109 candidate_label->font().DeriveFont(kFontSizeDelta));
110 candidate_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
111
112 return candidate_label;
113 }
114
115 // Creates the annotation label, and return it (never returns NULL).
116 // The label text is not set in this function.
117 views::Label* CreateAnnotationLabel(
118 CandidateWindow::Orientation orientation, const ui::NativeTheme& theme) {
119 // Create the annotation label.
120 views::Label* annotation_label = new views::Label;
121
122 // Change the font size and color.
123 annotation_label->SetFont(
124 annotation_label->font().DeriveFont(kFontSizeDelta));
125 annotation_label->SetEnabledColor(theme.GetSystemColor(
126 ui::NativeTheme::kColorId_LabelDisabledColor));
127 annotation_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
128
129 return annotation_label;
130 }
131
132 } // namespace
133
134 CandidateView::CandidateView(
135 views::ButtonListener* listener,
136 CandidateWindow::Orientation orientation)
137 : views::CustomButton(listener),
138 orientation_(orientation),
139 shortcut_label_(NULL),
140 candidate_label_(NULL),
141 annotation_label_(NULL),
142 infolist_icon_(NULL) {
143 set_border(views::Border::CreateEmptyBorder(1, 1, 1, 1));
144
145 const ui::NativeTheme& theme = *GetNativeTheme();
146 shortcut_label_ = CreateShortcutLabel(orientation, theme);
147 candidate_label_ = CreateCandidateLabel(orientation);
148 annotation_label_ = CreateAnnotationLabel(orientation, theme);
149
150 AddChildView(shortcut_label_);
151 AddChildView(candidate_label_);
152 AddChildView(annotation_label_);
153
154 if (orientation == CandidateWindow::VERTICAL) {
155 infolist_icon_ = new views::View;
156 infolist_icon_->set_background(
157 views::Background::CreateSolidBackground(theme.GetSystemColor(
158 ui::NativeTheme::kColorId_FocusedBorderColor)));
159 AddChildView(infolist_icon_);
160 }
161 }
162
163 void CandidateView::GetPreferredWidths(int* shortcut_width,
164 int* candidate_width) {
165 *shortcut_width = shortcut_label_->GetPreferredSize().width();
166 *candidate_width = candidate_label_->GetPreferredSize().width();
167 }
168
169 void CandidateView::SetWidths(int shortcut_width, int candidate_width) {
170 shortcut_width_ = shortcut_width;
171 shortcut_label_->SetVisible(shortcut_width_ != 0);
172 candidate_width_ = candidate_width;
173 }
174
175 void CandidateView::SetEntry(const CandidateWindow::Entry& entry) {
176 std::string label = entry.label;
177 if (!label.empty() && orientation_ != CandidateWindow::VERTICAL)
178 label += '.';
179 shortcut_label_->SetText(base::UTF8ToUTF16(label));
180 candidate_label_->SetText(base::UTF8ToUTF16(entry.value));
181 annotation_label_->SetText(base::UTF8ToUTF16(entry.annotation));
182 }
183
184 void CandidateView::SetInfolistIcon(bool enable) {
185 if (infolist_icon_)
186 infolist_icon_->SetVisible(enable);
187 SchedulePaint();
188 }
189
190 void CandidateView::StateChanged() {
191 shortcut_label_->SetEnabled(state() != STATE_DISABLED);
192 if (state() == STATE_PRESSED) {
193 ui::NativeTheme* theme = GetNativeTheme();
194 set_background(
195 views::Background::CreateSolidBackground(theme->GetSystemColor(
196 ui::NativeTheme::kColorId_TextfieldSelectionBackgroundFocused)));
197 set_border(views::Border::CreateSolidBorder(
198 1, theme->GetSystemColor(
199 ui::NativeTheme::kColorId_FocusedBorderColor)));
200
201 // Cancel currently focused one.
202 for (int i = 0; i < parent()->child_count(); ++i) {
203 CandidateView* view =
204 static_cast<CandidateView*>((parent()->child_at(i)));
205 if (view != this && view->state() == STATE_PRESSED)
206 view->SetState(STATE_NORMAL);
207 }
208 } else {
209 set_background(NULL);
210 set_border(views::Border::CreateEmptyBorder(1, 1, 1, 1));
211 }
212 }
213
214 bool CandidateView::OnMouseDragged(const ui::MouseEvent& event) {
215 if (!HitTestPoint(event.location())) {
216 // Moves the drag target to the sibling view.
217 gfx::Point location_in_widget(event.location());
218 ConvertPointToWidget(this, &location_in_widget);
219 for (int i = 0; i < parent()->child_count(); ++i) {
220 views::View* sibling = parent()->child_at(i);
221 if (sibling == this)
222 continue;
223 gfx::Point location_in_sibling(location_in_widget);
224 ConvertPointFromWidget(sibling, &location_in_sibling);
225 if (sibling->HitTestPoint(location_in_sibling)) {
226 GetWidget()->GetRootView()->SetMouseHandler(sibling);
227 return sibling->OnMouseDragged(event);
228 }
229 }
230
231 return false;
232 }
233
234 return views::CustomButton::OnMouseDragged(event);
235 }
236
237 void CandidateView::Layout() {
238 const int padding_width =
239 orientation_ == CandidateWindow::VERTICAL ? 4 : 6;
240 int x = 0;
241 shortcut_label_->SetBounds(x, 0, shortcut_width_, height());
242 if (shortcut_width_ > 0)
243 x += shortcut_width_ + padding_width;
244 candidate_label_->SetBounds(x, 0, candidate_width_, height());
245 x += candidate_width_ + padding_width;
246
247 int right = bounds().right();
248 if (infolist_icon_ && infolist_icon_->visible()) {
249 infolist_icon_->SetBounds(
250 right - kInfolistIndicatorIconWidth - kInfolistIndicatorIconPadding,
251 kInfolistIndicatorIconPadding,
252 kInfolistIndicatorIconWidth,
253 height() - kInfolistIndicatorIconPadding * 2);
254 right -= kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2;
255 }
256 annotation_label_->SetBounds(x, 0, right - x, height());
257 }
258
259 gfx::Size CandidateView::GetPreferredSize() {
260 const int padding_width =
261 orientation_ == CandidateWindow::VERTICAL ? 4 : 6;
262 gfx::Size size;
263 if (shortcut_label_->visible()) {
264 size = shortcut_label_->GetPreferredSize();
265 size.Enlarge(padding_width, 0);
266 }
267 gfx::Size candidate_size = candidate_label_->GetPreferredSize();
268 size.Enlarge(candidate_size.width() + padding_width, 0);
269 size.SetToMax(candidate_size);
270 if (annotation_label_->visible()) {
271 gfx::Size annotation_size = annotation_label_->GetPreferredSize();
272 size.Enlarge(annotation_size.width() + padding_width, 0);
273 size.SetToMax(annotation_size);
274 }
275
276 // Reserves the margin for infolist_icon even if it's not visible.
277 size.Enlarge(
278 kInfolistIndicatorIconWidth + kInfolistIndicatorIconPadding * 2, 0);
279 return size;
280 }
281
282 } // namespace input_method
283 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698