Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(683)

Side by Side Diff: chrome/browser/ui/views/autocomplete/autocomplete_result_view.cc

Issue 6731036: Enabled pressing TAB to cycle through the Omnibox results. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: '' Created 8 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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))),
117 ALLOW_THIS_IN_INITIALIZER_LIST(keyword_icon_(new views::ImageView())) {
115 CHECK_GE(model_index, 0); 118 CHECK_GE(model_index, 0);
116 if (default_icon_size_ == 0) { 119 if (default_icon_size_ == 0) {
117 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed( 120 default_icon_size_ = ResourceBundle::GetSharedInstance().GetBitmapNamed(
118 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))-> 121 AutocompleteMatch::TypeToIcon(AutocompleteMatch::URL_WHAT_YOU_TYPED))->
119 width(); 122 width();
120 } 123 }
124 keyword_icon_->set_parent_owned(false);
125 keyword_icon_->EnableCanvasFlippingForRTLUI(true);
126 keyword_icon_->SetImage(GetKeywordIcon());
127 keyword_icon_->SetSize(keyword_icon_->GetPreferredSize());
121 } 128 }
122 129
123 AutocompleteResultView::~AutocompleteResultView() { 130 AutocompleteResultView::~AutocompleteResultView() {
124 } 131 }
125 132
126 // static 133 // static
127 SkColor AutocompleteResultView::GetColor(ResultViewState state, 134 SkColor AutocompleteResultView::GetColor(ResultViewState state,
128 ColorKind kind) { 135 ColorKind kind) {
129 static bool initialized = false; 136 static bool initialized = false;
130 static SkColor colors[NUM_STATES][NUM_KINDS]; 137 static SkColor colors[NUM_STATES][NUM_KINDS];
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 colors[i][BACKGROUND]); 173 colors[i][BACKGROUND]);
167 } 174 }
168 initialized = true; 175 initialized = true;
169 } 176 }
170 177
171 return colors[state][kind]; 178 return colors[state][kind];
172 } 179 }
173 180
174 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) { 181 void AutocompleteResultView::SetMatch(const AutocompleteMatch& match) {
175 match_ = match; 182 match_ = match;
183 animation_->Reset();
184
185 if (match.associated_keyword.get()) {
186 keyword_icon_->SetImage(GetKeywordIcon());
187
188 if (!keyword_icon_->parent())
189 AddChildView(keyword_icon_.get());
190 } else if (keyword_icon_->parent()) {
191 RemoveChildView(keyword_icon_.get());
192 }
193
176 Layout(); 194 Layout();
177 } 195 }
178 196
197 void AutocompleteResultView::ShowKeyword(bool show_keyword) {
198 if (show_keyword)
199 animation_->Show();
200 else
201 animation_->Hide();
202 }
203
204 void AutocompleteResultView::Invalidate() {
205 keyword_icon_->SetImage(GetKeywordIcon());
206 SchedulePaint();
207 }
208
209 gfx::Size AutocompleteResultView::GetPreferredSize() {
210 return gfx::Size(0, std::max(
211 default_icon_size_ + (kMinimumIconVerticalPadding * 2),
212 GetTextHeight() + (kMinimumTextVerticalPadding * 2)));
213 }
214
179 //////////////////////////////////////////////////////////////////////////////// 215 ////////////////////////////////////////////////////////////////////////////////
180 // AutocompleteResultView, protected: 216 // AutocompleteResultView, protected:
181 217
182 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas, 218 void AutocompleteResultView::PaintMatch(gfx::Canvas* canvas,
183 const AutocompleteMatch& match, 219 const AutocompleteMatch& match,
184 int x) { 220 int x) {
185 x = DrawString(canvas, match.contents, match.contents_class, false, x, 221 x = DrawString(canvas, match.contents, match.contents_class, false, x,
186 text_bounds_.y()); 222 text_bounds_.y());
187 223
188 // Paint the description. 224 // Paint the description.
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL; 267 return model_->IsHoveredIndex(model_index_) ? HOVERED : NORMAL;
232 } 268 }
233 269
234 const SkBitmap* AutocompleteResultView::GetIcon() const { 270 const SkBitmap* AutocompleteResultView::GetIcon() const {
235 const SkBitmap* bitmap = model_->GetIconIfExtensionMatch(model_index_); 271 const SkBitmap* bitmap = model_->GetIconIfExtensionMatch(model_index_);
236 if (bitmap) 272 if (bitmap)
237 return bitmap; 273 return bitmap;
238 274
239 int icon = match_.starred ? 275 int icon = match_.starred ?
240 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type); 276 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type);
241 if (model_->IsSelectedIndex(model_index_)) { 277 if (GetState() == SELECTED) {
242 switch (icon) { 278 switch (icon) {
243 case IDR_OMNIBOX_EXTENSION_APP: 279 case IDR_OMNIBOX_EXTENSION_APP:
244 icon = IDR_OMNIBOX_EXTENSION_APP_SELECTED; 280 icon = IDR_OMNIBOX_EXTENSION_APP_SELECTED;
245 break; 281 break;
246 case IDR_OMNIBOX_HTTP: 282 case IDR_OMNIBOX_HTTP:
247 icon = IDR_OMNIBOX_HTTP_SELECTED; 283 icon = IDR_OMNIBOX_HTTP_SELECTED;
248 break; 284 break;
249 case IDR_OMNIBOX_HISTORY: 285 case IDR_OMNIBOX_HISTORY:
250 icon = IDR_OMNIBOX_HISTORY_SELECTED; 286 icon = IDR_OMNIBOX_HISTORY_SELECTED;
251 break; 287 break;
252 case IDR_OMNIBOX_SEARCH: 288 case IDR_OMNIBOX_SEARCH:
253 icon = IDR_OMNIBOX_SEARCH_SELECTED; 289 icon = IDR_OMNIBOX_SEARCH_SELECTED;
254 break; 290 break;
255 case IDR_OMNIBOX_STAR: 291 case IDR_OMNIBOX_STAR:
256 icon = IDR_OMNIBOX_STAR_SELECTED; 292 icon = IDR_OMNIBOX_STAR_SELECTED;
257 break; 293 break;
258 default: 294 default:
259 NOTREACHED(); 295 NOTREACHED();
260 break; 296 break;
261 } 297 }
262 } 298 }
263 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon); 299 return ResourceBundle::GetSharedInstance().GetBitmapNamed(icon);
264 } 300 }
265 301
302 const SkBitmap* AutocompleteResultView::GetKeywordIcon() const {
303 return ResourceBundle::GetSharedInstance().GetBitmapNamed(
304 (GetState() == SELECTED) ? IDR_OMNIBOX_TTS_SELECTED : IDR_OMNIBOX_TTS);
305 }
306
266 int AutocompleteResultView::DrawString( 307 int AutocompleteResultView::DrawString(
267 gfx::Canvas* canvas, 308 gfx::Canvas* canvas,
268 const string16& text, 309 const string16& text,
269 const ACMatchClassifications& classifications, 310 const ACMatchClassifications& classifications,
270 bool force_dim, 311 bool force_dim,
271 int x, 312 int x,
272 int y) { 313 int y) {
273 if (text.empty()) 314 if (text.empty())
274 return x; 315 return x;
275 316
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 // ellipsis we'll append to it, or 527 // ellipsis we'll append to it, or
487 // * It is also bold, in which case we don't want to fall back 528 // * It is also bold, in which case we don't want to fall back
488 // to a normal ellipsis anyway (see comment above). 529 // to a normal ellipsis anyway (see comment above).
489 } 530 }
490 } 531 }
491 532
492 // We couldn't draw anything. 533 // We couldn't draw anything.
493 runs->clear(); 534 runs->clear();
494 } 535 }
495 536
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() { 537 void AutocompleteResultView::Layout() {
503 const SkBitmap* icon = GetIcon(); 538 const SkBitmap* icon = GetIcon();
539
504 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding + 540 icon_bounds_.SetRect(LocationBarView::kEdgeItemPadding +
505 ((icon->width() == default_icon_size_) ? 541 ((icon->width() == default_icon_size_) ?
506 0 : LocationBarView::kIconInternalPadding), 542 0 : LocationBarView::kIconInternalPadding),
507 (height() - icon->height()) / 2, icon->width(), icon->height()); 543 (height() - icon->height()) / 2, icon->width(), icon->height());
508 544
509 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ + 545 int text_x = LocationBarView::kEdgeItemPadding + default_icon_size_ +
510 LocationBarView::kItemPadding; 546 LocationBarView::kItemPadding;
511 int text_height = GetTextHeight(); 547 int text_height = GetTextHeight();
548 int text_width;
549
550 if (match_.associated_keyword.get()) {
551 const int kw_collapsed_size = keyword_icon_->width() +
Peter Kasting 2012/01/13 19:18:19 You're right, the ImageView doesn't actually size
552 LocationBarView::kEdgeItemPadding;
553 const int max_kw_x = bounds().width() - kw_collapsed_size;
Peter Kasting 2012/01/13 19:18:19 Nit: bounds().width() -> width() (3 places)
554 const int kw_x = animation_->CurrentValueBetween(max_kw_x,
555 LocationBarView::kEdgeItemPadding);
556 const int kw_text_x = kw_x + keyword_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_->SetPosition(gfx::Point(kw_x,
564 (height() - keyword_icon_->height()) / 2));
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698