OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.compositor.overlays.strip; |
| 6 |
| 7 import static org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.Ani
matableAnimation.createAnimation; |
| 8 |
| 9 import android.content.Context; |
| 10 import android.content.res.Resources; |
| 11 import android.os.Handler; |
| 12 import android.os.Message; |
| 13 import android.os.SystemClock; |
| 14 import android.view.View; |
| 15 import android.widget.AdapterView; |
| 16 import android.widget.AdapterView.OnItemClickListener; |
| 17 import android.widget.ArrayAdapter; |
| 18 import android.widget.ListPopupWindow; |
| 19 |
| 20 import com.google.android.apps.chrome.R; |
| 21 |
| 22 import org.chromium.base.PerfTraceEvent; |
| 23 import org.chromium.base.VisibleForTesting; |
| 24 import org.chromium.base.annotations.SuppressFBWarnings; |
| 25 import org.chromium.chrome.browser.Tab; |
| 26 import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation; |
| 27 import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.Animatable
; |
| 28 import org.chromium.chrome.browser.compositor.layouts.ChromeAnimation.Animation; |
| 29 import org.chromium.chrome.browser.compositor.layouts.LayoutRenderHost; |
| 30 import org.chromium.chrome.browser.compositor.layouts.LayoutUpdateHost; |
| 31 import org.chromium.chrome.browser.compositor.layouts.components.CompositorButto
n; |
| 32 import org.chromium.chrome.browser.compositor.layouts.components.VirtualView; |
| 33 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; |
| 34 import org.chromium.chrome.browser.compositor.layouts.phone.stack.StackScroller; |
| 35 import org.chromium.chrome.browser.compositor.overlays.strip.TabLoadTracker.TabL
oadTrackerCallback; |
| 36 import org.chromium.chrome.browser.tabmodel.TabCreatorManager.TabCreator; |
| 37 import org.chromium.chrome.browser.tabmodel.TabModel; |
| 38 import org.chromium.chrome.browser.tabmodel.TabModelUtils; |
| 39 import org.chromium.chrome.browser.util.MathUtils; |
| 40 import org.chromium.ui.base.LocalizationUtils; |
| 41 |
| 42 import java.util.ArrayList; |
| 43 import java.util.List; |
| 44 |
| 45 /** |
| 46 * This class handles managing the positions and behavior of all tabs in a tab s
trip. It is |
| 47 * responsible for both responding to UI input events and model change notificat
ions, adjusting and |
| 48 * animating the tab strip as required. |
| 49 * |
| 50 * <p> |
| 51 * The stacking and visual behavior is driven by setting a {@link StripStacker}. |
| 52 */ |
| 53 public class StripLayoutHelper { |
| 54 // Drag Constants |
| 55 private static final int REORDER_SCROLL_NONE = 0; |
| 56 private static final int REORDER_SCROLL_LEFT = 1; |
| 57 private static final int REORDER_SCROLL_RIGHT = 2; |
| 58 |
| 59 // Behavior Constants |
| 60 private static final float EPSILON = 0.001f; |
| 61 private static final int MAX_TABS_TO_STACK = 4; |
| 62 private static final float TAN_OF_REORDER_ANGLE_START_THRESHOLD = |
| 63 (float) Math.tan(Math.PI / 4.0f); |
| 64 private static final float REORDER_OVERLAP_SWITCH_PERCENTAGE = 0.53f; |
| 65 |
| 66 // Animation/Timer Constants |
| 67 private static final int RESIZE_DELAY_MS = 1500; |
| 68 private static final int SPINNER_UPDATE_DELAY_MS = 66; |
| 69 // Degrees per milisecond. |
| 70 private static final float SPINNER_DPMS = 0.33f; |
| 71 private static final int EXPAND_DURATION_MS = 250; |
| 72 private static final int ANIM_TAB_CREATED_MS = 150; |
| 73 private static final int ANIM_TAB_CLOSED_MS = 150; |
| 74 private static final int ANIM_TAB_RESIZE_MS = 150; |
| 75 private static final int ANIM_TAB_MOVE_MS = 125; |
| 76 |
| 77 // Visibility Constants |
| 78 private static final float TAB_STACK_WIDTH_DP = 4.f; |
| 79 private static final float TAB_OVERLAP_WIDTH_DP = 24.f; |
| 80 private static final float MIN_TAB_WIDTH_DP = 190.f; |
| 81 private static final float MAX_TAB_WIDTH_DP = 265.f; |
| 82 private static final float REORDER_MOVE_START_THRESHOLD_DP = 50.f; |
| 83 private static final float REORDER_EDGE_SCROLL_MAX_SPEED_DP = 1000.f; |
| 84 private static final float REORDER_EDGE_SCROLL_START_MIN_DP = 87.4f; |
| 85 private static final float REORDER_EDGE_SCROLL_START_MAX_DP = 18.4f; |
| 86 private static final float NEW_TAB_BUTTON_Y_OFFSET_DP = 6.f; |
| 87 private static final float NEW_TAB_BUTTON_CLICK_SLOP_DP = 4.f; |
| 88 private static final float NEW_TAB_BUTTON_WIDTH_DP = 58.f; |
| 89 private static final float NEW_TAB_BUTTON_HEIGHT_DP = 32.5f; |
| 90 |
| 91 private static final int MESSAGE_RESIZE = 1; |
| 92 private static final int MESSAGE_UPDATE_SPINNER = 2; |
| 93 |
| 94 // External influences |
| 95 private final LayoutUpdateHost mUpdateHost; |
| 96 private final LayoutRenderHost mRenderHost; |
| 97 private TabModel mModel; |
| 98 private TabCreator mTabCreator; |
| 99 private TabContentManager mTabContentManager; |
| 100 private StripStacker mStripStacker = new StaticStripStacker(); |
| 101 |
| 102 // Internal State |
| 103 private StripLayoutTab[] mStripTabs = new StripLayoutTab[0]; |
| 104 private StripLayoutTab[] mStripTabsVisuallyOrdered = new StripLayoutTab[0]; |
| 105 private StripLayoutTab[] mStripTabsToRender = new StripLayoutTab[0]; |
| 106 private final StripTabEventHandler mStripTabEventHandler = new StripTabEvent
Handler(); |
| 107 private final TabLoadTrackerCallback mTabLoadTrackerHost = new TabLoadTracke
rCallbackImpl(); |
| 108 private ChromeAnimation<Animatable<?>> mLayoutAnimations; |
| 109 |
| 110 private final CompositorButton mNewTabButton; |
| 111 |
| 112 // Layout Constants |
| 113 private final float mTabStackWidth; |
| 114 private final float mTabOverlapWidth; |
| 115 private float mNewTabButtonWidth; |
| 116 private final float mMinTabWidth; |
| 117 private final float mMaxTabWidth; |
| 118 private final float mReorderMoveStartThreshold; |
| 119 private final ListPopupWindow mTabMenu; |
| 120 |
| 121 // Strip State |
| 122 private StackScroller mScroller; |
| 123 private int mScrollOffset; |
| 124 private float mMinScrollOffset; |
| 125 private float mCachedTabWidth; |
| 126 |
| 127 // Reorder State |
| 128 private int mReorderState = REORDER_SCROLL_NONE; |
| 129 private boolean mInReorderMode = false; |
| 130 private float mLastReorderX; |
| 131 private long mLastReorderScrollTime; |
| 132 |
| 133 // UI State |
| 134 private StripLayoutTab mInteractingTab; |
| 135 private CompositorButton mLastPressedCloseButton; |
| 136 private float mWidth; |
| 137 private float mHeight; |
| 138 private long mLastSpinnerUpdate; |
| 139 private float mLeftMargin; |
| 140 private float mRightMargin; |
| 141 private final boolean mIncognito; |
| 142 |
| 143 // Tab menu item IDs |
| 144 public static final int ID_CLOSE_ALL_TABS = 0; |
| 145 |
| 146 private Context mContext; |
| 147 /** |
| 148 * Creates an instance of the {@link StripLayoutHelper}. |
| 149 * @param context The current Android {@link Context}. |
| 150 * @param updateHost The parent {@link LayoutUpdateHost}. |
| 151 * @param renderHost The {@link LayoutRenderHost}. |
| 152 * @param incognito Whether or not this tab strip is incognito. |
| 153 */ |
| 154 public StripLayoutHelper(Context context, LayoutUpdateHost updateHost, |
| 155 LayoutRenderHost renderHost, boolean incognito) { |
| 156 mTabStackWidth = TAB_STACK_WIDTH_DP; |
| 157 mTabOverlapWidth = TAB_OVERLAP_WIDTH_DP; |
| 158 mNewTabButtonWidth = NEW_TAB_BUTTON_WIDTH_DP; |
| 159 |
| 160 if (LocalizationUtils.isLayoutRtl()) { |
| 161 // In rtl let the tab nest closer to the new tab button. |
| 162 mNewTabButtonWidth -= mTabOverlapWidth / 2; |
| 163 } |
| 164 mRightMargin = LocalizationUtils.isLayoutRtl() ? 0 : mNewTabButtonWidth; |
| 165 mLeftMargin = LocalizationUtils.isLayoutRtl() ? mNewTabButtonWidth : 0; |
| 166 mMinTabWidth = MIN_TAB_WIDTH_DP; |
| 167 mMaxTabWidth = MAX_TAB_WIDTH_DP; |
| 168 mReorderMoveStartThreshold = REORDER_MOVE_START_THRESHOLD_DP; |
| 169 mUpdateHost = updateHost; |
| 170 mRenderHost = renderHost; |
| 171 mNewTabButton = |
| 172 new CompositorButton(context, NEW_TAB_BUTTON_WIDTH_DP, NEW_TAB_B
UTTON_HEIGHT_DP); |
| 173 mNewTabButton.setResources(R.drawable.btn_tabstrip_new_tab_normal, |
| 174 R.drawable.btn_tabstrip_new_tab_pressed, |
| 175 R.drawable.btn_tabstrip_new_incognito_tab_normal, |
| 176 R.drawable.btn_tabstrip_new_incognito_tab_pressed); |
| 177 mNewTabButton.setIncognito(incognito); |
| 178 mNewTabButton.setY(NEW_TAB_BUTTON_Y_OFFSET_DP); |
| 179 mNewTabButton.setClickSlop(NEW_TAB_BUTTON_CLICK_SLOP_DP); |
| 180 Resources res = context.getResources(); |
| 181 mNewTabButton.setAccessibilityDescription( |
| 182 res.getString(R.string.accessibility_toolbar_btn_new_tab), |
| 183 res.getString(R.string.accessibility_toolbar_btn_new_incognito_t
ab)); |
| 184 mContext = context; |
| 185 mIncognito = incognito; |
| 186 |
| 187 // Create tab menu |
| 188 mTabMenu = new ListPopupWindow(mContext); |
| 189 mTabMenu.setAdapter(new ArrayAdapter<String>(mContext, R.layout.eb_popup
_item, |
| 190 new String[] { |
| 191 mContext.getString(!mIncognito ? R.string.menu_close_all
_tabs |
| 192 : R.string.menu_close_all
_incognito_tabs)})); |
| 193 mTabMenu.setOnItemClickListener(new OnItemClickListener() { |
| 194 @Override |
| 195 public void onItemClick(AdapterView<?> parent, View view, int positi
on, long id) { |
| 196 mTabMenu.dismiss(); |
| 197 if (position == ID_CLOSE_ALL_TABS) { |
| 198 mModel.closeAllTabs(false, false); |
| 199 } |
| 200 } |
| 201 }); |
| 202 |
| 203 int menuWidth = mContext.getResources().getDimensionPixelSize(R.dimen.me
nu_width); |
| 204 mTabMenu.setWidth(menuWidth); |
| 205 mTabMenu.setModal(true); |
| 206 } |
| 207 |
| 208 /** |
| 209 * Get a list of virtual views for accessibility. |
| 210 * |
| 211 * @param views A List to populate with virtual views. |
| 212 */ |
| 213 public void getVirtualViews(List<VirtualView> views) { |
| 214 for (int i = mStripTabsToRender.length - 1; i >= 0; i--) { |
| 215 StripLayoutTab tab = mStripTabsToRender[i]; |
| 216 tab.getVirtualViews(views); |
| 217 } |
| 218 if (mNewTabButton.isVisible()) views.add(mNewTabButton); |
| 219 } |
| 220 |
| 221 /** |
| 222 * @return The visually ordered list of visible {@link StripLayoutTab}s. |
| 223 */ |
| 224 @SuppressFBWarnings("EI_EXPOSE_REP") |
| 225 public StripLayoutTab[] getStripLayoutTabsToRender() { |
| 226 return mStripTabsToRender; |
| 227 } |
| 228 |
| 229 @VisibleForTesting |
| 230 public int getTabCount() { |
| 231 return mStripTabs.length; |
| 232 } |
| 233 |
| 234 /** |
| 235 * @return A {@link CompositorButton} that represents the positioning of the
new tab button. |
| 236 */ |
| 237 public CompositorButton getNewTabButton() { |
| 238 return mNewTabButton; |
| 239 } |
| 240 |
| 241 /** |
| 242 * @return The brightness of background tabs in the tabstrip. |
| 243 */ |
| 244 public float getStripBrightness() { |
| 245 return mInReorderMode ? 0.75f : 1.0f; |
| 246 } |
| 247 |
| 248 /** |
| 249 * Allows changing the visual behavior of the tabs in this stack, as specifi
ed by |
| 250 * {@code stacker}. |
| 251 * @param stacker The {@link StripStacker} that should specify how the tabs
should be |
| 252 * presented. |
| 253 */ |
| 254 public void setTabStacker(StripStacker stacker) { |
| 255 if (stacker != mStripStacker) mUpdateHost.requestUpdate(); |
| 256 mStripStacker = stacker; |
| 257 |
| 258 // Push Stacker properties to tabs. |
| 259 for (int i = 0; i < mStripTabs.length; i++) { |
| 260 pushStackerPropertiesToTab(mStripTabs[i]); |
| 261 } |
| 262 } |
| 263 |
| 264 /** |
| 265 * @parm margin The width of the distance between the left edge of |
| 266 * the screen and first tab. |
| 267 */ |
| 268 public void setLeftMargin(float margin) { |
| 269 mLeftMargin = margin; |
| 270 mLeftMargin += LocalizationUtils.isLayoutRtl() ? mNewTabButtonWidth : 0; |
| 271 } |
| 272 |
| 273 /** |
| 274 * @param margin The distance between the rightmost tab and the edge of the |
| 275 * screen. |
| 276 */ |
| 277 public void setRightMargin(float margin) { |
| 278 mRightMargin = margin; |
| 279 mRightMargin += LocalizationUtils.isLayoutRtl() ? 0 : mNewTabButtonWidth
; |
| 280 } |
| 281 |
| 282 /** |
| 283 * Updates the size of the virtual tab strip, making the tabs resize and mov
e accordingly. |
| 284 * @param width The new available width. |
| 285 * @param height The new height this stack should be. |
| 286 */ |
| 287 public void onSizeChanged(float width, float height) { |
| 288 if (mWidth == width && mHeight == height) return; |
| 289 |
| 290 boolean widthChanged = mWidth != width; |
| 291 |
| 292 mWidth = width; |
| 293 mHeight = height; |
| 294 |
| 295 for (int i = 0; i < mStripTabs.length; i++) { |
| 296 mStripTabs[i].setHeight(mHeight); |
| 297 } |
| 298 |
| 299 if (widthChanged) computeAndUpdateTabWidth(false); |
| 300 if (mStripTabs.length > 0) mUpdateHost.requestUpdate(); |
| 301 |
| 302 // Dismiss tab menu, similar to how the app menu is dismissed on orienta
tion change |
| 303 mTabMenu.dismiss(); |
| 304 } |
| 305 |
| 306 /** |
| 307 * Updates all internal resources and dimensions. |
| 308 * @param context The current Android {@link Context}. |
| 309 */ |
| 310 public void onContextChanged(Context context) { |
| 311 mScroller = new StackScroller(context); |
| 312 mContext = context; |
| 313 } |
| 314 |
| 315 /** |
| 316 * Notify the a title has changed. |
| 317 * |
| 318 * @param tabId The id of the tab that has changed. |
| 319 * @param title The new title. |
| 320 */ |
| 321 public void tabTitleChanged(int tabId, String title) { |
| 322 StripLayoutTab tab = findTabById(tabId); |
| 323 if (tab != null) tab.setAccessibilityDescription(title); |
| 324 } |
| 325 |
| 326 /** |
| 327 * Sets the {@link TabModel} that this {@link StripLayoutHelper} will visual
ly represent. |
| 328 * @param model The {@link TabModel} to visually represent. |
| 329 */ |
| 330 public void setTabModel(TabModel model, TabContentManager manager, TabCreato
r tabCreator) { |
| 331 if (mModel == model) return; |
| 332 mModel = model; |
| 333 mTabContentManager = manager; |
| 334 mTabCreator = tabCreator; |
| 335 computeAndUpdateTabOrders(false); |
| 336 } |
| 337 |
| 338 /** |
| 339 * Helper-specific updates. Cascades the values updated by the animations an
d flings. |
| 340 * @param time The current time of the app in ms. |
| 341 * @param dt The delta time between update frames in ms. |
| 342 * @return Whether or not animations are done. |
| 343 */ |
| 344 public boolean updateLayout(long time, long dt) { |
| 345 PerfTraceEvent.instant("StripLayoutHelper:updateLayout"); |
| 346 final boolean doneAnimating = onUpdateAnimation(time, false); |
| 347 updateStrip(time, dt); |
| 348 return doneAnimating; |
| 349 } |
| 350 |
| 351 /** |
| 352 * Called when a tab get selected. |
| 353 * @param time The current time of the app in ms. |
| 354 * @param id The id of the selected tab. |
| 355 * @param prevId The id of the previously selected tab. |
| 356 */ |
| 357 public void tabSelected(long time, int id, int prevId) { |
| 358 if (findTabById(id) == null) { |
| 359 tabCreated(time, id, prevId, true); |
| 360 } else { |
| 361 updateVisualTabOrdering(); |
| 362 mUpdateHost.requestUpdate(); |
| 363 } |
| 364 } |
| 365 |
| 366 /** |
| 367 * Called when a tab has been moved in the tabModel. |
| 368 * @param time The current time of the app in ms. |
| 369 * @param id The id of the Tab. |
| 370 * @param oldIndex The old index of the tab in the {@link TabModel}. |
| 371 * @param newIndex The new index of the tab in the {@link TabModel}. |
| 372 */ |
| 373 public void tabMoved(long time, int id, int oldIndex, int newIndex) { |
| 374 reorderTab(id, oldIndex, newIndex, false); |
| 375 |
| 376 updateVisualTabOrdering(); |
| 377 mUpdateHost.requestUpdate(); |
| 378 } |
| 379 |
| 380 /** |
| 381 * Called when a tab is being closed. When called, the closing tab will not |
| 382 * be part of the model. |
| 383 * @param time The current time of the app in ms. |
| 384 * @param id The id of the tab being closed. |
| 385 */ |
| 386 public void tabClosed(long time, int id) { |
| 387 if (findTabById(id) == null) return; |
| 388 |
| 389 // 1. Find out if we're closing the last tab. This determines if we res
ize immediately. |
| 390 // We know mStripTabs.length >= 1 because findTabById did not return nul
l. |
| 391 boolean closingLastTab = mStripTabs[mStripTabs.length - 1].getId() == id
; |
| 392 |
| 393 // 2. Rebuild the strip. |
| 394 computeAndUpdateTabOrders(!closingLastTab); |
| 395 |
| 396 mUpdateHost.requestUpdate(); |
| 397 } |
| 398 |
| 399 /** |
| 400 * Called when a tab close has been undone and the tab has been restored. |
| 401 * @param time The current time of the app in ms. |
| 402 * @param id The id of the Tab. |
| 403 */ |
| 404 public void tabClosureCancelled(long time, int id) { |
| 405 final boolean selected = TabModelUtils.getCurrentTabId(mModel) == id; |
| 406 tabCreated(time, id, Tab.INVALID_TAB_ID, selected); |
| 407 } |
| 408 |
| 409 /** |
| 410 * Called when a tab is created from the top left button. |
| 411 * @param time The current time of the app in ms. |
| 412 * @param id The id of the newly created tab. |
| 413 * @param prevId The id of the source tab. |
| 414 * @param selected Whether the tab will be selected. |
| 415 */ |
| 416 public void tabCreated(long time, int id, int prevId, boolean selected) { |
| 417 if (findTabById(id) != null) return; |
| 418 |
| 419 // 1. Build any tabs that are missing. |
| 420 computeAndUpdateTabOrders(false); |
| 421 |
| 422 // 2. Start an animation for the newly created tab. |
| 423 StripLayoutTab tab = findTabById(id); |
| 424 if (tab != null) startAnimation(buildTabCreatedAnimation(tab), true); |
| 425 |
| 426 // 3. Figure out which tab needs to be visible. |
| 427 StripLayoutTab fastExpandTab = findTabById(prevId); |
| 428 boolean allowLeftExpand = false; |
| 429 if (!selected) { |
| 430 fastExpandTab = tab; |
| 431 allowLeftExpand = true; |
| 432 } |
| 433 |
| 434 // 4. Scroll the stack so that the fast expand tab is visible. |
| 435 if (fastExpandTab != null) { |
| 436 float delta = |
| 437 calculateOffsetToMakeTabVisible(fastExpandTab, false, allowL
eftExpand, true); |
| 438 if (delta != 0.f) { |
| 439 mScroller.startScroll(mScrollOffset, 0, (int) delta, 0, time, EX
PAND_DURATION_MS); |
| 440 } |
| 441 } |
| 442 |
| 443 mUpdateHost.requestUpdate(); |
| 444 } |
| 445 |
| 446 /** |
| 447 * Called when a tab has started loading. |
| 448 * @param id The id of the Tab. |
| 449 */ |
| 450 public void tabPageLoadStarted(int id) { |
| 451 StripLayoutTab tab = findTabById(id); |
| 452 if (tab != null) tab.pageLoadingStarted(); |
| 453 } |
| 454 |
| 455 /** |
| 456 * Called when a tab has finished loading. |
| 457 * @param id The id of the Tab. |
| 458 */ |
| 459 public void tabPageLoadFinished(int id) { |
| 460 StripLayoutTab tab = findTabById(id); |
| 461 if (tab != null) tab.pageLoadingFinished(); |
| 462 } |
| 463 |
| 464 /** |
| 465 * Called when a tab has started loading resources. |
| 466 * @param id The id of the Tab. |
| 467 */ |
| 468 public void tabLoadStarted(int id) { |
| 469 StripLayoutTab tab = findTabById(id); |
| 470 if (tab != null) tab.loadingStarted(); |
| 471 } |
| 472 |
| 473 /** |
| 474 * Called when a tab has stopped loading resources. |
| 475 * @param id The id of the Tab. |
| 476 */ |
| 477 public void tabLoadFinished(int id) { |
| 478 StripLayoutTab tab = findTabById(id); |
| 479 if (tab != null) tab.loadingFinished(); |
| 480 } |
| 481 |
| 482 /** |
| 483 * Called on touch drag event. |
| 484 * @param time The current time of the app in ms. |
| 485 * @param x The y coordinate of the end of the drag event. |
| 486 * @param y The y coordinate of the end of the drag event. |
| 487 * @param deltaX The number of pixels dragged in the x direction. |
| 488 * @param deltaY The number of pixels dragged in the y direction. |
| 489 * @param totalX The total delta x since the drag started. |
| 490 * @param totalY The total delta y since the drag started. |
| 491 */ |
| 492 public void drag( |
| 493 long time, float x, float y, float deltaX, float deltaY, float total
X, float totalY) { |
| 494 resetResizeTimeout(false); |
| 495 |
| 496 deltaX = MathUtils.flipSignIf(deltaX, LocalizationUtils.isLayoutRtl()); |
| 497 |
| 498 // 1. Reset the button state. |
| 499 mNewTabButton.drag(x, y); |
| 500 if (mLastPressedCloseButton != null) { |
| 501 if (!mLastPressedCloseButton.drag(x, y)) mLastPressedCloseButton = n
ull; |
| 502 } |
| 503 |
| 504 if (mInReorderMode) { |
| 505 // 2.a. Handle reordering tabs. |
| 506 // This isn't the accumulated delta since the beginning of the drag.
It accumulates |
| 507 // the delta X until a threshold is crossed and then the event gets
processed. |
| 508 float accumulatedDeltaX = x - mLastReorderX; |
| 509 |
| 510 if (Math.abs(accumulatedDeltaX) >= 1.f) { |
| 511 if (!LocalizationUtils.isLayoutRtl()) { |
| 512 if (deltaX >= 1.f) { |
| 513 mReorderState |= REORDER_SCROLL_RIGHT; |
| 514 } else if (deltaX <= -1.f) { |
| 515 mReorderState |= REORDER_SCROLL_LEFT; |
| 516 } |
| 517 } else { |
| 518 if (deltaX >= 1.f) { |
| 519 mReorderState |= REORDER_SCROLL_LEFT; |
| 520 } else if (deltaX <= -1.f) { |
| 521 mReorderState |= REORDER_SCROLL_RIGHT; |
| 522 } |
| 523 } |
| 524 |
| 525 mLastReorderX = x; |
| 526 updateReorderPosition(accumulatedDeltaX); |
| 527 } |
| 528 } else if (!mScroller.isFinished()) { |
| 529 // 2.b. Still scrolling, update the scroll destination here. |
| 530 mScroller.setFinalX((int) (mScroller.getFinalX() + deltaX)); |
| 531 } else { |
| 532 // 2.c. Not scrolling. Check if we need to fast expand. |
| 533 float fastExpandDelta = |
| 534 calculateOffsetToMakeTabVisible(mInteractingTab, true, true,
true); |
| 535 |
| 536 if (mInteractingTab != null && fastExpandDelta != 0.f) { |
| 537 if ((fastExpandDelta > 0 && deltaX > 0) || (fastExpandDelta < 0
&& deltaX < 0)) { |
| 538 mScroller.startScroll( |
| 539 mScrollOffset, 0, (int) fastExpandDelta, 0, time, EX
PAND_DURATION_MS); |
| 540 } |
| 541 } else { |
| 542 updateScrollOffsetPosition((int) (mScrollOffset + deltaX)); |
| 543 } |
| 544 } |
| 545 |
| 546 // 3. Check if we should start the reorder mode |
| 547 if (!mInReorderMode) { |
| 548 final float absTotalX = Math.abs(totalX); |
| 549 final float absTotalY = Math.abs(totalY); |
| 550 if (totalY > mReorderMoveStartThreshold && absTotalX < mReorderMoveS
tartThreshold * 2.f |
| 551 && (absTotalX > EPSILON |
| 552 && (absTotalY / absTotalX) > TAN_OF_REORDER_ANGLE
_START_THRESHOLD)) { |
| 553 startReorderMode(time, x, x - totalX); |
| 554 } |
| 555 } |
| 556 |
| 557 // If we're scrolling at all we aren't interacting with any particular t
ab. |
| 558 // We already kicked off a fast expansion earlier if we needed one. Reo
rder mode will |
| 559 // repopulate this if necessary. |
| 560 if (!mInReorderMode) mInteractingTab = null; |
| 561 mUpdateHost.requestUpdate(); |
| 562 } |
| 563 |
| 564 /** |
| 565 * Called on touch fling event. This is called before the onUpOrCancel event
. |
| 566 * @param time The current time of the app in ms. |
| 567 * @param x The y coordinate of the start of the fling event. |
| 568 * @param y The y coordinate of the start of the fling event. |
| 569 * @param velocityX The amount of velocity in the x direction. |
| 570 * @param velocityY The amount of velocity in the y direction. |
| 571 */ |
| 572 public void fling(long time, float x, float y, float velocityX, float veloci
tyY) { |
| 573 resetResizeTimeout(false); |
| 574 |
| 575 velocityX = MathUtils.flipSignIf(velocityX, LocalizationUtils.isLayoutRt
l()); |
| 576 |
| 577 // 1. If we're currently in reorder mode, don't allow the user to fling. |
| 578 if (mInReorderMode) return; |
| 579 |
| 580 // 2. If we're fast expanding or scrolling, figure out the destination o
f the scroll so we |
| 581 // can apply it to the end of this fling. |
| 582 int scrollDeltaRemaining = 0; |
| 583 if (!mScroller.isFinished()) { |
| 584 scrollDeltaRemaining = mScroller.getFinalX() - mScrollOffset; |
| 585 |
| 586 mInteractingTab = null; |
| 587 mScroller.forceFinished(true); |
| 588 } |
| 589 |
| 590 // 3. Kick off the fling. |
| 591 mScroller.fling( |
| 592 mScrollOffset, 0, (int) velocityX, 0, (int) mMinScrollOffset, 0,
0, 0, 0, 0, time); |
| 593 mScroller.setFinalX(mScroller.getFinalX() + scrollDeltaRemaining); |
| 594 mUpdateHost.requestUpdate(); |
| 595 } |
| 596 |
| 597 /** |
| 598 * Called on onDown event. |
| 599 * @param time The time stamp in millisecond of the event. |
| 600 * @param x The x position of the event. |
| 601 * @param y The y position of the event. |
| 602 */ |
| 603 public void onDown(long time, float x, float y) { |
| 604 resetResizeTimeout(false); |
| 605 |
| 606 if (mNewTabButton.onDown(x, y)) { |
| 607 mRenderHost.requestRender(); |
| 608 return; |
| 609 } |
| 610 |
| 611 final StripLayoutTab clickedTab = getTabAtPosition(x); |
| 612 final int index = clickedTab != null |
| 613 ? TabModelUtils.getTabIndexById(mModel, clickedTab.getId()) |
| 614 : TabModel.INVALID_TAB_INDEX; |
| 615 // http://crbug.com/472186 : Needs to handle a case that index is invali
d. |
| 616 // The case could happen when the current tab is touched while we're inf
lating the rest of |
| 617 // the tabs from disk. |
| 618 mInteractingTab = index != TabModel.INVALID_TAB_INDEX && index < mStripT
abs.length |
| 619 ? mStripTabs[index] |
| 620 : null; |
| 621 if (clickedTab != null && clickedTab.checkCloseHitTest(x, y)) { |
| 622 clickedTab.setClosePressed(true); |
| 623 mLastPressedCloseButton = clickedTab.getCloseButton(); |
| 624 mRenderHost.requestRender(); |
| 625 } |
| 626 |
| 627 if (!mScroller.isFinished()) { |
| 628 mScroller.forceFinished(true); |
| 629 mInteractingTab = null; |
| 630 } |
| 631 } |
| 632 |
| 633 /** |
| 634 * Called on long press touch event. |
| 635 * @param time The current time of the app in ms. |
| 636 * @param x The x coordinate of the position of the press event. |
| 637 * @param y The y coordinate of the position of the press event. |
| 638 */ |
| 639 public void onLongPress(long time, float x, float y) { |
| 640 final StripLayoutTab clickedTab = getTabAtPosition(x); |
| 641 if (clickedTab != null && clickedTab.checkCloseHitTest(x, y)) { |
| 642 clickedTab.setClosePressed(false); |
| 643 mRenderHost.requestRender(); |
| 644 showTabMenu(clickedTab); |
| 645 } else { |
| 646 resetResizeTimeout(false); |
| 647 startReorderMode(time, x, x); |
| 648 } |
| 649 } |
| 650 |
| 651 /** |
| 652 * Called on click. This is called before the onUpOrCancel event. |
| 653 * @param time The current time of the app in ms. |
| 654 * @param x The x coordinate of the position of the click. |
| 655 * @param y The y coordinate of the position of the click. |
| 656 */ |
| 657 public void click(long time, float x, float y) { |
| 658 resetResizeTimeout(false); |
| 659 |
| 660 if (mNewTabButton.click(x, y) && mModel != null) { |
| 661 mTabCreator.launchNTP(); |
| 662 return; |
| 663 } |
| 664 |
| 665 final StripLayoutTab clickedTab = getTabAtPosition(x); |
| 666 if (clickedTab == null || clickedTab.isDying()) return; |
| 667 if (clickedTab.checkCloseHitTest(x, y)) { |
| 668 // 1. Start the close animation. |
| 669 startAnimation(buildTabClosedAnimation(clickedTab), true); |
| 670 |
| 671 // 2. Set the dying state of the tab. |
| 672 clickedTab.setIsDying(true); |
| 673 |
| 674 // 3. Fake a selection on the next tab now. |
| 675 Tab nextTab = mModel.getNextTabIfClosed(clickedTab.getId()); |
| 676 if (nextTab != null) tabSelected(time, nextTab.getId(), clickedTab.g
etId()); |
| 677 |
| 678 // 4. Find out if we're closing the last tab. This determines if we
resize immediately. |
| 679 boolean lastTab = mStripTabs.length == 0 |
| 680 || mStripTabs[mStripTabs.length - 1].getId() == clickedTab.g
etId(); |
| 681 |
| 682 // 5. Resize the tabs appropriately. |
| 683 resizeTabStrip(!lastTab); |
| 684 } else { |
| 685 int newIndex = TabModelUtils.getTabIndexById(mModel, clickedTab.getI
d()); |
| 686 TabModelUtils.setIndex(mModel, newIndex); |
| 687 } |
| 688 } |
| 689 |
| 690 /** |
| 691 * Called on up or cancel touch events. This is called after the click and f
ling event if any. |
| 692 * @param time The current time of the app in ms. |
| 693 */ |
| 694 public void onUpOrCancel(long time) { |
| 695 // 1. Reset the last close button pressed state. |
| 696 if (mLastPressedCloseButton != null) mLastPressedCloseButton.onUpOrCance
l(); |
| 697 mLastPressedCloseButton = null; |
| 698 |
| 699 // 2. Stop any reordering that is happening. |
| 700 stopReorderMode(); |
| 701 |
| 702 // 3. Reset state |
| 703 mInteractingTab = null; |
| 704 mReorderState = REORDER_SCROLL_NONE; |
| 705 if (mNewTabButton.onUpOrCancel() && mModel != null) { |
| 706 mTabCreator.launchNTP(); |
| 707 } |
| 708 } |
| 709 |
| 710 private boolean onUpdateAnimation(long time, boolean jumpToEnd) { |
| 711 // 1. Handle any Scroller movements (flings). |
| 712 if (!jumpToEnd) updateScrollOffset(time); |
| 713 |
| 714 // 2. Handle reordering automatically scrolling the tab strip. |
| 715 handleReorderAutoScrolling(time); |
| 716 |
| 717 // 3. Handle layout-wide animations. |
| 718 boolean update = false; |
| 719 boolean finished = true; |
| 720 if (mLayoutAnimations != null) { |
| 721 if (jumpToEnd) { |
| 722 finished = mLayoutAnimations.finished(); |
| 723 } else { |
| 724 finished = mLayoutAnimations.update(time); |
| 725 } |
| 726 if (jumpToEnd || finished) finishAnimation(); |
| 727 |
| 728 update = true; |
| 729 } |
| 730 |
| 731 // 4. Handle tab-specific content animations. |
| 732 for (int i = 0; i < mStripTabs.length; i++) { |
| 733 StripLayoutTab tab = mStripTabs[i]; |
| 734 if (tab.isAnimating()) { |
| 735 update = true; |
| 736 finished &= tab.onUpdateAnimation(time, jumpToEnd); |
| 737 } |
| 738 } |
| 739 |
| 740 // 5. Update tab spinners. |
| 741 updateSpinners(time); |
| 742 |
| 743 // 6. Stop any flings if we're trying to stop animations. |
| 744 if (jumpToEnd) mScroller.forceFinished(true); |
| 745 |
| 746 // 7. Request another update if anything requires it. |
| 747 if (update) mUpdateHost.requestUpdate(); |
| 748 |
| 749 return finished; |
| 750 } |
| 751 |
| 752 /** |
| 753 * @return Whether or not the tabs are moving. |
| 754 */ |
| 755 @VisibleForTesting |
| 756 public boolean isAnimating() { |
| 757 return mLayoutAnimations != null || !mScroller.isFinished(); |
| 758 } |
| 759 |
| 760 /** |
| 761 * Finishes any outstanding animations and propagates any related changes to
the |
| 762 * {@link TabModel}. |
| 763 */ |
| 764 public void finishAnimation() { |
| 765 if (mLayoutAnimations == null) return; |
| 766 |
| 767 // 1. Force any outstanding animations to finish. |
| 768 mLayoutAnimations.updateAndFinish(); |
| 769 mLayoutAnimations = null; |
| 770 |
| 771 // 2. Figure out which tabs need to be closed. |
| 772 ArrayList<StripLayoutTab> tabsToRemove = new ArrayList<StripLayoutTab>()
; |
| 773 for (int i = 0; i < mStripTabs.length; i++) { |
| 774 StripLayoutTab tab = mStripTabs[i]; |
| 775 if (tab.isDying()) tabsToRemove.add(tab); |
| 776 } |
| 777 |
| 778 // 3. Pass the close notifications to the model. |
| 779 for (StripLayoutTab tab : tabsToRemove) { |
| 780 TabModelUtils.closeTabById(mModel, tab.getId(), true); |
| 781 } |
| 782 |
| 783 if (!tabsToRemove.isEmpty()) mUpdateHost.requestUpdate(); |
| 784 } |
| 785 |
| 786 private void startAnimation(Animation<Animatable<?>> animation, boolean fini
shPrevious) { |
| 787 if (finishPrevious) finishAnimation(); |
| 788 |
| 789 if (mLayoutAnimations == null) { |
| 790 mLayoutAnimations = new ChromeAnimation<ChromeAnimation.Animatable<?
>>(); |
| 791 } |
| 792 |
| 793 mLayoutAnimations.add(animation); |
| 794 |
| 795 mUpdateHost.requestUpdate(); |
| 796 } |
| 797 |
| 798 private void cancelAnimation(StripLayoutTab tab, StripLayoutTab.Property pro
perty) { |
| 799 if (mLayoutAnimations == null) return; |
| 800 mLayoutAnimations.cancel(tab, property); |
| 801 } |
| 802 |
| 803 private void updateSpinners(long time) { |
| 804 long diff = time - mLastSpinnerUpdate; |
| 805 float degrees = diff * SPINNER_DPMS; |
| 806 boolean tabsToLoad = false; |
| 807 for (int i = 0; i < mStripTabs.length; i++) { |
| 808 StripLayoutTab tab = mStripTabs[i]; |
| 809 // TODO(clholgat): Only update if the tab is visible. |
| 810 if (tab.isLoading()) { |
| 811 tab.addLoadingSpinnerRotation(degrees); |
| 812 tabsToLoad = true; |
| 813 } |
| 814 } |
| 815 mLastSpinnerUpdate = time; |
| 816 if (tabsToLoad) { |
| 817 mStripTabEventHandler.removeMessages(MESSAGE_UPDATE_SPINNER); |
| 818 mStripTabEventHandler.sendEmptyMessageDelayed( |
| 819 MESSAGE_UPDATE_SPINNER, SPINNER_UPDATE_DELAY_MS); |
| 820 } |
| 821 } |
| 822 |
| 823 private void updateScrollOffsetPosition(int pos) { |
| 824 int oldScrollOffset = mScrollOffset; |
| 825 mScrollOffset = MathUtils.clamp(pos, (int) mMinScrollOffset, 0); |
| 826 |
| 827 if (mInReorderMode && mScroller.isFinished()) { |
| 828 int delta = MathUtils.flipSignIf( |
| 829 oldScrollOffset - mScrollOffset, LocalizationUtils.isLayoutR
tl()); |
| 830 updateReorderPosition(delta); |
| 831 } |
| 832 } |
| 833 |
| 834 private void updateScrollOffset(long time) { |
| 835 if (mScroller.computeScrollOffset(time)) { |
| 836 updateScrollOffsetPosition(mScroller.getCurrX()); |
| 837 mUpdateHost.requestUpdate(); |
| 838 } |
| 839 } |
| 840 |
| 841 private void updateScrollOffsetLimits() { |
| 842 // 1. Compute the width of the available space for all tabs. |
| 843 float stripWidth = mWidth - mLeftMargin - mRightMargin; |
| 844 |
| 845 // 2. Compute the effective width of every tab. |
| 846 float tabsWidth = 0.f; |
| 847 for (int i = 0; i < mStripTabs.length; i++) { |
| 848 final StripLayoutTab tab = mStripTabs[i]; |
| 849 tabsWidth += (tab.getWidth() - mTabOverlapWidth) * tab.getWidthWeigh
t(); |
| 850 } |
| 851 |
| 852 // 3. Correct fencepost error in tabswidth; |
| 853 tabsWidth = tabsWidth + mTabOverlapWidth; |
| 854 |
| 855 // 4. Calculate the minimum scroll offset. Round > -EPSILON to 0. |
| 856 mMinScrollOffset = Math.min(0.f, stripWidth - tabsWidth); |
| 857 if (mMinScrollOffset > -EPSILON) mMinScrollOffset = 0.f; |
| 858 |
| 859 // 5. Clamp mScrollOffset to make sure it's in the valid range. |
| 860 updateScrollOffsetPosition(mScrollOffset); |
| 861 } |
| 862 |
| 863 private void computeAndUpdateTabOrders(boolean delayResize) { |
| 864 final int count = mModel.getCount(); |
| 865 StripLayoutTab[] tabs = new StripLayoutTab[count]; |
| 866 |
| 867 for (int i = 0; i < count; i++) { |
| 868 int id = mModel.getTabAt(i).getId(); |
| 869 StripLayoutTab oldTab = findTabById(id); |
| 870 tabs[i] = oldTab != null ? oldTab : createStripTab(id); |
| 871 tabs[i].setAccessibilityDescription(mModel.getTabAt(i).getTitle()); |
| 872 } |
| 873 |
| 874 int oldStripLength = mStripTabs.length; |
| 875 mStripTabs = tabs; |
| 876 |
| 877 if (mStripTabs.length != oldStripLength) resizeTabStrip(delayResize); |
| 878 |
| 879 updateVisualTabOrdering(); |
| 880 } |
| 881 |
| 882 private void resizeTabStrip(boolean delay) { |
| 883 if (delay) { |
| 884 resetResizeTimeout(true); |
| 885 } else { |
| 886 computeAndUpdateTabWidth(true); |
| 887 } |
| 888 } |
| 889 |
| 890 private void updateVisualTabOrdering() { |
| 891 if (mStripTabs.length != mStripTabsVisuallyOrdered.length) { |
| 892 mStripTabsVisuallyOrdered = new StripLayoutTab[mStripTabs.length]; |
| 893 } |
| 894 |
| 895 mStripStacker.createVisualOrdering(mModel.index(), mStripTabs, mStripTab
sVisuallyOrdered); |
| 896 } |
| 897 |
| 898 private StripLayoutTab createStripTab(int id) { |
| 899 // TODO: Cache these |
| 900 StripLayoutTab tab = |
| 901 new StripLayoutTab(mContext, id, mTabLoadTrackerHost, mRenderHos
t, mIncognito); |
| 902 tab.setHeight(mHeight); |
| 903 pushStackerPropertiesToTab(tab); |
| 904 return tab; |
| 905 } |
| 906 |
| 907 private void pushStackerPropertiesToTab(StripLayoutTab tab) { |
| 908 tab.setCanShowCloseButton(mStripStacker.canShowCloseButton()); |
| 909 // TODO(dtrainor): Push more properties as they are added (title text sl
ide, etc?) |
| 910 } |
| 911 |
| 912 /** |
| 913 * @param id The Tab id. |
| 914 * @return The StripLayoutTab that corresponds to that tabid. |
| 915 */ |
| 916 @VisibleForTesting |
| 917 public StripLayoutTab findTabById(int id) { |
| 918 if (mStripTabs == null) return null; |
| 919 for (int i = 0; i < mStripTabs.length; i++) { |
| 920 if (mStripTabs[i].getId() == id) return mStripTabs[i]; |
| 921 } |
| 922 return null; |
| 923 } |
| 924 |
| 925 private int findIndexForTab(int id) { |
| 926 if (mStripTabs == null) return TabModel.INVALID_TAB_INDEX; |
| 927 for (int i = 0; i < mStripTabs.length; i++) { |
| 928 if (mStripTabs[i].getId() == id) return i; |
| 929 } |
| 930 return TabModel.INVALID_TAB_INDEX; |
| 931 } |
| 932 |
| 933 private void computeAndUpdateTabWidth(boolean animate) { |
| 934 // Remove any queued resize messages. |
| 935 mStripTabEventHandler.removeMessages(MESSAGE_RESIZE); |
| 936 |
| 937 int numTabs = Math.max(mStripTabs.length, 1); |
| 938 |
| 939 // 1. Compute the width of the available space for all tabs. |
| 940 float stripWidth = mWidth - mLeftMargin - mRightMargin; |
| 941 |
| 942 // 2. Compute additional width we gain from overlapping the tabs. |
| 943 float overlapWidth = mTabOverlapWidth * (numTabs - 1); |
| 944 |
| 945 // 3. Calculate the optimal tab width. |
| 946 float optimalTabWidth = (stripWidth + overlapWidth) / numTabs; |
| 947 |
| 948 // 4. Calculate the realistic tab width. |
| 949 mCachedTabWidth = MathUtils.clamp(optimalTabWidth, mMinTabWidth, mMaxTab
Width); |
| 950 |
| 951 // 5. Propagate the tab width to all tabs. |
| 952 for (int i = 0; i < mStripTabs.length; i++) { |
| 953 StripLayoutTab tab = mStripTabs[i]; |
| 954 if (tab.isDying()) continue; |
| 955 |
| 956 // 5.a. Cancel any outstanding tab width animations. |
| 957 cancelAnimation(mStripTabs[i], StripLayoutTab.Property.WIDTH); |
| 958 |
| 959 if (animate) { |
| 960 startAnimation(buildTabResizeAnimation(tab, mCachedTabWidth), fa
lse); |
| 961 } else { |
| 962 mStripTabs[i].setWidth(mCachedTabWidth); |
| 963 } |
| 964 } |
| 965 } |
| 966 |
| 967 private void updateStrip(long time, long dt) { |
| 968 if (mModel == null) return; |
| 969 |
| 970 // TODO(dtrainor): Remove this once tabCreated() is refactored to be cal
led even from |
| 971 // restore. |
| 972 if (mStripTabs == null || mModel.getCount() != mStripTabs.length) { |
| 973 computeAndUpdateTabOrders(false); |
| 974 } |
| 975 |
| 976 // 1. Update the scroll offset limits |
| 977 updateScrollOffsetLimits(); |
| 978 |
| 979 // 2. Calculate the ideal tab positions |
| 980 computeTabInitialPositions(); |
| 981 |
| 982 // 3. Calculate the tab stacking. |
| 983 computeTabOffsetHelper(); |
| 984 |
| 985 // 4. Calculate which tabs are visible. |
| 986 mStripStacker.performOcclusionPass(mModel.index(), mStripTabs); |
| 987 |
| 988 // 5. Create render list. |
| 989 createRenderList(); |
| 990 |
| 991 // 6. Figure out where to put the new tab button. |
| 992 updateNewTabButtonState(); |
| 993 |
| 994 // 7. Check if we have any animations and request an update if so. |
| 995 for (int i = 0; i < mStripTabs.length; i++) { |
| 996 if (mStripTabs[i].isAnimating()) { |
| 997 mUpdateHost.requestUpdate(); |
| 998 break; |
| 999 } |
| 1000 } |
| 1001 } |
| 1002 |
| 1003 private void computeTabInitialPositions() { |
| 1004 // Shift all of the tabs over by the the left margin because we're |
| 1005 // no longer base lined at 0 |
| 1006 float tabPosition; |
| 1007 if (!LocalizationUtils.isLayoutRtl()) { |
| 1008 tabPosition = mScrollOffset + mLeftMargin; |
| 1009 } else { |
| 1010 tabPosition = mWidth - mCachedTabWidth - mScrollOffset - mRightMargi
n; |
| 1011 } |
| 1012 |
| 1013 for (int i = 0; i < mStripTabs.length; i++) { |
| 1014 StripLayoutTab tab = mStripTabs[i]; |
| 1015 tab.setIdealX(tabPosition); |
| 1016 float delta = (tab.getWidth() - mTabOverlapWidth) * tab.getWidthWeig
ht(); |
| 1017 delta = MathUtils.flipSignIf(delta, LocalizationUtils.isLayoutRtl())
; |
| 1018 tabPosition += delta; |
| 1019 } |
| 1020 } |
| 1021 |
| 1022 private void computeTabOffsetHelper() { |
| 1023 final int selIndex = mModel.index(); |
| 1024 |
| 1025 // 1. Calculate the size of the selected tab. This is used later to fig
ure out how |
| 1026 // occluded the tabs are. |
| 1027 final StripLayoutTab selTab = selIndex >= 0 ? mStripTabs[selIndex] : nul
l; |
| 1028 final float selTabWidth = selTab != null ? selTab.getWidth() : 0; |
| 1029 final float selTabVisibleSize = selTabWidth - mTabStackWidth - mTabOverl
apWidth; |
| 1030 |
| 1031 for (int i = 0; i < mStripTabs.length; i++) { |
| 1032 StripLayoutTab tab = mStripTabs[i]; |
| 1033 |
| 1034 float posX = tab.getIdealX(); |
| 1035 |
| 1036 // 2. Calculate how many tabs are stacked on the left or the right,
giving us an idea |
| 1037 // of where we can stack this current tab. |
| 1038 int leftStackCount = (i < selIndex) ? Math.min(i, MAX_TABS_TO_STACK) |
| 1039 : Math.min(MAX_TABS_TO_STACK, se
lIndex) |
| 1040 + Math.min(MAX_TABS_TO_STACK, i - selIndex); |
| 1041 |
| 1042 int rightStackCount = (i >= selIndex) |
| 1043 ? Math.min(mStripTabs.length - 1 - i, MAX_TABS_TO_STACK) |
| 1044 : Math.min(mStripTabs.length - 1 - selIndex, MAX_TABS_TO_STA
CK) |
| 1045 + Math.min(selIndex - i, MAX_TABS_TO_STACK); |
| 1046 |
| 1047 if (LocalizationUtils.isLayoutRtl()) { |
| 1048 int oldLeft = leftStackCount; |
| 1049 leftStackCount = rightStackCount; |
| 1050 rightStackCount = oldLeft; |
| 1051 } |
| 1052 |
| 1053 // 3. Calculate the proper draw position for the tab. Clamp based o
n stacking |
| 1054 // rules. |
| 1055 float minDrawX = mTabStackWidth * leftStackCount + mLeftMargin; |
| 1056 float maxDrawX = mWidth - mTabStackWidth * rightStackCount - mRightM
argin; |
| 1057 |
| 1058 float drawX = |
| 1059 MathUtils.clamp(posX + tab.getOffsetX(), minDrawX, maxDrawX
- tab.getWidth()); |
| 1060 |
| 1061 // TODO(dtrainor): Don't set drawX if the tab is closing? |
| 1062 tab.setDrawX(drawX); |
| 1063 tab.setDrawY(tab.getOffsetY()); |
| 1064 |
| 1065 // 4. Calculate how visible this tab is. |
| 1066 float visiblePercentage = 1.f; |
| 1067 if (i != selIndex) { |
| 1068 final float effectiveTabWidth = Math.max(tab.getWidth(), 1.f); |
| 1069 final boolean leftStack = |
| 1070 LocalizationUtils.isLayoutRtl() ? i > selIndex : i < sel
Index; |
| 1071 final float minVisible = !leftStack ? minDrawX + selTabVisibleSi
ze : minDrawX; |
| 1072 final float maxVisible = leftStack ? maxDrawX - selTabVisibleSiz
e : maxDrawX; |
| 1073 |
| 1074 final float clippedTabWidth = |
| 1075 Math.min(posX + effectiveTabWidth, maxVisible) - Math.ma
x(posX, minVisible); |
| 1076 visiblePercentage = MathUtils.clamp(clippedTabWidth / effectiveT
abWidth, 0.f, 1.f); |
| 1077 } |
| 1078 tab.setVisiblePercentage(visiblePercentage); |
| 1079 |
| 1080 // 5. Calculate which index we start sliding content for. |
| 1081 // When reordering, we don't want to slide the content of the adjace
nt tabs. |
| 1082 int contentOffsetIndex = mInReorderMode ? selIndex + 1 : selIndex; |
| 1083 |
| 1084 // 6. Calculate how much the tab is overlapped on the left side or r
ight for RTL. |
| 1085 float hiddenAmount = 0.f; |
| 1086 if (i > contentOffsetIndex && i > 0 && mStripStacker.canSlideTitleTe
xt()) { |
| 1087 // 6.a. Get the effective right edge of the previous tab. |
| 1088 final StripLayoutTab prevTab = mStripTabs[i - 1]; |
| 1089 final float prevLayoutWidth = |
| 1090 (prevTab.getWidth() - mTabOverlapWidth) * prevTab.getWid
thWeight(); |
| 1091 float prevTabRight = prevTab.getDrawX(); |
| 1092 if (!LocalizationUtils.isLayoutRtl()) prevTabRight += prevLayout
Width; |
| 1093 |
| 1094 // 6.b. Subtract our current draw X from the previous tab's righ
t edge and |
| 1095 // get the percentage covered. |
| 1096 hiddenAmount = Math.max(prevTabRight - drawX, 0); |
| 1097 if (LocalizationUtils.isLayoutRtl()) { |
| 1098 // Invert The amount because we're RTL. |
| 1099 hiddenAmount = prevLayoutWidth - hiddenAmount; |
| 1100 } |
| 1101 } |
| 1102 |
| 1103 tab.setContentOffsetX(hiddenAmount); |
| 1104 } |
| 1105 } |
| 1106 |
| 1107 private void createRenderList() { |
| 1108 // 1. Figure out how many tabs will need to be rendered. |
| 1109 int renderCount = 0; |
| 1110 for (int i = 0; i < mStripTabsVisuallyOrdered.length; ++i) { |
| 1111 if (mStripTabsVisuallyOrdered[i].isVisible()) renderCount++; |
| 1112 } |
| 1113 |
| 1114 // 2. Reallocate the render list if necessary. |
| 1115 if (mStripTabsToRender.length != renderCount) { |
| 1116 mStripTabsToRender = new StripLayoutTab[renderCount]; |
| 1117 } |
| 1118 |
| 1119 // 3. Populate it with the visible tabs. |
| 1120 int renderIndex = 0; |
| 1121 for (int i = 0; i < mStripTabsVisuallyOrdered.length; ++i) { |
| 1122 if (mStripTabsVisuallyOrdered[i].isVisible()) { |
| 1123 mStripTabsToRender[renderIndex++] = mStripTabsVisuallyOrdered[i]
; |
| 1124 } |
| 1125 } |
| 1126 } |
| 1127 |
| 1128 private void updateNewTabButtonState() { |
| 1129 // 1. Don't display the new tab button if we're in reorder mode. |
| 1130 if (mInReorderMode || mStripTabs.length == 0) { |
| 1131 mNewTabButton.setVisible(false); |
| 1132 return; |
| 1133 } |
| 1134 mNewTabButton.setVisible(true); |
| 1135 |
| 1136 float leftEdge = mWidth - mRightMargin; |
| 1137 float rightEdge = mLeftMargin; |
| 1138 |
| 1139 for (int i = 0; i < mStripTabs.length; i++) { |
| 1140 StripLayoutTab tab = mStripTabs[i]; |
| 1141 float layoutWidth = (tab.getWidth() - mTabOverlapWidth) * tab.getWid
thWeight(); |
| 1142 rightEdge = Math.max(tab.getDrawX() + layoutWidth, rightEdge); |
| 1143 leftEdge = Math.min(tab.getDrawX(), leftEdge); |
| 1144 } |
| 1145 rightEdge = Math.min(rightEdge + mTabOverlapWidth, mWidth - mRightMargin
); |
| 1146 leftEdge = Math.max(leftEdge, mLeftMargin); |
| 1147 |
| 1148 rightEdge -= mTabOverlapWidth / 2; |
| 1149 |
| 1150 // 3. Position the new tab button. |
| 1151 if (!LocalizationUtils.isLayoutRtl()) { |
| 1152 mNewTabButton.setX(rightEdge); |
| 1153 } else { |
| 1154 mNewTabButton.setX(leftEdge - mNewTabButtonWidth); |
| 1155 } |
| 1156 } |
| 1157 |
| 1158 private float calculateOffsetToMakeTabVisible(StripLayoutTab tab, boolean ca
nExpandSelectedTab, |
| 1159 boolean canExpandLeft, boolean canExpandRight) { |
| 1160 if (tab == null) return 0.f; |
| 1161 |
| 1162 final int selIndex = mModel.index(); |
| 1163 final int index = TabModelUtils.getTabIndexById(mModel, tab.getId()); |
| 1164 |
| 1165 // 1. The selected tab is always visible. Early out unless we want to u
nstack it. |
| 1166 if (selIndex == index && !canExpandSelectedTab) return 0.f; |
| 1167 |
| 1168 // TODO(dtrainor): Use real tab widths here? |
| 1169 float stripWidth = mWidth - mLeftMargin - mRightMargin; |
| 1170 final float tabWidth = mCachedTabWidth - mTabOverlapWidth; |
| 1171 |
| 1172 // TODO(dtrainor): Handle maximum number of tabs that can be visibly sta
cked in these |
| 1173 // optimal positions. |
| 1174 |
| 1175 // 2. Calculate the optimal minimum and maximum scroll offsets to show t
he tab. |
| 1176 float optimalLeft = -index * tabWidth; |
| 1177 float optimalRight = stripWidth - (index + 1) * tabWidth; |
| 1178 |
| 1179 // 3. Account for the selected tab always being visible. Need to buffer
by one extra |
| 1180 // tab width depending on if the tab is to the left or right of the sele
cted tab. |
| 1181 if (index < selIndex) { |
| 1182 optimalRight -= tabWidth; |
| 1183 } else if (index > selIndex) { |
| 1184 optimalLeft += tabWidth; |
| 1185 } |
| 1186 |
| 1187 // 4. Return the proper deltaX that has to be applied to the current scr
oll to see the |
| 1188 // tab. |
| 1189 if (mScrollOffset < optimalLeft && canExpandLeft) { |
| 1190 return optimalLeft - mScrollOffset; |
| 1191 } else if (mScrollOffset > optimalRight && canExpandRight) { |
| 1192 return optimalRight - mScrollOffset; |
| 1193 } |
| 1194 |
| 1195 // 5. We don't have to do anything. Return no delta. |
| 1196 return 0.f; |
| 1197 } |
| 1198 |
| 1199 private StripLayoutTab getTabAtPosition(float x) { |
| 1200 for (int i = mStripTabsVisuallyOrdered.length - 1; i >= 0; i--) { |
| 1201 final StripLayoutTab tab = mStripTabsVisuallyOrdered[i]; |
| 1202 if (tab.isVisible() && tab.getDrawX() <= x && x <= (tab.getDrawX() +
tab.getWidth())) { |
| 1203 return tab; |
| 1204 } |
| 1205 } |
| 1206 |
| 1207 return null; |
| 1208 } |
| 1209 |
| 1210 /** |
| 1211 * @param tab The StripLayoutTab to look for. |
| 1212 * @return The index of the tab in the visual ordering. |
| 1213 */ |
| 1214 @VisibleForTesting |
| 1215 public int visualIndexOfTab(StripLayoutTab tab) { |
| 1216 for (int i = 0; i < mStripTabsVisuallyOrdered.length; i++) { |
| 1217 if (mStripTabsVisuallyOrdered[i] == tab) { |
| 1218 return i; |
| 1219 } |
| 1220 } |
| 1221 return -1; |
| 1222 } |
| 1223 |
| 1224 /** |
| 1225 * @param tab The StripLayoutTab you're looking at. |
| 1226 * @return Whether or not this tab is the foreground tab. |
| 1227 */ |
| 1228 @VisibleForTesting |
| 1229 public boolean isForegroundTab(StripLayoutTab tab) { |
| 1230 return tab == mStripTabsVisuallyOrdered[mStripTabsVisuallyOrdered.length
- 1]; |
| 1231 } |
| 1232 |
| 1233 private void startReorderMode(long time, float currentX, float startX) { |
| 1234 if (mInReorderMode) return; |
| 1235 |
| 1236 // 1. Reset the last pressed close button state. |
| 1237 if (mLastPressedCloseButton != null && mLastPressedCloseButton.isPressed
()) { |
| 1238 mLastPressedCloseButton.setPressed(false); |
| 1239 } |
| 1240 mLastPressedCloseButton = null; |
| 1241 |
| 1242 // 2. Check to see if we have a valid tab to start dragging. |
| 1243 mInteractingTab = getTabAtPosition(startX); |
| 1244 if (mInteractingTab == null) return; |
| 1245 |
| 1246 // 3. Set initial state parameters. |
| 1247 mLastReorderScrollTime = 0; |
| 1248 mReorderState = REORDER_SCROLL_NONE; |
| 1249 mLastReorderX = startX; |
| 1250 mInReorderMode = true; |
| 1251 |
| 1252 // 4. Select this tab so that it is always in the foreground. |
| 1253 TabModelUtils.setIndex( |
| 1254 mModel, TabModelUtils.getTabIndexById(mModel, mInteractingTab.ge
tId())); |
| 1255 |
| 1256 // 5. Fast expand to make sure this tab is visible. |
| 1257 float fastExpandDelta = calculateOffsetToMakeTabVisible(mInteractingTab,
true, true, true); |
| 1258 mScroller.startScroll(mScrollOffset, 0, (int) fastExpandDelta, 0, time,
EXPAND_DURATION_MS); |
| 1259 |
| 1260 // 6. Request an update. |
| 1261 mUpdateHost.requestUpdate(); |
| 1262 } |
| 1263 |
| 1264 private void stopReorderMode() { |
| 1265 if (!mInReorderMode) return; |
| 1266 |
| 1267 // 1. Reset the state variables. |
| 1268 mLastReorderScrollTime = 0; |
| 1269 mReorderState = REORDER_SCROLL_NONE; |
| 1270 mLastReorderX = 0.f; |
| 1271 mInReorderMode = false; |
| 1272 |
| 1273 // 2. Clear any drag offset. |
| 1274 startAnimation(buildTabMoveAnimation(mInteractingTab, mInteractingTab.ge
tOffsetX()), true); |
| 1275 |
| 1276 // 3. Request an update. |
| 1277 mUpdateHost.requestUpdate(); |
| 1278 } |
| 1279 |
| 1280 private void updateReorderPosition(float deltaX) { |
| 1281 if (!mInReorderMode || mInteractingTab == null) return; |
| 1282 |
| 1283 float offset = mInteractingTab.getOffsetX() + deltaX; |
| 1284 int curIndex = findIndexForTab(mInteractingTab.getId()); |
| 1285 |
| 1286 // 1. Compute the reorder threshold values. |
| 1287 final float flipWidth = mCachedTabWidth - mTabOverlapWidth; |
| 1288 final float flipThreshold = REORDER_OVERLAP_SWITCH_PERCENTAGE * flipWidt
h; |
| 1289 |
| 1290 // 2. Check if we should swap tabs and track the new destination index. |
| 1291 int destIndex = TabModel.INVALID_TAB_INDEX; |
| 1292 boolean pastLeftThreshold = offset < -flipThreshold; |
| 1293 boolean pastRightThreshold = offset > flipThreshold; |
| 1294 boolean isNotRightMost = curIndex < mStripTabs.length - 1; |
| 1295 boolean isNotLeftMost = curIndex > 0; |
| 1296 |
| 1297 if (LocalizationUtils.isLayoutRtl()) { |
| 1298 boolean oldLeft = pastLeftThreshold; |
| 1299 pastLeftThreshold = pastRightThreshold; |
| 1300 pastRightThreshold = oldLeft; |
| 1301 } |
| 1302 |
| 1303 if (pastRightThreshold && isNotRightMost) { |
| 1304 destIndex = curIndex + 2; |
| 1305 } else if (pastLeftThreshold && isNotLeftMost) { |
| 1306 destIndex = curIndex - 1; |
| 1307 } |
| 1308 |
| 1309 // 3. If we should swap tabs, make the swap. |
| 1310 if (destIndex != TabModel.INVALID_TAB_INDEX) { |
| 1311 // 3.a. Since we're about to move the tab we're dragging, adjust it'
s offset so it |
| 1312 // stays in the same apparent position. |
| 1313 boolean shouldFlip = |
| 1314 LocalizationUtils.isLayoutRtl() ? destIndex < curIndex : des
tIndex > curIndex; |
| 1315 offset += MathUtils.flipSignIf(flipWidth, shouldFlip); |
| 1316 |
| 1317 // 3.b. Swap the tabs. |
| 1318 reorderTab(mInteractingTab.getId(), curIndex, destIndex, true); |
| 1319 mModel.moveTab(mInteractingTab.getId(), destIndex); |
| 1320 |
| 1321 // 3.c. Update our curIndex as we have just moved the tab. |
| 1322 curIndex += destIndex > curIndex ? 1 : -1; |
| 1323 |
| 1324 // 3.d. Update visual tab ordering. |
| 1325 updateVisualTabOrdering(); |
| 1326 } |
| 1327 |
| 1328 // 4. Limit offset based on tab position. First tab can't drag left, la
st tab can't drag |
| 1329 // right. |
| 1330 if (curIndex == 0) { |
| 1331 offset = |
| 1332 LocalizationUtils.isLayoutRtl() ? Math.min(0.f, offset) : Ma
th.max(0.f, offset); |
| 1333 } |
| 1334 if (curIndex == mStripTabs.length - 1) { |
| 1335 offset = |
| 1336 LocalizationUtils.isLayoutRtl() ? Math.max(0.f, offset) : Ma
th.min(0.f, offset); |
| 1337 } |
| 1338 |
| 1339 // 5. Set the new offset. |
| 1340 mInteractingTab.setOffsetX(offset); |
| 1341 } |
| 1342 |
| 1343 private void reorderTab(int id, int oldIndex, int newIndex, boolean animate)
{ |
| 1344 StripLayoutTab tab = findTabById(id); |
| 1345 if (tab == null || oldIndex == newIndex) return; |
| 1346 |
| 1347 // 1. If the tab is already at the right spot, don't do anything. |
| 1348 int index = findIndexForTab(id); |
| 1349 if (index == newIndex) return; |
| 1350 |
| 1351 // 2. Check if it's the tab we are dragging, but we have an old source i
ndex. Ignore in |
| 1352 // this case because we probably just already moved it. |
| 1353 if (mInReorderMode && index != oldIndex && tab == mInteractingTab) retur
n; |
| 1354 |
| 1355 // 3. Swap the tabs. |
| 1356 moveElement(mStripTabs, index, newIndex); |
| 1357 |
| 1358 // 4. Update newIndex to point to the proper element. |
| 1359 if (index < newIndex) newIndex--; |
| 1360 |
| 1361 // 5. Animate if necessary. |
| 1362 if (animate) { |
| 1363 final float flipWidth = mCachedTabWidth - mTabOverlapWidth; |
| 1364 final int direction = oldIndex <= newIndex ? 1 : -1; |
| 1365 final float animationLength = |
| 1366 MathUtils.flipSignIf(direction * flipWidth, LocalizationUtil
s.isLayoutRtl()); |
| 1367 StripLayoutTab slideTab = mStripTabs[newIndex - direction]; |
| 1368 startAnimation(buildTabMoveAnimation(slideTab, animationLength), tru
e); |
| 1369 } |
| 1370 } |
| 1371 |
| 1372 private void handleReorderAutoScrolling(long time) { |
| 1373 if (!mInReorderMode) return; |
| 1374 |
| 1375 // 1. Track the delta time since the last auto scroll. |
| 1376 final float deltaSec = |
| 1377 mLastReorderScrollTime == 0 ? 0.f : (time - mLastReorderScrollTi
me) / 1000.f; |
| 1378 mLastReorderScrollTime = time; |
| 1379 |
| 1380 final float x = mInteractingTab.getDrawX(); |
| 1381 |
| 1382 // 2. Calculate the gutters for accelerating the scroll speed. |
| 1383 // Speed: MAX MIN MIN MAX |
| 1384 // |-------|======|--------------------|======|-------| |
| 1385 final float dragRange = REORDER_EDGE_SCROLL_START_MAX_DP - REORDER_EDGE_
SCROLL_START_MIN_DP; |
| 1386 final float leftMinX = REORDER_EDGE_SCROLL_START_MIN_DP + mLeftMargin; |
| 1387 final float leftMaxX = REORDER_EDGE_SCROLL_START_MAX_DP + mLeftMargin; |
| 1388 final float rightMinX = |
| 1389 mWidth - mLeftMargin - mRightMargin - REORDER_EDGE_SCROLL_START_
MIN_DP; |
| 1390 final float rightMaxX = |
| 1391 mWidth - mLeftMargin - mRightMargin - REORDER_EDGE_SCROLL_START_
MAX_DP; |
| 1392 |
| 1393 // 3. See if the current draw position is in one of the gutters and figu
re out how far in. |
| 1394 // Note that we only allow scrolling in each direction if the user has a
lready manually |
| 1395 // moved that way. |
| 1396 float dragSpeedRatio = 0.f; |
| 1397 if ((mReorderState & REORDER_SCROLL_LEFT) != 0 && x < leftMinX) { |
| 1398 dragSpeedRatio = -(leftMinX - Math.max(x, leftMaxX)) / dragRange; |
| 1399 } else if ((mReorderState & REORDER_SCROLL_RIGHT) != 0 && x + mCachedTab
Width > rightMinX) { |
| 1400 dragSpeedRatio = (Math.min(x + mCachedTabWidth, rightMaxX) - rightMi
nX) / dragRange; |
| 1401 } |
| 1402 |
| 1403 dragSpeedRatio = MathUtils.flipSignIf(dragSpeedRatio, LocalizationUtils.
isLayoutRtl()); |
| 1404 |
| 1405 if (dragSpeedRatio != 0.f) { |
| 1406 // 4.a. We're in a gutter. Update the scroll offset. |
| 1407 float dragSpeed = REORDER_EDGE_SCROLL_MAX_SPEED_DP * dragSpeedRatio; |
| 1408 updateScrollOffsetPosition((int) (mScrollOffset + dragSpeed * deltaS
ec)); |
| 1409 |
| 1410 mUpdateHost.requestUpdate(); |
| 1411 } else { |
| 1412 // 4.b. We're not in a gutter. Reset the scroll delta time tracker. |
| 1413 mLastReorderScrollTime = 0; |
| 1414 } |
| 1415 } |
| 1416 |
| 1417 private void resetResizeTimeout(boolean postIfNotPresent) { |
| 1418 final boolean present = mStripTabEventHandler.hasMessages(MESSAGE_RESIZE
); |
| 1419 |
| 1420 if (present) mStripTabEventHandler.removeMessages(MESSAGE_RESIZE); |
| 1421 |
| 1422 if (present || postIfNotPresent) { |
| 1423 mStripTabEventHandler.sendEmptyMessageAtTime(MESSAGE_RESIZE, RESIZE_
DELAY_MS); |
| 1424 } |
| 1425 } |
| 1426 |
| 1427 private class StripTabEventHandler extends Handler { |
| 1428 @Override |
| 1429 public void handleMessage(Message m) { |
| 1430 switch (m.what) { |
| 1431 case MESSAGE_RESIZE: |
| 1432 computeAndUpdateTabWidth(true); |
| 1433 mUpdateHost.requestUpdate(); |
| 1434 break; |
| 1435 case MESSAGE_UPDATE_SPINNER: |
| 1436 mUpdateHost.requestUpdate(); |
| 1437 break; |
| 1438 default: |
| 1439 assert false : "StripTabEventHandler got unknown message " +
m.what; |
| 1440 } |
| 1441 } |
| 1442 } |
| 1443 |
| 1444 private class TabLoadTrackerCallbackImpl implements TabLoadTrackerCallback { |
| 1445 @Override |
| 1446 public void loadStateChanged(int id) { |
| 1447 mUpdateHost.requestUpdate(); |
| 1448 } |
| 1449 } |
| 1450 |
| 1451 private static Animation<Animatable<?>> buildTabCreatedAnimation(StripLayout
Tab tab) { |
| 1452 return createAnimation(tab, StripLayoutTab.Property.Y_OFFSET, tab.getHei
ght(), 0.f, |
| 1453 ANIM_TAB_CREATED_MS, 0, false, ChromeAnimation.getLinearInterpol
ator()); |
| 1454 } |
| 1455 |
| 1456 private static Animation<Animatable<?>> buildTabClosedAnimation(StripLayoutT
ab tab) { |
| 1457 return createAnimation(tab, StripLayoutTab.Property.Y_OFFSET, tab.getOff
setY(), |
| 1458 tab.getHeight(), ANIM_TAB_CLOSED_MS, 0, false, |
| 1459 ChromeAnimation.getLinearInterpolator()); |
| 1460 } |
| 1461 |
| 1462 private static Animation<Animatable<?>> buildTabResizeAnimation( |
| 1463 StripLayoutTab tab, float width) { |
| 1464 return createAnimation(tab, StripLayoutTab.Property.WIDTH, tab.getWidth(
), width, |
| 1465 ANIM_TAB_RESIZE_MS, 0, false, ChromeAnimation.getLinearInterpola
tor()); |
| 1466 } |
| 1467 |
| 1468 private static Animation<Animatable<?>> buildTabMoveAnimation( |
| 1469 StripLayoutTab tab, float startX) { |
| 1470 return createAnimation(tab, StripLayoutTab.Property.X_OFFSET, startX, 0.
f, ANIM_TAB_MOVE_MS, |
| 1471 0, false, ChromeAnimation.getLinearInterpolator()); |
| 1472 } |
| 1473 |
| 1474 private static <T> void moveElement(T[] array, int oldIndex, int newIndex) { |
| 1475 if (oldIndex <= newIndex) { |
| 1476 moveElementUp(array, oldIndex, newIndex); |
| 1477 } else { |
| 1478 moveElementDown(array, oldIndex, newIndex); |
| 1479 } |
| 1480 } |
| 1481 |
| 1482 private static <T> void moveElementUp(T[] array, int oldIndex, int newIndex)
{ |
| 1483 assert oldIndex <= newIndex; |
| 1484 if (oldIndex == newIndex || oldIndex + 1 == newIndex) return; |
| 1485 |
| 1486 T elem = array[oldIndex]; |
| 1487 for (int i = oldIndex; i < newIndex - 1; i++) { |
| 1488 array[i] = array[i + 1]; |
| 1489 } |
| 1490 array[newIndex - 1] = elem; |
| 1491 } |
| 1492 |
| 1493 private static <T> void moveElementDown(T[] array, int oldIndex, int newInde
x) { |
| 1494 assert oldIndex >= newIndex; |
| 1495 if (oldIndex == newIndex) return; |
| 1496 |
| 1497 T elem = array[oldIndex]; |
| 1498 for (int i = oldIndex - 1; i >= newIndex; i--) { |
| 1499 array[i + 1] = array[i]; |
| 1500 } |
| 1501 array[newIndex] = elem; |
| 1502 } |
| 1503 |
| 1504 /** |
| 1505 * Sets the current scroll offset of the TabStrip. |
| 1506 * @param offset The offset to set the TabStrip's scroll state to. |
| 1507 */ |
| 1508 @VisibleForTesting |
| 1509 public void testSetScrollOffset(int offset) { |
| 1510 mScrollOffset = offset; |
| 1511 } |
| 1512 |
| 1513 /** |
| 1514 * Starts a fling with the specified velocity. |
| 1515 * @param velocity The velocity to trigger the fling with. Negative to go l
eft, positive to go |
| 1516 * right. |
| 1517 */ |
| 1518 @VisibleForTesting |
| 1519 public void testFling(float velocity) { |
| 1520 fling(SystemClock.uptimeMillis(), 0, 0, velocity, 0); |
| 1521 } |
| 1522 |
| 1523 /** |
| 1524 * Displays the tab menu below the anchor tab. |
| 1525 * @param anchorTab The tab the menu will be anchored to |
| 1526 */ |
| 1527 private void showTabMenu(StripLayoutTab anchorTab) { |
| 1528 // 1. Bring the anchor tab to the foreground. |
| 1529 int tabIndex = TabModelUtils.getTabIndexById(mModel, anchorTab.getId()); |
| 1530 TabModelUtils.setIndex(mModel, tabIndex); |
| 1531 |
| 1532 // 2. Anchor the popupMenu to the view associated with the tab |
| 1533 View tabView = TabModelUtils.getCurrentTab(mModel).getView(); |
| 1534 mTabMenu.setAnchorView(tabView); |
| 1535 |
| 1536 // 3. Set the vertical offset to align the tab menu with bottom of the t
ab strip |
| 1537 int verticalOffset = |
| 1538 -(tabView.getHeight() |
| 1539 - (int) mContext.getResources().getDimension(R.dimen.tab
_strip_height)); |
| 1540 mTabMenu.setVerticalOffset(verticalOffset); |
| 1541 |
| 1542 // 4. Set the horizontal offset to align the tab menu with the right sid
e of the tab |
| 1543 int horizontalOffset = Math.round((anchorTab.getDrawX() + anchorTab.getW
idth()) |
| 1544 * mContext.getResources().getDisplayMetri
cs().density) |
| 1545 - mTabMenu.getWidth(); |
| 1546 mTabMenu.setHorizontalOffset(horizontalOffset); |
| 1547 |
| 1548 mTabMenu.show(); |
| 1549 } |
| 1550 |
| 1551 /** |
| 1552 * @return true if the tab menu is showing |
| 1553 */ |
| 1554 @VisibleForTesting |
| 1555 public boolean isTabMenuShowing() { |
| 1556 return mTabMenu.isShowing(); |
| 1557 } |
| 1558 |
| 1559 /** |
| 1560 * @param menuItemId The id of the menu item to click |
| 1561 */ |
| 1562 @VisibleForTesting |
| 1563 public void clickTabMenuItem(int menuItemId) { |
| 1564 mTabMenu.performItemClick(menuItemId); |
| 1565 } |
| 1566 } |
OLD | NEW |