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 } |
121 } | 123 } |
122 | 124 |
123 AutocompleteResultView::~AutocompleteResultView() { | 125 AutocompleteResultView::~AutocompleteResultView() { |
124 } | 126 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
166 colors[i][BACKGROUND]); | 168 colors[i][BACKGROUND]); |
167 } | 169 } |
168 initialized = true; | 170 initialized = true; |
169 } | 171 } |
170 | 172 |
171 return colors[state][kind]; | 173 return colors[state][kind]; |
172 } | 174 } |
173 | 175 |
174 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { | 176 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { |
175 match_ = match; | 177 match_ = match; |
178 animation_->Reset(); | |
179 | |
180 if (match.associated_keyword.get()) { | |
Peter Kasting
2012/01/12 22:59:59
There is a memory safety issue here, as well as a
aaron.randolph
2012/01/13 00:03:48
I'm not following how this eliminates the Invalida
Peter Kasting
2012/01/13 00:36:41
You're right, I missed the normal vs. selected par
| |
181 keyword_icon_.reset(new views::ImageView()); | |
182 keyword_icon_->EnableCanvasFlippingForRTLUI(true); | |
183 keyword_icon_->SetImage(GetKeywordIcon()); | |
184 AddChildView(keyword_icon_.get()); | |
185 } else if (keyword_icon_.get()) { | |
186 RemoveChildView(keyword_icon_.get()); | |
187 keyword_icon_.reset(); | |
188 } | |
189 | |
176 Layout(); | 190 Layout(); |
177 } | 191 } |
178 | 192 |
193 void AutocompleteResultView::ShowKeyword(bool show_keyword) { | |
194 if (show_keyword) | |
195 animation_->Show(); | |
196 else | |
197 animation_->Hide(); | |
198 } | |
199 | |
200 void AutocompleteResultView::Invalidate() { | |
201 if (keyword_icon_.get()) | |
202 keyword_icon_->SetImage(GetKeywordIcon()); | |
203 | |
204 SchedulePaint(); | |
205 } | |
206 | |
207 gfx::Size AutocompleteResultView::GetPreferredSize() { | |
208 return gfx::Size(0, std::max( | |
209 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
210 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | |
211 } | |
212 | |
179 //////////////////////////////////////////////////////////////////////////////// | 213 //////////////////////////////////////////////////////////////////////////////// |
180 // AutocompleteResultView, protected: | 214 // AutocompleteResultView, protected: |
181 | 215 |
182 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | 216 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
183 const AutocompleteMatch& match, | 217 const AutocompleteMatch& match, |
184 int x) { | 218 int x) { |
185 x = DrawString(canvas, match.contents, match.contents_class, false, x, | 219 x = DrawString(canvas, match.contents, match.contents_class, false, x, |
186 text_bounds_.y()); | 220 text_bounds_.y()); |
187 | 221 |
188 // Paint the description. | 222 // Paint the description. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 icon = IDR_OMNIBOX_STAR_SELECTED; | 290 icon = IDR_OMNIBOX_STAR_SELECTED; |
257 break; | 291 break; |
258 default: | 292 default: |
259 NOTREACHED(); | 293 NOTREACHED(); |
260 break; | 294 break; |
261 } | 295 } |
262 } | 296 } |
263 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); | 297 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); |
264 } | 298 } |
265 | 299 |
300 const SkBitmap* AutocompleteResultView::GetKeywordIcon() const { | |
301 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
302 model_->IsSelectedIndex(model_index_) ? IDR_OMNIBOX_TTS_SELECTED : | |
Peter Kasting
2012/01/12 22:59:59
Nit: Break after '?' rather than ':'
Peter Kasting
2012/01/13 00:36:41
Nit: (GetState() == SELECTED) seems fractionally c
| |
303 IDR_OMNIBOX_TTS); | |
304 } | |
305 | |
266 int AutocompleteResultView::DrawString( | 306 int AutocompleteResultView::DrawString( |
267 gfx::Canvas* canvas, | 307 gfx::Canvas* canvas, |
268 const string16& text, | 308 const string16& text, |
269 const ACMatchClassifications& classifications, | 309 const ACMatchClassifications& classifications, |
270 bool force_dim, | 310 bool force_dim, |
271 int x, | 311 int x, |
272 int y) { | 312 int y) { |
273 if (text.empty()) | 313 if (text.empty()) |
274 return x; | 314 return x; |
275 | 315 |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
486 // ellipsis we'll append to it, or | 526 // ellipsis we'll append to it, or |
487 // * It is also bold, in which case we don't want to fall back | 527 // * It is also bold, in which case we don't want to fall back |
488 // to a normal ellipsis anyway (see comment above). | 528 // to a normal ellipsis anyway (see comment above). |
489 } | 529 } |
490 } | 530 } |
491 | 531 |
492 // We couldn't draw anything. | 532 // We couldn't draw anything. |
493 runs->clear(); | 533 runs->clear(); |
494 } | 534 } |
495 | 535 |
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() { | 536 void AutocompleteResultView::Layout() { |
503 const SkBitmap* icon = GetIcon(); | 537 const SkBitmap* icon = GetIcon(); |
538 | |
504 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + | 539 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + |
505 ((icon->width() == default_icon_size_) ? | 540 ((icon->width() == default_icon_size_) ? |
506 0 : LocationBarView::kIconInternalPadding), | 541 0 : LocationBarView::kIconInternalPadding), |
507 (height() - icon->height()) / 2, icon->width(), icon->height()); | 542 (height() - icon->height()) / 2, icon->width(), icon->height()); |
508 | 543 |
509 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + | 544 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + |
510 LocationBarView::kItemPadding; | 545 LocationBarView::kItemPadding; |
511 int text_height = GetTextHeight(); | 546 int text_height = GetTextHeight(); |
547 int text_width; | |
548 | |
549 if (match_.associated_keyword.get()) { | |
550 const SkBitmap* kw_icon = GetKeywordIcon(); | |
Peter Kasting
2012/01/13 00:36:41
Nit: Rather than calling GetKeywordIcon() here, it
aaron.randolph
2012/01/13 03:08:07
Actually, it doesn't appear that ImageView sets th
| |
551 const int kw_collapsed_size = kw_icon->width() + | |
552 LocationBarView::kEdgeItemPadding; | |
553 const int max_kw_x = bounds().width() - kw_collapsed_size; | |
554 const int kw_x = animation_->CurrentValueBetween(max_kw_x, | |
555 LocationBarView::kEdgeItemPadding); | |
556 const int kw_text_x = kw_x + kw_icon->width() + | |
557 LocationBarView::kItemPadding; | |
558 | |
559 text_width = kw_x - text_x - LocationBarView::kItemPadding; | |
560 keyword_text_bounds_.SetRect(kw_text_x, 0, std::max( | |
561 bounds().width() - kw_text_x - LocationBarView::kEdgeItemPadding, 0), | |
562 text_height); | |
563 keyword_icon_->SetBounds(kw_x, (height() - kw_icon->height()) / 2, | |
Peter Kasting
2012/01/13 00:36:41
Nit: This can just be SetPosition().
| |
564 kw_icon->width(), kw_icon->height()); | |
565 } else { | |
566 text_width = bounds().width() - text_x - LocationBarView::kEdgeItemPadding; | |
567 } | |
568 | |
512 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), | 569 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), |
513 std::max(bounds().width() - text_x - LocationBarView::kEdgeItemPadding, | 570 std::max(text_width, 0), text_height); |
514 0), text_height); | 571 } |
572 | |
573 void AutocompleteResultView::OnBoundsChanged( | |
574 const gfx::Rect& previous_bounds) { | |
575 animation_->SetSlideDuration(width() / 4); | |
515 } | 576 } |
516 | 577 |
517 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { | 578 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { |
518 const ResultViewState state = GetState(); | 579 const ResultViewState state = GetState(); |
519 if (state != NORMAL) | 580 if (state != NORMAL) |
520 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); | 581 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); |
521 | 582 |
522 // Paint the icon. | 583 if (!match_.associated_keyword.get() || |
523 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | 584 keyword_icon_->x() > icon_bounds_.right()) { |
524 icon_bounds_.y()); | 585 // Paint the icon. |
586 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | |
587 icon_bounds_.y()); | |
525 | 588 |
526 // Paint the text. | 589 // Paint the text. |
527 int x = GetMirroredXForRect(text_bounds_); | 590 int x = GetMirroredXForRect(text_bounds_); |
528 mirroring_context_->Initialize(x, text_bounds_.width()); | 591 mirroring_context_->Initialize(x, text_bounds_.width()); |
529 PaintMatch(canvas, match_, x); | 592 PaintMatch(canvas, match_, x); |
593 } | |
594 | |
595 if (match_.associated_keyword.get()) { | |
596 // Paint the keyword text. | |
597 int x = GetMirroredXForRect(keyword_text_bounds_); | |
598 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | |
599 PaintMatch(canvas, *match_.associated_keyword.get(), x); | |
600 } | |
530 } | 601 } |
602 | |
603 void AutocompleteResultView::AnimationProgressed( | |
604 const ui::Animation* animation) { | |
605 Layout(); | |
606 SchedulePaint(); | |
607 } | |
608 | |
OLD | NEW |