Commit eaf47024 authored by pkotwicz's avatar pkotwicz Committed by Commit bot

Simplify HomeCardGestureManager::UpdateScrollState()

This CL:
- Prevents HomeCardGestureManager from calling OnGestureProgressed() with
  |from_state| == |to_state|
  - This fixes the minimized home card's opacity being incorrectly set when
    dragging down from the minimized state
- Removes unnecessary |HomeCardGestureManager::last_state_|

BUG=409666
TEST=HomeCardGestureManagerTest.*

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

Cr-Commit-Position: refs/heads/master@{#293232}
parent 707f860a
...@@ -12,7 +12,6 @@ namespace athena { ...@@ -12,7 +12,6 @@ namespace athena {
HomeCardGestureManager::HomeCardGestureManager(Delegate* delegate, HomeCardGestureManager::HomeCardGestureManager(Delegate* delegate,
const gfx::Rect& screen_bounds) const gfx::Rect& screen_bounds)
: delegate_(delegate), : delegate_(delegate),
last_state_(HomeCard::Get()->GetState()),
y_offset_(0), y_offset_(0),
last_estimated_height_(0), last_estimated_height_(0),
screen_bounds_(screen_bounds) {} screen_bounds_(screen_bounds) {}
...@@ -75,16 +74,15 @@ void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) { ...@@ -75,16 +74,15 @@ void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) {
if (last_estimated_height_ <= kHomeCardMinimizedHeight) { if (last_estimated_height_ <= kHomeCardMinimizedHeight) {
delegate_->OnGestureProgressed( delegate_->OnGestureProgressed(
last_state_, HomeCard::VISIBLE_MINIMIZED, 1.0f); HomeCard::VISIBLE_BOTTOM, HomeCard::VISIBLE_MINIMIZED, 1.0f);
last_state_ = HomeCard::VISIBLE_MINIMIZED;
return; return;
} }
HomeCard::State state = HomeCard::VISIBLE_BOTTOM; HomeCard::State bigger_state = HomeCard::VISIBLE_BOTTOM;
float smaller_height = kHomeCardMinimizedHeight; float smaller_height = kHomeCardMinimizedHeight;
float bigger_height = kHomeCardHeight; float bigger_height = kHomeCardHeight;
if (last_estimated_height_ > kHomeCardHeight) { if (last_estimated_height_ > kHomeCardHeight) {
state = HomeCard::VISIBLE_CENTERED; bigger_state = HomeCard::VISIBLE_CENTERED;
smaller_height = kHomeCardHeight; smaller_height = kHomeCardHeight;
bigger_height = screen_bounds_.height(); bigger_height = screen_bounds_.height();
} }
...@@ -94,16 +92,10 @@ void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) { ...@@ -94,16 +92,10 @@ void HomeCardGestureManager::UpdateScrollState(const ui::GestureEvent& event) {
(bigger_height - smaller_height); (bigger_height - smaller_height);
progress = std::min(1.0f, std::max(0.0f, progress)); progress = std::min(1.0f, std::max(0.0f, progress));
if (last_state_ == state) { delegate_->OnGestureProgressed(
if (event.details().scroll_y() > 0) { static_cast<HomeCard::State>(bigger_state + 1),
state = static_cast<HomeCard::State>(state + 1); bigger_state,
progress = 1.0f - progress; progress);
} else {
last_state_ = static_cast<HomeCard::State>(last_state_ + 1);
}
}
delegate_->OnGestureProgressed(last_state_, state, progress);
last_state_ = state;
} }
} // namespace athena } // namespace athena
...@@ -24,13 +24,13 @@ class ATHENA_EXPORT HomeCardGestureManager { ...@@ -24,13 +24,13 @@ class ATHENA_EXPORT HomeCardGestureManager {
// end up with |final_state|. // end up with |final_state|.
virtual void OnGestureEnded(HomeCard::State final_state) = 0; virtual void OnGestureEnded(HomeCard::State final_state) = 0;
// Called when the gesture position is updated so that |delegate| should // Called when the gesture position is updated so that |delegate| updates
// update the visual. The arguments represent the state of the current // the visual. The arguments indicate that the gesture is switching between
// gesture position is switching from |from_state| to |to_state|, and // |from_state| and |to_state|, and that the level of progress is at
// the level of the progress is at |progress|, which is 0 to 1. // |progress|, which is in the range (0, 1]. The home card was previously
// |from_state| and |to_state| could be same. For example, if the user moves // at either |from_state| or |to_state|. In particular, the home card may
// the finger down to the bottom of the screen, both states are MINIMIZED. // never have been at |from_state|. |from_state| is never equal to
// In that case |progress| is 0. // |to_state|.
virtual void OnGestureProgressed( virtual void OnGestureProgressed(
HomeCard::State from_state, HomeCard::State from_state,
HomeCard::State to_state, HomeCard::State to_state,
...@@ -51,7 +51,6 @@ class ATHENA_EXPORT HomeCardGestureManager { ...@@ -51,7 +51,6 @@ class ATHENA_EXPORT HomeCardGestureManager {
void UpdateScrollState(const ui::GestureEvent& event); void UpdateScrollState(const ui::GestureEvent& event);
Delegate* delegate_; // Not owned. Delegate* delegate_; // Not owned.
HomeCard::State last_state_;
// The offset from the top edge of the home card and the initial position of // The offset from the top edge of the home card and the initial position of
// gesture. // gesture.
......
...@@ -48,11 +48,18 @@ class HomeCardGestureManagerTest : public test::AthenaTestBase, ...@@ -48,11 +48,18 @@ class HomeCardGestureManagerTest : public test::AthenaTestBase,
bool ProcessGestureEvent(ui::EventType type, int y) { bool ProcessGestureEvent(ui::EventType type, int y) {
ui::GestureEvent event(0, y, ui::EF_NONE, base::TimeDelta(), ui::GestureEvent event(0, y, ui::EF_NONE, base::TimeDelta(),
ui::GestureEventDetails(type, 0, (y - last_y_))); ui::GestureEventDetails(type, 0, (y - last_y_)));
// Assumes the initial location is based on minimized height.
if (type == ui::ET_GESTURE_SCROLL_BEGIN) { if (type == ui::ET_GESTURE_SCROLL_BEGIN) {
// Compute the position that the home card would have wrt to the top of
// the screen if the screen had screen_bounds().
HomeCard::State state = HomeCard::Get()->GetState();
int home_card_top = 0;
if (state == HomeCard::VISIBLE_BOTTOM)
home_card_top = screen_bounds().height() - kHomeCardHeight;
else if (state == HomeCard::VISIBLE_MINIMIZED)
home_card_top = screen_bounds().height() - kHomeCardMinimizedHeight;
gfx::Point location = event.location(); gfx::Point location = event.location();
location.set_y( location.set_y(location.y() - home_card_top);
location.y() - (screen_bounds().bottom() - kHomeCardMinimizedHeight));
event.set_location(location); event.set_location(location);
} }
gesture_manager_->ProcessGestureEvent(&event); gesture_manager_->ProcessGestureEvent(&event);
...@@ -100,6 +107,8 @@ class HomeCardGestureManagerTest : public test::AthenaTestBase, ...@@ -100,6 +107,8 @@ class HomeCardGestureManagerTest : public test::AthenaTestBase,
}; };
TEST_F(HomeCardGestureManagerTest, Basic) { TEST_F(HomeCardGestureManagerTest, Basic) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020)); EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset()); EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset()); EXPECT_EQ(0, GetProgressCountAndReset());
...@@ -110,6 +119,13 @@ TEST_F(HomeCardGestureManagerTest, Basic) { ...@@ -110,6 +119,13 @@ TEST_F(HomeCardGestureManagerTest, Basic) {
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_to_state_); EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_to_state_);
EXPECT_GT(1.0f, last_progress_); EXPECT_GT(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1020);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1022);
EXPECT_EQ(2, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_MINIMIZED, last_to_state_);
EXPECT_EQ(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010); ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1010);
float progress_1010 = last_progress_; float progress_1010 = last_progress_;
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1008); ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 1008);
...@@ -134,16 +150,44 @@ TEST_F(HomeCardGestureManagerTest, Basic) { ...@@ -134,16 +150,44 @@ TEST_F(HomeCardGestureManagerTest, Basic) {
EXPECT_LT(progress_800, last_progress_); EXPECT_LT(progress_800, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 810); ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 810);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_from_state_); EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_to_state_); EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_GT(progress_800, (1.0f - last_progress_)); EXPECT_GT(progress_800, last_progress_);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END, 810)); EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END, 810));
EXPECT_EQ(1, GetEndCountAndReset()); EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_); EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
} }
// Test gesture progress when the gesture is initiated when the home card is in
// the centered state.
TEST_F(HomeCardGestureManagerTest, StartCentered) {
HomeCard::Get()->SetState(HomeCard::VISIBLE_CENTERED);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 20));
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 10);
EXPECT_EQ(1, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_EQ(1.0f, last_progress_);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 900);
ProcessGestureEvent(ui::ET_GESTURE_SCROLL_UPDATE, 910);
EXPECT_EQ(2, GetProgressCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, last_from_state_);
EXPECT_EQ(HomeCard::VISIBLE_CENTERED, last_to_state_);
EXPECT_GT(1.0f, last_progress_);
EXPECT_LT(0.0f, last_progress_);
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_END, 1000));
EXPECT_EQ(1, GetEndCountAndReset());
EXPECT_EQ(HomeCard::VISIBLE_BOTTOM, final_state_);
}
TEST_F(HomeCardGestureManagerTest, FlingUpAtEnd) { TEST_F(HomeCardGestureManagerTest, FlingUpAtEnd) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020)); EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset()); EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset()); EXPECT_EQ(0, GetProgressCountAndReset());
...@@ -156,6 +200,8 @@ TEST_F(HomeCardGestureManagerTest, FlingUpAtEnd) { ...@@ -156,6 +200,8 @@ TEST_F(HomeCardGestureManagerTest, FlingUpAtEnd) {
} }
TEST_F(HomeCardGestureManagerTest, FlingDownAtEnd) { TEST_F(HomeCardGestureManagerTest, FlingDownAtEnd) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020)); EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset()); EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset()); EXPECT_EQ(0, GetProgressCountAndReset());
...@@ -169,6 +215,8 @@ TEST_F(HomeCardGestureManagerTest, FlingDownAtEnd) { ...@@ -169,6 +215,8 @@ TEST_F(HomeCardGestureManagerTest, FlingDownAtEnd) {
} }
TEST_F(HomeCardGestureManagerTest, WeakFling) { TEST_F(HomeCardGestureManagerTest, WeakFling) {
ASSERT_EQ(HomeCard::VISIBLE_MINIMIZED, HomeCard::Get()->GetState());
EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020)); EXPECT_TRUE(ProcessGestureEvent(ui::ET_GESTURE_SCROLL_BEGIN, 1020));
EXPECT_EQ(0, GetEndCountAndReset()); EXPECT_EQ(0, GetEndCountAndReset());
EXPECT_EQ(0, GetProgressCountAndReset()); EXPECT_EQ(0, GetProgressCountAndReset());
......
...@@ -157,10 +157,7 @@ class HomeCardView : public views::WidgetDelegateView { ...@@ -157,10 +157,7 @@ class HomeCardView : public views::WidgetDelegateView {
HomeCard::State to_state, HomeCard::State to_state,
float progress) { float progress) {
// TODO(mukai): not clear the focus, but simply close the virtual keyboard. // TODO(mukai): not clear the focus, but simply close the virtual keyboard.
if (from_state != HomeCard::VISIBLE_CENTERED || GetFocusManager()->ClearFocus();
to_state != HomeCard::VISIBLE_CENTERED) {
GetFocusManager()->ClearFocus();
}
if (from_state == HomeCard::VISIBLE_CENTERED) if (from_state == HomeCard::VISIBLE_CENTERED)
main_view_->SetLayoutState(1.0f - progress); main_view_->SetLayoutState(1.0f - progress);
else if (to_state == HomeCard::VISIBLE_CENTERED) else if (to_state == HomeCard::VISIBLE_CENTERED)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment