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 "ui/views/controls/scroll_view.h" | 5 #include "ui/views/controls/scroll_view.h" |
6 | 6 |
7 #include "base/feature_list.h" | 7 #include "base/feature_list.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "ui/base/material_design/material_design_controller.h" | 10 #include "ui/base/material_design/material_design_controller.h" |
(...skipping 14 matching lines...) Expand all Loading... | |
25 | 25 |
26 const base::Feature kToolkitViewsScrollWithLayers { | 26 const base::Feature kToolkitViewsScrollWithLayers { |
27 "ToolkitViewsScrollWithLayers", | 27 "ToolkitViewsScrollWithLayers", |
28 #if defined(OS_MACOSX) | 28 #if defined(OS_MACOSX) |
29 base::FEATURE_ENABLED_BY_DEFAULT | 29 base::FEATURE_ENABLED_BY_DEFAULT |
30 #else | 30 #else |
31 base::FEATURE_DISABLED_BY_DEFAULT | 31 base::FEATURE_DISABLED_BY_DEFAULT |
32 #endif | 32 #endif |
33 }; | 33 }; |
34 | 34 |
35 // Set if the kToolkitViewsScrollWithLayers feature is enabled. | |
36 // TODO(ananta) | |
37 // Remove this when the feature is enabled by default. | |
38 bool g_scroll_with_layers_enabled = false; | |
39 | |
35 class ScrollCornerView : public View { | 40 class ScrollCornerView : public View { |
36 public: | 41 public: |
37 ScrollCornerView() {} | 42 ScrollCornerView() {} |
38 | 43 |
39 void OnPaint(gfx::Canvas* canvas) override { | 44 void OnPaint(gfx::Canvas* canvas) override { |
40 ui::NativeTheme::ExtraParams ignored; | 45 ui::NativeTheme::ExtraParams ignored; |
41 GetNativeTheme()->Paint(canvas->sk_canvas(), | 46 GetNativeTheme()->Paint(canvas->sk_canvas(), |
42 ui::NativeTheme::kScrollbarCorner, | 47 ui::NativeTheme::kScrollbarCorner, |
43 ui::NativeTheme::kNormal, | 48 ui::NativeTheme::kNormal, |
44 GetLocalBounds(), | 49 GetLocalBounds(), |
(...skipping 15 matching lines...) Expand all Loading... | |
60 return current_pos; | 65 return current_pos; |
61 } | 66 } |
62 | 67 |
63 // Make sure the content is not scrolled out of bounds | 68 // Make sure the content is not scrolled out of bounds |
64 void ConstrainScrollToBounds(View* viewport, View* view) { | 69 void ConstrainScrollToBounds(View* viewport, View* view) { |
65 if (!view) | 70 if (!view) |
66 return; | 71 return; |
67 | 72 |
68 // Note that even when ScrollView::ScrollsWithLayers() is true, the header row | 73 // Note that even when ScrollView::ScrollsWithLayers() is true, the header row |
69 // scrolls by repainting. | 74 // scrolls by repainting. |
70 const bool scrolls_with_layers = viewport->layer() != nullptr; | 75 const bool scrolls_with_layers = |
76 g_scroll_with_layers_enabled && viewport->layer() != nullptr; | |
sky
2017/04/17 15:24:01
Make ScrollView have a member for scroll_with_laye
ananta
2017/04/18 03:04:25
Done.
| |
71 if (scrolls_with_layers) { | 77 if (scrolls_with_layers) { |
72 DCHECK(view->layer()); | 78 DCHECK(view->layer()); |
73 DCHECK_EQ(0, view->x()); | 79 DCHECK_EQ(0, view->x()); |
74 DCHECK_EQ(0, view->y()); | 80 DCHECK_EQ(0, view->y()); |
75 } | 81 } |
76 gfx::ScrollOffset offset = scrolls_with_layers | 82 gfx::ScrollOffset offset = scrolls_with_layers |
77 ? view->layer()->CurrentScrollOffset() | 83 ? view->layer()->CurrentScrollOffset() |
78 : gfx::ScrollOffset(-view->x(), -view->y()); | 84 : gfx::ScrollOffset(-view->x(), -view->y()); |
79 | 85 |
80 int x = CheckScrollBounds(viewport->width(), view->width(), offset.x()); | 86 int x = CheckScrollBounds(viewport->width(), view->width(), offset.x()); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
159 AddChildView(header_viewport_); | 165 AddChildView(header_viewport_); |
160 | 166 |
161 // Don't add the scrollbars as children until we discover we need them | 167 // Don't add the scrollbars as children until we discover we need them |
162 // (ShowOrHideScrollBar). | 168 // (ShowOrHideScrollBar). |
163 horiz_sb_->SetVisible(false); | 169 horiz_sb_->SetVisible(false); |
164 horiz_sb_->set_controller(this); | 170 horiz_sb_->set_controller(this); |
165 vert_sb_->SetVisible(false); | 171 vert_sb_->SetVisible(false); |
166 vert_sb_->set_controller(this); | 172 vert_sb_->set_controller(this); |
167 corner_view_->SetVisible(false); | 173 corner_view_->SetVisible(false); |
168 | 174 |
169 if (!base::FeatureList::IsEnabled(kToolkitViewsScrollWithLayers)) | 175 g_scroll_with_layers_enabled = |
176 base::FeatureList::IsEnabled(kToolkitViewsScrollWithLayers); | |
177 if (!g_scroll_with_layers_enabled) | |
170 return; | 178 return; |
179 | |
171 EnableViewPortLayer(); | 180 EnableViewPortLayer(); |
172 } | 181 } |
173 | 182 |
174 ScrollView::~ScrollView() { | 183 ScrollView::~ScrollView() { |
175 // The scrollbars may not have been added, delete them to ensure they get | 184 // The scrollbars may not have been added, delete them to ensure they get |
176 // deleted. | 185 // deleted. |
177 delete horiz_sb_; | 186 delete horiz_sb_; |
178 delete vert_sb_; | 187 delete vert_sb_; |
179 delete corner_view_; | 188 delete corner_view_; |
180 } | 189 } |
(...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
520 } | 529 } |
521 | 530 |
522 const char* ScrollView::GetClassName() const { | 531 const char* ScrollView::GetClassName() const { |
523 return kViewClassName; | 532 return kViewClassName; |
524 } | 533 } |
525 | 534 |
526 void ScrollView::OnNativeThemeChanged(const ui::NativeTheme* theme) { | 535 void ScrollView::OnNativeThemeChanged(const ui::NativeTheme* theme) { |
527 UpdateBorder(); | 536 UpdateBorder(); |
528 } | 537 } |
529 | 538 |
539 void ScrollView::OnChildLayerChanged(View* child) { | |
540 EnableViewPortLayer(); | |
541 } | |
542 | |
530 void ScrollView::ScrollToPosition(ScrollBar* source, int position) { | 543 void ScrollView::ScrollToPosition(ScrollBar* source, int position) { |
531 if (!contents_) | 544 if (!contents_) |
532 return; | 545 return; |
533 | 546 |
534 gfx::ScrollOffset offset = CurrentOffset(); | 547 gfx::ScrollOffset offset = CurrentOffset(); |
535 if (source == horiz_sb_ && horiz_sb_->visible()) { | 548 if (source == horiz_sb_ && horiz_sb_->visible()) { |
536 position = AdjustPosition(offset.x(), position, contents_->width(), | 549 position = AdjustPosition(offset.x(), position, contents_->width(), |
537 contents_viewport_->width()); | 550 contents_viewport_->width()); |
538 if (offset.x() == position) | 551 if (offset.x() == position) |
539 return; | 552 return; |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
710 // and commits a frame, which isn't true in some tests. | 723 // and commits a frame, which isn't true in some tests. |
711 // See http://crbug.com/637521. | 724 // See http://crbug.com/637521. |
712 OnLayerScrolled(offset); | 725 OnLayerScrolled(offset); |
713 } else { | 726 } else { |
714 contents_->SetPosition(gfx::Point(-offset.x(), -offset.y())); | 727 contents_->SetPosition(gfx::Point(-offset.x(), -offset.y())); |
715 ScrollHeader(); | 728 ScrollHeader(); |
716 } | 729 } |
717 } | 730 } |
718 | 731 |
719 bool ScrollView::ScrollsWithLayers() const { | 732 bool ScrollView::ScrollsWithLayers() const { |
733 if (!g_scroll_with_layers_enabled) | |
734 return false; | |
720 // Just check for the presence of a layer since it's cheaper than querying the | 735 // Just check for the presence of a layer since it's cheaper than querying the |
721 // Feature flag each time. | 736 // Feature flag each time. |
722 return contents_viewport_->layer() != nullptr; | 737 return contents_viewport_->layer() != nullptr; |
723 } | 738 } |
724 | 739 |
725 void ScrollView::EnableViewPortLayer() { | 740 void ScrollView::EnableViewPortLayer() { |
741 if (viewport_layer_enabled_) | |
742 return; | |
743 | |
744 viewport_layer_enabled_ = true; | |
726 background_color_ = SK_ColorWHITE; | 745 background_color_ = SK_ColorWHITE; |
727 contents_viewport_->set_background( | 746 contents_viewport_->set_background( |
728 Background::CreateSolidBackground(background_color_)); | 747 Background::CreateSolidBackground(background_color_)); |
729 contents_viewport_->SetPaintToLayer(); | 748 contents_viewport_->SetPaintToLayer(); |
730 contents_viewport_->layer()->SetMasksToBounds(true); | 749 contents_viewport_->layer()->SetMasksToBounds(true); |
731 } | 750 } |
732 | 751 |
733 void ScrollView::OnLayerScrolled(const gfx::ScrollOffset&) { | 752 void ScrollView::OnLayerScrolled(const gfx::ScrollOffset&) { |
734 UpdateScrollBarPositions(); | 753 UpdateScrollBarPositions(); |
735 ScrollHeader(); | 754 ScrollHeader(); |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
830 | 849 |
831 VariableRowHeightScrollHelper::RowInfo | 850 VariableRowHeightScrollHelper::RowInfo |
832 FixedRowHeightScrollHelper::GetRowInfo(int y) { | 851 FixedRowHeightScrollHelper::GetRowInfo(int y) { |
833 if (y < top_margin_) | 852 if (y < top_margin_) |
834 return RowInfo(0, top_margin_); | 853 return RowInfo(0, top_margin_); |
835 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, | 854 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, |
836 row_height_); | 855 row_height_); |
837 } | 856 } |
838 | 857 |
839 } // namespace views | 858 } // namespace views |
OLD | NEW |