| 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/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/macros.h" | 9 #include "base/macros.h" |
| 9 #include "ui/events/event.h" | 10 #include "ui/events/event.h" |
| 10 #include "ui/gfx/canvas.h" | 11 #include "ui/gfx/canvas.h" |
| 11 #include "ui/native_theme/native_theme.h" | 12 #include "ui/native_theme/native_theme.h" |
| 13 #include "ui/views/background.h" |
| 12 #include "ui/views/border.h" | 14 #include "ui/views/border.h" |
| 13 #include "ui/views/style/platform_style.h" | 15 #include "ui/views/style/platform_style.h" |
| 14 #include "ui/views/widget/root_view.h" | 16 #include "ui/views/widget/widget.h" |
| 15 | 17 |
| 16 namespace views { | 18 namespace views { |
| 17 | 19 |
| 18 const char ScrollView::kViewClassName[] = "ScrollView"; | 20 const char ScrollView::kViewClassName[] = "ScrollView"; |
| 19 | 21 |
| 20 namespace { | 22 namespace { |
| 21 | 23 |
| 24 const base::Feature kToolkitViewsScrollWithLayers { |
| 25 "ToolkitViewsScrollWithLayers", |
| 26 #if defined(OS_MACOSX) |
| 27 base::FEATURE_ENABLED_BY_DEFAULT |
| 28 #else |
| 29 base::FEATURE_DISABLED_BY_DEFAULT |
| 30 #endif |
| 31 }; |
| 32 |
| 22 // Subclass of ScrollView that resets the border when the theme changes. | 33 // Subclass of ScrollView that resets the border when the theme changes. |
| 23 class ScrollViewWithBorder : public views::ScrollView { | 34 class ScrollViewWithBorder : public views::ScrollView { |
| 24 public: | 35 public: |
| 25 ScrollViewWithBorder() {} | 36 ScrollViewWithBorder() {} |
| 26 | 37 |
| 27 // View overrides; | 38 // View overrides; |
| 28 void OnNativeThemeChanged(const ui::NativeTheme* theme) override { | 39 void OnNativeThemeChanged(const ui::NativeTheme* theme) override { |
| 29 SetBorder(Border::CreateSolidBorder( | 40 SetBorder(Border::CreateSolidBorder( |
| 30 1, | 41 1, |
| 31 theme->GetSystemColor(ui::NativeTheme::kColorId_UnfocusedBorderColor))); | 42 theme->GetSystemColor(ui::NativeTheme::kColorId_UnfocusedBorderColor))); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 57 int CheckScrollBounds(int viewport_size, int content_size, int current_pos) { | 68 int CheckScrollBounds(int viewport_size, int content_size, int current_pos) { |
| 58 int max = std::max(content_size - viewport_size, 0); | 69 int max = std::max(content_size - viewport_size, 0); |
| 59 if (current_pos < 0) | 70 if (current_pos < 0) |
| 60 return 0; | 71 return 0; |
| 61 if (current_pos > max) | 72 if (current_pos > max) |
| 62 return max; | 73 return max; |
| 63 return current_pos; | 74 return current_pos; |
| 64 } | 75 } |
| 65 | 76 |
| 66 // Make sure the content is not scrolled out of bounds | 77 // Make sure the content is not scrolled out of bounds |
| 67 void CheckScrollBounds(View* viewport, View* view) { | 78 void ConstrainScrollToBounds(View* viewport, View* view) { |
| 68 if (!view) | 79 if (!view) |
| 69 return; | 80 return; |
| 70 | 81 |
| 71 int x = CheckScrollBounds(viewport->width(), view->width(), -view->x()); | 82 // Note that even when ScrollView::ScrollsWithLayers() is true, the header row |
| 72 int y = CheckScrollBounds(viewport->height(), view->height(), -view->y()); | 83 // scrolls by repainting. |
| 84 const bool scrolls_with_layers = viewport->layer() != nullptr; |
| 85 if (scrolls_with_layers) { |
| 86 DCHECK(view->layer()); |
| 87 DCHECK_EQ(0, view->x()); |
| 88 DCHECK_EQ(0, view->y()); |
| 89 } |
| 90 gfx::ScrollOffset offset = scrolls_with_layers |
| 91 ? view->layer()->CurrentScrollOffset() |
| 92 : gfx::ScrollOffset(-view->x(), -view->y()); |
| 73 | 93 |
| 74 // This is no op if bounds are the same | 94 int x = CheckScrollBounds(viewport->width(), view->width(), offset.x()); |
| 75 view->SetBounds(-x, -y, view->width(), view->height()); | 95 int y = CheckScrollBounds(viewport->height(), view->height(), offset.y()); |
| 96 |
| 97 if (scrolls_with_layers) { |
| 98 view->layer()->SetScrollOffset(gfx::ScrollOffset(x, y)); |
| 99 } else { |
| 100 // This is no op if bounds are the same |
| 101 view->SetBounds(-x, -y, view->width(), view->height()); |
| 102 } |
| 76 } | 103 } |
| 77 | 104 |
| 78 // Used by ScrollToPosition() to make sure the new position fits within the | 105 // Used by ScrollToPosition() to make sure the new position fits within the |
| 79 // allowed scroll range. | 106 // allowed scroll range. |
| 80 int AdjustPosition(int current_position, | 107 int AdjustPosition(int current_position, |
| 81 int new_position, | 108 int new_position, |
| 82 int content_size, | 109 int content_size, |
| 83 int viewport_size) { | 110 int viewport_size) { |
| 84 if (-current_position == new_position) | 111 if (-current_position == new_position) |
| 85 return new_position; | 112 return new_position; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 98 ~Viewport() override {} | 125 ~Viewport() override {} |
| 99 | 126 |
| 100 const char* GetClassName() const override { return "ScrollView::Viewport"; } | 127 const char* GetClassName() const override { return "ScrollView::Viewport"; } |
| 101 | 128 |
| 102 void ScrollRectToVisible(const gfx::Rect& rect) override { | 129 void ScrollRectToVisible(const gfx::Rect& rect) override { |
| 103 if (!has_children() || !parent()) | 130 if (!has_children() || !parent()) |
| 104 return; | 131 return; |
| 105 | 132 |
| 106 View* contents = child_at(0); | 133 View* contents = child_at(0); |
| 107 gfx::Rect scroll_rect(rect); | 134 gfx::Rect scroll_rect(rect); |
| 108 scroll_rect.Offset(-contents->x(), -contents->y()); | 135 |
| 109 static_cast<ScrollView*>(parent())->ScrollContentsRegionToBeVisible( | 136 ScrollView* scroll_view = static_cast<ScrollView*>(parent()); |
| 110 scroll_rect); | 137 if (scroll_view->ScrollsWithLayers()) { |
| 138 // With layer scrolling, there's no need to "undo" the offset done in the |
| 139 // child's View::ScrollRectToVisible() before it calls this. |
| 140 DCHECK_EQ(0, contents->x()); |
| 141 DCHECK_EQ(0, contents->y()); |
| 142 } else { |
| 143 scroll_rect.Offset(-contents->x(), -contents->y()); |
| 144 } |
| 145 |
| 146 scroll_view->ScrollContentsRegionToBeVisible(scroll_rect); |
| 111 } | 147 } |
| 112 | 148 |
| 113 void ChildPreferredSizeChanged(View* child) override { | 149 void ChildPreferredSizeChanged(View* child) override { |
| 114 if (parent()) | 150 if (parent()) |
| 115 parent()->Layout(); | 151 parent()->Layout(); |
| 116 } | 152 } |
| 117 | 153 |
| 118 private: | 154 private: |
| 119 DISALLOW_COPY_AND_ASSIGN(Viewport); | 155 DISALLOW_COPY_AND_ASSIGN(Viewport); |
| 120 }; | 156 }; |
| 121 | 157 |
| 122 ScrollView::ScrollView() | 158 ScrollView::ScrollView() |
| 123 : contents_(NULL), | 159 : contents_(NULL), |
| 124 contents_viewport_(new Viewport()), | 160 contents_viewport_(new Viewport()), |
| 125 header_(NULL), | 161 header_(NULL), |
| 126 header_viewport_(new Viewport()), | 162 header_viewport_(new Viewport()), |
| 127 horiz_sb_(PlatformStyle::CreateScrollBar(true).release()), | 163 horiz_sb_(PlatformStyle::CreateScrollBar(true).release()), |
| 128 vert_sb_(PlatformStyle::CreateScrollBar(false).release()), | 164 vert_sb_(PlatformStyle::CreateScrollBar(false).release()), |
| 129 corner_view_(new ScrollCornerView()), | 165 corner_view_(new ScrollCornerView()), |
| 130 min_height_(-1), | 166 min_height_(-1), |
| 131 max_height_(-1), | 167 max_height_(-1), |
| 168 background_color_(SK_ColorTRANSPARENT), |
| 132 hide_horizontal_scrollbar_(false) { | 169 hide_horizontal_scrollbar_(false) { |
| 133 set_notify_enter_exit_on_child(true); | 170 set_notify_enter_exit_on_child(true); |
| 134 | 171 |
| 135 AddChildView(contents_viewport_); | 172 AddChildView(contents_viewport_); |
| 136 AddChildView(header_viewport_); | 173 AddChildView(header_viewport_); |
| 137 | 174 |
| 138 // Don't add the scrollbars as children until we discover we need them | 175 // Don't add the scrollbars as children until we discover we need them |
| 139 // (ShowOrHideScrollBar). | 176 // (ShowOrHideScrollBar). |
| 140 horiz_sb_->SetVisible(false); | 177 horiz_sb_->SetVisible(false); |
| 141 horiz_sb_->set_controller(this); | 178 horiz_sb_->set_controller(this); |
| 142 vert_sb_->SetVisible(false); | 179 vert_sb_->SetVisible(false); |
| 143 vert_sb_->set_controller(this); | 180 vert_sb_->set_controller(this); |
| 144 corner_view_->SetVisible(false); | 181 corner_view_->SetVisible(false); |
| 182 |
| 183 if (!base::FeatureList::IsEnabled(kToolkitViewsScrollWithLayers)) |
| 184 return; |
| 185 |
| 186 background_color_ = SK_ColorWHITE; |
| 187 contents_viewport_->set_background( |
| 188 Background::CreateSolidBackground(background_color_)); |
| 189 contents_viewport_->SetPaintToLayer(true); |
| 190 contents_viewport_->layer()->SetMasksToBounds(true); |
| 145 } | 191 } |
| 146 | 192 |
| 147 ScrollView::~ScrollView() { | 193 ScrollView::~ScrollView() { |
| 148 // The scrollbars may not have been added, delete them to ensure they get | 194 // The scrollbars may not have been added, delete them to ensure they get |
| 149 // deleted. | 195 // deleted. |
| 150 delete horiz_sb_; | 196 delete horiz_sb_; |
| 151 delete vert_sb_; | 197 delete vert_sb_; |
| 152 delete corner_view_; | 198 delete corner_view_; |
| 153 } | 199 } |
| 154 | 200 |
| 155 // static | 201 // static |
| 156 ScrollView* ScrollView::CreateScrollViewWithBorder() { | 202 ScrollView* ScrollView::CreateScrollViewWithBorder() { |
| 157 return new ScrollViewWithBorder(); | 203 return new ScrollViewWithBorder(); |
| 158 } | 204 } |
| 159 | 205 |
| 160 void ScrollView::SetContents(View* a_view) { | 206 void ScrollView::SetContents(View* a_view) { |
| 207 // Protect against clients passing a contents view that has its own Layer. |
| 208 DCHECK(!a_view->layer()); |
| 209 if (ScrollsWithLayers()) { |
| 210 if (!a_view->background() && background_color_ != SK_ColorTRANSPARENT) { |
| 211 a_view->set_background( |
| 212 Background::CreateSolidBackground(background_color_)); |
| 213 } |
| 214 a_view->SetPaintToLayer(true); |
| 215 a_view->layer()->SetScrollable( |
| 216 contents_viewport_->layer(), |
| 217 base::Bind(&ScrollView::OnLayerScrolled, base::Unretained(this))); |
| 218 } |
| 161 SetHeaderOrContents(contents_viewport_, a_view, &contents_); | 219 SetHeaderOrContents(contents_viewport_, a_view, &contents_); |
| 162 } | 220 } |
| 163 | 221 |
| 164 void ScrollView::SetHeader(View* header) { | 222 void ScrollView::SetHeader(View* header) { |
| 165 SetHeaderOrContents(header_viewport_, header, &header_); | 223 SetHeaderOrContents(header_viewport_, header, &header_); |
| 166 } | 224 } |
| 167 | 225 |
| 226 void ScrollView::SetBackgroundColor(SkColor color) { |
| 227 background_color_ = color; |
| 228 contents_viewport_->set_background( |
| 229 Background::CreateSolidBackground(background_color_)); |
| 230 if (contents_ && ScrollsWithLayers() && |
| 231 background_color_ != SK_ColorTRANSPARENT) { |
| 232 contents_->set_background( |
| 233 Background::CreateSolidBackground(background_color_)); |
| 234 } |
| 235 } |
| 236 |
| 168 gfx::Rect ScrollView::GetVisibleRect() const { | 237 gfx::Rect ScrollView::GetVisibleRect() const { |
| 169 if (!contents_) | 238 if (!contents_) |
| 170 return gfx::Rect(); | 239 return gfx::Rect(); |
| 171 return gfx::Rect(-contents_->x(), -contents_->y(), | 240 gfx::ScrollOffset offset = CurrentOffset(); |
| 172 contents_viewport_->width(), contents_viewport_->height()); | 241 return gfx::Rect(offset.x(), offset.y(), contents_viewport_->width(), |
| 242 contents_viewport_->height()); |
| 173 } | 243 } |
| 174 | 244 |
| 175 void ScrollView::ClipHeightTo(int min_height, int max_height) { | 245 void ScrollView::ClipHeightTo(int min_height, int max_height) { |
| 176 min_height_ = min_height; | 246 min_height_ = min_height; |
| 177 max_height_ = max_height; | 247 max_height_ = max_height; |
| 178 } | 248 } |
| 179 | 249 |
| 180 int ScrollView::GetScrollBarWidth() const { | 250 int ScrollView::GetScrollBarWidth() const { |
| 181 return vert_sb_ ? vert_sb_->GetLayoutSize() : 0; | 251 return vert_sb_ ? vert_sb_->GetLayoutSize() : 0; |
| 182 } | 252 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 322 // Show the resize corner. | 392 // Show the resize corner. |
| 323 corner_view_->SetBounds(vert_sb_->bounds().x(), horiz_sb_->bounds().y(), | 393 corner_view_->SetBounds(vert_sb_->bounds().x(), horiz_sb_->bounds().y(), |
| 324 vert_sb_width, horiz_sb_height); | 394 vert_sb_width, horiz_sb_height); |
| 325 } | 395 } |
| 326 | 396 |
| 327 // Update to the real client size with the visible scrollbars. | 397 // Update to the real client size with the visible scrollbars. |
| 328 contents_viewport_->SetBoundsRect(viewport_bounds); | 398 contents_viewport_->SetBoundsRect(viewport_bounds); |
| 329 if (should_layout_contents && contents_) | 399 if (should_layout_contents && contents_) |
| 330 contents_->Layout(); | 400 contents_->Layout(); |
| 331 | 401 |
| 402 // Even when |contents_| needs to scroll, it can still be narrower or wider |
| 403 // the viewport. So ensure the scrolling layer can fill the viewport, so that |
| 404 // events will correctly hit it, and overscroll looks correct. |
| 405 if (contents_ && ScrollsWithLayers()) { |
| 406 gfx::Size container_size = contents_ ? contents_->size() : gfx::Size(); |
| 407 container_size.SetToMax(viewport_bounds.size()); |
| 408 contents_->SetBoundsRect(gfx::Rect(container_size)); |
| 409 } |
| 410 |
| 332 header_viewport_->SetBounds(contents_x, contents_y, | 411 header_viewport_->SetBounds(contents_x, contents_y, |
| 333 viewport_bounds.width(), header_height); | 412 viewport_bounds.width(), header_height); |
| 334 if (header_) | 413 if (header_) |
| 335 header_->Layout(); | 414 header_->Layout(); |
| 336 | 415 |
| 337 CheckScrollBounds(header_viewport_, header_); | 416 ConstrainScrollToBounds(header_viewport_, header_); |
| 338 CheckScrollBounds(contents_viewport_, contents_); | 417 ConstrainScrollToBounds(contents_viewport_, contents_); |
| 339 SchedulePaint(); | 418 SchedulePaint(); |
| 340 UpdateScrollBarPositions(); | 419 UpdateScrollBarPositions(); |
| 341 } | 420 } |
| 342 | 421 |
| 343 bool ScrollView::OnKeyPressed(const ui::KeyEvent& event) { | 422 bool ScrollView::OnKeyPressed(const ui::KeyEvent& event) { |
| 344 bool processed = false; | 423 bool processed = false; |
| 345 | 424 |
| 346 // Give vertical scrollbar priority | 425 // Give vertical scrollbar priority |
| 347 if (vert_sb_->visible()) | 426 if (vert_sb_->visible()) |
| 348 processed = vert_sb_->OnKeyPressed(event); | 427 processed = vert_sb_->OnKeyPressed(event); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 399 } | 478 } |
| 400 | 479 |
| 401 const char* ScrollView::GetClassName() const { | 480 const char* ScrollView::GetClassName() const { |
| 402 return kViewClassName; | 481 return kViewClassName; |
| 403 } | 482 } |
| 404 | 483 |
| 405 void ScrollView::ScrollToPosition(ScrollBar* source, int position) { | 484 void ScrollView::ScrollToPosition(ScrollBar* source, int position) { |
| 406 if (!contents_) | 485 if (!contents_) |
| 407 return; | 486 return; |
| 408 | 487 |
| 488 gfx::ScrollOffset offset = CurrentOffset(); |
| 409 if (source == horiz_sb_ && horiz_sb_->visible()) { | 489 if (source == horiz_sb_ && horiz_sb_->visible()) { |
| 410 position = AdjustPosition(contents_->x(), position, contents_->width(), | 490 position = AdjustPosition(offset.x(), position, contents_->width(), |
| 411 contents_viewport_->width()); | 491 contents_viewport_->width()); |
| 412 if (-contents_->x() == position) | 492 if (offset.x() == position) |
| 413 return; | 493 return; |
| 414 contents_->SetX(-position); | 494 offset.set_x(position); |
| 415 if (header_) { | |
| 416 header_->SetX(-position); | |
| 417 header_->SchedulePaintInRect(header_->GetVisibleBounds()); | |
| 418 } | |
| 419 } else if (source == vert_sb_ && vert_sb_->visible()) { | 495 } else if (source == vert_sb_ && vert_sb_->visible()) { |
| 420 position = AdjustPosition(contents_->y(), position, contents_->height(), | 496 position = AdjustPosition(offset.y(), position, contents_->height(), |
| 421 contents_viewport_->height()); | 497 contents_viewport_->height()); |
| 422 if (-contents_->y() == position) | 498 if (offset.y() == position) |
| 423 return; | 499 return; |
| 424 contents_->SetY(-position); | 500 offset.set_y(position); |
| 425 } | 501 } |
| 426 contents_->SchedulePaintInRect(contents_->GetVisibleBounds()); | 502 ScrollToOffset(offset); |
| 503 |
| 504 if (!ScrollsWithLayers()) |
| 505 contents_->SchedulePaintInRect(contents_->GetVisibleBounds()); |
| 427 } | 506 } |
| 428 | 507 |
| 429 int ScrollView::GetScrollIncrement(ScrollBar* source, bool is_page, | 508 int ScrollView::GetScrollIncrement(ScrollBar* source, bool is_page, |
| 430 bool is_positive) { | 509 bool is_positive) { |
| 431 bool is_horizontal = source->IsHorizontal(); | 510 bool is_horizontal = source->IsHorizontal(); |
| 432 int amount = 0; | 511 int amount = 0; |
| 433 if (contents_) { | 512 if (contents_) { |
| 434 if (is_page) { | 513 if (is_page) { |
| 435 amount = contents_->GetPageScrollIncrement( | 514 amount = contents_->GetPageScrollIncrement( |
| 436 this, is_horizontal, is_positive); | 515 this, is_horizontal, is_positive); |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 // corner. This is done by setting the offset to -X or -Y. For down | 576 // corner. This is done by setting the offset to -X or -Y. For down |
| 498 // or right shifts we need to make sure it appears in the | 577 // or right shifts we need to make sure it appears in the |
| 499 // lower/right corner. This is calculated by taking max_x or max_y | 578 // lower/right corner. This is calculated by taking max_x or max_y |
| 500 // and scaling it back by the size of the viewport. | 579 // and scaling it back by the size of the viewport. |
| 501 const int new_x = | 580 const int new_x = |
| 502 (vis_rect.x() > x) ? x : std::max(0, max_x - contents_viewport_->width()); | 581 (vis_rect.x() > x) ? x : std::max(0, max_x - contents_viewport_->width()); |
| 503 const int new_y = | 582 const int new_y = |
| 504 (vis_rect.y() > y) ? y : std::max(0, max_y - | 583 (vis_rect.y() > y) ? y : std::max(0, max_y - |
| 505 contents_viewport_->height()); | 584 contents_viewport_->height()); |
| 506 | 585 |
| 507 contents_->SetX(-new_x); | 586 ScrollToOffset(gfx::ScrollOffset(new_x, new_y)); |
| 508 if (header_) | |
| 509 header_->SetX(-new_x); | |
| 510 contents_->SetY(-new_y); | |
| 511 UpdateScrollBarPositions(); | 587 UpdateScrollBarPositions(); |
| 512 } | 588 } |
| 513 | 589 |
| 514 void ScrollView::ComputeScrollBarsVisibility(const gfx::Size& vp_size, | 590 void ScrollView::ComputeScrollBarsVisibility(const gfx::Size& vp_size, |
| 515 const gfx::Size& content_size, | 591 const gfx::Size& content_size, |
| 516 bool* horiz_is_shown, | 592 bool* horiz_is_shown, |
| 517 bool* vert_is_shown) const { | 593 bool* vert_is_shown) const { |
| 518 // Try to fit both ways first, then try vertical bar only, then horizontal | 594 // Try to fit both ways first, then try vertical bar only, then horizontal |
| 519 // bar only, then defaults to both shown. | 595 // bar only, then defaults to both shown. |
| 520 if (content_size.width() <= vp_size.width() && | 596 if (content_size.width() <= vp_size.width() && |
| (...skipping 27 matching lines...) Expand all Loading... |
| 548 } else { | 624 } else { |
| 549 RemoveChildView(control); | 625 RemoveChildView(control); |
| 550 control->SetVisible(false); | 626 control->SetVisible(false); |
| 551 } | 627 } |
| 552 } | 628 } |
| 553 | 629 |
| 554 void ScrollView::UpdateScrollBarPositions() { | 630 void ScrollView::UpdateScrollBarPositions() { |
| 555 if (!contents_) | 631 if (!contents_) |
| 556 return; | 632 return; |
| 557 | 633 |
| 634 const gfx::ScrollOffset offset = CurrentOffset(); |
| 558 if (horiz_sb_->visible()) { | 635 if (horiz_sb_->visible()) { |
| 559 int vw = contents_viewport_->width(); | 636 int vw = contents_viewport_->width(); |
| 560 int cw = contents_->width(); | 637 int cw = contents_->width(); |
| 561 int origin = contents_->x(); | 638 horiz_sb_->Update(vw, cw, offset.x()); |
| 562 horiz_sb_->Update(vw, cw, -origin); | |
| 563 } | 639 } |
| 564 if (vert_sb_->visible()) { | 640 if (vert_sb_->visible()) { |
| 565 int vh = contents_viewport_->height(); | 641 int vh = contents_viewport_->height(); |
| 566 int ch = contents_->height(); | 642 int ch = contents_->height(); |
| 567 int origin = contents_->y(); | 643 vert_sb_->Update(vh, ch, offset.y()); |
| 568 vert_sb_->Update(vh, ch, -origin); | |
| 569 } | 644 } |
| 570 } | 645 } |
| 571 | 646 |
| 647 gfx::ScrollOffset ScrollView::CurrentOffset() const { |
| 648 return ScrollsWithLayers() |
| 649 ? contents_->layer()->CurrentScrollOffset() |
| 650 : gfx::ScrollOffset(-contents_->x(), -contents_->y()); |
| 651 } |
| 652 |
| 653 void ScrollView::ScrollToOffset(const gfx::ScrollOffset& offset) { |
| 654 if (ScrollsWithLayers()) { |
| 655 contents_->layer()->SetScrollOffset(offset); |
| 656 |
| 657 // TODO(tapted): Remove this call to OnLayerScrolled(). It's unnecessary, |
| 658 // but will only be invoked (asynchronously) when a Compositor is present |
| 659 // and commits a frame, which isn't true in some tests. |
| 660 // See http://crbug.com/637521. |
| 661 OnLayerScrolled(); |
| 662 } else { |
| 663 contents_->SetPosition(gfx::Point(-offset.x(), -offset.y())); |
| 664 ScrollHeader(); |
| 665 } |
| 666 } |
| 667 |
| 668 bool ScrollView::ScrollsWithLayers() const { |
| 669 // Just check for the presence of a layer since it's cheaper than querying the |
| 670 // Feature flag each time. |
| 671 return contents_viewport_->layer() != nullptr; |
| 672 } |
| 673 |
| 674 void ScrollView::OnLayerScrolled() { |
| 675 UpdateScrollBarPositions(); |
| 676 ScrollHeader(); |
| 677 } |
| 678 |
| 679 void ScrollView::ScrollHeader() { |
| 680 if (!header_) |
| 681 return; |
| 682 |
| 683 int x_offset = CurrentOffset().x(); |
| 684 if (header_->x() != -x_offset) { |
| 685 header_->SetX(-x_offset); |
| 686 header_->SchedulePaintInRect(header_->GetVisibleBounds()); |
| 687 } |
| 688 } |
| 689 |
| 572 // VariableRowHeightScrollHelper ---------------------------------------------- | 690 // VariableRowHeightScrollHelper ---------------------------------------------- |
| 573 | 691 |
| 574 VariableRowHeightScrollHelper::VariableRowHeightScrollHelper( | 692 VariableRowHeightScrollHelper::VariableRowHeightScrollHelper( |
| 575 Controller* controller) : controller_(controller) { | 693 Controller* controller) : controller_(controller) { |
| 576 } | 694 } |
| 577 | 695 |
| 578 VariableRowHeightScrollHelper::~VariableRowHeightScrollHelper() { | 696 VariableRowHeightScrollHelper::~VariableRowHeightScrollHelper() { |
| 579 } | 697 } |
| 580 | 698 |
| 581 int VariableRowHeightScrollHelper::GetPageScrollIncrement( | 699 int VariableRowHeightScrollHelper::GetPageScrollIncrement( |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 636 | 754 |
| 637 VariableRowHeightScrollHelper::RowInfo | 755 VariableRowHeightScrollHelper::RowInfo |
| 638 FixedRowHeightScrollHelper::GetRowInfo(int y) { | 756 FixedRowHeightScrollHelper::GetRowInfo(int y) { |
| 639 if (y < top_margin_) | 757 if (y < top_margin_) |
| 640 return RowInfo(0, top_margin_); | 758 return RowInfo(0, top_margin_); |
| 641 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, | 759 return RowInfo((y - top_margin_) / row_height_ * row_height_ + top_margin_, |
| 642 row_height_); | 760 row_height_); |
| 643 } | 761 } |
| 644 | 762 |
| 645 } // namespace views | 763 } // namespace views |
| OLD | NEW |