diff options
author | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-12 14:27:29 +0200 |
---|---|---|
committer | Allan Sandfeld Jensen <allan.jensen@qt.io> | 2020-10-13 09:35:20 +0000 |
commit | c30a6232df03e1efbd9f3b226777b07e087a1122 (patch) | |
tree | e992f45784689f373bcc38d1b79a239ebe17ee23 /chromium/cc/input/scrollbar_controller.cc | |
parent | 7b5b123ac58f58ffde0f4f6e488bcd09aa4decd3 (diff) | |
download | qtwebengine-chromium-85-based.tar.gz |
BASELINE: Update Chromium to 85.0.4183.14085-based
Change-Id: Iaa42f4680837c57725b1344f108c0196741f6057
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'chromium/cc/input/scrollbar_controller.cc')
-rw-r--r-- | chromium/cc/input/scrollbar_controller.cc | 171 |
1 files changed, 99 insertions, 72 deletions
diff --git a/chromium/cc/input/scrollbar_controller.cc b/chromium/cc/input/scrollbar_controller.cc index aac1d4ceccd..490cd44d1d4 100644 --- a/chromium/cc/input/scrollbar_controller.cc +++ b/chromium/cc/input/scrollbar_controller.cc @@ -8,6 +8,7 @@ #include "base/cancelable_callback.h" #include "cc/base/math_util.h" +#include "cc/input/scroll_utils.h" #include "cc/input/scrollbar.h" #include "cc/input/scrollbar_controller.h" #include "cc/trees/layer_tree_impl.h" @@ -35,7 +36,7 @@ void ScrollbarController::WillBeginImplFrame() { } // Retrieves the ScrollbarLayerImplBase corresponding to the stashed ElementId. -ScrollbarLayerImplBase* ScrollbarController::ScrollbarLayer() { +ScrollbarLayerImplBase* ScrollbarController::ScrollbarLayer() const { if (!captured_scrollbar_metadata_.has_value()) return nullptr; @@ -52,7 +53,7 @@ ScrollbarLayerImplBase* ScrollbarController::ScrollbarLayer() { // GSU. InputHandlerPointerResult ScrollbarController::HandlePointerDown( const gfx::PointF position_in_widget, - bool shift_modifier) { + bool jump_key_modifier) { LayerImpl* layer_impl = GetLayerHitByPoint(position_in_widget); // If a non-custom scrollbar layer was not found, we return early as there is @@ -88,12 +89,15 @@ InputHandlerPointerResult ScrollbarController::HandlePointerDown( scroll_result.type = PointerResultType::kScrollbarScroll; layer_tree_host_impl_->active_tree()->UpdateScrollbarGeometries(); const ScrollbarPart scrollbar_part = - GetScrollbarPartFromPointerDown(scrollbar, position_in_widget); + GetScrollbarPartFromPointerDown(position_in_widget); + const bool perform_jump_click_on_track = + scrollbar->JumpOnTrackClick() != jump_key_modifier; scroll_result.scroll_offset = GetScrollOffsetForScrollbarPart( - scrollbar, scrollbar_part, shift_modifier); + scrollbar_part, perform_jump_click_on_track); last_known_pointer_position_ = position_in_widget; scrollbar_scroll_is_active_ = true; - scroll_result.scroll_units = Granularity(scrollbar_part, shift_modifier); + scroll_result.scroll_units = + Granularity(scrollbar_part, perform_jump_click_on_track); if (scrollbar_part == ScrollbarPart::THUMB) { drag_state_ = DragState(); drag_state_->drag_origin = position_in_widget; @@ -112,12 +116,12 @@ InputHandlerPointerResult ScrollbarController::HandlePointerDown( // have the potential of initiating an autoscroll (if held down for long // enough). DCHECK(scrollbar_part != ScrollbarPart::THUMB); - cancelable_autoscroll_task_ = std::make_unique<base::CancelableOnceClosure>( - base::BindOnce(&ScrollbarController::StartAutoScrollAnimation, - base::Unretained(this), - InitialDeltaToAutoscrollVelocity( - scrollbar, scroll_result.scroll_offset), - scrollbar, scrollbar_part)); + cancelable_autoscroll_task_ = + std::make_unique<base::CancelableOnceClosure>(base::BindOnce( + &ScrollbarController::StartAutoScrollAnimation, + base::Unretained(this), + InitialDeltaToAutoscrollVelocity(scroll_result.scroll_offset), + scrollbar_part)); layer_tree_host_impl_->task_runner_provider() ->ImplThreadTaskRunner() ->PostDelayedTask(FROM_HERE, cancelable_autoscroll_task_->callback(), @@ -127,16 +131,16 @@ InputHandlerPointerResult ScrollbarController::HandlePointerDown( } bool ScrollbarController::SnapToDragOrigin( - const ScrollbarLayerImplBase* scrollbar, - const gfx::PointF pointer_position_in_widget) { + const gfx::PointF pointer_position_in_widget) const { // Consult the ScrollbarTheme to check if thumb snapping is supported on the // current platform. + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); if (!(scrollbar && scrollbar->SupportsDragSnapBack())) return false; bool clipped = false; - const gfx::PointF pointer_position_in_layer = GetScrollbarRelativePosition( - scrollbar, pointer_position_in_widget, &clipped); + const gfx::PointF pointer_position_in_layer = + GetScrollbarRelativePosition(pointer_position_in_widget, &clipped); if (clipped) return false; @@ -189,10 +193,10 @@ bool ScrollbarController::SnapToDragOrigin( ui::ScrollGranularity ScrollbarController::Granularity( const ScrollbarPart scrollbar_part, - const bool shift_modifier) { + const bool jump_key_modifier) const { const bool shift_click_on_scrollbar_track = - shift_modifier && (scrollbar_part == ScrollbarPart::FORWARD_TRACK || - scrollbar_part == ScrollbarPart::BACK_TRACK); + jump_key_modifier && (scrollbar_part == ScrollbarPart::FORWARD_TRACK || + scrollbar_part == ScrollbarPart::BACK_TRACK); if (shift_click_on_scrollbar_track || scrollbar_part == ScrollbarPart::THUMB) return ui::ScrollGranularity::kScrollByPrecisePixel; @@ -201,17 +205,17 @@ ui::ScrollGranularity ScrollbarController::Granularity( return ui::ScrollGranularity::kScrollByPixel; } -float ScrollbarController::GetScrollDeltaForAbsoluteJump( - const ScrollbarLayerImplBase* scrollbar) { +float ScrollbarController::GetScrollDeltaForAbsoluteJump() const { layer_tree_host_impl_->active_tree()->UpdateScrollbarGeometries(); bool clipped = false; - const gfx::PointF pointer_position_in_layer = GetScrollbarRelativePosition( - scrollbar, last_known_pointer_position_, &clipped); + const gfx::PointF pointer_position_in_layer = + GetScrollbarRelativePosition(last_known_pointer_position_, &clipped); if (clipped) return 0; + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); const float pointer_location = scrollbar->orientation() == ScrollbarOrientation::VERTICAL ? pointer_position_in_layer.y() @@ -232,19 +236,18 @@ float ScrollbarController::GetScrollDeltaForAbsoluteJump( const float delta = round(std::abs(desired_thumb_origin - current_thumb_origin)); - return delta * GetScrollerToScrollbarRatio(scrollbar); + return delta * GetScrollerToScrollbarRatio(); } int ScrollbarController::GetScrollDeltaForDragPosition( - const ScrollbarLayerImplBase* scrollbar, - const gfx::PointF pointer_position_in_widget) { + const gfx::PointF pointer_position_in_widget) const { + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); const float pointer_delta = scrollbar->orientation() == ScrollbarOrientation::VERTICAL ? pointer_position_in_widget.y() - drag_state_->drag_origin.y() : pointer_position_in_widget.x() - drag_state_->drag_origin.x(); - const float new_offset = - pointer_delta * GetScrollerToScrollbarRatio(scrollbar); + const float new_offset = pointer_delta * GetScrollerToScrollbarRatio(); const float scroll_delta = drag_state_->scroll_position_at_start_ + new_offset - scrollbar->current_pos(); @@ -276,7 +279,7 @@ InputHandlerPointerResult ScrollbarController::HandlePointerMove( if (drag_processed_for_current_frame_) return scroll_result; - if (SnapToDragOrigin(scrollbar, position_in_widget)) { + if (SnapToDragOrigin(position_in_widget)) { const float delta = scrollbar->current_pos() - drag_state_->scroll_position_at_start_; scroll_result.scroll_units = ui::ScrollGranularity::kScrollByPrecisePixel; @@ -302,7 +305,7 @@ InputHandlerPointerResult ScrollbarController::HandlePointerMove( // valid ScrollNode. DCHECK(target_node); - int delta = GetScrollDeltaForDragPosition(scrollbar, position_in_widget); + int delta = GetScrollDeltaForDragPosition(position_in_widget); if (drag_state_->scroller_length_at_previous_move != scrollbar->scroll_layer_length()) { drag_state_->scroller_displacement = delta; @@ -339,8 +342,7 @@ InputHandlerPointerResult ScrollbarController::HandlePointerMove( return scroll_result; } -float ScrollbarController::GetScrollerToScrollbarRatio( - const ScrollbarLayerImplBase* scrollbar) { +float ScrollbarController::GetScrollerToScrollbarRatio() const { // Calculating the delta by which the scroller layer should move when // dragging the thumb depends on the following factors: // - scrollbar_track_length @@ -381,6 +383,7 @@ float ScrollbarController::GetScrollerToScrollbarRatio( // |<- scrollbar_thumb_length ->| // layer_tree_host_impl_->active_tree()->UpdateScrollbarGeometries(); + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); float scroll_layer_length = scrollbar->scroll_layer_length(); float scrollbar_track_length = scrollbar->TrackLength(); gfx::Rect thumb_rect(scrollbar->ComputeThumbQuadRect()); @@ -388,7 +391,7 @@ float ScrollbarController::GetScrollerToScrollbarRatio( scrollbar->orientation() == ScrollbarOrientation::VERTICAL ? thumb_rect.height() : thumb_rect.width(); - int viewport_length = GetViewportLength(scrollbar); + int viewport_length = GetViewportLength(); return (scroll_layer_length - viewport_length) / (scrollbar_track_length - scrollbar_thumb_length); @@ -399,11 +402,18 @@ void ScrollbarController::ResetState() { drag_state_ = base::nullopt; autoscroll_state_ = base::nullopt; captured_scrollbar_metadata_ = base::nullopt; + if (cancelable_autoscroll_task_) { + cancelable_autoscroll_task_->Cancel(); + cancelable_autoscroll_task_.reset(); + } } -void ScrollbarController::DidUnregisterScrollbar(ElementId element_id) { +void ScrollbarController::DidUnregisterScrollbar( + ElementId element_id, + ScrollbarOrientation orientation) { if (captured_scrollbar_metadata_.has_value() && - captured_scrollbar_metadata_->scroll_element_id == element_id) + captured_scrollbar_metadata_->scroll_element_id == element_id && + captured_scrollbar_metadata_->orientation == orientation) ResetState(); } @@ -413,12 +423,10 @@ void ScrollbarController::RecomputeAutoscrollStateIfNeeded() { return; layer_tree_host_impl_->active_tree()->UpdateScrollbarGeometries(); - const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); - const gfx::Rect thumb_quad = scrollbar->ComputeThumbQuadRect(); bool clipped; - gfx::PointF scroller_relative_position(GetScrollbarRelativePosition( - scrollbar, last_known_pointer_position_, &clipped)); + gfx::PointF scroller_relative_position( + GetScrollbarRelativePosition(last_known_pointer_position_, &clipped)); if (clipped) return; @@ -429,6 +437,8 @@ void ScrollbarController::RecomputeAutoscrollStateIfNeeded() { int thumb_start = 0; int thumb_end = 0; int pointer_position = 0; + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); + const gfx::Rect thumb_quad = scrollbar->ComputeThumbQuadRect(); if (scrollbar->orientation() == ScrollbarOrientation::VERTICAL) { thumb_start = thumb_quad.y(); thumb_end = thumb_quad.y() + thumb_quad.height(); @@ -458,7 +468,7 @@ void ScrollbarController::RecomputeAutoscrollStateIfNeeded() { const float scroll_layer_length = scrollbar->scroll_layer_length(); if (autoscroll_state_->scroll_layer_length != scroll_layer_length) { layer_tree_host_impl_->mutator_host()->ScrollAnimationAbort(); - StartAutoScrollAnimation(autoscroll_state_->velocity, scrollbar, + StartAutoScrollAnimation(autoscroll_state_->velocity, autoscroll_state_->pressed_scrollbar_part); } } @@ -466,8 +476,8 @@ void ScrollbarController::RecomputeAutoscrollStateIfNeeded() { // The animations need to be aborted/restarted based on the pointer location // (i.e leaving/entering the track/arrows, reaching the track end etc). The // autoscroll_state_ however, needs to be reset on pointer changes. - const gfx::RectF scrollbar_part_rect(GetRectForScrollbarPart( - scrollbar, autoscroll_state_->pressed_scrollbar_part)); + const gfx::RectF scrollbar_part_rect( + GetRectForScrollbarPart(autoscroll_state_->pressed_scrollbar_part)); if (!scrollbar_part_rect.Contains(scroller_relative_position)) { // Stop animating if pointer moves outside the rect bounds. layer_tree_host_impl_->mutator_host()->ScrollAnimationAbort(); @@ -475,17 +485,17 @@ void ScrollbarController::RecomputeAutoscrollStateIfNeeded() { !layer_tree_host_impl_->mutator_host()->IsElementAnimating( scrollbar->scroll_element_id())) { // Start animating if pointer re-enters the bounds. - StartAutoScrollAnimation(autoscroll_state_->velocity, scrollbar, + StartAutoScrollAnimation(autoscroll_state_->velocity, autoscroll_state_->pressed_scrollbar_part); } } // Helper to calculate the autoscroll velocity. float ScrollbarController::InitialDeltaToAutoscrollVelocity( - const ScrollbarLayerImplBase* scrollbar, gfx::ScrollOffset scroll_offset) const { + DCHECK(captured_scrollbar_metadata_.has_value()); const float scroll_delta = - scrollbar->orientation() == ScrollbarOrientation::VERTICAL + ScrollbarLayer()->orientation() == ScrollbarOrientation::VERTICAL ? scroll_offset.y() : scroll_offset.x(); return scroll_delta * kAutoscrollMultiplier; @@ -493,15 +503,17 @@ float ScrollbarController::InitialDeltaToAutoscrollVelocity( void ScrollbarController::StartAutoScrollAnimation( const float velocity, - const ScrollbarLayerImplBase* scrollbar, ScrollbarPart pressed_scrollbar_part) { // Autoscroll and thumb drag are mutually exclusive. Both can't be active at // the same time. DCHECK(!drag_state_.has_value()); + DCHECK(captured_scrollbar_metadata_.has_value()); DCHECK_NE(velocity, 0); + DCHECK(ScrollbarLayer()); // scroll_node is set up while handling GSB. If there's no node to scroll, we // don't need to create any animation for it. + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); ScrollTree& scroll_tree = layer_tree_host_impl_->active_tree()->property_trees()->scroll_tree; ScrollNode* scroll_node = @@ -552,18 +564,13 @@ InputHandlerPointerResult ScrollbarController::HandlePointerUp( if (autoscroll_state_.has_value()) layer_tree_host_impl_->mutator_host()->ScrollAnimationAbort(); - if (cancelable_autoscroll_task_) { - cancelable_autoscroll_task_->Cancel(); - cancelable_autoscroll_task_.reset(); - } - ResetState(); return scroll_result; } // Returns the layer that is hit by the position_in_widget. LayerImpl* ScrollbarController::GetLayerHitByPoint( - const gfx::PointF position_in_widget) { + const gfx::PointF position_in_widget) const { LayerTreeImpl* active_tree = layer_tree_host_impl_->active_tree(); gfx::Point viewport_point(position_in_widget.x(), position_in_widget.y()); @@ -575,8 +582,8 @@ LayerImpl* ScrollbarController::GetLayerHitByPoint( return layer_impl; } -int ScrollbarController::GetViewportLength( - const ScrollbarLayerImplBase* scrollbar) const { +int ScrollbarController::GetViewportLength() const { + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); const ScrollNode* scroll_node = layer_tree_host_impl_->active_tree() ->property_trees() @@ -587,30 +594,51 @@ int ScrollbarController::GetViewportLength( : scroll_node->container_bounds.width(); } +int ScrollbarController::GetScrollDeltaForPercentBasedScroll() const { + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); + + const ScrollNode* scroll_node = + layer_tree_host_impl_->active_tree() + ->property_trees() + ->scroll_tree.FindNodeFromElementId(scrollbar->scroll_element_id()); + DCHECK(scroll_node); + + const gfx::Vector2dF scroll_delta = + scrollbar->orientation() == ScrollbarOrientation::VERTICAL + ? gfx::Vector2dF(0, kPercentDeltaForDirectionalScroll) + : gfx::Vector2dF(kPercentDeltaForDirectionalScroll, 0); + + const gfx::Vector2dF pixel_delta = + layer_tree_host_impl_->ResolveScrollGranularityToPixels( + *scroll_node, scroll_delta, + ui::ScrollGranularity::kScrollByPercentage); + + return scrollbar->orientation() == ScrollbarOrientation::VERTICAL + ? pixel_delta.y() + : pixel_delta.x(); +} + int ScrollbarController::GetScrollDeltaForScrollbarPart( - const ScrollbarLayerImplBase* scrollbar, const ScrollbarPart scrollbar_part, - const bool shift_modifier) { + const bool jump_key_modifier) const { int scroll_delta = 0; switch (scrollbar_part) { case ScrollbarPart::BACK_BUTTON: case ScrollbarPart::FORWARD_BUTTON: if (layer_tree_host_impl_->settings().percent_based_scrolling) { - scroll_delta = - kPercentDeltaForDirectionalScroll * GetViewportLength(scrollbar); + scroll_delta = GetScrollDeltaForPercentBasedScroll(); } else { scroll_delta = kPixelsPerLineStep * ScreenSpaceScaleFactor(); } break; case ScrollbarPart::BACK_TRACK: case ScrollbarPart::FORWARD_TRACK: { - if (shift_modifier) { - scroll_delta = GetScrollDeltaForAbsoluteJump(scrollbar); + if (jump_key_modifier) { + scroll_delta = GetScrollDeltaForAbsoluteJump(); break; } - scroll_delta = - GetViewportLength(scrollbar) * kMinFractionToStepWhenPaging; + scroll_delta = GetViewportLength() * kMinFractionToStepWhenPaging; break; } default: @@ -634,9 +662,8 @@ float ScrollbarController::ScreenSpaceScaleFactor() const { } gfx::PointF ScrollbarController::GetScrollbarRelativePosition( - const ScrollbarLayerImplBase* scrollbar, const gfx::PointF position_in_widget, - bool* clipped) { + bool* clipped) const { gfx::Transform inverse_screen_space_transform( gfx::Transform::kSkipInitialization); @@ -647,7 +674,7 @@ gfx::PointF ScrollbarController::GetScrollbarRelativePosition( ? 1.f / layer_tree_host_impl_->active_tree()->device_scale_factor() : 1.f; gfx::Transform scaled_screen_space_transform( - scrollbar->ScreenSpaceTransform()); + ScrollbarLayer()->ScreenSpaceTransform()); scaled_screen_space_transform.PostScale(scale, scale); if (!scaled_screen_space_transform.GetInverse( &inverse_screen_space_transform)) @@ -659,14 +686,14 @@ gfx::PointF ScrollbarController::GetScrollbarRelativePosition( // Determines the ScrollbarPart based on the position_in_widget. ScrollbarPart ScrollbarController::GetScrollbarPartFromPointerDown( - const ScrollbarLayerImplBase* scrollbar, - const gfx::PointF position_in_widget) { + const gfx::PointF position_in_widget) const { // position_in_widget needs to be transformed and made relative to the // scrollbar layer because hit testing assumes layer relative coordinates. + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); bool clipped = false; const gfx::PointF scroller_relative_position( - GetScrollbarRelativePosition(scrollbar, position_in_widget, &clipped)); + GetScrollbarRelativePosition(position_in_widget, &clipped)); if (clipped) return ScrollbarPart::NO_PART; @@ -676,8 +703,8 @@ ScrollbarPart ScrollbarController::GetScrollbarPartFromPointerDown( // Determines the corresponding rect for the given scrollbar part. gfx::Rect ScrollbarController::GetRectForScrollbarPart( - const ScrollbarLayerImplBase* scrollbar, - const ScrollbarPart scrollbar_part) { + const ScrollbarPart scrollbar_part) const { + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); if (scrollbar_part == ScrollbarPart::BACK_BUTTON) return scrollbar->BackButtonRect(); if (scrollbar_part == ScrollbarPart::FORWARD_BUTTON) @@ -692,11 +719,11 @@ gfx::Rect ScrollbarController::GetRectForScrollbarPart( // Determines the scroll offsets based on the ScrollbarPart and the scrollbar // orientation. gfx::ScrollOffset ScrollbarController::GetScrollOffsetForScrollbarPart( - const ScrollbarLayerImplBase* scrollbar, const ScrollbarPart scrollbar_part, - const bool shift_modifier) { + const bool jump_key_modifier) const { + const ScrollbarLayerImplBase* scrollbar = ScrollbarLayer(); float scroll_delta = - GetScrollDeltaForScrollbarPart(scrollbar, scrollbar_part, shift_modifier); + GetScrollDeltaForScrollbarPart(scrollbar_part, jump_key_modifier); // See CreateScrollStateForGesture for more information on how these values // will be interpreted. |