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