Skip to content

Commit

Permalink
Extract some tab switcher button logic from ToolbarPhone
Browse files Browse the repository at this point in the history
Move some of the tab switcher button logic that is shared between
browsing and tab switcher modes from ToolbarPhone into a new
ToggleTabStackButton. This new class will eventually be replaced by
TabSwitcherButtonCoordinator, but more refactoring is needed first.

BUG=897294

Change-Id: Iaa123432148ccea7181f9f222df30056a308b12f
Reviewed-on: https://chromium-review.googlesource.com/c/1358914
Reviewed-by: Matthew Jones <mdjones@chromium.org>
Commit-Queue: Theresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613648}
  • Loading branch information
Theresa authored and Commit Bot committed Dec 4, 2018
1 parent 76da8c7 commit 93b7c08
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 80 deletions.
3 changes: 2 additions & 1 deletion chrome/android/java/res/layout/toolbar_phone.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
android:paddingStart="8dp"
android:visibility="gone" />

<ImageButton android:id="@+id/tab_switcher_button"
<org.chromium.chrome.browser.toolbar.top.ToggleTabStackButton
android:id="@+id/tab_switcher_button"
style="@style/ToolbarButton"
android:paddingStart="8dp"
android:layout_gravity="top"
Expand Down
2 changes: 1 addition & 1 deletion chrome/android/java/res/layout/toolbar_tablet.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
android:paddingEnd="@dimen/location_bar_lateral_padding"
android:paddingStart="2dp" />

<ImageButton
<org.chromium.chrome.browser.toolbar.top.ToggleTabStackButton
android:id="@+id/tab_switcher_button"
style="@style/ToolbarButton"
android:contentDescription="@string/accessibility_toolbar_btn_tabswitcher_toggle_default"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,28 @@ public interface TabCountObserver {
}

/**
* @param observer The observer that will have events broadcast to.
* @param observer The observer to add.
*/
public void addObserver(TabCountObserver observer) {
mTabCountObservers.addObserver(observer);
}

/**
* @param observer The observer that will be removed.
* Adds an observer and triggers the {@link TabCountObserver#onTabCountChanged(int, boolean)}
* for the added observer.
* @param observer The observer to add.
*/
public void addObserverAndTrigger(TabCountObserver observer) {
addObserver(observer);

if (mTabModelSelector != null) {
observer.onTabCountChanged(mTabModelSelector.getCurrentModel().getCount(),
mTabModelSelector.isIncognitoSelected());
}
}

/**
* @param observer The observer to remove.
*/
public void removeObserver(TabCountObserver observer) {
mTabCountObservers.removeObserver(observer);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.chrome.browser.toolbar.top;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageButton;

import org.chromium.chrome.R;
import org.chromium.chrome.browser.toolbar.TabCountProvider;
import org.chromium.chrome.browser.toolbar.TabSwitcherDrawable;
import org.chromium.chrome.browser.util.AccessibilityUtil;

/**
* A button displaying the number of open tabs. Clicking the button toggles the tab switcher view.
* TODO(twellington): Replace with TabSwitcherButtonCoordinator so code can be shared with bottom
* toolbar.
*/
public class ToggleTabStackButton extends ImageButton implements TabCountProvider.TabCountObserver,
View.OnClickListener,
View.OnLongClickListener {
private TabSwitcherDrawable mTabSwitcherButtonDrawable;
private TabSwitcherDrawable mTabSwitcherButtonDrawableLight;
private TabCountProvider mTabCountProvider;
private OnClickListener mTabSwitcherListener;

public ToggleTabStackButton(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

@Override
public void onFinishInflate() {
super.onFinishInflate();

mTabSwitcherButtonDrawable =
TabSwitcherDrawable.createTabSwitcherDrawable(getContext(), false);
mTabSwitcherButtonDrawableLight =
TabSwitcherDrawable.createTabSwitcherDrawable(getContext(), true);
setImageDrawable(mTabSwitcherButtonDrawable);
setOnClickListener(this);
setOnLongClickListener(this);
}

/**
* Called to destroy the tab stack button.
*/
void destroy() {
if (mTabCountProvider != null) mTabCountProvider.removeObserver(this);
}

/**
* Sets the OnClickListener that will be notified when the TabSwitcher button is pressed.
* @param listener The callback that will be notified when the TabSwitcher button is pressed.
*/
void setOnTabSwitcherClickHandler(OnClickListener listener) {
mTabSwitcherListener = listener;
}

/**
* Updates the contained drawable.
* @param useLightDrawables Whether light drawables should be used.
*/
void setUseLightDrawables(boolean useLightDrawables) {
setImageDrawable(
useLightDrawables ? mTabSwitcherButtonDrawableLight : mTabSwitcherButtonDrawable);
}

/**
* @param provider The {@link TabCountProvider} used to observe the number of tabs in the
* current model.
*/
void setTabCountProvider(TabCountProvider provider) {
mTabCountProvider = provider;
mTabCountProvider.addObserverAndTrigger(this);
}

@Override
public void onTabCountChanged(int numberOfTabs, boolean isIncognito) {
setEnabled(numberOfTabs >= 1);
setContentDescription(getResources().getQuantityString(
R.plurals.accessibility_toolbar_btn_tabswitcher_toggle, numberOfTabs,
numberOfTabs));
mTabSwitcherButtonDrawableLight.updateForTabCount(numberOfTabs, isIncognito);
mTabSwitcherButtonDrawable.updateForTabCount(numberOfTabs, isIncognito);
}

@Override
public void onClick(View v) {
if (mTabSwitcherListener != null && isClickable()) {
mTabSwitcherListener.onClick(this);
}
}

@Override
public boolean onLongClick(View v) {
CharSequence description = getResources().getString(org.chromium.chrome.R.string.open_tabs);
return AccessibilityUtil.showAccessibilityToast(getContext(), v, description);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public class ToolbarPhone

protected ViewGroup mToolbarButtonsContainer;
private IncognitoToggleTabLayout mIncognitoToggleTabLayout;
protected ImageView mToggleTabStackButton;
protected ToggleTabStackButton mToggleTabStackButton;
protected NewTabButton mNewTabButton;
protected @Nullable ImageButton mHomeButton;
private TextView mUrlBar;
Expand Down Expand Up @@ -192,7 +192,6 @@ public class ToolbarPhone
@ViewDebug.ExportedProperty(category = "chrome")
private Rect mClipRect;

private OnClickListener mTabSwitcherListener;
private OnClickListener mNewTabListener;

@ViewDebug.ExportedProperty(category = "chrome")
Expand Down Expand Up @@ -231,8 +230,6 @@ public class ToolbarPhone
private Drawable mActiveLocationBarBackground;

protected boolean mForceDrawLocationBarBackground;
private TabSwitcherDrawable mTabSwitcherButtonDrawable;
protected TabSwitcherDrawable mTabSwitcherButtonDrawableLight;

private final int mLightModeDefaultColor;
private final int mDarkModeDefaultColor;
Expand Down Expand Up @@ -455,27 +452,20 @@ private int getLocationBarColorForToolbarColor(int toolbarColor) {
}

private void inflateTabSwitchingResources() {
mToggleTabStackButton = (ImageView) findViewById(R.id.tab_switcher_button);
mNewTabButton = (NewTabButton) findViewById(R.id.new_tab_button);
mNewTabButton = findViewById(R.id.new_tab_button);
mToggleTabStackButton = findViewById(R.id.tab_switcher_button);
if (FeatureUtilities.isBottomToolbarEnabled()) {
UiUtils.removeViewFromParent(mToggleTabStackButton);
UiUtils.removeViewFromParent(mNewTabButton);
mToggleTabStackButton = null;
mNewTabButton = null;
} else {
mToggleTabStackButton.setClickable(false);
mTabSwitcherButtonDrawable =
TabSwitcherDrawable.createTabSwitcherDrawable(getContext(), false);
mTabSwitcherButtonDrawableLight =
TabSwitcherDrawable.createTabSwitcherDrawable(getContext(), true);
mToggleTabStackButton.setImageDrawable(mTabSwitcherButtonDrawable);
mTabSwitcherModeViews.add(mNewTabButton);
}
}

private void enableTabSwitchingResources() {
mToggleTabStackButton.setOnClickListener(this);
mToggleTabStackButton.setOnLongClickListener(this);
mToggleTabStackButton.setOnKeyListener(new KeyboardNavigationListener() {
@Override
public View getNextFocusForward() {
Expand Down Expand Up @@ -569,9 +559,7 @@ public void onClick(View v) {
// Don't allow clicks while the omnibox is being focused.
if (mLocationBar != null && mLocationBar.hasFocus()) return;

if (mToggleTabStackButton == v) {
handleToggleTabStack();
} else if (mNewTabButton == v) {
if (mNewTabButton == v) {
v.setEnabled(false);

if (mNewTabListener != null) {
Expand All @@ -587,31 +575,18 @@ public void onClick(View v) {
}
}

private void handleToggleTabStack() {
// The button is clickable before the native library is loaded
// and the listener is setup.
if (mToggleTabStackButton != null && mToggleTabStackButton.isClickable()
&& mTabSwitcherListener != null) {
cancelAppMenuUpdateBadgeAnimation();
mTabSwitcherListener.onClick(mToggleTabStackButton);
}
}

@Override
public boolean onLongClick(View v) {
CharSequence description = null;
if (v == mToggleTabStackButton) {
description = getResources().getString(R.string.open_tabs);
} else if (v == mNewTabButton) {
description = getResources().getString(isIncognito()
if (v == mNewTabButton) {
CharSequence description = getResources().getString(isIncognito()
? (ChromeFeatureList.isEnabled(ChromeFeatureList.INCOGNITO_STRINGS)
? R.string.button_new_private_tab
: R.string.button_new_incognito_tab)
? R.string.button_new_private_tab
: R.string.button_new_incognito_tab)
: R.string.button_new_tab);
return AccessibilityUtil.showAccessibilityToast(getContext(), v, description);
} else {
return false;
}
return AccessibilityUtil.showAccessibilityToast(getContext(), v, description);
}

@Override
Expand Down Expand Up @@ -1844,6 +1819,8 @@ public void setTabSwitcherMode(
*/
public void setTabSwitcherMode(boolean inTabSwitcherMode, boolean showToolbar,
boolean delayAnimation, boolean animate) {
if (inTabSwitcherMode) cancelAppMenuUpdateBadgeAnimation();

// If setting tab switcher mode to true and the browser is already animating or in the tab
// switcher skip.
if (inTabSwitcherMode
Expand Down Expand Up @@ -1946,7 +1923,7 @@ public void onTabSwitcherTransitionFinished() {

@Override
public void setOnTabSwitcherClickHandler(OnClickListener listener) {
mTabSwitcherListener = listener;
mToggleTabStackButton.setOnTabSwitcherClickHandler(listener);
}

@Override
Expand Down Expand Up @@ -2148,6 +2125,7 @@ private void populateUrlClearFocusingAnimatorSet(List<Animator> animators) {
public void onUrlFocusChange(final boolean hasFocus) {
super.onUrlFocusChange(hasFocus);

mToggleTabStackButton.setClickable(!hasFocus);
triggerUrlFocusAnimation(hasFocus);
}

Expand Down Expand Up @@ -2204,6 +2182,7 @@ public void onEnd(Animator animation) {
public void setTabCountProvider(TabCountProvider tabCountProvider) {
mTabCountProvider = tabCountProvider;
mTabCountProvider.addObserver(this);
mToggleTabStackButton.setTabCountProvider(tabCountProvider);
}

@Override
Expand All @@ -2212,13 +2191,6 @@ public void onTabCountChanged(int numberOfTabs, boolean isIncognito) {

if (mToggleTabStackButton == null) return;

mToggleTabStackButton.setEnabled(numberOfTabs >= 1);
mToggleTabStackButton.setContentDescription(getResources().getQuantityString(
R.plurals.accessibility_toolbar_btn_tabswitcher_toggle, numberOfTabs,
numberOfTabs));
mTabSwitcherButtonDrawableLight.updateForTabCount(numberOfTabs, isIncognito);
mTabSwitcherButtonDrawable.updateForTabCount(numberOfTabs, isIncognito);

boolean useTabStackDrawableLight =
isIncognito || ColorUtils.shouldUseLightForegroundOnBackground(getTabThemeColor());
if (mTabSwitcherAnimationTabStackDrawable == null
Expand Down Expand Up @@ -2536,9 +2508,7 @@ && getToolbarDataProvider().getTab().isNativePage()) {
}

if (mToggleTabStackButton != null) {
mToggleTabStackButton.setImageDrawable(mUseLightToolbarDrawables
? mTabSwitcherButtonDrawableLight
: mTabSwitcherButtonDrawable);
mToggleTabStackButton.setUseLightDrawables(mUseLightToolbarDrawables);
if (mTabSwitcherAnimationTabStackDrawable != null) {
mTabSwitcherAnimationTabStackDrawable.setTint(
mUseLightToolbarDrawables ? mLightModeTint : mDarkModeTint);
Expand Down
Loading

0 comments on commit 93b7c08

Please sign in to comment.