| 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/lazy_instance.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "chrome/browser/search/search.h" | 12 #include "chrome/browser/search/search.h" |
| 13 #include "chrome/browser/themes/theme_properties.h" | 13 #include "chrome/browser/themes/theme_properties.h" |
| 14 #include "chrome/browser/ui/layout_constants.h" | 14 #include "chrome/browser/ui/layout_constants.h" |
| 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" | 15 #include "chrome/browser/ui/views/location_bar/location_bar_view.h" |
| 16 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" | 16 #include "chrome/browser/ui/views/omnibox/omnibox_result_view.h" |
| 17 #include "chrome/browser/ui/views/theme_copying_widget.h" | 17 #include "chrome/browser/ui/views/theme_copying_widget.h" |
| 18 #include "components/omnibox/browser/omnibox_view.h" | 18 #include "components/omnibox/browser/omnibox_view.h" |
| 19 #include "third_party/skia/include/core/SkDrawLooper.h" | 19 #include "third_party/skia/include/core/SkDrawLooper.h" |
| 20 #include "ui/base/theme_provider.h" | 20 #include "ui/base/theme_provider.h" |
| 21 #include "ui/compositor/clip_recorder.h" | 21 #include "ui/compositor/clip_recorder.h" |
| 22 #include "ui/compositor/paint_recorder.h" | 22 #include "ui/compositor/paint_recorder.h" |
| 23 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
| 24 #include "ui/gfx/geometry/safe_integer_conversions.h" | 24 #include "ui/gfx/geometry/safe_integer_conversions.h" |
| 25 #include "ui/gfx/image/canvas_image_source.h" | |
| 26 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 26 #include "ui/gfx/image/image_skia_operations.h" |
| 27 #include "ui/gfx/path.h" | 27 #include "ui/gfx/path.h" |
| 28 #include "ui/gfx/shadow_value.h" | 28 #include "ui/gfx/shadow_value.h" |
| 29 #include "ui/gfx/skia_util.h" | |
| 30 #include "ui/views/controls/image_view.h" | 29 #include "ui/views/controls/image_view.h" |
| 31 #include "ui/views/view_targeter.h" | 30 #include "ui/views/view_targeter.h" |
| 32 #include "ui/views/widget/widget.h" | 31 #include "ui/views/widget/widget.h" |
| 33 #include "ui/views/window/non_client_view.h" | 32 #include "ui/views/window/non_client_view.h" |
| 34 | 33 |
| 35 namespace { | 34 namespace { |
| 36 | 35 |
| 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 | 36 // Cache the shadow images so that potentially expensive shadow drawing isn't |
| 72 // repeated. | 37 // repeated. |
| 73 base::LazyInstance<gfx::ImageSkia> g_top_shadow = LAZY_INSTANCE_INITIALIZER; | 38 base::LazyInstance<gfx::ImageSkia> g_top_shadow = LAZY_INSTANCE_INITIALIZER; |
| 74 base::LazyInstance<gfx::ImageSkia> g_bottom_shadow = LAZY_INSTANCE_INITIALIZER; | 39 base::LazyInstance<gfx::ImageSkia> g_bottom_shadow = LAZY_INSTANCE_INITIALIZER; |
| 75 | 40 |
| 76 } // namespace | 41 } // namespace |
| 77 | 42 |
| 78 class OmniboxPopupContentsView::AutocompletePopupWidget | 43 class OmniboxPopupContentsView::AutocompletePopupWidget |
| 79 : public ThemeCopyingWidget, | 44 : public ThemeCopyingWidget, |
| 80 public base::SupportsWeakPtr<AutocompletePopupWidget> { | 45 public base::SupportsWeakPtr<AutocompletePopupWidget> { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 size_animation_(this), | 80 size_animation_(this), |
| 116 start_margin_(0), | 81 start_margin_(0), |
| 117 end_margin_(0) { | 82 end_margin_(0) { |
| 118 // The contents is owned by the LocationBarView. | 83 // The contents is owned by the LocationBarView. |
| 119 set_owned_by_client(); | 84 set_owned_by_client(); |
| 120 | 85 |
| 121 if (g_top_shadow.Get().isNull()) { | 86 if (g_top_shadow.Get().isNull()) { |
| 122 std::vector<gfx::ShadowValue> shadows; | 87 std::vector<gfx::ShadowValue> shadows; |
| 123 // Blur by 1dp. See comment below about blur accounting. | 88 // Blur by 1dp. See comment below about blur accounting. |
| 124 shadows.emplace_back(gfx::Vector2d(), 2, SK_ColorBLACK); | 89 shadows.emplace_back(gfx::Vector2d(), 2, SK_ColorBLACK); |
| 125 | 90 g_top_shadow.Get() = |
| 126 auto source = new ShadowImageSource(shadows, false); | 91 gfx::ImageSkiaOperations::CreateHorizontalShadow(shadows, false); |
| 127 g_top_shadow.Get() = gfx::ImageSkia(source, source->size()); | |
| 128 } | 92 } |
| 129 if (g_bottom_shadow.Get().isNull()) { | 93 if (g_bottom_shadow.Get().isNull()) { |
| 130 const int kSmallShadowBlur = 3; | 94 const int kSmallShadowBlur = 3; |
| 131 const int kLargeShadowBlur = 8; | 95 const int kLargeShadowBlur = 8; |
| 132 const int kLargeShadowYOffset = 3; | 96 const int kLargeShadowYOffset = 3; |
| 133 | 97 |
| 134 std::vector<gfx::ShadowValue> shadows; | 98 std::vector<gfx::ShadowValue> shadows; |
| 135 // gfx::ShadowValue counts blur pixels both inside and outside the shape, | 99 // gfx::ShadowValue counts blur pixels both inside and outside the shape, |
| 136 // whereas these blur values only describe the outside portion, hence they | 100 // whereas these blur values only describe the outside portion, hence they |
| 137 // must be doubled. | 101 // must be doubled. |
| 138 shadows.emplace_back(gfx::Vector2d(), 2 * kSmallShadowBlur, | 102 shadows.emplace_back(gfx::Vector2d(), 2 * kSmallShadowBlur, |
| 139 SK_ColorBLACK); | 103 SK_ColorBLACK); |
| 140 shadows.emplace_back(gfx::Vector2d(0, kLargeShadowYOffset), | 104 shadows.emplace_back(gfx::Vector2d(0, kLargeShadowYOffset), |
| 141 2 * kLargeShadowBlur, SK_ColorBLACK); | 105 2 * kLargeShadowBlur, SK_ColorBLACK); |
| 142 | 106 |
| 143 auto source = new ShadowImageSource(shadows, true); | 107 g_bottom_shadow.Get() = |
| 144 g_bottom_shadow.Get() = gfx::ImageSkia(source, source->size()); | 108 gfx::ImageSkiaOperations::CreateHorizontalShadow(shadows, true); |
| 145 } | 109 } |
| 146 | 110 |
| 147 SetEventTargeter( | 111 SetEventTargeter( |
| 148 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); | 112 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); |
| 149 } | 113 } |
| 150 | 114 |
| 151 void OmniboxPopupContentsView::Init() { | 115 void OmniboxPopupContentsView::Init() { |
| 152 // This can't be done in the constructor as at that point we aren't | 116 // This can't be done in the constructor as at that point we aren't |
| 153 // necessarily our final class yet, and we may have subclasses | 117 // necessarily our final class yet, and we may have subclasses |
| 154 // overriding CreateResultView. | 118 // overriding CreateResultView. |
| (...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 size_t index = GetIndexForPoint(event.location()); | 525 size_t index = GetIndexForPoint(event.location()); |
| 562 if (!HasMatchAt(index)) | 526 if (!HasMatchAt(index)) |
| 563 return; | 527 return; |
| 564 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition, | 528 omnibox_view_->OpenMatch(model_->result().match_at(index), disposition, |
| 565 GURL(), base::string16(), index); | 529 GURL(), base::string16(), index); |
| 566 } | 530 } |
| 567 | 531 |
| 568 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { | 532 OmniboxResultView* OmniboxPopupContentsView::result_view_at(size_t i) { |
| 569 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); | 533 return static_cast<OmniboxResultView*>(child_at(static_cast<int>(i))); |
| 570 } | 534 } |
| OLD | NEW |