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 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 * @param controlContainer The container for the toolbar. | 227 * @param controlContainer The container for the toolbar. |
228 */ | 228 */ |
229 public void init(View root, View controlContainer) { | 229 public void init(View root, View controlContainer) { |
230 mToolbarHeight = controlContainer.getHeight(); | 230 mToolbarHeight = controlContainer.getHeight(); |
231 mCurrentState = SHEET_STATE_PEEK; | 231 mCurrentState = SHEET_STATE_PEEK; |
232 | 232 |
233 // Listen to height changes on the root. | 233 // Listen to height changes on the root. |
234 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { | 234 root.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
235 public void onLayoutChange(View v, int left, int top, int right, int
bottom, | 235 public void onLayoutChange(View v, int left, int top, int right, int
bottom, |
236 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 236 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 237 // Make sure the size of the layout actually changed. |
| 238 if (bottom - top == oldBottom - oldTop && right - left == oldRig
ht - oldLeft) { |
| 239 return; |
| 240 } |
| 241 |
237 mContainerHeight = bottom - top; | 242 mContainerHeight = bottom - top; |
238 updateSheetPeekHeight(); | 243 updateSheetPeekHeight(); |
| 244 |
| 245 cancelAnimation(); |
239 setSheetState(mCurrentState, false); | 246 setSheetState(mCurrentState, false); |
240 } | 247 } |
241 }); | 248 }); |
242 | 249 |
243 // Listen to height changes on the toolbar. | 250 // Listen to height changes on the toolbar. |
244 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen
er() { | 251 controlContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListen
er() { |
245 public void onLayoutChange(View v, int left, int top, int right, int
bottom, | 252 public void onLayoutChange(View v, int left, int top, int right, int
bottom, |
246 int oldLeft, int oldTop, int oldRight, int oldBottom) { | 253 int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 254 // Make sure the size of the layout actually changed. |
| 255 if (bottom - top == oldBottom - oldTop && right - left == oldRig
ht - oldLeft) { |
| 256 return; |
| 257 } |
| 258 |
247 mToolbarHeight = bottom - top; | 259 mToolbarHeight = bottom - top; |
248 updateSheetPeekHeight(); | 260 updateSheetPeekHeight(); |
| 261 |
| 262 cancelAnimation(); |
249 setSheetState(mCurrentState, false); | 263 setSheetState(mCurrentState, false); |
250 } | 264 } |
251 }); | 265 }); |
252 } | 266 } |
253 | 267 |
254 /** | 268 /** |
255 * Creates an unadjusted version of a MotionEvent. | 269 * Creates an unadjusted version of a MotionEvent. |
256 * @param e The original event. | 270 * @param e The original event. |
257 * @return The unadjusted version of the event. | 271 * @return The unadjusted version of the event. |
258 */ | 272 */ |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 private void setSheetOffsetFromBottom(float offset) { | 359 private void setSheetOffsetFromBottom(float offset) { |
346 setTranslationY(mContainerHeight - offset); | 360 setTranslationY(mContainerHeight - offset); |
347 } | 361 } |
348 | 362 |
349 /** | 363 /** |
350 * Moves the sheet to the provided state. | 364 * Moves the sheet to the provided state. |
351 * @param state The state to move the panel to. | 365 * @param state The state to move the panel to. |
352 * @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 |
353 * move there instantly. | 367 * move there instantly. |
354 */ | 368 */ |
355 private void setSheetState(@SheetState int state, boolean animate) { | 369 public void setSheetState(@SheetState int state, boolean animate) { |
356 mCurrentState = state; | 370 mCurrentState = state; |
357 | 371 |
358 if (animate) { | 372 if (animate) { |
359 createSettleAnimation(state); | 373 createSettleAnimation(state); |
360 } else { | 374 } else { |
361 setSheetOffsetFromBottom(getSheetHeightForState(state)); | 375 setSheetOffsetFromBottom(getSheetHeightForState(state)); |
362 } | 376 } |
363 } | 377 } |
364 | 378 |
365 /** | 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 /** |
366 * 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. |
367 * @param state The state to get the height from. | 397 * @param state The state to get the height from. |
368 * @return The height of the sheet at the provided state. | 398 * @return The height of the sheet at the provided state. |
369 */ | 399 */ |
370 private float getSheetHeightForState(@SheetState int state) { | 400 private float getSheetHeightForState(@SheetState int state) { |
371 return mStateRatios[state] * mContainerHeight; | 401 return mStateRatios[state] * mContainerHeight; |
372 } | 402 } |
373 | 403 |
374 /** | 404 /** |
375 * 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 28 matching lines...) Expand all Loading... |
404 | 434 |
405 float inverseThreshold = 1.0f - THRESHOLD_TO_NEXT_STATE; | 435 float inverseThreshold = 1.0f - THRESHOLD_TO_NEXT_STATE; |
406 float thresholdToNextState = yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE
: inverseThreshold; | 436 float thresholdToNextState = yVelocity < 0.0f ? THRESHOLD_TO_NEXT_STATE
: inverseThreshold; |
407 | 437 |
408 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { | 438 if ((sheetHeight - lowerBound) / distance > thresholdToNextState) { |
409 return nextState; | 439 return nextState; |
410 } | 440 } |
411 return prevState; | 441 return prevState; |
412 } | 442 } |
413 } | 443 } |
OLD | NEW |