| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" | 5 #include "chrome/browser/ui/views/omnibox/omnibox_popup_contents_view.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" |
| 9 #include "base/macros.h" | 10 #include "base/macros.h" |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 #include "chrome/browser/search/search.h" | 12 #include "chrome/browser/search/search.h" |
| 12 #include "chrome/browser/themes/theme_properties.h" | 13 #include "chrome/browser/themes/theme_properties.h" |
| 13 #include "chrome/browser/ui/layout_constants.h" | 14 #include "chrome/browser/ui/layout_constants.h" |
| 14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 15 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" | 16 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" |
| 16 #include "chrome/browser/ui/views/theme_copying_widget.h" | 17 #include "chrome/browser/ui/views/theme_copying_widget.h" |
| 17 #include "chrome/grit/theme_resources.h" | |
| 18 #include "components/omnibox/browser/omnibox_view.h" | 18 #include "components/omnibox/browser/omnibox_view.h" |
| 19 #include "ui/base/material_design/material_design_controller.h" | 19 #include "third_party/skia/include/core/SkDrawLooper.h" |
| 20 #include "ui/base/resource/resource_bundle.h" | |
| 21 #include "ui/base/theme_provider.h" | 20 #include "ui/base/theme_provider.h" |
| 22 #include "ui/compositor/clip_recorder.h" | 21 #include "ui/compositor/clip_recorder.h" |
| 23 #include "ui/compositor/paint_recorder.h" | 22 #include "ui/compositor/paint_recorder.h" |
| 24 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
| 24 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 25 #include "ui/gfx/image/canvas_image_source.h" |
| 25 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/path.h" | 27 #include "ui/gfx/path.h" |
| 28 #include "ui/gfx/shadow_value.h" |
| 29 #include "ui/gfx/skia_util.h" |
| 27 #include "ui/views/controls/image_view.h" | 30 #include "ui/views/controls/image_view.h" |
| 28 #include "ui/views/resources/grit/views_resources.h" | |
| 29 #include "ui/views/view_targeter.h" | 31 #include "ui/views/view_targeter.h" |
| 30 #include "ui/views/widget/widget.h" | 32 #include "ui/views/widget/widget.h" |
| 31 #include "ui/views/window/non_client_view.h" | 33 #include "ui/views/window/non_client_view.h" |
| 32 | 34 |
| 35 namespace { |
| 36 |
| 37 // This creates a shadow image that is 1dp wide, suitable for tiling along the |
| 38 // top or bottom edge of the omnibox popup. |
| 39 class ShadowImageSource : public gfx::CanvasImageSource { |
| 40 public: |
| 41 ShadowImageSource(const std::vector<gfx::ShadowValue>& shadows, bool bottom) |
| 42 : gfx::CanvasImageSource(gfx::Size(1, GetHeightForShadows(shadows)), |
| 43 false), |
| 44 shadows_(shadows), |
| 45 bottom_(bottom) {} |
| 46 ~ShadowImageSource() override {} |
| 47 |
| 48 void Draw(gfx::Canvas* canvas) override { |
| 49 SkPaint paint; |
| 50 paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows_)); |
| 51 canvas->DrawRect(gfx::RectF(0, bottom_ ? -1 : size().height(), 1, 1), |
| 52 paint); |
| 53 } |
| 54 |
| 55 private: |
| 56 static int GetHeightForShadows(const std::vector<gfx::ShadowValue>& shadows) { |
| 57 int height = 0; |
| 58 for (const auto& shadow : shadows) { |
| 59 height = |
| 60 std::max(height, shadow.y() + gfx::ToCeiledInt(shadow.blur() / 2)); |
| 61 } |
| 62 return height; |
| 63 } |
| 64 |
| 65 const std::vector<gfx::ShadowValue> shadows_; |
| 66 bool bottom_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(ShadowImageSource); |
| 69 }; |
| 70 |
| 71 // Cache the shadow images so that potentially expensive shadow drawing isn't |
| 72 // repeated. |
| 73 base::LazyInstance<gfx::ImageSkia> g_top_shadow = LAZY_INSTANCE_INITIALIZER; |
| 74 base::LazyInstance<gfx::ImageSkia> g_bottom_shadow = LAZY_INSTANCE_INITIALIZER; |
| 75 |
| 76 } // namespace |
| 77 |
| 33 class OmniboxPopupContentsView::AutocompletePopupWidget | 78 class OmniboxPopupContentsView::AutocompletePopupWidget |
| 34 : public ThemeCopyingWidget, | 79 : public ThemeCopyingWidget, |
| 35 public base::SupportsWeakPtr<AutocompletePopupWidget> { | 80 public base::SupportsWeakPtr<AutocompletePopupWidget> { |
| 36 public: | 81 public: |
| 37 explicit AutocompletePopupWidget(views::Widget* role_model) | 82 explicit AutocompletePopupWidget(views::Widget* role_model) |
| 38 : ThemeCopyingWidget(role_model) {} | 83 : ThemeCopyingWidget(role_model) {} |
| 39 ~AutocompletePopupWidget() override {} | 84 ~AutocompletePopupWidget() override {} |
| 40 | 85 |
| 41 private: | 86 private: |
| 42 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); | 87 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); |
| (...skipping 23 matching lines...) Expand all Loading... |
| 66 omnibox_view_(omnibox_view), | 111 omnibox_view_(omnibox_view), |
| 67 location_bar_view_(location_bar_view), | 112 location_bar_view_(location_bar_view), |
| 68 font_list_(font_list), | 113 font_list_(font_list), |
| 69 ignore_mouse_drag_(false), | 114 ignore_mouse_drag_(false), |
| 70 size_animation_(this), | 115 size_animation_(this), |
| 71 start_margin_(0), | 116 start_margin_(0), |
| 72 end_margin_(0) { | 117 end_margin_(0) { |
| 73 // The contents is owned by the LocationBarView. | 118 // The contents is owned by the LocationBarView. |
| 74 set_owned_by_client(); | 119 set_owned_by_client(); |
| 75 | 120 |
| 76 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 121 if (g_top_shadow.Get().isNull()) { |
| 77 if (ui::MaterialDesignController::IsModeMaterial()) { | 122 std::vector<gfx::ShadowValue> shadows; |
| 78 top_shadow_ = rb->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_TOP); | 123 // Blur by 1dp. See comment below about blur accounting. |
| 79 bottom_shadow_ = rb->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_BOTTOM); | 124 shadows.emplace_back(gfx::Vector2d(), 2, SK_ColorBLACK); |
| 80 } else { | 125 |
| 81 bottom_shadow_ = rb->GetImageSkiaNamed(IDR_BUBBLE_B); | 126 auto source = new ShadowImageSource(shadows, false); |
| 127 g_top_shadow.Get() = gfx::ImageSkia(source, source->size()); |
| 128 } |
| 129 if (g_bottom_shadow.Get().isNull()) { |
| 130 const int kSmallShadowBlur = 3; |
| 131 const int kLargeShadowBlur = 8; |
| 132 const int kLargeShadowYOffset = 3; |
| 133 |
| 134 std::vector<gfx::ShadowValue> shadows; |
| 135 // gfx::ShadowValue counts blur pixels both inside and outside the shape, |
| 136 // whereas these blur values only describe the outside portion, hence they |
| 137 // must be doubled. |
| 138 shadows.emplace_back(gfx::Vector2d(), 2 * kSmallShadowBlur, |
| 139 SK_ColorBLACK); |
| 140 shadows.emplace_back(gfx::Vector2d(0, kLargeShadowYOffset), |
| 141 2 * kLargeShadowBlur, SK_ColorBLACK); |
| 142 |
| 143 auto source = new ShadowImageSource(shadows, true); |
| 144 g_bottom_shadow.Get() = gfx::ImageSkia(source, source->size()); |
| 82 } | 145 } |
| 83 | 146 |
| 84 SetEventTargeter( | 147 SetEventTargeter( |
| 85 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); | 148 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); |
| 86 } | 149 } |
| 87 | 150 |
| 88 void OmniboxPopupContentsView::Init() { | 151 void OmniboxPopupContentsView::Init() { |
| 89 // This can't be done in the constructor as at that point we aren't | 152 // This can't be done in the constructor as at that point we aren't |
| 90 // necessarily our final class yet, and we may have subclasses | 153 // necessarily our final class yet, and we may have subclasses |
| 91 // overriding CreateResultView. | 154 // overriding CreateResultView. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 114 int current_height_delta = static_cast<int>( | 177 int current_height_delta = static_cast<int>( |
| 115 size_animation_.GetCurrentValue() * total_height_delta - 0.5); | 178 size_animation_.GetCurrentValue() * total_height_delta - 0.5); |
| 116 current_frame_bounds.set_height( | 179 current_frame_bounds.set_height( |
| 117 current_frame_bounds.height() + current_height_delta); | 180 current_frame_bounds.height() + current_height_delta); |
| 118 return current_frame_bounds; | 181 return current_frame_bounds; |
| 119 } | 182 } |
| 120 | 183 |
| 121 void OmniboxPopupContentsView::LayoutChildren() { | 184 void OmniboxPopupContentsView::LayoutChildren() { |
| 122 gfx::Rect contents_rect = GetContentsBounds(); | 185 gfx::Rect contents_rect = GetContentsBounds(); |
| 123 contents_rect.Inset(GetLayoutInsets(OMNIBOX_DROPDOWN)); | 186 contents_rect.Inset(GetLayoutInsets(OMNIBOX_DROPDOWN)); |
| 124 contents_rect.Inset(start_margin_, | 187 contents_rect.Inset(start_margin_, g_top_shadow.Get().height(), end_margin_, |
| 125 views::NonClientFrameView::kClientEdgeThickness, | 188 0); |
| 126 end_margin_, 0); | |
| 127 | 189 |
| 128 int top = contents_rect.y(); | 190 int top = contents_rect.y(); |
| 129 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { | 191 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { |
| 130 View* v = child_at(i); | 192 View* v = child_at(i); |
| 131 if (v->visible()) { | 193 if (v->visible()) { |
| 132 v->SetBounds(contents_rect.x(), top, contents_rect.width(), | 194 v->SetBounds(contents_rect.x(), top, contents_rect.width(), |
| 133 v->GetPreferredSize().height()); | 195 v->GetPreferredSize().height()); |
| 134 top = v->bounds().bottom(); | 196 top = v->bounds().bottom(); |
| 135 } | 197 } |
| 136 } | 198 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 190 } | 252 } |
| 191 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | 253 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { |
| 192 max_match_contents_width_ = std::max( | 254 max_match_contents_width_ = std::max( |
| 193 max_match_contents_width_, view->GetMatchContentsWidth()); | 255 max_match_contents_width_, view->GetMatchContentsWidth()); |
| 194 } | 256 } |
| 195 } | 257 } |
| 196 | 258 |
| 197 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) | 259 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) |
| 198 child_at(i)->SetVisible(false); | 260 child_at(i)->SetVisible(false); |
| 199 | 261 |
| 200 // In non-material mode, we want the popup to appear as if it's overlaying | 262 // We want the popup to appear to overlay the bottom of the toolbar. So we |
| 201 // the top of the page content, i.e., is flush against the client edge at the | 263 // shift the popup to completely cover the client edge, and then draw an |
| 202 // bottom of the toolbar. However, if the bookmarks bar is attached, we want | 264 // additional semitransparent shadow above that. |
| 203 // to draw over it (so as not to push the results below it), but that means | 265 int top_edge_overlap = views::NonClientFrameView::kClientEdgeThickness + |
| 204 // the toolbar won't be drawing a client edge separating itself from the | 266 g_top_shadow.Get().height(); |
| 205 // popup. So we unconditionally overlap the toolbar by the thickness of the | |
| 206 // client edge and draw our own edge (see OnPaint()), which fixes the | |
| 207 // attached bookmark bar case without breaking the other case. | |
| 208 int top_edge_overlap = views::NonClientFrameView::kClientEdgeThickness; | |
| 209 if (ui::MaterialDesignController::IsModeMaterial()) { | |
| 210 // In material mode, we cover the bookmark bar similarly, but instead of | |
| 211 // appearing below the client edge, we want the popup to appear to overlay | |
| 212 // the bottom of the toolbar. So instead of drawing a client edge atop the | |
| 213 // popup, we shift the popup to completely cover the client edge, and then | |
| 214 // draw an additional semitransparent shadow above that. So the total | |
| 215 // overlap necessary is the client edge thickness plus the shadow height. | |
| 216 top_edge_overlap += top_shadow_->height(); | |
| 217 } | |
| 218 | 267 |
| 219 gfx::Point top_left_screen_coord; | 268 gfx::Point top_left_screen_coord; |
| 220 int width; | 269 int width; |
| 221 location_bar_view_->GetOmniboxPopupPositioningInfo( | 270 location_bar_view_->GetOmniboxPopupPositioningInfo( |
| 222 &top_left_screen_coord, &width, &start_margin_, | 271 &top_left_screen_coord, &width, &start_margin_, |
| 223 &end_margin_, top_edge_overlap); | 272 &end_margin_, top_edge_overlap); |
| 224 gfx::Rect new_target_bounds(top_left_screen_coord, | 273 gfx::Rect new_target_bounds(top_left_screen_coord, |
| 225 gfx::Size(width, CalculatePopupHeight())); | 274 gfx::Size(width, CalculatePopupHeight())); |
| 226 | 275 |
| 227 // If we're animating and our target height changes, reset the animation. | 276 // If we're animating and our target height changes, reset the animation. |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 | 463 |
| 415 int OmniboxPopupContentsView::CalculatePopupHeight() { | 464 int OmniboxPopupContentsView::CalculatePopupHeight() { |
| 416 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); | 465 DCHECK_GE(static_cast<size_t>(child_count()), model_->result().size()); |
| 417 int popup_height = 0; | 466 int popup_height = 0; |
| 418 for (size_t i = 0; i < model_->result().size(); ++i) | 467 for (size_t i = 0; i < model_->result().size(); ++i) |
| 419 popup_height += child_at(i)->GetPreferredSize().height(); | 468 popup_height += child_at(i)->GetPreferredSize().height(); |
| 420 | 469 |
| 421 // Add enough space on the top and bottom so it looks like there is the same | 470 // Add enough space on the top and bottom so it looks like there is the same |
| 422 // amount of space between the text and the popup border as there is in the | 471 // amount of space between the text and the popup border as there is in the |
| 423 // interior between each row of text. | 472 // interior between each row of text. |
| 424 return popup_height + views::NonClientFrameView::kClientEdgeThickness + | 473 return popup_height + GetLayoutInsets(OMNIBOX_DROPDOWN).height() + |
| 425 GetLayoutInsets(OMNIBOX_DROPDOWN).height() + | 474 g_top_shadow.Get().height() + g_bottom_shadow.Get().height(); |
| 426 bottom_shadow_->height() - | |
| 427 GetLayoutConstant(OMNIBOX_DROPDOWN_BORDER_INTERIOR); | |
| 428 } | 475 } |
| 429 | 476 |
| 430 OmniboxResultView* OmniboxPopupContentsView::CreateResultView( | 477 OmniboxResultView* OmniboxPopupContentsView::CreateResultView( |
| 431 int model_index, | 478 int model_index, |
| 432 const gfx::FontList& font_list) { | 479 const gfx::FontList& font_list) { |
| 433 return new OmniboxResultView(this, model_index, location_bar_view_, | 480 return new OmniboxResultView(this, model_index, location_bar_view_, |
| 434 font_list); | 481 font_list); |
| 435 } | 482 } |
| 436 | 483 |
| 437 //////////////////////////////////////////////////////////////////////////////// | 484 //////////////////////////////////////////////////////////////////////////////// |
| 438 // OmniboxPopupContentsView, views::View overrides, private: | 485 // OmniboxPopupContentsView, views::View overrides, private: |
| 439 | 486 |
| 440 const char* OmniboxPopupContentsView::GetClassName() const { | 487 const char* OmniboxPopupContentsView::GetClassName() const { |
| 441 return "OmniboxPopupContentsView"; | 488 return "OmniboxPopupContentsView"; |
| 442 } | 489 } |
| 443 | 490 |
| 444 void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) { | 491 void OmniboxPopupContentsView::OnPaint(gfx::Canvas* canvas) { |
| 445 // Top border. | 492 canvas->TileImageInt(g_top_shadow.Get(), 0, 0, width(), |
| 446 if (ui::MaterialDesignController::IsModeMaterial()) { | 493 g_top_shadow.Get().height()); |
| 447 canvas->TileImageInt(*top_shadow_, 0, 0, width(), top_shadow_->height()); | 494 canvas->TileImageInt(g_bottom_shadow.Get(), 0, |
| 448 } else { | 495 height() - g_bottom_shadow.Get().height(), width(), |
| 449 canvas->FillRect(gfx::Rect(0, 0, width(), | 496 g_bottom_shadow.Get().height()); |
| 450 views::NonClientFrameView::kClientEdgeThickness), | |
| 451 location_bar_view_->GetThemeProvider()->GetColor( | |
| 452 ThemeProperties::COLOR_TOOLBAR_BOTTOM_SEPARATOR)); | |
| 453 } | |
| 454 | |
| 455 // Bottom border. | |
| 456 canvas->TileImageInt(*bottom_shadow_, 0, height() - bottom_shadow_->height(), | |
| 457 width(), bottom_shadow_->height()); | |
| 458 } | 497 } |
| 459 | 498 |
| 460 void OmniboxPopupContentsView::PaintChildren(const ui::PaintContext& context) { | 499 void OmniboxPopupContentsView::PaintChildren(const ui::PaintContext& context) { |
| 461 gfx::Rect contents_bounds = GetContentsBounds(); | 500 gfx::Rect contents_bounds = GetContentsBounds(); |
| 462 const int interior = GetLayoutConstant(OMNIBOX_DROPDOWN_BORDER_INTERIOR); | 501 contents_bounds.Inset(0, g_top_shadow.Get().height(), 0, |
| 463 contents_bounds.Inset(0, views::NonClientFrameView::kClientEdgeThickness, 0, | 502 g_bottom_shadow.Get().height()); |
| 464 bottom_shadow_->height() - interior); | |
| 465 | 503 |
| 466 ui::ClipRecorder clip_recorder(context); | 504 ui::ClipRecorder clip_recorder(context); |
| 467 clip_recorder.ClipRect(contents_bounds); | 505 clip_recorder.ClipRect(contents_bounds); |
| 468 { | 506 { |
| 469 ui::PaintRecorder recorder(context, size()); | 507 ui::PaintRecorder recorder(context, size()); |
| 470 SkColor background_color = result_view_at(0)->GetColor( | 508 SkColor background_color = result_view_at(0)->GetColor( |
| 471 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND); | 509 OmniboxResultView::NORMAL, OmniboxResultView::BACKGROUND); |
| 472 recorder.canvas()->DrawColor(background_color); | 510 recorder.canvas()->DrawColor(background_color); |
| 473 } | 511 } |
| 474 View::PaintChildren(context); | 512 View::PaintChildren(context); |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 524 size_t index = GetIndexForPoint(event.location()); | 562 size_t index = GetIndexForPoint(event.location()); |
| 525 if (!HasMatchAt(index)) | 563 if (!HasMatchAt(index)) |
| 526 return; | 564 return; |
| 527 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition, | 565 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition, |
| 528 GURL(), base::string16(), index); | 566 GURL(), base::string16(), index); |
| 529 } | 567 } |
| 530 | 568 |
| 531 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { | 569 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { |
| 532 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); | 570 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); |
| 533 } | 571 } |
| OLD | NEW |