Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2039)

Unified Diff: cc/input/scrollbar_animation_controller.cc

Issue 2841943002: Overlay scrollbars expand only when mouse is near thumb (Closed)
Patch Set: add tests Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: cc/input/scrollbar_animation_controller.cc
diff --git a/cc/input/scrollbar_animation_controller.cc b/cc/input/scrollbar_animation_controller.cc
index 68f98fb3bbbdadcb4af8f9207805735eb1719972..edd3ee6d24d3d1ed048d8139940f7b1a1d9e51f3 100644
--- a/cc/input/scrollbar_animation_controller.cc
+++ b/cc/input/scrollbar_animation_controller.cc
@@ -11,6 +11,42 @@
namespace cc {
+namespace {
+
+float DistanceToScrollbarTrack(const gfx::PointF& device_viewport_point,
+ const ScrollbarLayerImplBase* scrollbar) {
+ if (!scrollbar)
+ return std::numeric_limits<float>::max();
+
+ gfx::Rect scrollbar_bounds(scrollbar->bounds());
+
+ gfx::RectF device_viewport_scrollbar_bounds = MathUtil::MapClippedRect(
+ scrollbar->ScreenSpaceTransform(), gfx::RectF(scrollbar_bounds));
+
+ return device_viewport_scrollbar_bounds.ManhattanDistanceToPoint(
+ device_viewport_point) /
+ scrollbar->layer_tree_impl()->device_scale_factor();
+}
+
+float DistanceToScrollbarThumb(const gfx::PointF& device_viewport_point,
+ const ScrollbarLayerImplBase* scrollbar) {
+ if (!scrollbar)
+ return std::numeric_limits<float>::max();
+
+ gfx::Rect thumb_bounds(scrollbar->ComputeExpandedThumbQuadRect());
+
+ gfx::RectF device_viewport_scrollbar_thumb_bounds = MathUtil::MapClippedRect(
+ scrollbar->ScreenSpaceTransform(), gfx::RectF(thumb_bounds));
+
+ return device_viewport_scrollbar_thumb_bounds.ManhattanDistanceToPoint(
+ device_viewport_point) /
+ scrollbar->layer_tree_impl()->device_scale_factor();
+}
+
+const ScrollbarOrientation orientations[] = {HORIZONTAL, VERTICAL};
bokan 2017/04/28 14:15:17 This is only used for one loop. Move this into the
+
+} // namespace
+
std::unique_ptr<ScrollbarAnimationController>
ScrollbarAnimationController::CreateScrollbarAnimationControllerAndroid(
ElementId scroll_element_id,
@@ -46,7 +82,7 @@ ScrollbarAnimationController::ScrollbarAnimationController(
fade_delay_(fade_delay),
fade_out_resize_delay_(fade_out_resize_delay),
fade_duration_(fade_duration),
- need_trigger_scrollbar_show_(false),
+ need_trigger_scrollbar_fade_in_(false),
is_animating_(false),
animation_change_(NONE),
scroll_element_id_(scroll_element_id),
@@ -70,7 +106,7 @@ ScrollbarAnimationController::ScrollbarAnimationController(
fade_delay_(fade_delay),
fade_out_resize_delay_(fade_out_resize_delay),
fade_duration_(fade_duration),
- need_trigger_scrollbar_show_(false),
+ need_trigger_scrollbar_fade_in_(false),
is_animating_(false),
animation_change_(NONE),
scroll_element_id_(scroll_element_id),
@@ -80,12 +116,18 @@ ScrollbarAnimationController::ScrollbarAnimationController(
show_scrollbars_on_scroll_gesture_(true),
need_thinning_animation_(true),
weak_factory_(this) {
- vertical_controller_ = SingleScrollbarAnimationControllerThinning::Create(
- scroll_element_id, ScrollbarOrientation::VERTICAL, client,
- thinning_duration);
- horizontal_controller_ = SingleScrollbarAnimationControllerThinning::Create(
- scroll_element_id, ScrollbarOrientation::HORIZONTAL, client,
- thinning_duration);
+ scrollbar_controllers_[ScrollbarOrientation::HORIZONTAL] =
+ SingleScrollbarAnimationControllerThinning::Create(
+ scroll_element_id, ScrollbarOrientation::HORIZONTAL, client,
+ thinning_duration);
+ scrollbar_controllers_[ScrollbarOrientation::VERTICAL] =
+ SingleScrollbarAnimationControllerThinning::Create(
+ scroll_element_id, ScrollbarOrientation::VERTICAL, client,
+ thinning_duration);
+
+ mouse_is_near_scrollbar_[ScrollbarOrientation::HORIZONTAL] = false;
+ mouse_is_near_scrollbar_[ScrollbarOrientation::VERTICAL] = false;
+
ApplyOpacityToScrollbars(0.0f);
}
@@ -95,14 +137,24 @@ ScrollbarSet ScrollbarAnimationController::Scrollbars() const {
return client_->ScrollbarsFor(scroll_element_id_);
}
+ScrollbarLayerImplBase* ScrollbarAnimationController::GetScrollbar(
+ ScrollbarOrientation orientation) const {
+ for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) {
+ if (!scrollbar->is_overlay_scrollbar())
+ continue;
+
+ if (scrollbar->orientation() == orientation)
+ return scrollbar;
+ }
+
+ return nullptr;
+}
+
SingleScrollbarAnimationControllerThinning&
ScrollbarAnimationController::GetScrollbarAnimationController(
ScrollbarOrientation orientation) const {
DCHECK(need_thinning_animation_);
- if (orientation == ScrollbarOrientation::VERTICAL)
- return *(vertical_controller_.get());
- else
- return *(horizontal_controller_.get());
+ return *scrollbar_controllers_[orientation].get();
}
void ScrollbarAnimationController::StartAnimation() {
@@ -151,8 +203,8 @@ bool ScrollbarAnimationController::Animate(base::TimeTicks now) {
}
if (need_thinning_animation_) {
- animated |= vertical_controller_->Animate(now);
- animated |= horizontal_controller_->Animate(now);
+ for (auto& controller : scrollbar_controllers_)
+ animated |= controller->Animate(now);
}
return animated;
@@ -219,8 +271,8 @@ void ScrollbarAnimationController::DidScrollUpdate() {
}
if (need_thinning_animation_) {
- vertical_controller_->UpdateThumbThicknessScale();
- horizontal_controller_->UpdateThumbThicknessScale();
+ for (auto& controller : scrollbar_controllers_)
+ controller->UpdateThumbThicknessScale();
}
}
@@ -250,16 +302,16 @@ void ScrollbarAnimationController::DidMouseDown() {
if (!need_thinning_animation_ || ScrollbarsHidden())
return;
- vertical_controller_->DidMouseDown();
- horizontal_controller_->DidMouseDown();
+ for (auto& controller : scrollbar_controllers_)
+ controller->DidMouseDown();
}
void ScrollbarAnimationController::DidMouseUp() {
if (!need_thinning_animation_ || !Captured())
return;
- vertical_controller_->DidMouseUp();
- horizontal_controller_->DidMouseUp();
+ for (auto& controller : scrollbar_controllers_)
+ controller->DidMouseUp();
if (!MouseIsNearAnyScrollbar())
PostDelayedAnimation(FADE_OUT, false);
@@ -269,11 +321,14 @@ void ScrollbarAnimationController::DidMouseLeave() {
if (!need_thinning_animation_)
return;
- vertical_controller_->DidMouseLeave();
- horizontal_controller_->DidMouseLeave();
+ for (auto& controller : scrollbar_controllers_)
+ controller->DidMouseLeave();
+
+ for (bool& mouse_is_near_scrollbar : mouse_is_near_scrollbar_)
+ mouse_is_near_scrollbar = false;
delayed_scrollbar_animation_.Cancel();
- need_trigger_scrollbar_show_ = false;
+ need_trigger_scrollbar_fade_in_ = false;
if (ScrollbarsHidden() || Captured())
return;
@@ -281,25 +336,36 @@ void ScrollbarAnimationController::DidMouseLeave() {
PostDelayedAnimation(FADE_OUT, false);
}
-void ScrollbarAnimationController::DidMouseMoveNear(
- ScrollbarOrientation orientation,
- float distance) {
+void ScrollbarAnimationController::DidMouseMove(
+ const gfx::PointF& device_viewport_point) {
if (!need_thinning_animation_)
return;
- bool need_trigger_scrollbar_show_before = need_trigger_scrollbar_show_;
+ bool need_trigger_scrollbar_fade_in_before = need_trigger_scrollbar_fade_in_;
+
+ for (ScrollbarOrientation orientation : orientations) {
+ ScrollbarLayerImplBase* scrollbar = GetScrollbar(orientation);
+ float distance_to_scrollbar_track =
+ DistanceToScrollbarTrack(device_viewport_point, scrollbar);
+ float distance_to_scrollbar_thumb =
+ DistanceToScrollbarThumb(device_viewport_point, scrollbar);
- GetScrollbarAnimationController(orientation).DidMouseMoveNear(distance);
+ GetScrollbarAnimationController(orientation)
+ .DidMouseMove(distance_to_scrollbar_thumb);
+
+ mouse_is_near_scrollbar_[orientation] =
+ distance_to_scrollbar_track < kMouseMoveDistanceToTriggerFadeIn;
+ }
- need_trigger_scrollbar_show_ =
- CalcNeedTriggerScrollbarShow(orientation, distance);
+ need_trigger_scrollbar_fade_in_ = MouseIsNearAnyScrollbar();
if (Captured())
return;
if (ScrollbarsHidden()) {
- if (need_trigger_scrollbar_show_before != need_trigger_scrollbar_show_) {
- if (need_trigger_scrollbar_show_) {
+ if (need_trigger_scrollbar_fade_in_before !=
+ need_trigger_scrollbar_fade_in_) {
+ if (need_trigger_scrollbar_fade_in_) {
PostDelayedAnimation(FADE_IN, false);
} else {
delayed_scrollbar_animation_.Cancel();
@@ -315,42 +381,29 @@ void ScrollbarAnimationController::DidMouseMoveNear(
}
}
-bool ScrollbarAnimationController::CalcNeedTriggerScrollbarShow(
- ScrollbarOrientation orientation,
- float distance) const {
+bool ScrollbarAnimationController::MouseIsOverScrollbarThumb(
+ ScrollbarOrientation orientation) const {
DCHECK(need_thinning_animation_);
-
- if (vertical_controller_->mouse_is_over_scrollbar() ||
- horizontal_controller_->mouse_is_over_scrollbar())
- return true;
-
- for (ScrollbarLayerImplBase* scrollbar : Scrollbars()) {
- if (scrollbar->orientation() != orientation)
- continue;
-
- if (distance < kMouseMoveDistanceToTriggerFadeIn)
- return true;
- }
-
- return false;
+ return GetScrollbarAnimationController(orientation)
+ .mouse_is_over_scrollbar_thumb();
}
-bool ScrollbarAnimationController::MouseIsOverScrollbar(
+bool ScrollbarAnimationController::MouseIsNearScrollbarThumb(
ScrollbarOrientation orientation) const {
DCHECK(need_thinning_animation_);
- return GetScrollbarAnimationController(orientation).mouse_is_over_scrollbar();
+ return GetScrollbarAnimationController(orientation)
+ .mouse_is_near_scrollbar_thumb();
}
bool ScrollbarAnimationController::MouseIsNearScrollbar(
ScrollbarOrientation orientation) const {
DCHECK(need_thinning_animation_);
- return GetScrollbarAnimationController(orientation).mouse_is_near_scrollbar();
+ return mouse_is_near_scrollbar_[orientation];
}
bool ScrollbarAnimationController::MouseIsNearAnyScrollbar() const {
DCHECK(need_thinning_animation_);
- return vertical_controller_->mouse_is_near_scrollbar() ||
- horizontal_controller_->mouse_is_near_scrollbar();
+ return MouseIsNearScrollbar(VERTICAL) || MouseIsNearScrollbar(HORIZONTAL);
}
bool ScrollbarAnimationController::ScrollbarsHidden() const {
@@ -359,7 +412,8 @@ bool ScrollbarAnimationController::ScrollbarsHidden() const {
bool ScrollbarAnimationController::Captured() const {
DCHECK(need_thinning_animation_);
- return vertical_controller_->captured() || horizontal_controller_->captured();
+ return GetScrollbarAnimationController(VERTICAL).captured() ||
+ GetScrollbarAnimationController(HORIZONTAL).captured();
}
void ScrollbarAnimationController::Show() {

Powered by Google App Engine
This is Rietveld 408576698