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/image/canvas_image_source.h" | |
25 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
26 #include "ui/gfx/path.h" | 26 #include "ui/gfx/path.h" |
27 #include "ui/gfx/shadow_value.h" | |
28 #include "ui/gfx/skia_util.h" | |
27 #include "ui/views/controls/image_view.h" | 29 #include "ui/views/controls/image_view.h" |
28 #include "ui/views/resources/grit/views_resources.h" | |
29 #include "ui/views/view_targeter.h" | 30 #include "ui/views/view_targeter.h" |
30 #include "ui/views/widget/widget.h" | 31 #include "ui/views/widget/widget.h" |
31 #include "ui/views/window/non_client_view.h" | 32 #include "ui/views/window/non_client_view.h" |
32 | 33 |
34 namespace { | |
35 | |
36 // This creates a shadow image that is 1dp wide, suitable for tiling along the | |
37 // top or bottom edge of the omnibox popup. | |
38 class ShadowImageSource : public gfx::CanvasImageSource { | |
39 public: | |
40 ShadowImageSource(const std::vector<gfx::ShadowValue>& shadows, bool bottom) | |
41 : gfx::CanvasImageSource(gfx::Size(1, GetHeightForShadows(shadows)), | |
42 false), | |
43 shadows_(shadows), | |
44 bottom_(bottom) {} | |
45 ~ShadowImageSource() override {} | |
46 | |
47 void Draw(gfx::Canvas* canvas) override { | |
48 SkPaint paint; | |
49 paint.setLooper(gfx::CreateShadowDrawLooperCorrectBlur(shadows_)); | |
50 canvas->DrawRect(gfx::RectF(0, bottom_ ? -1 : size().height(), 1, 1), | |
51 paint); | |
52 } | |
53 | |
54 private: | |
55 static int GetHeightForShadows(const std::vector<gfx::ShadowValue>& shadows) { | |
56 int height = 0; | |
57 for (const auto& shadow : shadows) { | |
58 height = | |
59 std::max(height, shadow.y() + static_cast<int>(shadow.blur()) / 2); | |
Peter Kasting
2016/09/23 19:38:44
Functionally, shouldn't this be more like "ceil(bl
Evan Stade
2016/09/26 18:11:42
the blur value should always be even, but I've upd
| |
60 } | |
61 return height; | |
62 } | |
63 | |
64 const std::vector<gfx::ShadowValue> shadows_; | |
65 bool bottom_; | |
66 | |
67 DISALLOW_COPY_AND_ASSIGN(ShadowImageSource); | |
68 }; | |
69 | |
70 // Cache the shadow images so that potentially expensive shadow drawing isn't | |
71 // repeated. | |
72 base::LazyInstance<gfx::ImageSkia> g_top_shadow = LAZY_INSTANCE_INITIALIZER; | |
73 base::LazyInstance<gfx::ImageSkia> g_bottom_shadow = LAZY_INSTANCE_INITIALIZER; | |
74 | |
75 } // namespace | |
76 | |
33 class OmniboxPopupContentsView::AutocompletePopupWidget | 77 class OmniboxPopupContentsView::AutocompletePopupWidget |
34 : public ThemeCopyingWidget, | 78 : public ThemeCopyingWidget, |
35 public base::SupportsWeakPtr<AutocompletePopupWidget> { | 79 public base::SupportsWeakPtr<AutocompletePopupWidget> { |
36 public: | 80 public: |
37 explicit AutocompletePopupWidget(views::Widget* role_model) | 81 explicit AutocompletePopupWidget(views::Widget* role_model) |
38 : ThemeCopyingWidget(role_model) {} | 82 : ThemeCopyingWidget(role_model) {} |
39 ~AutocompletePopupWidget() override {} | 83 ~AutocompletePopupWidget() override {} |
40 | 84 |
41 private: | 85 private: |
42 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); | 86 DISALLOW_COPY_AND_ASSIGN(AutocompletePopupWidget); |
(...skipping 23 matching lines...) Expand all Loading... | |
66 omnibox_view_(omnibox_view), | 110 omnibox_view_(omnibox_view), |
67 location_bar_view_(location_bar_view), | 111 location_bar_view_(location_bar_view), |
68 font_list_(font_list), | 112 font_list_(font_list), |
69 ignore_mouse_drag_(false), | 113 ignore_mouse_drag_(false), |
70 size_animation_(this), | 114 size_animation_(this), |
71 start_margin_(0), | 115 start_margin_(0), |
72 end_margin_(0) { | 116 end_margin_(0) { |
73 // The contents is owned by the LocationBarView. | 117 // The contents is owned by the LocationBarView. |
74 set_owned_by_client(); | 118 set_owned_by_client(); |
75 | 119 |
76 ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance(); | 120 if (g_top_shadow.Get().isNull()) { |
77 if (ui::MaterialDesignController::IsModeMaterial()) { | 121 std::vector<gfx::ShadowValue> shadows; |
78 top_shadow_ = rb->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_TOP); | 122 // Blur by 1dp. See comment below about blur accounting. |
Peter Kasting
2016/09/23 19:38:44
Argument for another day: It sure is weird to me t
Evan Stade
2016/09/26 18:11:42
that works better when there's no bookmark bar ---
Peter Kasting
2016/09/26 19:57:34
Yeah, we'd have to draw it ourselves, I just think
| |
79 bottom_shadow_ = rb->GetImageSkiaNamed(IDR_OMNIBOX_DROPDOWN_SHADOW_BOTTOM); | 123 shadows.emplace_back(gfx::Vector2d(), 2, SK_ColorBLACK); |
80 } else { | 124 |
81 bottom_shadow_ = rb->GetImageSkiaNamed(IDR_BUBBLE_B); | 125 auto source = new ShadowImageSource(shadows, false); |
126 g_top_shadow.Get() = gfx::ImageSkia(source, source->size()); | |
Peter Kasting
2016/09/23 19:38:44
Musing: Wow, we really should fix https://bugs.chr
| |
127 } | |
128 if (g_bottom_shadow.Get().isNull()) { | |
129 const int kSmallShadowBlur = 3; | |
130 const int kLargeShadowBlur = 8; | |
131 const int kLargeShadowYOffset = 3; | |
132 | |
133 std::vector<gfx::ShadowValue> shadows; | |
134 // gfx::ShadowValue counts blur pixels both inside and outside the shape, | |
135 // whereas these blur values only describe the outside portion, hence they | |
136 // must be doubled. | |
137 shadows.emplace_back(gfx::Vector2d(0, 0), 2 * kSmallShadowBlur, | |
Peter Kasting
2016/09/23 19:38:44
Nit: Omit 0, 0
Evan Stade
2016/09/26 18:11:42
Done.
| |
138 SK_ColorBLACK); | |
139 shadows.emplace_back(gfx::Vector2d(0, kLargeShadowYOffset), | |
140 2 * kLargeShadowBlur, SK_ColorBLACK); | |
141 | |
142 auto source = new ShadowImageSource(shadows, true); | |
143 g_bottom_shadow.Get() = gfx::ImageSkia(source, source->size()); | |
82 } | 144 } |
83 | 145 |
84 SetEventTargeter( | 146 SetEventTargeter( |
85 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); | 147 std::unique_ptr<views::ViewTargeter>(new views::ViewTargeter(this))); |
86 } | 148 } |
87 | 149 |
88 void OmniboxPopupContentsView::Init() { | 150 void OmniboxPopupContentsView::Init() { |
89 // This can't be done in the constructor as at that point we aren't | 151 // 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 | 152 // necessarily our final class yet, and we may have subclasses |
91 // overriding CreateResultView. | 153 // overriding CreateResultView. |
(...skipping 22 matching lines...) Expand all Loading... | |
114 int current_height_delta = static_cast<int>( | 176 int current_height_delta = static_cast<int>( |
115 size_animation_.GetCurrentValue() * total_height_delta - 0.5); | 177 size_animation_.GetCurrentValue() * total_height_delta - 0.5); |
116 current_frame_bounds.set_height( | 178 current_frame_bounds.set_height( |
117 current_frame_bounds.height() + current_height_delta); | 179 current_frame_bounds.height() + current_height_delta); |
118 return current_frame_bounds; | 180 return current_frame_bounds; |
119 } | 181 } |
120 | 182 |
121 void OmniboxPopupContentsView::LayoutChildren() { | 183 void OmniboxPopupContentsView::LayoutChildren() { |
122 gfx::Rect contents_rect = GetContentsBounds(); | 184 gfx::Rect contents_rect = GetContentsBounds(); |
123 contents_rect.Inset(GetLayoutInsets(OMNIBOX_DROPDOWN)); | 185 contents_rect.Inset(GetLayoutInsets(OMNIBOX_DROPDOWN)); |
124 contents_rect.Inset(start_margin_, | 186 contents_rect.Inset(start_margin_, g_top_shadow.Get().height(), end_margin_, |
125 views::NonClientFrameView::kClientEdgeThickness, | 187 0); |
126 end_margin_, 0); | |
127 | 188 |
128 int top = contents_rect.y(); | 189 int top = contents_rect.y(); |
129 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { | 190 for (size_t i = 0; i < AutocompleteResult::kMaxMatches; ++i) { |
130 View* v = child_at(i); | 191 View* v = child_at(i); |
131 if (v->visible()) { | 192 if (v->visible()) { |
132 v->SetBounds(contents_rect.x(), top, contents_rect.width(), | 193 v->SetBounds(contents_rect.x(), top, contents_rect.width(), |
133 v->GetPreferredSize().height()); | 194 v->GetPreferredSize().height()); |
134 top = v->bounds().bottom(); | 195 top = v->bounds().bottom(); |
135 } | 196 } |
136 } | 197 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 } | 251 } |
191 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { | 252 if (match.type == AutocompleteMatchType::SEARCH_SUGGEST_TAIL) { |
192 max_match_contents_width_ = std::max( | 253 max_match_contents_width_ = std::max( |
193 max_match_contents_width_, view->GetMatchContentsWidth()); | 254 max_match_contents_width_, view->GetMatchContentsWidth()); |
194 } | 255 } |
195 } | 256 } |
196 | 257 |
197 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) | 258 for (size_t i = result_size; i < AutocompleteResult::kMaxMatches; ++i) |
198 child_at(i)->SetVisible(false); | 259 child_at(i)->SetVisible(false); |
199 | 260 |
200 // In non-material mode, we want the popup to appear as if it's overlaying | 261 // 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 | 262 // 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 | 263 // additional semitransparent shadow above that. So the total overlap |
203 // to draw over it (so as not to push the results below it), but that means | 264 // necessary is the client edge thickness plus the shadow height. |
Peter Kasting
2016/09/23 19:38:44
Nit: Wondering if the last sentence just restates
Evan Stade
2016/09/26 18:11:42
Done.
| |
204 // the toolbar won't be drawing a client edge separating itself from the | 265 int top_edge_overlap = views::NonClientFrameView::kClientEdgeThickness + |
205 // popup. So we unconditionally overlap the toolbar by the thickness of the | 266 g_top_shadow.Get().height(); |
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 |