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 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
109 AutocompleteResultView::AutocompleteResultView( | 110 AutocompleteResultView::AutocompleteResultView( |
110 AutocompleteResultViewModel* model, | 111 AutocompleteResultViewModel* model, |
111 int model_index, | 112 int model_index, |
112 const gfx::Font& font, | 113 const gfx::Font& font, |
113 const gfx::Font& bold_font) | 114 const gfx::Font& bold_font) |
114 : model_(model), | 115 : model_(model), |
115 model_index_(model_index), | 116 model_index_(model_index), |
116 normal_font_(font), | 117 normal_font_(font), |
117 bold_font_(bold_font), | 118 bold_font_(bold_font), |
118 ellipsis_width_(font.GetStringWidth(string16(kEllipsis))), | 119 ellipsis_width_(font.GetStringWidth(string16(kEllipsis))), |
119 mirroring_context_(new MirroringContext()) { | 120 mirroring_context_(new MirroringContext()), |
121 ALLOW_THIS_IN_INITIALIZER_LIST(animation_(new ui::SlideAnimation(this))) { | |
120 CHECK_GE(model_index, 0); | 122 CHECK_GE(model_index, 0); |
121 if (default_icon_size_ == 0) { | 123 if (default_icon_size_ == 0) { |
122 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( | 124 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( |
123 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> | 125 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> |
124 width(); | 126 width(); |
125 } | 127 } |
128 | |
129 animation_->SetDuration(500); | |
126 } | 130 } |
127 | 131 |
128 AutocompleteResultView::~AutocompleteResultView() { | 132 AutocompleteResultView::~AutocompleteResultView() { |
129 } | 133 } |
130 | 134 |
131 // static | 135 // static |
132 SkColor AutocompleteResultView::GetColor(ResultViewState state, | 136 SkColor AutocompleteResultView::GetColor(ResultViewState state, |
133 ColorKind kind) { | 137 ColorKind kind) { |
134 static bool initialized = false; | 138 static bool initialized = false; |
135 static SkColor colors[NUM_STATES][NUM_KINDS]; | 139 static SkColor colors[NUM_STATES][NUM_KINDS]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 colors[i][BACKGROUND]); | 175 colors[i][BACKGROUND]); |
172 } | 176 } |
173 initialized = true; | 177 initialized = true; |
174 } | 178 } |
175 | 179 |
176 return colors[state][kind]; | 180 return colors[state][kind]; |
177 } | 181 } |
178 | 182 |
179 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { | 183 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { |
180 match_ = match; | 184 match_ = match; |
185 animation_->Reset(); | |
186 | |
187 if (!match.associated_keyword.get() && keyword_button_.get()) { | |
188 RemoveChildView(keyword_button_.get()); | |
189 keyword_button_.reset(NULL); | |
190 } else if (match.associated_keyword.get() && !keyword_button_.get()) { | |
191 ResourceBundle& resources = ResourceBundle::GetSharedInstance(); | |
192 keyword_button_.reset(new views::ImageButton(this)); | |
Peter Kasting
2011/12/15 22:56:04
After talking with Glen some more we agreed that i
| |
193 keyword_button_->set_id(VIEW_ID_KEYWORD); | |
194 keyword_button_->SetTooltipText(match_.associated_keyword->description); | |
195 keyword_button_->SetImage(views::CustomButton::BS_NORMAL, | |
196 resources.GetBitmapNamed(IDR_OMNIBOX_KEYWORD_SEARCH)); | |
197 keyword_button_->SetImage(views::CustomButton::BS_HOT, | |
198 resources.GetBitmapNamed(IDR_OMNIBOX_KEYWORD_SEARCH_H)); | |
199 keyword_button_->SetImage(views::CustomButton::BS_PUSHED, | |
200 resources.GetBitmapNamed(IDR_OMNIBOX_KEYWORD_SEARCH_P)); | |
201 AddChildView(keyword_button_.get()); | |
202 } | |
203 | |
181 Layout(); | 204 Layout(); |
182 } | 205 } |
183 | 206 |
207 void AutocompleteResultView::ShowKeyword(bool show_keyword) { | |
208 if (show_keyword) { | |
209 keyword_button_->SetTooltipText(match_.description); | |
210 animation_->Show(); | |
211 } else { | |
212 keyword_button_->SetTooltipText(match_.associated_keyword->description); | |
213 animation_->Hide(); | |
214 } | |
215 } | |
216 | |
217 gfx::Size AutocompleteResultView::GetPreferredSize() { | |
218 return gfx::Size(0, std::max( | |
219 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
220 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | |
221 } | |
222 | |
184 //////////////////////////////////////////////////////////////////////////////// | 223 //////////////////////////////////////////////////////////////////////////////// |
185 // AutocompleteResultView, protected: | 224 // AutocompleteResultView, protected: |
186 | 225 |
187 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, | 226 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, |
188 const AutocompleteMatch& match, | 227 const AutocompleteMatch& match, |
189 int x) { | 228 int x) { |
190 x = DrawString(canvas, match.contents, match.contents_class, false, x, | 229 x = DrawString(canvas, match.contents, match.contents_class, false, x, |
191 text_bounds_.y()); | 230 text_bounds_.y()); |
192 | 231 |
193 // Paint the description. | 232 // Paint the description. |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 // ellipsis we'll append to it, or | 530 // ellipsis we'll append to it, or |
492 // * 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 |
493 // to a normal ellipsis anyway (see comment above). | 532 // to a normal ellipsis anyway (see comment above). |
494 } | 533 } |
495 } | 534 } |
496 | 535 |
497 // We couldn't draw anything. | 536 // We couldn't draw anything. |
498 runs->clear(); | 537 runs->clear(); |
499 } | 538 } |
500 | 539 |
501 gfx::Size AutocompleteResultView::GetPreferredSize() { | |
502 return gfx::Size(0, std::max( | |
503 default_icon_size_ + (kMinimumIconVerticalPadding * 2), | |
504 GetTextHeight() + (kMinimumTextVerticalPadding * 2))); | |
505 } | |
506 | |
507 void AutocompleteResultView::Layout() { | 540 void AutocompleteResultView::Layout() { |
508 const SkBitmap* icon = GetIcon(); | 541 const SkBitmap* icon = GetIcon(); |
509 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + | 542 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + |
510 ((icon->width() == default_icon_size_) ? | 543 ((icon->width() == default_icon_size_) ? |
511 0 : LocationBarView::kIconInternalPadding), | 544 0 : LocationBarView::kIconInternalPadding), |
512 (height() - icon->height()) / 2, icon->width(), icon->height()); | 545 (height() - icon->height()) / 2, icon->width(), icon->height()); |
513 | 546 |
514 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + | 547 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + |
515 LocationBarView::kItemPadding; | 548 LocationBarView::kItemPadding; |
516 int text_height = GetTextHeight(); | 549 int text_height = GetTextHeight(); |
550 int text_width; | |
551 | |
552 if (match_.associated_keyword.get()) { | |
553 gfx::Size kw_icon_size = keyword_button_->GetPreferredSize(); | |
554 const int kw_collapsed_size = kw_icon_size.width() + | |
555 LocationBarView::kEdgeItemPadding; | |
556 const int max_kw_x = bounds().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 + kw_icon_size.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 bounds().width() - kw_text_x - LocationBarView::kEdgeItemPadding, 0), | |
565 text_height); | |
566 keyword_button_->SetBounds(GetMirroredXInView(kw_x), | |
567 (height() - kw_icon_size.height()) / 2, | |
568 kw_icon_size.width(), kw_icon_size.height()); | |
569 } else { | |
570 text_width = bounds().width() - text_x - LocationBarView::kEdgeItemPadding; | |
571 } | |
572 | |
517 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), | 573 text_bounds_.SetRect(text_x, std::max(0, (height() - text_height) / 2), |
518 std::max(bounds().width() - text_x - LocationBarView::kEdgeItemPadding, | 574 std::max(text_width, 0), text_height); |
519 0), text_height); | |
520 } | 575 } |
521 | 576 |
522 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { | 577 void AutocompleteResultView::OnPaint(gfx::Canvas* canvas) { |
523 const ResultViewState state = GetState(); | 578 const ResultViewState state = GetState(); |
524 if (state != NORMAL) | 579 if (state != NORMAL) |
525 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); | 580 canvas->GetSkCanvas()->drawColor(GetColor(state, BACKGROUND)); |
526 | 581 |
527 // Paint the icon. | 582 if (!keyword_button_.get() || keyword_button_->x() > icon_bounds_.right()) { |
528 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), | 583 // Paint the icon. |
529 icon_bounds_.y()); | 584 canvas->DrawBitmapInt(*GetIcon(), GetMirroredXForRect(icon_bounds_), |
585 icon_bounds_.y()); | |
530 | 586 |
531 // Paint the text. | 587 // Paint the text. |
532 int x = GetMirroredXForRect(text_bounds_); | 588 int x = GetMirroredXForRect(text_bounds_); |
533 mirroring_context_->Initialize(x, text_bounds_.width()); | 589 mirroring_context_->Initialize(x, text_bounds_.width()); |
534 PaintMatch(canvas, match_, x); | 590 PaintMatch(canvas, match_, x); |
591 } | |
592 | |
593 // Paint the keyword text | |
594 if (match_.associated_keyword.get()) { | |
595 int x = GetMirroredXForRect(keyword_text_bounds_); | |
596 mirroring_context_->Initialize(x, keyword_text_bounds_.width()); | |
597 PaintMatch(canvas, *match_.associated_keyword.get(), x); | |
598 } | |
535 } | 599 } |
600 | |
601 void AutocompleteResultView::AnimationProgressed( | |
602 const ui::Animation* animation) { | |
603 Layout(); | |
604 SchedulePaint(); | |
605 } | |
606 | |
607 void AutocompleteResultView::ButtonPressed(views::Button* sender, | |
608 const views::Event& event) { | |
609 popup_model_->SetSelectedLine(model_index_, false, false); | |
610 | |
611 if (popup_model_->selected_line_state() == AutocompletePopupModel::NORMAL) | |
612 popup_model_->edit_model()->AcceptKeyword(); | |
613 else | |
614 popup_model_->edit_model()->ClearKeyword( | |
Peter Kasting
2011/12/15 22:56:04
Nit: If one arm of a conditional spans more than o
| |
615 popup_model_->edit_model()->view()->GetText()); | |
616 } | |
617 | |
OLD | NEW |