Commit d31e2539 authored by Pedro Amaral's avatar Pedro Amaral Committed by Commit Bot

Generic Button Component

A generic button component to be used in the bottom toolbar.
Also migrated the home button to use the generic button component to
illustrate how it would be used.

Change-Id: Ic02a7c7412ecf3e7ae580396c09ebba39ef96a30
Reviewed-on: https://chromium-review.googlesource.com/1115812Reviewed-by: default avatarTed Choc <tedchoc@chromium.org>
Reviewed-by: default avatarMatthew Jones <mdjones@chromium.org>
Commit-Queue: Pedro Amaral <amaralp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#570940}
parent bea9ba77
......@@ -34,6 +34,9 @@ public class BottomToolbarCoordinator {
/** The tab switcher button component that lives in the bottom toolbar. */
private final TabSwitcherButtonCoordinator mTabSwitcherButtonCoordinator;
/** The home button component that lives in the bottom toolbar. */
private final ToolbarButtonCoordinator mHomeButtonCoordinator;
/**
* Build the coordinator that manages the bottom toolbar.
* @param fullscreenManager A {@link ChromeFullscreenManager} to update the bottom controls
......@@ -58,6 +61,8 @@ public class BottomToolbarCoordinator {
model, new ViewHolder(toolbarRoot), new BottomToolbarViewBinder());
model.addObserver(processor);
mTabSwitcherButtonCoordinator = new TabSwitcherButtonCoordinator(toolbarRoot);
mHomeButtonCoordinator =
new ToolbarButtonCoordinator(toolbarRoot.findViewById(R.id.home_button));
}
/**
......@@ -86,8 +91,7 @@ public class BottomToolbarCoordinator {
OnClickListener homeButtonListener, OnTouchListener menuButtonListener,
TabModelSelector tabModelSelector, OverviewModeBehavior overviewModeBehavior,
ContextualSearchManager contextualSearchManager) {
mMediator.setButtonListeners(
searchAcceleratorListener, homeButtonListener, menuButtonListener);
mMediator.setButtonListeners(searchAcceleratorListener, menuButtonListener);
mMediator.setLayoutManager(layoutManager);
mMediator.setResourceManager(resourceManager);
mMediator.setOverviewModeBehavior(overviewModeBehavior);
......@@ -96,6 +100,10 @@ public class BottomToolbarCoordinator {
mTabSwitcherButtonCoordinator.setTabSwitcherListener(tabSwitcherListener);
mTabSwitcherButtonCoordinator.setTabModelSelector(tabModelSelector);
mHomeButtonCoordinator.setButtonListeners(homeButtonListener, null);
mHomeButtonCoordinator.setOverviewModeBehavior(
overviewModeBehavior, ToolbarButtonCoordinator.BROWSING_MODE);
}
/**
......
......@@ -124,10 +124,9 @@ class BottomToolbarMediator
mModel.setValue(BottomToolbarModel.ANDROID_VIEW_VISIBLE, true);
}
public void setButtonListeners(OnClickListener searchAcceleratorListener,
OnClickListener homeButtonListener, OnTouchListener menuButtonListener) {
public void setButtonListeners(
OnClickListener searchAcceleratorListener, OnTouchListener menuButtonListener) {
mModel.setValue(BottomToolbarModel.SEARCH_ACCELERATOR_LISTENER, searchAcceleratorListener);
mModel.setValue(BottomToolbarModel.HOME_BUTTON_LISTENER, homeButtonListener);
mModel.setValue(BottomToolbarModel.MENU_BUTTON_LISTENER, menuButtonListener);
}
......
......@@ -26,10 +26,6 @@ public class BottomToolbarModel extends PropertyModel {
public static final ObjectPropertyKey<OnClickListener> SEARCH_ACCELERATOR_LISTENER =
new ObjectPropertyKey<>();
/** The touch listener for the home button. */
public static final ObjectPropertyKey<OnClickListener> HOME_BUTTON_LISTENER =
new ObjectPropertyKey<>();
/** The touch listener for the menu button. */
public static final ObjectPropertyKey<OnTouchListener> MENU_BUTTON_LISTENER =
new ObjectPropertyKey<>();
......@@ -53,8 +49,8 @@ public class BottomToolbarModel extends PropertyModel {
/** Default constructor. */
public BottomToolbarModel() {
super(Y_OFFSET, ANDROID_VIEW_VISIBLE, SEARCH_ACCELERATOR_LISTENER, HOME_BUTTON_LISTENER,
MENU_BUTTON_LISTENER, LAYOUT_MANAGER, RESOURCE_MANAGER, SEARCH_ACCELERATOR_VISIBLE,
UPDATE_BADGE_VISIBLE, TOOLBAR_SWIPE_HANDLER);
super(Y_OFFSET, ANDROID_VIEW_VISIBLE, SEARCH_ACCELERATOR_LISTENER, MENU_BUTTON_LISTENER,
LAYOUT_MANAGER, RESOURCE_MANAGER, SEARCH_ACCELERATOR_VISIBLE, UPDATE_BADGE_VISIBLE,
TOOLBAR_SWIPE_HANDLER);
}
}
......@@ -72,9 +72,6 @@ public class BottomToolbarViewBinder
.getDynamicResourceLoader()
.registerResource(
view.toolbarRoot.getId(), view.toolbarRoot.getResourceAdapter());
} else if (BottomToolbarModel.HOME_BUTTON_LISTENER == propertyKey) {
view.toolbarRoot.findViewById(R.id.home_button)
.setOnClickListener(model.getValue(BottomToolbarModel.HOME_BUTTON_LISTENER));
} else if (BottomToolbarModel.SEARCH_ACCELERATOR_VISIBLE == propertyKey) {
view.toolbarRoot.findViewById(R.id.search_button)
.setVisibility(model.getValue(BottomToolbarModel.SEARCH_ACCELERATOR_VISIBLE)
......
// 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;
import android.support.annotation.IntDef;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior;
import org.chromium.chrome.browser.modelutil.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel;
import org.chromium.chrome.browser.modelutil.PropertyModelChangeProcessor;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* The controller for a generic bottom toolbar button. This class handles all interactions that the
* button has with the outside world.
*/
public class ToolbarButtonCoordinator {
/** The mediator that handles events from outside the button. */
private final ToolbarButtonMediator mMediator;
@IntDef({BROWSING_MODE, TAB_SWITCHER_MODE})
@Retention(RetentionPolicy.SOURCE)
public @interface ButtonVisibility {}
/* The button should be visible in browsing mode and not in tab switcher mode */
public static final int BROWSING_MODE = 0;
/* The button should be visible in tab switcher mode and not in browsing mode */
public static final int TAB_SWITCHER_MODE = 1;
/**
* Build the controller that manages the button.
* @param view The button's view.
*/
public ToolbarButtonCoordinator(View view) {
PropertyModel model = new PropertyModel(ToolbarButtonProperties.ALL_KEYS);
mMediator = new ToolbarButtonMediator(model);
PropertyModelChangeProcessor<PropertyModel, View, PropertyKey> processor =
new PropertyModelChangeProcessor<>(model, view, new ToolbarButtonViewBinder());
model.addObserver(processor);
}
/**
* @param onClickListener An {@link OnClickListener} that is triggered when the button is
* clicked.
*/
public void setButtonListeners(
OnClickListener onClickListener, OnLongClickListener onLongClickListener) {
mMediator.setButtonListeners(onClickListener, onLongClickListener);
}
/**
* @param overviewModeBehavior The overview mode manager.
* @param buttonVisibility Whether the button should be shown for browsing mode vs tab switcher
* mode.
*/
public void setOverviewModeBehavior(
OverviewModeBehavior overviewModeBehavior, @ButtonVisibility int buttonVisibility) {
mMediator.setOverviewModeBehavior(overviewModeBehavior, buttonVisibility);
}
}
// 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;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior;
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior.OverviewModeObserver;
import org.chromium.chrome.browser.modelutil.PropertyModel;
/**
* This class is responsible for reacting to events from the outside world, interacting with other
* coordinators, running most of the business logic associated with the button, and updating
* the model accordingly.
*/
class ToolbarButtonMediator implements OverviewModeObserver {
/** The model for the button that holds all of its state. */
private PropertyModel mModel;
/** The overview mode manager. */
private OverviewModeBehavior mOverviewModeBehavior;
private @ToolbarButtonCoordinator.ButtonVisibility int mButtonVisibility;
/**
* Build a new mediator that handles events from outside the button.
* @param model The {@link PropertyModel} that holds all the state for the button.
*/
public ToolbarButtonMediator(PropertyModel model) {
mModel = model;
}
/**
* Clean up anything that needs to be when the button is destroyed.
*/
public void destroy() {
mOverviewModeBehavior.removeOverviewModeObserver(this);
}
@Override
public void onOverviewModeStartedShowing(boolean showToolbar) {
mModel.setValue(ToolbarButtonProperties.IS_VISIBLE,
mButtonVisibility == ToolbarButtonCoordinator.TAB_SWITCHER_MODE);
}
@Override
public void onOverviewModeFinishedShowing() {}
@Override
public void onOverviewModeStartedHiding(boolean showToolbar, boolean delayAnimation) {
mModel.setValue(ToolbarButtonProperties.IS_VISIBLE,
mButtonVisibility == ToolbarButtonCoordinator.BROWSING_MODE);
}
@Override
public void onOverviewModeFinishedHiding() {}
public void setButtonListeners(
OnClickListener onClickListener, OnLongClickListener onLongClickListener) {
mModel.setValue(ToolbarButtonProperties.ON_CLICK_LISTENER, onClickListener);
mModel.setValue(ToolbarButtonProperties.ON_LONG_CLICK_LISTENER, onLongClickListener);
}
public void setOverviewModeBehavior(OverviewModeBehavior overviewModeBehavior,
@ToolbarButtonCoordinator.ButtonVisibility int buttonVisibility) {
mOverviewModeBehavior = overviewModeBehavior;
mOverviewModeBehavior.addOverviewModeObserver(this);
mButtonVisibility = buttonVisibility;
}
}
// 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;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import org.chromium.chrome.browser.modelutil.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel.BooleanPropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel.ObjectPropertyKey;
/**
* The properties needed to render the button.
*/
public interface ToolbarButtonProperties {
/** The click listener for the button. */
public static final ObjectPropertyKey<OnClickListener> ON_CLICK_LISTENER =
new ObjectPropertyKey<>();
/** The long click listener for the button. */
public static final ObjectPropertyKey<OnLongClickListener> ON_LONG_CLICK_LISTENER =
new ObjectPropertyKey<>();
/** Whether the button is visible. */
public static final BooleanPropertyKey IS_VISIBLE = new BooleanPropertyKey();
public static final PropertyKey[] ALL_KEYS =
new PropertyKey[] {ON_CLICK_LISTENER, ON_LONG_CLICK_LISTENER, IS_VISIBLE};
}
// 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;
import android.view.View;
import org.chromium.chrome.browser.modelutil.PropertyKey;
import org.chromium.chrome.browser.modelutil.PropertyModel;
import org.chromium.chrome.browser.modelutil.PropertyModelChangeProcessor;
/**
* This class is responsible for pushing updates to the Android view of the button. These
* updates are pulled from the {@link PropertyModel} when a notification of an update is
* received.
*/
public class ToolbarButtonViewBinder
implements PropertyModelChangeProcessor.ViewBinder<PropertyModel, View, PropertyKey> {
/**
* Build a binder that handles interaction between the model and the views that make up the
* button.
*/
public ToolbarButtonViewBinder() {}
@Override
public final void bind(PropertyModel model, View view, PropertyKey propertyKey) {
if (ToolbarButtonProperties.ON_CLICK_LISTENER == propertyKey) {
view.setOnClickListener(model.getValue(ToolbarButtonProperties.ON_CLICK_LISTENER));
} else if (ToolbarButtonProperties.ON_LONG_CLICK_LISTENER == propertyKey) {
view.setOnLongClickListener(
model.getValue(ToolbarButtonProperties.ON_LONG_CLICK_LISTENER));
} else if (ToolbarButtonProperties.IS_VISIBLE == propertyKey) {
view.setVisibility(model.getValue(ToolbarButtonProperties.IS_VISIBLE) ? View.VISIBLE
: View.INVISIBLE);
} else {
assert false : "Unhandled property detected in ToolbarButtonViewBinder!";
}
}
}
......@@ -1367,6 +1367,10 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/toolbar/BottomToolbarViewBinder.java",
"java/src/org/chromium/chrome/browser/toolbar/CustomTabToolbar.java",
"java/src/org/chromium/chrome/browser/toolbar/CustomTabToolbarAnimationDelegate.java",
"java/src/org/chromium/chrome/browser/toolbar/ToolbarButtonCoordinator.java",
"java/src/org/chromium/chrome/browser/toolbar/ToolbarButtonMediator.java",
"java/src/org/chromium/chrome/browser/toolbar/ToolbarButtonProperties.java",
"java/src/org/chromium/chrome/browser/toolbar/ToolbarButtonViewBinder.java",
"java/src/org/chromium/chrome/browser/toolbar/HomePageButton.java",
"java/src/org/chromium/chrome/browser/toolbar/KeyboardNavigationListener.java",
"java/src/org/chromium/chrome/browser/toolbar/ScrollingBottomViewResourceFrameLayout.java",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment