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 android.content.Context; |
| 8 import android.content.res.Resources; |
| 9 import android.graphics.RectF; |
| 10 |
| 11 import com.google.android.apps.chrome.R; |
| 12 |
| 13 import org.chromium.base.VisibleForTesting; |
| 14 import org.chromium.chrome.browser.compositor.LayerTitleCache; |
| 15 import org.chromium.chrome.browser.compositor.layouts.LayoutRenderHost; |
| 16 import org.chromium.chrome.browser.compositor.layouts.LayoutUpdateHost; |
| 17 import org.chromium.chrome.browser.compositor.layouts.components.CompositorButto
n; |
| 18 import org.chromium.chrome.browser.compositor.layouts.components.VirtualView; |
| 19 import org.chromium.chrome.browser.compositor.layouts.content.TabContentManager; |
| 20 import org.chromium.chrome.browser.compositor.layouts.eventfilter.AreaGestureEve
ntFilter; |
| 21 import org.chromium.chrome.browser.compositor.layouts.eventfilter.EventFilter; |
| 22 import org.chromium.chrome.browser.compositor.overlays.SceneOverlay; |
| 23 import org.chromium.chrome.browser.compositor.scene_layer.SceneOverlayLayer; |
| 24 import org.chromium.chrome.browser.compositor.scene_layer.TabStripSceneLayer; |
| 25 import org.chromium.chrome.browser.fullscreen.ChromeFullscreenManager; |
| 26 import org.chromium.chrome.browser.tabmodel.TabCreatorManager; |
| 27 import org.chromium.chrome.browser.tabmodel.TabModelSelector; |
| 28 import org.chromium.ui.resources.ResourceManager; |
| 29 |
| 30 import java.util.List; |
| 31 |
| 32 /** |
| 33 * This class handles managing which {@link StripLayoutHelper} is currently acti
ve and dispatches |
| 34 * all input and model events to the proper destination. |
| 35 */ |
| 36 public class StripLayoutHelperManager implements SceneOverlay { |
| 37 // Visibility Constants |
| 38 private static final float TAB_STRIP_HEIGHT_DP = 40.f; |
| 39 |
| 40 // Caching Variables |
| 41 private final RectF mStripFilterArea = new RectF(); |
| 42 |
| 43 // 1px border colors |
| 44 private static final float BORDER_OPACITY = 0.2f; |
| 45 private static final float BORDER_OPACITY_INCOGNITO = 0.4f; |
| 46 |
| 47 // Model selector buttons constants. |
| 48 private static final float MODEL_SELECTOR_BUTTON_Y_OFFSET_DP = 10.f; |
| 49 private static final float MODEL_SELECTOR_BUTTON_RIGHT_PADDING_DP = 6.f; |
| 50 private static final float MODEL_SELECTOR_BUTTON_LEFT_PADDING_DP = 3.f; |
| 51 private static final float MODEL_SELECTOR_BUTTON_WIDTH_DP = 24.f; |
| 52 private static final float MODEL_SELECTOR_BUTTON_HEIGHT_DP = 24.f; |
| 53 |
| 54 // External influences |
| 55 private TabModelSelector mTabModelSelector; |
| 56 private final LayoutUpdateHost mUpdateHost; |
| 57 private final LayoutRenderHost mRenderHost; |
| 58 |
| 59 // Event Filters |
| 60 private final AreaGestureEventFilter mEventFilter; |
| 61 |
| 62 // Internal state |
| 63 private boolean mIsIncognito; |
| 64 private final StripLayoutHelper mNormalHelper; |
| 65 private final StripLayoutHelper mIncognitoHelper; |
| 66 |
| 67 // UI State |
| 68 private float mWidth; |
| 69 private final float mHeight; |
| 70 private final CompositorButton mModelSelectorButton; |
| 71 |
| 72 private TabStripSceneLayer mTabStripTreeProvider; |
| 73 |
| 74 /** |
| 75 * Creates an instance of the {@link StripLayoutHelperManager}. |
| 76 * @param context The current Android {@link Context}. |
| 77 * @param updateHost The parent {@link LayoutUpdateHost}. |
| 78 * @param renderHost The {@link LayoutRenderHost}. |
| 79 */ |
| 80 public StripLayoutHelperManager(Context context, LayoutUpdateHost updateHost
, |
| 81 LayoutRenderHost renderHost, AreaGestureEventFilter eventFilter) { |
| 82 mHeight = TAB_STRIP_HEIGHT_DP; |
| 83 |
| 84 mUpdateHost = updateHost; |
| 85 mRenderHost = renderHost; |
| 86 mTabStripTreeProvider = new TabStripSceneLayer(context); |
| 87 |
| 88 mEventFilter = eventFilter; |
| 89 |
| 90 mNormalHelper = new StripLayoutHelper(context, updateHost, renderHost, f
alse); |
| 91 mIncognitoHelper = new StripLayoutHelper(context, updateHost, renderHost
, true); |
| 92 |
| 93 mModelSelectorButton = new CompositorButton( |
| 94 context, MODEL_SELECTOR_BUTTON_WIDTH_DP, MODEL_SELECTOR_BUTTON_H
EIGHT_DP); |
| 95 mModelSelectorButton.setIncognito(false); |
| 96 mModelSelectorButton.setVisible(false); |
| 97 // Pressed resources are the same as the unpressed resources. |
| 98 mModelSelectorButton.setResources(R.drawable.btn_tabstrip_switch_normal, |
| 99 R.drawable.btn_tabstrip_switch_normal, R.drawable.btn_tabstrip_s
witch_incognito, |
| 100 R.drawable.btn_tabstrip_switch_incognito); |
| 101 mModelSelectorButton.setY(MODEL_SELECTOR_BUTTON_Y_OFFSET_DP); |
| 102 |
| 103 Resources res = context.getResources(); |
| 104 mModelSelectorButton.setAccessibilityDescription( |
| 105 res.getString(R.string.accessibility_tabstrip_btn_incognito_togg
le_standard), |
| 106 res.getString(R.string.accessibility_tabstrip_btn_incognito_togg
le_incognito)); |
| 107 |
| 108 onContextChanged(context); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Cleans up internal state. |
| 113 */ |
| 114 public void destroy() { |
| 115 mTabStripTreeProvider.destroy(); |
| 116 mTabStripTreeProvider = null; |
| 117 } |
| 118 |
| 119 @Override |
| 120 public SceneOverlayLayer getUpdatedSceneOverlayTree(LayerTitleCache layerTit
leCache, |
| 121 ResourceManager resourceManager, ChromeFullscreenManager fullscreenM
anager) { |
| 122 assert mTabStripTreeProvider != null; |
| 123 |
| 124 mTabStripTreeProvider.pushAndUpdateStrip(this, layerTitleCache, resource
Manager, |
| 125 fullscreenManager, getActiveStripLayoutHelper().getStripLayoutTa
bsToRender()); |
| 126 return mTabStripTreeProvider; |
| 127 } |
| 128 |
| 129 @Override |
| 130 public EventFilter getEventFilter() { |
| 131 return mEventFilter; |
| 132 } |
| 133 |
| 134 @Override |
| 135 public void onSizeChanged(float width, float height, float visibleViewportOf
fsetY) { |
| 136 mWidth = width; |
| 137 mModelSelectorButton.setX( |
| 138 mWidth - MODEL_SELECTOR_BUTTON_WIDTH_DP - MODEL_SELECTOR_BUTTON_
RIGHT_PADDING_DP); |
| 139 mNormalHelper.onSizeChanged(mWidth, mHeight); |
| 140 mIncognitoHelper.onSizeChanged(mWidth, mHeight); |
| 141 |
| 142 mStripFilterArea.set(0, 0, mWidth, Math.min(getHeight(), visibleViewport
OffsetY)); |
| 143 mEventFilter.setEventArea(mStripFilterArea); |
| 144 } |
| 145 |
| 146 public CompositorButton getNewTabButton() { |
| 147 return getActiveStripLayoutHelper().getNewTabButton(); |
| 148 } |
| 149 |
| 150 public CompositorButton getModelSelectorButton() { |
| 151 return mModelSelectorButton; |
| 152 } |
| 153 |
| 154 @Override |
| 155 public void getVirtualViews(List<VirtualView> views) { |
| 156 if (mModelSelectorButton.isVisible()) views.add(mModelSelectorButton); |
| 157 getActiveStripLayoutHelper().getVirtualViews(views); |
| 158 } |
| 159 |
| 160 public float getStripBrightness() { |
| 161 return getActiveStripLayoutHelper().getStripBrightness(); |
| 162 } |
| 163 |
| 164 /** |
| 165 * Sets the {@link TabModelSelector} that this {@link StripLayoutHelperManag
er} will visually |
| 166 * represent, and various objects associated with it. |
| 167 * @param modelSelector The {@link TabModelSelector} to visually represent. |
| 168 * @param tabCreatorManager The {@link TabCreatorManager}, used to create ne
w tabs. |
| 169 * @param tabContentManager The {@link TabContentManager}, used to provide d
isplay content for |
| 170 * tabs. |
| 171 */ |
| 172 public void setTabModelSelector(TabModelSelector modelSelector, |
| 173 TabCreatorManager tabCreatorManager, TabContentManager tabContentMan
ager) { |
| 174 if (mTabModelSelector == modelSelector) return; |
| 175 mTabModelSelector = modelSelector; |
| 176 mNormalHelper.setTabModel(mTabModelSelector.getModel(false), tabContentM
anager, |
| 177 tabCreatorManager.getTabCreator(false)); |
| 178 mIncognitoHelper.setTabModel(mTabModelSelector.getModel(true), tabConten
tManager, |
| 179 tabCreatorManager.getTabCreator(true)); |
| 180 tabModelSwitched(mTabModelSelector.isIncognitoSelected()); |
| 181 } |
| 182 |
| 183 @Override |
| 184 public void tabTitleChanged(int tabId, String title) { |
| 185 getActiveStripLayoutHelper().tabTitleChanged(tabId, title); |
| 186 } |
| 187 |
| 188 public float getHeight() { |
| 189 return mHeight; |
| 190 } |
| 191 |
| 192 public float getWidth() { |
| 193 return mWidth; |
| 194 } |
| 195 |
| 196 public float getBorderOpacity() { |
| 197 return mIsIncognito ? BORDER_OPACITY_INCOGNITO : BORDER_OPACITY; |
| 198 } |
| 199 |
| 200 /** |
| 201 * Updates all internal resources and dimensions. |
| 202 * @param context The current Android {@link Context}. |
| 203 */ |
| 204 public void onContextChanged(Context context) { |
| 205 mNormalHelper.onContextChanged(context); |
| 206 mIncognitoHelper.onContextChanged(context); |
| 207 } |
| 208 |
| 209 @Override |
| 210 public boolean updateOverlay(long time, long dt) { |
| 211 getInactiveStripLayoutHelper().finishAnimation(); |
| 212 return getActiveStripLayoutHelper().updateLayout(time, dt); |
| 213 } |
| 214 |
| 215 @Override |
| 216 public void tabModelSwitched(boolean incognito) { |
| 217 if (incognito == mIsIncognito) return; |
| 218 mIsIncognito = incognito; |
| 219 |
| 220 updateModelSwitcherButton(); |
| 221 |
| 222 mUpdateHost.requestUpdate(); |
| 223 } |
| 224 |
| 225 private void updateModelSwitcherButton() { |
| 226 mModelSelectorButton.setIncognito(mIsIncognito); |
| 227 if (mTabModelSelector != null) { |
| 228 boolean isVisible = mTabModelSelector.getModel(true).getCount() != 0
; |
| 229 mModelSelectorButton.setVisible(isVisible); |
| 230 |
| 231 float rightMargin = isVisible |
| 232 ? MODEL_SELECTOR_BUTTON_WIDTH_DP + MODEL_SELECTOR_BUTTON_RIG
HT_PADDING_DP |
| 233 + MODEL_SELECTOR_BUTTON_LEFT_PADDING_DP |
| 234 : 0.0f; |
| 235 mNormalHelper.setRightMargin(rightMargin); |
| 236 mIncognitoHelper.setRightMargin(rightMargin); |
| 237 } |
| 238 } |
| 239 |
| 240 @Override |
| 241 public void tabSelected(long time, boolean incognito, int id, int prevId) { |
| 242 getStripLayoutHelper(incognito).tabSelected(time, id, prevId); |
| 243 } |
| 244 |
| 245 @Override |
| 246 public void tabMoved(long time, boolean incognito, int id, int oldIndex, int
newIndex) { |
| 247 getStripLayoutHelper(incognito).tabMoved(time, id, oldIndex, newIndex); |
| 248 } |
| 249 |
| 250 @Override |
| 251 public void tabClosed(long time, boolean incognito, int id) { |
| 252 getStripLayoutHelper(incognito).tabClosed(time, id); |
| 253 updateModelSwitcherButton(); |
| 254 } |
| 255 |
| 256 @Override |
| 257 public void tabClosureCancelled(long time, boolean incognito, int id) { |
| 258 getStripLayoutHelper(incognito).tabClosureCancelled(time, id); |
| 259 updateModelSwitcherButton(); |
| 260 } |
| 261 |
| 262 @Override |
| 263 public void tabCreated(long time, boolean incognito, int id, int prevId, boo
lean selected) { |
| 264 getStripLayoutHelper(incognito).tabCreated(time, id, prevId, selected); |
| 265 } |
| 266 |
| 267 @Override |
| 268 public void tabPageLoadStarted(int id, boolean incognito) { |
| 269 getStripLayoutHelper(incognito).tabPageLoadStarted(id); |
| 270 } |
| 271 |
| 272 @Override |
| 273 public void tabPageLoadFinished(int id, boolean incognito) { |
| 274 getStripLayoutHelper(incognito).tabPageLoadFinished(id); |
| 275 } |
| 276 |
| 277 @Override |
| 278 public void tabLoadStarted(int id, boolean incognito) { |
| 279 getStripLayoutHelper(incognito).tabLoadStarted(id); |
| 280 } |
| 281 |
| 282 @Override |
| 283 public void tabLoadFinished(int id, boolean incognito) { |
| 284 getStripLayoutHelper(incognito).tabLoadFinished(id); |
| 285 } |
| 286 |
| 287 /** |
| 288 * Called on touch drag event. |
| 289 * @param time The current time of the app in ms. |
| 290 * @param x The y coordinate of the end of the drag event. |
| 291 * @param y The y coordinate of the end of the drag event. |
| 292 * @param deltaX The number of pixels dragged in the x direction. |
| 293 * @param deltaY The number of pixels dragged in the y direction. |
| 294 * @param totalX The total delta x since the drag started. |
| 295 * @param totalY The total delta y since the drag started. |
| 296 */ |
| 297 public void drag( |
| 298 long time, float x, float y, float deltaX, float deltaY, float total
X, float totalY) { |
| 299 mModelSelectorButton.drag(x, y); |
| 300 getActiveStripLayoutHelper().drag(time, x, y, deltaX, deltaY, totalX, to
talY); |
| 301 } |
| 302 |
| 303 /** |
| 304 * Called on touch fling event. This is called before the onUpOrCancel event
. |
| 305 * @param time The current time of the app in ms. |
| 306 * @param x The y coordinate of the start of the fling event. |
| 307 * @param y The y coordinate of the start of the fling event. |
| 308 * @param velocityX The amount of velocity in the x direction. |
| 309 * @param velocityY The amount of velocity in the y direction. |
| 310 */ |
| 311 public void fling(long time, float x, float y, float velocityX, float veloci
tyY) { |
| 312 getActiveStripLayoutHelper().fling(time, x, y, velocityX, velocityY); |
| 313 } |
| 314 |
| 315 /** |
| 316 * Called on onDown event. |
| 317 * @param time The time stamp in millisecond of the event. |
| 318 * @param x The x position of the event. |
| 319 * @param y The y position of the event. |
| 320 */ |
| 321 public void onDown(long time, float x, float y) { |
| 322 if (mModelSelectorButton.onDown(x, y)) return; |
| 323 getActiveStripLayoutHelper().onDown(time, x, y); |
| 324 } |
| 325 |
| 326 /** |
| 327 * Called on long press touch event. |
| 328 * @param time The current time of the app in ms. |
| 329 * @param x The x coordinate of the position of the press event. |
| 330 * @param y The y coordinate of the position of the press event. |
| 331 */ |
| 332 public void onLongPress(long time, float x, float y) { |
| 333 getActiveStripLayoutHelper().onLongPress(time, x, y); |
| 334 } |
| 335 |
| 336 /** |
| 337 * Called on click. This is called before the onUpOrCancel event. |
| 338 * @param time The current time of the app in ms. |
| 339 * @param x The x coordinate of the position of the click. |
| 340 * @param y The y coordinate of the position of the click. |
| 341 */ |
| 342 public void click(long time, float x, float y) { |
| 343 if (mModelSelectorButton.click(x, y) && mTabModelSelector != null) { |
| 344 mTabModelSelector.selectModel(!mTabModelSelector.isIncognitoSelected
()); |
| 345 return; |
| 346 } |
| 347 getActiveStripLayoutHelper().click(time, x, y); |
| 348 } |
| 349 |
| 350 /** |
| 351 * Called on up or cancel touch events. This is called after the click and f
ling event if any. |
| 352 * @param time The current time of the app in ms. |
| 353 */ |
| 354 public void onUpOrCancel(long time) { |
| 355 if (mModelSelectorButton.onUpOrCancel() && mTabModelSelector != null) { |
| 356 mTabModelSelector.selectModel(!mTabModelSelector.isIncognitoSelected
()); |
| 357 return; |
| 358 } |
| 359 getActiveStripLayoutHelper().onUpOrCancel(time); |
| 360 } |
| 361 |
| 362 /** |
| 363 * @param incognito Whether or not you want the incognito StripLayoutHelper |
| 364 * @return The requested StripLayoutHelper. |
| 365 */ |
| 366 @VisibleForTesting |
| 367 public StripLayoutHelper getStripLayoutHelper(boolean incognito) { |
| 368 return incognito ? mIncognitoHelper : mNormalHelper; |
| 369 } |
| 370 |
| 371 /** |
| 372 * @return The currently visible strip layout helper. |
| 373 */ |
| 374 @VisibleForTesting |
| 375 public StripLayoutHelper getActiveStripLayoutHelper() { |
| 376 return getStripLayoutHelper(mIsIncognito); |
| 377 } |
| 378 |
| 379 private StripLayoutHelper getInactiveStripLayoutHelper() { |
| 380 return mIsIncognito ? mNormalHelper : mIncognitoHelper; |
| 381 } |
| 382 } |
OLD | NEW |