Skip to content

Commit

Permalink
Hide scrollbars when their dimensions are not scrollable.
Browse files Browse the repository at this point in the history
Make scrollbars disappear by setting their opacity to 0
(i.e. making them completely transparent) when they
cannot be scrolled.

This CL has been reviewed on https://codereview.chromium.org/640063002/

BUG=370892

Review URL: https://codereview.chromium.org/655783004

Cr-Commit-Position: refs/heads/master@{#299628}
  • Loading branch information
timav authored and Commit bot committed Oct 15, 2014
1 parent b43b921 commit 3ca6969
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cc/animation/scrollbar_animation_controller_linear_fade.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ void ScrollbarAnimationControllerLinearFade::ApplyOpacityToScrollbars(
it != scrollbars->end();
++it) {
ScrollbarLayerImplBase* scrollbar = *it;

if (scrollbar->is_overlay_scrollbar())
scrollbar->SetOpacity(opacity);
scrollbar->SetOpacity(scrollbar->can_scroll_orientation() ? opacity : 0);
}
}

Expand Down
139 changes: 137 additions & 2 deletions cc/animation/scrollbar_animation_controller_linear_fade_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "cc/layers/solid_color_scrollbar_layer_impl.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/trees/layer_tree_impl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
Expand Down Expand Up @@ -41,7 +43,7 @@ class ScrollbarAnimationControllerLinearFadeTest
scrollbar_layer_ =
SolidColorScrollbarLayerImpl::Create(host_impl_.active_tree(),
2,
HORIZONTAL,
orientation(),
kThumbThickness,
kTrackStart,
kIsLeftSideVerticalScrollbar,
Expand All @@ -54,7 +56,7 @@ class ScrollbarAnimationControllerLinearFadeTest
scrollbar_layer_->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr->id(),
clip_layer_->id());
clip_layer_->SetBounds(gfx::Size(100, 100));
scroll_layer_ptr->SetBounds(gfx::Size(50, 50));
scroll_layer_ptr->SetBounds(gfx::Size(200, 200));

scrollbar_controller_ = ScrollbarAnimationControllerLinearFade::Create(
scroll_layer_ptr,
Expand All @@ -64,6 +66,8 @@ class ScrollbarAnimationControllerLinearFadeTest
base::TimeDelta::FromSeconds(3));
}

virtual ScrollbarOrientation orientation() const { return HORIZONTAL; }

FakeImplProxy proxy_;
TestSharedBitmapManager shared_bitmap_manager_;
FakeLayerTreeHostImpl host_impl_;
Expand All @@ -76,6 +80,12 @@ class ScrollbarAnimationControllerLinearFadeTest
int needs_frame_count_;
};

class VerticalScrollbarAnimationControllerLinearFadeTest
: public ScrollbarAnimationControllerLinearFadeTest {
protected:
virtual ScrollbarOrientation orientation() const override { return VERTICAL; }
};

TEST_F(ScrollbarAnimationControllerLinearFadeTest, DelayAnimationOnResize) {
scrollbar_layer_->SetOpacity(0.0f);
scrollbar_controller_->DidScrollBegin();
Expand Down Expand Up @@ -119,6 +129,131 @@ TEST_F(ScrollbarAnimationControllerLinearFadeTest,
EXPECT_EQ(0, needs_frame_count_);
}

TEST_F(ScrollbarAnimationControllerLinearFadeTest, HideOnResize) {
LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer->bounds());

EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());

// Shrink along X axis, horizontal scrollbar should appear.
clip_layer_->SetBounds(gfx::Size(100, 200));
EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();

// Shrink along Y axis and expand along X, horizontal scrollbar
// should disappear.
clip_layer_->SetBounds(gfx::Size(200, 100));
EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest, HideOnResize) {
LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer->bounds());

EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());

// Shrink along X axis, vertical scrollbar should remain invisible.
clip_layer_->SetBounds(gfx::Size(100, 200));
EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();

// Shrink along Y axis and expand along X, vertical scrollbar should appear.
clip_layer_->SetBounds(gfx::Size(200, 100));
EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(ScrollbarAnimationControllerLinearFadeTest,
HideOnUserNonScrollableHorz) {
EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());

LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
scroll_layer->set_user_scrollable_horizontal(false);

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(ScrollbarAnimationControllerLinearFadeTest,
ShowOnUserNonScrollableVert) {
EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());

LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
scroll_layer->set_user_scrollable_vertical(false);

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest,
HideOnUserNonScrollableVert) {
EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());

LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
scroll_layer->set_user_scrollable_vertical(false);

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(VerticalScrollbarAnimationControllerLinearFadeTest,
ShowOnUserNonScrollableHorz) {
EXPECT_EQ(VERTICAL, scrollbar_layer_->orientation());

LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
scroll_layer->set_user_scrollable_horizontal(false);

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

TEST_F(ScrollbarAnimationControllerLinearFadeTest, AwakenByScrollingGesture) {
base::TimeTicks time;
time += base::TimeDelta::FromSeconds(1);
Expand Down
8 changes: 6 additions & 2 deletions cc/animation/scrollbar_animation_controller_thinning.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,12 @@ void ScrollbarAnimationControllerThinning::ApplyOpacityAndThumbThicknessScale(
++it) {
ScrollbarLayerImplBase* scrollbar = *it;
if (scrollbar->is_overlay_scrollbar()) {
scrollbar->SetOpacity(
AdjustScale(opacity, scrollbar->opacity(), opacity_change_));
float effectiveOpacity =
scrollbar->can_scroll_orientation()
? AdjustScale(opacity, scrollbar->opacity(), opacity_change_)
: 0;

scrollbar->SetOpacity(effectiveOpacity);
scrollbar->SetThumbThicknessScaleFactor(
AdjustScale(thumb_thickness_scale,
scrollbar->thumb_thickness_scale_factor(),
Expand Down
36 changes: 35 additions & 1 deletion cc/animation/scrollbar_animation_controller_thinning_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include "cc/layers/solid_color_scrollbar_layer_impl.h"
#include "cc/test/fake_impl_proxy.h"
#include "cc/test/fake_layer_tree_host_impl.h"
#include "cc/test/geometry_test_utils.h"
#include "cc/test/test_shared_bitmap_manager.h"
#include "cc/trees/layer_tree_impl.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace cc {
Expand Down Expand Up @@ -52,7 +54,7 @@ class ScrollbarAnimationControllerThinningTest
scrollbar_layer_->SetScrollLayerAndClipLayerByIds(scroll_layer_ptr->id(),
clip_layer_->id());
clip_layer_->SetBounds(gfx::Size(100, 100));
scroll_layer_ptr->SetBounds(gfx::Size(50, 50));
scroll_layer_ptr->SetBounds(gfx::Size(200, 200));

scrollbar_controller_ = ScrollbarAnimationControllerThinning::Create(
scroll_layer_ptr,
Expand All @@ -79,6 +81,38 @@ TEST_F(ScrollbarAnimationControllerThinningTest, Idle) {
EXPECT_FLOAT_EQ(0.4f, scrollbar_layer_->thumb_thickness_scale_factor());
}

// Check that scrollbar disappears when the layer becomes non-scrollable.
TEST_F(ScrollbarAnimationControllerThinningTest, HideOnResize) {
LayerImpl* scroll_layer = host_impl_.active_tree()->LayerById(1);
ASSERT_TRUE(scroll_layer);
EXPECT_SIZE_EQ(gfx::Size(200, 200), scroll_layer->bounds());

EXPECT_EQ(HORIZONTAL, scrollbar_layer_->orientation());

// Shrink along X axis, horizontal scrollbar should appear.
clip_layer_->SetBounds(gfx::Size(100, 200));
EXPECT_SIZE_EQ(gfx::Size(100, 200), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(1.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();

// Shrink along Y axis and expand along X, horizontal scrollbar
// should disappear.
clip_layer_->SetBounds(gfx::Size(200, 100));
EXPECT_SIZE_EQ(gfx::Size(200, 100), clip_layer_->bounds());

scrollbar_controller_->DidScrollBegin();

scrollbar_controller_->DidScrollUpdate(false);
EXPECT_FLOAT_EQ(0.0f, scrollbar_layer_->opacity());

scrollbar_controller_->DidScrollEnd();
}

// Scroll content. Confirm the scrollbar gets dark and then becomes light
// after stopping.
TEST_F(ScrollbarAnimationControllerThinningTest, AwakenByProgrammaticScroll) {
Expand Down
5 changes: 5 additions & 0 deletions cc/layers/layer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,11 @@ void LayerImpl::SetScrollClipLayer(int scroll_clip_layer_id) {
scroll_clip_layer_ = layer_tree_impl()->LayerById(scroll_clip_layer_id);
}

bool LayerImpl::user_scrollable(ScrollbarOrientation orientation) const {
return (orientation == HORIZONTAL) ? user_scrollable_horizontal_
: user_scrollable_vertical_;
}

void LayerImpl::ApplySentScrollDeltasFromAbortedCommit() {
// Pending tree never has sent scroll deltas
DCHECK(layer_tree_impl()->IsActiveTree());
Expand Down
3 changes: 3 additions & 0 deletions cc/layers/layer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "cc/base/region.h"
#include "cc/base/scoped_ptr_vector.h"
#include "cc/input/input_handler.h"
#include "cc/input/scrollbar.h"
#include "cc/layers/draw_properties.h"
#include "cc/layers/layer_lists.h"
#include "cc/layers/layer_position_constraint.h"
Expand Down Expand Up @@ -419,6 +420,8 @@ class CC_EXPORT LayerImpl : public LayerAnimationValueObserver,
}
bool user_scrollable_vertical() const { return user_scrollable_vertical_; }

bool user_scrollable(ScrollbarOrientation orientation) const;

void ApplySentScrollDeltasFromAbortedCommit();
void ApplyScrollDeltasSinceBeginMainFrame();

Expand Down
6 changes: 6 additions & 0 deletions cc/layers/scrollbar_layer_impl_base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ bool ScrollbarLayerImplBase::SetMaximum(int maximum) {
return true;
}

bool ScrollbarLayerImplBase::can_scroll_orientation() const {
if (!scroll_layer_)
return false;
return scroll_layer_->user_scrollable(orientation()) && (0 < maximum());
}

bool ScrollbarLayerImplBase::SetVerticalAdjust(float vertical_adjust) {
if (vertical_adjust_ == vertical_adjust)
return false;
Expand Down
2 changes: 2 additions & 0 deletions cc/layers/scrollbar_layer_impl_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CC_EXPORT ScrollbarLayerImplBase : public LayerImpl {
return is_left_side_vertical_scrollbar_;
}

bool can_scroll_orientation() const;

virtual void PushPropertiesTo(LayerImpl* layer) override;
virtual ScrollbarLayerImplBase* ToScrollbarLayer() override;
void PushScrollClipPropertiesTo(LayerImpl* layer);
Expand Down

0 comments on commit 3ca6969

Please sign in to comment.