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 | 4 |
5 // For WinDDK ATL compatibility, these ATL headers must come first. | 5 // For WinDDK ATL compatibility, these ATL headers must come first. |
6 #include "build/build_config.h" | 6 #include "build/build_config.h" |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <atlbase.h> // NOLINT | 8 #include <atlbase.h> // NOLINT |
9 #include <atlwin.h> // NOLINT | 9 #include <atlwin.h> // NOLINT |
10 #endif | 10 #endif |
11 | 11 |
12 #include "chrome/browser/ui/views/autocomplete/autocomplete_result_view.h" | 12 #include "chrome/browser/ui/views/autocomplete/autocomplete_result_view.h" |
13 | 13 |
14 #include <algorithm> // NOLINT | 14 #include <algorithm> // NOLINT |
15 | 15 |
16 #include "base/i18n/bidi_line_iterator.h" | 16 #include "base/i18n/bidi_line_iterator.h" |
17 #include "chrome/browser/autocomplete/autocomplete_popup_model.h" | |
18 #include "chrome/browser/ui/views/autocomplete/autocomplete_result_view_model.h" | 17 #include "chrome/browser/ui/views/autocomplete/autocomplete_result_view_model.h" |
19 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 18 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
20 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
21 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
22 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
23 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
24 #include "ui/base/text/text_elider.h" | 23 #include "ui/base/text/text_elider.h" |
25 #include "ui/gfx/canvas_skia.h" | 24 #include "ui/gfx/canvas_skia.h" |
26 #include "ui/gfx/color_utils.h" | 25 #include "ui/gfx/color_utils.h" |
27 #include "ui/gfx/native_theme.h" | 26 #include "ui/gfx/native_theme.h" |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
106 AutocompleteResultView::AutocompleteResultView( | 105 AutocompleteResultView::AutocompleteResultView( |
107 AutocompleteResultViewModel* model, | 106 AutocompleteResultViewModel* model, |
108 int model_index, | 107 int model_index, |
109 const gfx::Font& font, | 108 const gfx::Font& font, |
110 const gfx::Font& bold_font) | 109 const gfx::Font& bold_font) |
111 : model_(model), | 110 : model_(model), |
112 model_index_(model_index), | 111 model_index_(model_index), |
113 normal_font_(font), | 112 normal_font_(font), |
114 bold_font_(bold_font), | 113 bold_font_(bold_font), |
115 ellipsis_width_(font.GetStringWidth(string16(kEllipsis))), | 114 ellipsis_width_(font.GetStringWidth(string16(kEllipsis))), |
116 mirroring_context_(new MirroringContext()), | 115 mirroring_context_(new MirroringContext()) { |
117 keyword_icon_(new views::ImageView()), | |
118 ALLOW_THIS_IN_INITIALIZER_LIST( | |
119 animation_(new ui::SlideAnimation(this))) { | |
120 CHECK_GE(model_index, 0); | 116 CHECK_GE(model_index, 0); |
121 if (default_icon_size_ == 0) { | 117 if (default_icon_size_ == 0) { |
122 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( | 118 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
123 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> | 119 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> |
124 width(); | 120 width(); |
125 } | 121 } |
126 keyword_icon_->set_parent_owned(false); | |
127 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | |
128 keyword_icon_->SetImage(GetKeywordIcon()); | |
129 keyword_icon_->SizeToPreferredSize(); | |
130 } | 122 } |
131 | 123 |
132 AutocompleteResultView::~AutocompleteResultView() { | 124 AutocompleteResultView::~AutocompleteResultView() { |
133 } | 125 } |
134 | 126 |
135 // static | 127 // static |
136 SkColor AutocompleteResultView::GetColor(ResultViewState state, | 128 SkColor AutocompleteResultView::GetColor(ResultViewState state, |
137 ColorKind kind) { | 129 ColorKind kind) { |
138 static bool initialized = false; | 130 static bool initialized = false; |
139 static SkColor colors[NUM_STATES][NUM_KINDS]; | 131 static SkColor colors[NUM_STATES][NUM_KINDS]; |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 colors[i][BACKGROUND]); | 171 colors[i][BACKGROUND]); |
180 } | 172 } |
181 initialized = true; | 173 initialized = true; |
182 } | 174 } |
183 | 175 |
184 return colors[state][kind]; | 176 return colors[state][kind]; |
185 } | 177 } |
186 | 178 |
187 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { | 179 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { |
188 match_ = match; | 180 match_ = match; |
189 animation_->Reset(); | |
190 | |
191 if (match.associated_keyword.get()) { | |
192 keyword_icon_->SetImage(GetKeywordIcon()); | |
193 | |
194 if (!keyword_icon_->parent()) | |
195 AddChildView(keyword_icon_.get()); | |
196 } else if (keyword_icon_->parent()) { | |
197 RemoveChildView(keyword_icon_.get()); | |
198 } | |
199 | |
200 Layout(); | 181 Layout(); |
201 } | 182 } |
202 | 183 |
203 void AutocompleteResultView::ShowKeyword(bool show_keyword) { | |
204 if (show_keyword) | |
205 animation_->Show(); | |
206 else | |
207 animation_->Hide(); | |
208 } | |
209 | |
210 void AutocompleteResultView::Invalidate() { | |
211 keyword_icon_->SetImage(GetKeywordIcon()); | |
212 SchedulePaint(); | |
213 } | |
214 | |
215 gfx::Size AutocompleteResultView::GetPreferredSize() { | |
216 return gfx::Size(0, std::max( | |
217 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
218 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | |
219 } | |
220 | |
221 //////////////////////////////////////////////////////////////////////////////// | 184 //////////////////////////////////////////////////////////////////////////////// |
222 // AutocompleteResultView, protected: | 185 // AutocompleteResultView, protected: |
223 | 186 |
224 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | 187 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
225 const AutocompleteMatch& match, | 188 const AutocompleteMatch& match, |
226 int x) { | 189 int x) { |
227 x = DrawString(canvas, match.contents, match.contents_class, false, x, | 190 x = DrawString(canvas, match.contents, match.contents_class, false, x, |
228 text_bounds_.y()); | 191 text_bounds_.y()); |
229 | 192 |
230 // Paint the description. | 193 // Paint the description. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
273 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; | 236 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; |
274 } | 237 } |
275 | 238 |
276 const SkBitmap* AutocompleteResultView::GetIcon() const { | 239 const SkBitmap* AutocompleteResultView::GetIcon() const { |
277 const SkBitmap* bitmap = model_->GetIconIfExtensionMatch(model_index_); | 240 const SkBitmap* bitmap = model_->GetIconIfExtensionMatch(model_index_); |
278 if (bitmap) | 241 if (bitmap) |
279 return bitmap; | 242 return bitmap; |
280 | 243 |
281 int icon = match_.starred ? | 244 int icon = match_.starred ? |
282 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type); | 245 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type); |
283 if (GetState() == SELECTED) { | 246 if (model_->IsSelectedIndex(model_index_)) { |
284 switch (icon) { | 247 switch (icon) { |
285 case IDR_OMNIBOX_EXTENSION_APP: | 248 case IDR_OMNIBOX_EXTENSION_APP: |
286 icon = IDR_OMNIBOX_EXTENSION_APP_SELECTED; | 249 icon = IDR_OMNIBOX_EXTENSION_APP_SELECTED; |
287 break; | 250 break; |
288 case IDR_OMNIBOX_HTTP: | 251 case IDR_OMNIBOX_HTTP: |
289 icon = IDR_OMNIBOX_HTTP_SELECTED; | 252 icon = IDR_OMNIBOX_HTTP_SELECTED; |
290 break; | 253 break; |
291 case IDR_OMNIBOX_HISTORY: | 254 case IDR_OMNIBOX_HISTORY: |
292 icon = IDR_OMNIBOX_HISTORY_SELECTED; | 255 icon = IDR_OMNIBOX_HISTORY_SELECTED; |
293 break; | 256 break; |
294 case IDR_OMNIBOX_SEARCH: | 257 case IDR_OMNIBOX_SEARCH: |
295 icon = IDR_OMNIBOX_SEARCH_SELECTED; | 258 icon = IDR_OMNIBOX_SEARCH_SELECTED; |
296 break; | 259 break; |
297 case IDR_OMNIBOX_STAR: | 260 case IDR_OMNIBOX_STAR: |
298 icon = IDR_OMNIBOX_STAR_SELECTED; | 261 icon = IDR_OMNIBOX_STAR_SELECTED; |
299 break; | 262 break; |
300 default: | 263 default: |
301 NOTREACHED(); | 264 NOTREACHED(); |
302 break; | 265 break; |
303 } | 266 } |
304 } | 267 } |
305 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); | 268 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); |
306 } | 269 } |
307 | 270 |
308 const SkBitmap* AutocompleteResultView::GetKeywordIcon() const { | |
309 // NOTE: If we ever begin returning icons of varying size, then callers need | |
310 // to ensure that |keyword_icon_| is resized each time its image is reset. | |
311 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
312 (GetState() == SELECTED) ? IDR_OMNIBOX_TTS_SELECTED : IDR_OMNIBOX_TTS); | |
313 } | |
314 | |
315 int AutocompleteResultView::DrawString( | 271 int AutocompleteResultView::DrawString( |
316 gfx::Canvas* canvas, | 272 gfx::Canvas* canvas, |
317 const string16& text, | 273 const string16& text, |
318 const ACMatchClassifications& classifications, | 274 const ACMatchClassifications& classifications, |
319 bool force_dim, | 275 bool force_dim, |
320 int x, | 276 int x, |
321 int y) { | 277 int y) { |
322 if (text.empty()) | 278 if (text.empty()) |
323 return x; | 279 return x; |
324 | 280 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
535 // ellipsis we'll append to it, or | 491 // ellipsis we'll append to it, or |
536 // * It is also bold, in which case we don't want to fall back | 492 // * It is also bold, in which case we don't want to fall back |
537 // to a normal ellipsis anyway (see comment above). | 493 // to a normal ellipsis anyway (see comment above). |
538 } | 494 } |
539 } | 495 } |
540 | 496 |
541 // We couldn't draw anything. | 497 // We couldn't draw anything. |
542 runs->clear(); | 498 runs->clear(); |
543 } | 499 } |
544 | 500 |
| 501 gfx::Size AutocompleteResultView::GetPreferredSize() { |
| 502 return gfx::Size(0, std::max( |
| 503 default_icon_size_ + (kMinimumIconVerticalPadding * 2), |
| 504 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); |
| 505 } |
| 506 |
545 void AutocompleteResultView::Layout() { | 507 void AutocompleteResultView::Layout() { |
546 const SkBitmap* icon = GetIcon(); | 508 const SkBitmap* icon = GetIcon(); |
547 | |
548 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + | 509 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + |
549 ((icon->width() == default_icon_size_) ? | 510 ((icon->width() == default_icon_size_) ? |
550 0 : LocationBarView::kIconInternalPadding), | 511 0 : LocationBarView::kIconInternalPadding), |
551 (height() - icon->height()) / 2, icon->width(), icon->height()); | 512 (height() - icon->height()) / 2, icon->width(), icon->height()); |
552 | 513 |
553 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + | 514 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + |
554 LocationBarView::kItemPadding; | 515 LocationBarView::kItemPadding; |
555 int text_height = GetTextHeight(); | 516 int text_height = GetTextHeight(); |
556 int text_width; | |
557 | |
558 if (match_.associated_keyword.get()) { | |
559 const int kw_collapsed_size = keyword_icon_->width() + | |
560 LocationBarView::kEdgeItemPadding; | |
561 const int max_kw_x = width() - kw_collapsed_size; | |
562 const int kw_x = animation_->CurrentValueBetween(max_kw_x, | |
563 LocationBarView::kEdgeItemPadding); | |
564 const int kw_text_x = kw_x + keyword_icon_->width() + | |
565 LocationBarView::kItemPadding; | |
566 | |
567 text_width = kw_x - text_x - LocationBarView::kItemPadding; | |
568 keyword_text_bounds_.SetRect(kw_text_x, 0, std::max( | |
569 width() - kw_text_x - LocationBarView::kEdgeItemPadding, 0), | |
570 text_height); | |
571 keyword_icon_->SetPosition(gfx::Point(kw_x, | |
572 (height() - keyword_icon_->height()) / 2)); | |
573 } else { | |
574 text_width = width() - text_x - LocationBarView::kEdgeItemPadding; | |
575 } | |
576 | |
577 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), | 517 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), |
578 std::max(text_width, 0), text_height); | 518 std::max(bounds().width() - text_x - LocationBarView::kEdgeItemPadding, |
579 } | 519 0), text_height); |
580 | |
581 void AutocompleteResultView::OnBoundsChanged( | |
582 const gfx::Rect& previous_bounds) { | |
583 animation_->SetSlideDuration(width() / 4); | |
584 } | 520 } |
585 | 521 |
586 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { | 522 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { |
587 const ResultViewState state = GetState(); | 523 const ResultViewState state = GetState(); |
588 if (state != NORMAL) | 524 if (state != NORMAL) |
589 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); | 525 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); |
590 | 526 |
591 if (!match_.associated_keyword.get() || | 527 // Paint the icon. |
592 keyword_icon_->x() > icon_bounds_.right()) { | 528 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), |
593 // Paint the icon. | 529 icon_bounds_.y()); |
594 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | |
595 icon_bounds_.y()); | |
596 | 530 |
597 // Paint the text. | 531 // Paint the text. |
598 int x = GetMirroredXForRect(text_bounds_); | 532 int x = GetMirroredXForRect(text_bounds_); |
599 mirroring_context_->Initialize(x, text_bounds_.width()); | 533 mirroring_context_->Initialize(x, text_bounds_.width()); |
600 PaintMatch(canvas, match_, x); | 534 PaintMatch(canvas, match_, x); |
601 } | |
602 | |
603 if (match_.associated_keyword.get()) { | |
604 // Paint the keyword text. | |
605 int x = GetMirroredXForRect(keyword_text_bounds_); | |
606 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | |
607 PaintMatch(canvas, *match_.associated_keyword.get(), x); | |
608 } | |
609 } | 535 } |
610 | |
611 void AutocompleteResultView::AnimationProgressed( | |
612 const ui::Animation* animation) { | |
613 Layout(); | |
614 SchedulePaint(); | |
615 } | |
616 | |
OLD | NEW |