Chromium Code Reviews| 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 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(new ui::SlideAnimation(this))) { | |
| 115 CHECK_GE(model_index, 0); | 117 CHECK_GE(model_index, 0); |
| 116 if (default_icon_size_ == 0) { | 118 if (default_icon_size_ == 0) { |
| 117 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( | 119 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 118 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> | 120 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> |
| 119 width(); | 121 width(); |
| 120 } | 122 } |
| 123 | |
| 124 animation_->SetDuration(500); | |
|
Peter Kasting
2012/01/11 03:00:16
I suspect this will be too slow, but I should patc
| |
| 121 } | 125 } |
| 122 | 126 |
| 123 AutocompleteResultView::~AutocompleteResultView() { | 127 AutocompleteResultView::~AutocompleteResultView() { |
| 124 } | 128 } |
| 125 | 129 |
| 126 // static | 130 // static |
| 127 SkColor AutocompleteResultView::GetColor(ResultViewState state, | 131 SkColor AutocompleteResultView::GetColor(ResultViewState state, |
| 128 ColorKind kind) { | 132 ColorKind kind) { |
| 129 static bool initialized = false; | 133 static bool initialized = false; |
| 130 static SkColor colors[NUM_STATES][NUM_KINDS]; | 134 static SkColor colors[NUM_STATES][NUM_KINDS]; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 166 colors[i][BACKGROUND]); | 170 colors[i][BACKGROUND]); |
| 167 } | 171 } |
| 168 initialized = true; | 172 initialized = true; |
| 169 } | 173 } |
| 170 | 174 |
| 171 return colors[state][kind]; | 175 return colors[state][kind]; |
| 172 } | 176 } |
| 173 | 177 |
| 174 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { | 178 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { |
| 175 match_ = match; | 179 match_ = match; |
| 180 animation_->Reset(); | |
| 181 keyword_icon_bounds_.SetRect(0, 0, 0, 0); | |
| 182 | |
| 176 Layout(); | 183 Layout(); |
| 177 } | 184 } |
| 178 | 185 |
| 186 void AutocompleteResultView::ShowKeyword(bool show_keyword) { | |
| 187 if (show_keyword) { | |
|
Peter Kasting
2012/01/11 03:00:16
Nit: No {} necessary
| |
| 188 animation_->Show(); | |
| 189 } else { | |
| 190 animation_->Hide(); | |
| 191 } | |
| 192 } | |
| 193 | |
| 194 gfx::Size AutocompleteResultView::GetPreferredSize() { | |
| 195 return gfx::Size(0, std::max( | |
| 196 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
| 197 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | |
| 198 } | |
| 199 | |
| 179 //////////////////////////////////////////////////////////////////////////////// | 200 //////////////////////////////////////////////////////////////////////////////// |
| 180 // AutocompleteResultView, protected: | 201 // AutocompleteResultView, protected: |
| 181 | 202 |
| 182 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | 203 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
| 183 const AutocompleteMatch& match, | 204 const AutocompleteMatch& match, |
| 184 int x) { | 205 int x) { |
| 185 x = DrawString(canvas, match.contents, match.contents_class, false, x, | 206 x = DrawString(canvas, match.contents, match.contents_class, false, x, |
| 186 text_bounds_.y()); | 207 text_bounds_.y()); |
| 187 | 208 |
| 188 // Paint the description. | 209 // Paint the description. |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 icon = IDR_OMNIBOX_STAR_SELECTED; | 277 icon = IDR_OMNIBOX_STAR_SELECTED; |
| 257 break; | 278 break; |
| 258 default: | 279 default: |
| 259 NOTREACHED(); | 280 NOTREACHED(); |
| 260 break; | 281 break; |
| 261 } | 282 } |
| 262 } | 283 } |
| 263 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); | 284 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); |
| 264 } | 285 } |
| 265 | 286 |
| 287 const SkBitmap* AutocompleteResultView::GetKeywordIcon() const { | |
| 288 int icon = IDR_OMNIBOX_TTS_NORMAL; | |
| 289 | |
| 290 if (model_->IsSelectedIndex(model_index_)) { | |
| 291 icon = !animation_->IsShowing() ? IDR_OMNIBOX_TTS_NORMAL_SELECTED : | |
| 292 #if defined(TOOLKIT_USES_GTK) | |
| 293 IDR_OMNIBOX_TTS_KEYWORD_DARK; | |
|
Peter Kasting
2012/01/11 03:00:16
We don't actually use the DARK variants for GTK-vi
| |
| 294 #else | |
| 295 IDR_OMNIBOX_TTS_KEYWORD_SELECTED; | |
| 296 #endif | |
| 297 } | |
| 298 | |
| 299 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); | |
| 300 } | |
| 301 | |
| 266 int AutocompleteResultView::DrawString( | 302 int AutocompleteResultView::DrawString( |
| 267 gfx::Canvas* canvas, | 303 gfx::Canvas* canvas, |
| 268 const string16& text, | 304 const string16& text, |
| 269 const ACMatchClassifications& classifications, | 305 const ACMatchClassifications& classifications, |
| 270 bool force_dim, | 306 bool force_dim, |
| 271 int x, | 307 int x, |
| 272 int y) { | 308 int y) { |
| 273 if (text.empty()) | 309 if (text.empty()) |
| 274 return x; | 310 return x; |
| 275 | 311 |
| (...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 486 // ellipsis we'll append to it, or | 522 // ellipsis we'll append to it, or |
| 487 // * It is also bold, in which case we don't want to fall back | 523 // * It is also bold, in which case we don't want to fall back |
| 488 // to a normal ellipsis anyway (see comment above). | 524 // to a normal ellipsis anyway (see comment above). |
| 489 } | 525 } |
| 490 } | 526 } |
| 491 | 527 |
| 492 // We couldn't draw anything. | 528 // We couldn't draw anything. |
| 493 runs->clear(); | 529 runs->clear(); |
| 494 } | 530 } |
| 495 | 531 |
| 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() { | 532 void AutocompleteResultView::Layout() { |
| 503 const SkBitmap* icon = GetIcon(); | 533 const SkBitmap* icon = GetIcon(); |
| 534 | |
| 504 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + | 535 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + |
| 505 ((icon->width() == default_icon_size_) ? | 536 ((icon->width() == default_icon_size_) ? |
| 506 0 : LocationBarView::kIconInternalPadding), | 537 0 : LocationBarView::kIconInternalPadding), |
| 507 (height() - icon->height()) / 2, icon->width(), icon->height()); | 538 (height() - icon->height()) / 2, icon->width(), icon->height()); |
| 508 | 539 |
| 509 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + | 540 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + |
| 510 LocationBarView::kItemPadding; | 541 LocationBarView::kItemPadding; |
| 511 int text_height = GetTextHeight(); | 542 int text_height = GetTextHeight(); |
| 543 int text_width; | |
| 544 | |
| 545 if (match_.associated_keyword.get()) { | |
| 546 const SkBitmap* kw_icon = GetKeywordIcon(); | |
| 547 const int kw_collapsed_size = kw_icon->width() + | |
| 548 LocationBarView::kEdgeItemPadding; | |
| 549 const int max_kw_x = bounds().width() - kw_collapsed_size; | |
| 550 const int kw_x = animation_->CurrentValueBetween(max_kw_x, | |
| 551 LocationBarView::kEdgeItemPadding); | |
| 552 const int kw_text_x = kw_x + kw_icon->width() + | |
| 553 LocationBarView::kItemPadding; | |
| 554 | |
| 555 text_width = kw_x - text_x - LocationBarView::kItemPadding; | |
| 556 keyword_text_bounds_.SetRect(kw_text_x, 0, std::max( | |
| 557 bounds().width() - kw_text_x - LocationBarView::kEdgeItemPadding, 0), | |
| 558 text_height); | |
| 559 keyword_icon_bounds_.SetRect(GetMirroredXInView(kw_x), | |
| 560 (height() - kw_icon->height()) / 2, | |
| 561 kw_icon->width(), kw_icon->height()); | |
| 562 } else { | |
| 563 text_width = bounds().width() - text_x - LocationBarView::kEdgeItemPadding; | |
| 564 } | |
| 565 | |
| 512 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), | 566 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), |
| 513 std::max(bounds().width() - text_x - LocationBarView::kEdgeItemPadding, | 567 std::max(text_width, 0), text_height); |
| 514 0), text_height); | |
| 515 } | 568 } |
| 516 | 569 |
| 517 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { | 570 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { |
| 518 const ResultViewState state = GetState(); | 571 const ResultViewState state = GetState(); |
| 519 if (state != NORMAL) | 572 if (state != NORMAL) |
| 520 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); | 573 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); |
| 521 | 574 |
| 522 // Paint the icon. | 575 if (keyword_icon_bounds_.IsEmpty() || |
| 523 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | 576 keyword_icon_bounds_.x() > icon_bounds_.right()) { |
| 524 icon_bounds_.y()); | 577 // Paint the icon. |
| 578 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | |
| 579 icon_bounds_.y()); | |
| 525 | 580 |
| 526 // Paint the text. | 581 // Paint the text. |
| 527 int x = GetMirroredXForRect(text_bounds_); | 582 int x = GetMirroredXForRect(text_bounds_); |
| 528 mirroring_context_->Initialize(x, text_bounds_.width()); | 583 mirroring_context_->Initialize(x, text_bounds_.width()); |
| 529 PaintMatch(canvas, match_, x); | 584 PaintMatch(canvas, match_, x); |
| 585 } | |
| 586 | |
| 587 if (match_.associated_keyword.get()) { | |
| 588 // Paint the keyword icon. | |
| 589 canvas->DrawBitmapInt(*GetKeywordIcon(), keyword_icon_bounds_.x(), | |
| 590 keyword_icon_bounds_.y()); | |
| 591 | |
| 592 // Paint the keyword text. | |
| 593 int x = GetMirroredXForRect(keyword_text_bounds_); | |
| 594 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | |
| 595 PaintMatch(canvas, *match_.associated_keyword.get(), x); | |
| 596 } | |
| 530 } | 597 } |
| 598 | |
| 599 void AutocompleteResultView::AnimationProgressed( | |
| 600 const ui::Animation* animation) { | |
| 601 Layout(); | |
| 602 SchedulePaint(); | |
| 603 } | |
| 604 | |
| OLD | NEW |