Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 package org.chromium.chrome.browser.widget; | 5 package org.chromium.chrome.browser.widget; |
| 6 | 6 |
| 7 import android.animation.Animator; | 7 import android.animation.Animator; |
| 8 import android.animation.AnimatorListenerAdapter; | 8 import android.animation.AnimatorListenerAdapter; |
| 9 import android.animation.ObjectAnimator; | 9 import android.animation.ObjectAnimator; |
| 10 import android.content.Context; | 10 import android.content.Context; |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 * @param controlContainer The container for the toolbar. | 225 * @param controlContainer The container for the toolbar. |
| 226 */ | 226 */ |
| 227 public void init(View root, View controlContainer) { | 227 public void init(View root, View controlContainer) { |
| 228 mToolbarHeight = controlContainer.getHeight(); | 228 mToolbarHeight = controlContainer.getHeight(); |
| 229 mCurrentState = SHEET_STATE_PEEK; | 229 mCurrentState = SHEET_STATE_PEEK; |
| 230 | 230 |
| 231 // Listen to height changes on the root. | 231 // Listen to height changes on the root. |
| 232 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { | 232 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
| 233 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 233 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 234 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 234 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 235 // Make sure the size of the layout actually changed. | |
|
gone
2017/01/20 19:04:08
Can't you just do equality checks? Subtraction of
mdjones
2017/01/20 20:11:47
Yeah, not really sure what I was thinking here. Fi
| |
| 236 if (top - oldTop == 0 && bottom - oldBottom == 0 && left - oldLe ft == 0 | |
| 237 && right - oldRight == 0) { | |
| 238 return; | |
| 239 } | |
| 240 | |
| 235 mContainerHeight = bottom - top; | 241 mContainerHeight = bottom - top; |
| 236 updateSheetPeekHeight(); | 242 updateSheetPeekHeight(); |
| 243 | |
| 244 cancelAnimation(); | |
| 237 setSheetState(mCurrentState, false); | 245 setSheetState(mCurrentState, false); |
| 238 } | 246 } |
| 239 }); | 247 }); |
| 240 | 248 |
| 241 // Listen to height changes on the toolbar. | 249 // Listen to height changes on the toolbar. |
| 242 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen er() { | 250 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen er() { |
| 243 public void onLayoutChange(View v, int left, int top, int right, int bottom, | 251 public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 244 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 252 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 253 // Make sure the size of the layout actually changed. | |
| 254 if (top - oldTop == 0 && bottom - oldBottom == 0 && left - oldLe ft == 0 | |
| 255 && right - oldRight == 0) { | |
| 256 return; | |
| 257 } | |
| 258 | |
| 245 mToolbarHeight = bottom - top; | 259 mToolbarHeight = bottom - top; |
| 246 updateSheetPeekHeight(); | 260 updateSheetPeekHeight(); |
| 261 | |
| 262 cancelAnimation(); | |
| 247 setSheetState(mCurrentState, false); | 263 setSheetState(mCurrentState, false); |
| 248 } | 264 } |
| 249 }); | 265 }); |
| 250 } | 266 } |
| 251 | 267 |
| 252 /** | 268 /** |
| 253 * Creates an unadjusted version of a MotionEvent. | 269 * Creates an unadjusted version of a MotionEvent. |
| 254 * @param e The original event. | 270 * @param e The original event. |
| 255 * @return The unadjusted version of the event. | 271 * @return The unadjusted version of the event. |
| 256 */ | 272 */ |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 private void setSheetOffsetFromBottom(float offset) { | 359 private void setSheetOffsetFromBottom(float offset) { |
| 344 setTranslationY(mContainerHeight - offset); | 360 setTranslationY(mContainerHeight - offset); |
| 345 } | 361 } |
| 346 | 362 |
| 347 /** | 363 /** |
| 348 * Moves the sheet to the provided state. | 364 * Moves the sheet to the provided state. |
| 349 * @param state The state to move the panel to. | 365 * @param state The state to move the panel to. |
| 350 * @param animate If true, the sheet will animate to the provided state, oth erwise it will | 366 * @param animate If true, the sheet will animate to the provided state, oth erwise it will |
| 351 * move there instantly. | 367 * move there instantly. |
| 352 */ | 368 */ |
| 353 private void setSheetState(@SheetState int state, boolean animate) { | 369 public void setSheetState(@SheetState int state, boolean animate) { |
| 354 mCurrentState = state; | 370 mCurrentState = state; |
| 355 | 371 |
| 356 if (animate) { | 372 if (animate) { |
| 357 createSettleAnimation(state); | 373 createSettleAnimation(state); |
| 358 } else { | 374 } else { |
| 359 setSheetOffsetFromBottom(getSheetHeightForState(state)); | 375 setSheetOffsetFromBottom(getSheetHeightForState(state)); |
| 360 } | 376 } |
| 361 } | 377 } |
| 362 | 378 |
| 363 /** | 379 /** |
| 380 * @return The current state of the bottom sheet. If the sheet is animating, this will be the | |
| 381 * state the sheet is animating to. | |
| 382 */ | |
| 383 public int getSheetState() { | |
| 384 return mCurrentState; | |
| 385 } | |
| 386 | |
| 387 /** | |
| 388 * If the animation to settle the sheet in one of its states is running. | |
| 389 * @return True if the animation is running. | |
| 390 */ | |
| 391 private boolean isRunningSettleAnimation() { | |
| 392 return mSettleAnimator != null; | |
| 393 } | |
| 394 | |
| 395 /** | |
| 364 * Gets the height of the bottom sheet based on a provided state. | 396 * Gets the height of the bottom sheet based on a provided state. |
| 365 * @param state The state to get the height from. | 397 * @param state The state to get the height from. |
| 366 * @return The height of the sheet at the provided state. | 398 * @return The height of the sheet at the provided state. |
| 367 */ | 399 */ |
| 368 private float getSheetHeightForState(@SheetState int state) { | 400 private float getSheetHeightForState(@SheetState int state) { |
| 369 return mStateRatios[state] * mContainerHeight; | 401 return mStateRatios[state] * mContainerHeight; |
| 370 } | 402 } |
| 371 | 403 |
| 372 /** | 404 /** |
| 373 * Gets the target state of the sheet based on the sheet's height and veloci ty. | 405 * Gets the target state of the sheet based on the sheet's height and veloci ty. |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 401 float distance = getSheetHeightForState(nextState) - lowerBound; | 433 float distance = getSheetHeightForState(nextState) - lowerBound; |
| 402 float thresholdToNextState = | 434 float thresholdToNextState = |
| 403 yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE : 1.0f - THRESHOLD_TO _NEXT_STATE; | 435 yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE : 1.0f - THRESHOLD_TO _NEXT_STATE; |
| 404 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { | 436 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { |
| 405 return nextState; | 437 return nextState; |
| 406 } else { | 438 } else { |
| 407 return prevState; | 439 return prevState; |
| 408 } | 440 } |
| 409 } | 441 } |
| 410 } | 442 } |
| OLD | NEW |