Commit 09b01647 authored by bttk's avatar bttk Committed by Chromium LUCI CQ

[ToolbarMVC] Add onPrimaryColorChanged to observed location bar data

This change dissolves method LocationBarLayout#updateVisualsForState().
Method LocationBarCoordinator#updateVisualsForState() remains, and is
used in Toolbar.

Bug: 1142883
Change-Id: If8b7b46bd6634adc514d607baab406205852263b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2545192Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Reviewed-by: default avatarPatrick Noland <pnoland@chromium.org>
Commit-Queue: bttk <bttk@chromium.org>
Cr-Commit-Position: refs/heads/master@{#833362}
parent 8fb68e33
...@@ -61,6 +61,7 @@ import org.chromium.chrome.browser.tab.Tab; ...@@ -61,6 +61,7 @@ import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TrustedCdn; import org.chromium.chrome.browser.tab.TrustedCdn;
import org.chromium.chrome.browser.toolbar.LocationBarModel; import org.chromium.chrome.browser.toolbar.LocationBarModel;
import org.chromium.chrome.browser.toolbar.ToolbarColors; import org.chromium.chrome.browser.toolbar.ToolbarColors;
import org.chromium.chrome.browser.toolbar.ToolbarProgressBar;
import org.chromium.chrome.browser.toolbar.top.ToolbarLayout; import org.chromium.chrome.browser.toolbar.top.ToolbarLayout;
import org.chromium.chrome.browser.toolbar.top.ToolbarPhone; import org.chromium.chrome.browser.toolbar.top.ToolbarPhone;
import org.chromium.components.browser_ui.styles.ChromeColors; import org.chromium.components.browser_ui.styles.ChromeColors;
...@@ -249,7 +250,6 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -249,7 +250,6 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
mLocationBarModel = locationBarModel; mLocationBarModel = locationBarModel;
mLocationBar = mLocationBar =
new CustomTabLocationBar(locationBarModel, actionModeCallback, (UrlBar) mUrlBar); new CustomTabLocationBar(locationBarModel, actionModeCallback, (UrlBar) mUrlBar);
mLocationBar.updateVisualsForState();
return mLocationBar; return mLocationBar;
} }
...@@ -495,6 +495,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -495,6 +495,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
super.onConfigurationChanged(newConfig); super.onConfigurationChanged(newConfig);
mLocationBarModel.notifyTitleChanged(); mLocationBarModel.notifyTitleChanged();
mLocationBarModel.notifyUrlChanged(); mLocationBarModel.notifyUrlChanged();
mLocationBarModel.notifyPrimaryColorChanged();
} }
@Override @Override
...@@ -540,9 +541,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -540,9 +541,7 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
// Using the current background color instead of the final color in case this // Using the current background color instead of the final color in case this
// animation was cancelled. This ensures the assets are updated to the visible // animation was cancelled. This ensures the assets are updated to the visible
// color. // color.
int backgroundColor = background.getColor(); updateUseDarkColors(background.getColor());
mUseDarkColors = !ColorUtils.shouldUseLightForegroundOnBackground(backgroundColor);
mLocationBar.updateVisualsForState();
} }
}); });
mBrandColorTransitionAnimation.start(); mBrandColorTransitionAnimation.start();
...@@ -550,7 +549,12 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -550,7 +549,12 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
if (!shouldAnimate) mBrandColorTransitionAnimation.end(); if (!shouldAnimate) mBrandColorTransitionAnimation.end();
} }
private void updateUseDarkColors(int backgroundColor) {
boolean useDarkColors = !ColorUtils.shouldUseLightForegroundOnBackground(backgroundColor);
if (mUseDarkColors == useDarkColors) return;
mUseDarkColors = useDarkColors;
mLocationBar.onUseDarkColorsChanged();
}
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
...@@ -613,8 +617,9 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -613,8 +617,9 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
ActionMode.Callback actionModeCallback, UrlBar urlBar) { ActionMode.Callback actionModeCallback, UrlBar urlBar) {
mLocationBarDataProvider = locationBarDataProvider; mLocationBarDataProvider = locationBarDataProvider;
mLocationBarDataProvider.addObserver(this); mLocationBarDataProvider.addObserver(this);
mUrlCoordinator = new UrlBarCoordinator( mUrlCoordinator = new UrlBarCoordinator(urlBar, /*windowDelegate=*/null,
urlBar, null, actionModeCallback, /*focusChangeCallback=*/(unused) -> {}, this); actionModeCallback, /*focusChangeCallback=*/(unused) -> {}, this);
onPrimaryColorChanged();
} }
public void onNativeLibraryReady() { public void onNativeLibraryReady() {
...@@ -752,34 +757,48 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL ...@@ -752,34 +757,48 @@ public class CustomTabToolbar extends ToolbarLayout implements View.OnLongClickL
public void onNtpStartedLoading() {} public void onNtpStartedLoading() {}
@Override @Override
public void updateLoadingState(boolean updateUrl) { public void onPrimaryColorChanged() {
if (updateUrl) onUrlChanged(); onUseDarkColorsChanged();
updateStatusIcon(); updateProgressBarColors();
} }
@Override private void onUseDarkColorsChanged() {
public void updateVisualsForState() {
Resources resources = getResources();
updateStatusIcon();
updateButtonsTint(); updateButtonsTint();
if (mUrlCoordinator.setUseDarkTextColors(mUseDarkColors)) { if (mUrlCoordinator.setUseDarkTextColors(mUseDarkColors)) {
// Update the URL to make it use the new color scheme.
onUrlChanged(); onUrlChanged();
} }
mTitleBar.setTextColor(ApiCompatibilityUtils.getColor(resources, mTitleBar.setTextColor(ApiCompatibilityUtils.getColor(getResources(),
mUseDarkColors ? R.color.default_text_color_dark mUseDarkColors ? R.color.default_text_color_dark
: R.color.default_text_color_light)); : R.color.default_text_color_light));
}
if (getProgressBar() != null) { @Override
if (!ToolbarColors.isUsingDefaultToolbarColor( public void updateLoadingState(boolean updateUrl) {
getResources(), false, getBackground().getColor())) { if (updateUrl) onUrlChanged();
getProgressBar().setThemeColor(getBackground().getColor(), false); updateStatusIcon();
} else { }
getProgressBar().setBackgroundColor(ApiCompatibilityUtils.getColor(
resources, R.color.progress_bar_background)); @Override
getProgressBar().setForegroundColor(ApiCompatibilityUtils.getColor( public void updateVisualsForState() {
resources, R.color.progress_bar_foreground)); updateStatusIcon();
} updateProgressBarColors();
}
private void updateProgressBarColors() {
final ToolbarProgressBar progressBar = getProgressBar();
if (progressBar == null) return;
final Resources resources = getResources();
final int backgroundColor = getBackground().getColor();
if (ToolbarColors.isUsingDefaultToolbarColor(
resources, /*isIncognito=*/false, backgroundColor)) {
progressBar.setBackgroundColor(
ApiCompatibilityUtils.getColor(resources, R.color.progress_bar_background));
progressBar.setForegroundColor(
ApiCompatibilityUtils.getColor(resources, R.color.progress_bar_foreground));
} else {
progressBar.setThemeColor(backgroundColor, /*isIncognito=*/false);
} }
} }
......
...@@ -30,10 +30,16 @@ public interface LocationBarDataProvider { ...@@ -30,10 +30,16 @@ public interface LocationBarDataProvider {
* consumer will query the data it cares about. * consumer will query the data it cares about.
*/ */
interface Observer { interface Observer {
void onTitleChanged();
void onUrlChanged();
void onIncognitoStateChanged(); void onIncognitoStateChanged();
void onNtpStartedLoading(); void onNtpStartedLoading();
/**
* Notifies about a possible change of the value of {@link #getPrimaryColor()}, or {@link
* #isUsingBrandColor()}.
*/
void onPrimaryColorChanged();
void onTitleChanged();
void onUrlChanged();
// TODO(https://crbug.com/1139481): Add methods for other LocationBarDataProvider // TODO(https://crbug.com/1139481): Add methods for other LocationBarDataProvider
// data, e.g. security state. // data, e.g. security state.
} }
......
...@@ -325,7 +325,7 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -325,7 +325,7 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
} }
mDeferredNativeRunnables.clear(); mDeferredNativeRunnables.clear();
updateVisualsForState(); onPrimaryColorChanged();
updateMicButtonVisibility(); updateMicButtonVisibility();
...@@ -592,48 +592,48 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -592,48 +592,48 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
if (visibility == View.VISIBLE) updateMicButtonState(); if (visibility == View.VISIBLE) updateMicButtonState();
} }
/** Updates visuals after the primary color has changed. */
@CallSuper
public void onPrimaryColorChanged() {
updateAssistantVoiceSearchColors();
updateUseDarkColors();
}
/** /**
* Call to force the UI to update the state of various buttons based on whether or not the * Update visuals to use a correct light or dark color scheme depending on the primary color.
* current tab is incognito.
*/ */
public void updateVisualsForState() { private void updateUseDarkColors() {
// If the location bar is focused, the toolbar background color would be the default color // TODO(crbug.com/1114183): Unify light and dark color logic in chrome and make it clear
// regardless of whether it is branded or not. // whether the foreground or background color is dark.
final int defaultPrimaryColor = ChromeColors.getDefaultThemeColor(
getResources(), mLocationBarDataProvider.isIncognito());
final int primaryColor =
mUrlHasFocus ? defaultPrimaryColor : mLocationBarDataProvider.getPrimaryColor();
// This will be called between inflation and initialization. For those calls, using a null
// ColorStateList should have no visible impact to the user.
AssistantVoiceSearchService assistantVoiceSearchService =
mAssistantVoiceSearchServiceSupplier.get();
ColorStateList micColorStateList = assistantVoiceSearchService == null
? null
: assistantVoiceSearchService.getMicButtonColorStateList(
primaryColor, getContext());
ApiCompatibilityUtils.setImageTintList(mMicButton, micColorStateList);
final boolean useDarkColors = final boolean useDarkColors =
!ColorUtils.shouldUseLightForegroundOnBackground(primaryColor); !ColorUtils.shouldUseLightForegroundOnBackground(getPrimaryBackgroundColor());
ColorStateList colorStateList =
ChromeColors.getPrimaryIconTint(getContext(), !useDarkColors);
ApiCompatibilityUtils.setImageTintList(mDeleteButton, colorStateList);
ApiCompatibilityUtils.setImageTintList(
mDeleteButton, ChromeColors.getPrimaryIconTint(getContext(), !useDarkColors));
// If the URL changed colors and is not focused, update the URL to account for the new // If the URL changed colors and is not focused, update the URL to account for the new
// color scheme. // color scheme.
if (mUrlCoordinator.setUseDarkTextColors(useDarkColors) && !mUrlBar.hasFocus()) { if (mUrlCoordinator.setUseDarkTextColors(useDarkColors) && !mUrlBar.hasFocus()) {
setUrl(mLocationBarDataProvider.getCurrentUrl()); setUrl(mLocationBarDataProvider.getCurrentUrl());
} }
mStatusCoordinator.setUseDarkColors(useDarkColors); mStatusCoordinator.setUseDarkColors(useDarkColors);
if (mAutocompleteCoordinator != null) { if (mAutocompleteCoordinator != null) {
mAutocompleteCoordinator.updateVisualsForState( mAutocompleteCoordinator.updateVisualsForState(
useDarkColors, mLocationBarDataProvider.isIncognito()); useDarkColors, mLocationBarDataProvider.isIncognito());
} }
} }
/** Returns the primary color based on the url focus, and incognito state. */
private int getPrimaryBackgroundColor() {
// If the url bar is focused, the toolbar background color is the default color regardless
// of whether it is branded or not.
if (mUrlHasFocus) {
return ChromeColors.getDefaultThemeColor(
getResources(), mLocationBarDataProvider.isIncognito());
} else {
return mLocationBarDataProvider.getPrimaryColor();
}
}
protected void onNtpStartedLoading() {} protected void onNtpStartedLoading() {}
public View getContainerView() { public View getContainerView() {
...@@ -644,8 +644,6 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -644,8 +644,6 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
return mStatusCoordinator.getSecurityIconView(); return mStatusCoordinator.getSecurityIconView();
} }
public void setShowTitle(boolean showTitle) {}
protected WindowAndroid getWindowAndroid() { protected WindowAndroid getWindowAndroid() {
return mWindowAndroid; return mWindowAndroid;
} }
...@@ -656,13 +654,21 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -656,13 +654,21 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
assert assistantVoiceSearchService != null; assert assistantVoiceSearchService != null;
Drawable drawable = assistantVoiceSearchService.getCurrentMicDrawable(); Drawable drawable = assistantVoiceSearchService.getCurrentMicDrawable();
mMicButton.setImageDrawable(drawable); mMicButton.setImageDrawable(drawable);
updateAssistantVoiceSearchColors();
}
final int defaultPrimaryColor = ChromeColors.getDefaultThemeColor( private void updateAssistantVoiceSearchColors() {
getResources(), mLocationBarDataProvider.isIncognito()); AssistantVoiceSearchService assistantVoiceSearchService =
final int primaryColor = mAssistantVoiceSearchServiceSupplier.get();
mUrlHasFocus ? defaultPrimaryColor : mLocationBarDataProvider.getPrimaryColor(); ColorStateList colorStateList;
ColorStateList colorStateList = // This will be called between inflation and initialization. For those calls, using a null
assistantVoiceSearchService.getMicButtonColorStateList(primaryColor, getContext()); // ColorStateList should have no visible impact to the user.
if (assistantVoiceSearchService == null) {
colorStateList = null;
} else {
colorStateList = assistantVoiceSearchService.getMicButtonColorStateList(
getPrimaryBackgroundColor(), getContext());
}
ApiCompatibilityUtils.setImageTintList(mMicButton, colorStateList); ApiCompatibilityUtils.setImageTintList(mMicButton, colorStateList);
} }
...@@ -923,6 +929,7 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -923,6 +929,7 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
mUrlHasFocus = hasFocus; mUrlHasFocus = hasFocus;
updateButtonVisibility(); updateButtonVisibility();
updateShouldAnimateIconChanges(); updateShouldAnimateIconChanges();
onPrimaryColorChanged();
if (mUrlHasFocus) { if (mUrlHasFocus) {
if (mNativeInitialized) RecordUserAction.record("FocusLocation"); if (mNativeInitialized) RecordUserAction.record("FocusLocation");
...@@ -959,8 +966,6 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener { ...@@ -959,8 +966,6 @@ public class LocationBarLayout extends FrameLayout implements OnClickListener {
if (imm.isActive(mUrlBar)) imm.hideSoftInputFromWindow(getWindowToken(), 0, null); if (imm.isActive(mUrlBar)) imm.hideSoftInputFromWindow(getWindowToken(), 0, null);
} }
if (mLocationBarDataProvider.isUsingBrandColor()) updateVisualsForState();
mStatusCoordinator.onUrlFocusChange(mUrlHasFocus); mStatusCoordinator.onUrlFocusChange(mUrlHasFocus);
if (!mUrlFocusedWithoutAnimations) handleUrlFocusAnimation(mUrlHasFocus); if (!mUrlFocusedWithoutAnimations) handleUrlFocusAnimation(mUrlHasFocus);
......
...@@ -90,27 +90,30 @@ class LocationBarMediator implements LocationBar, LocationBarDataProvider.Observ ...@@ -90,27 +90,30 @@ class LocationBarMediator implements LocationBar, LocationBarDataProvider.Observ
} }
// LocationBarData.Observer implementation // LocationBarData.Observer implementation
@Override @Override
public void onTitleChanged() {} public void onIncognitoStateChanged() {
mLocationBarLayout.updateMicButtonState();
}
@Override @Override
public void onUrlChanged() { public void onNtpStartedLoading() {
mLocationBarLayout.setUrl(mLocationBarDataProvider.getCurrentUrl()); mLocationBarLayout.onNtpStartedLoading();
} }
@Override @Override
public void onIncognitoStateChanged() { public void onPrimaryColorChanged() {
mLocationBarLayout.updateMicButtonState(); mLocationBarLayout.onPrimaryColorChanged();
} }
@Override @Override
public void onNtpStartedLoading() { public void onTitleChanged() {}
mLocationBarLayout.onNtpStartedLoading();
@Override
public void onUrlChanged() {
mLocationBarLayout.setUrl(mLocationBarDataProvider.getCurrentUrl());
} }
// LocationBar implementation. // LocationBar implementation.
@Override @Override
public void destroy() { public void destroy() {
mLocationBarLayout = null; mLocationBarLayout = null;
...@@ -126,12 +129,12 @@ class LocationBarMediator implements LocationBar, LocationBarDataProvider.Observ ...@@ -126,12 +129,12 @@ class LocationBarMediator implements LocationBar, LocationBarDataProvider.Observ
@Override @Override
public void updateVisualsForState() { public void updateVisualsForState() {
mLocationBarLayout.updateVisualsForState(); mLocationBarLayout.onPrimaryColorChanged();
} }
@Override @Override
public void setShowTitle(boolean showTitle) { public void setShowTitle(boolean showTitle) {
mLocationBarLayout.setShowTitle(showTitle); // This method is only used in CustomTabToolbar.
} }
@Override @Override
......
...@@ -178,8 +178,8 @@ class LocationBarPhone extends LocationBarLayout { ...@@ -178,8 +178,8 @@ class LocationBarPhone extends LocationBarLayout {
} }
@Override @Override
public void updateVisualsForState() { public void onPrimaryColorChanged() {
super.updateVisualsForState(); super.onPrimaryColorChanged();
boolean isIncognito = mLocationBarDataProvider.isIncognito(); boolean isIncognito = mLocationBarDataProvider.isIncognito();
setShowIconsWhenUrlFocused(SearchEngineLogoUtils.shouldShowSearchEngineLogo(isIncognito)); setShowIconsWhenUrlFocused(SearchEngineLogoUtils.shouldShowSearchEngineLogo(isIncognito));
updateStatusVisibility(); updateStatusVisibility();
......
...@@ -121,6 +121,7 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro ...@@ -121,6 +121,7 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro
updateUsingBrandColor(); updateUsingBrandColor();
notifyTitleChanged(); notifyTitleChanged();
notifyUrlChanged(); notifyUrlChanged();
notifyPrimaryColorChanged();
} }
@Override @Override
...@@ -346,7 +347,10 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro ...@@ -346,7 +347,10 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro
} }
public void setShouldShowOmniboxInOverviewMode(boolean shouldShowOmniboxInOverviewMode) { public void setShouldShowOmniboxInOverviewMode(boolean shouldShowOmniboxInOverviewMode) {
mShouldShowOmniboxInOverviewMode = shouldShowOmniboxInOverviewMode; if (mShouldShowOmniboxInOverviewMode != shouldShowOmniboxInOverviewMode) {
mShouldShowOmniboxInOverviewMode = shouldShowOmniboxInOverviewMode;
notifyPrimaryColorChanged();
}
} }
/** /**
...@@ -356,6 +360,7 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro ...@@ -356,6 +360,7 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro
public void setPrimaryColor(int color) { public void setPrimaryColor(int color) {
mPrimaryColor = color; mPrimaryColor = color;
updateUsingBrandColor(); updateUsingBrandColor();
notifyPrimaryColorChanged();
} }
private void updateUsingBrandColor() { private void updateUsingBrandColor() {
...@@ -379,6 +384,12 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro ...@@ -379,6 +384,12 @@ public class LocationBarModel implements ToolbarDataProvider, LocationBarDataPro
return isInOverviewAndShowingOmnibox() || mIsUsingBrandColor; return isInOverviewAndShowingOmnibox() || mIsUsingBrandColor;
} }
public void notifyPrimaryColorChanged() {
for (LocationBarDataProvider.Observer observer : mLocationBarDataObservers) {
observer.onPrimaryColorChanged();
}
}
@Override @Override
public boolean isOfflinePage() { public boolean isOfflinePage() {
return hasTab() && OfflinePageUtils.isOfflinePage(mTab); return hasTab() && OfflinePageUtils.isOfflinePage(mTab);
......
...@@ -677,7 +677,7 @@ public class LocationBarLayoutTest { ...@@ -677,7 +677,7 @@ public class LocationBarLayoutTest {
Tab tab = mActivityTestRule.loadUrlInNewTab(url, incognito); Tab tab = mActivityTestRule.loadUrlInNewTab(url, incognito);
setupModelsForCurrentTab(); setupModelsForCurrentTab();
setUrlToPageUrl(locationBar); setUrlToPageUrl(locationBar);
TestThreadUtils.runOnUiThreadBlocking(() -> { locationBar.updateVisualsForState(); }); TestThreadUtils.runOnUiThreadBlocking(() -> { locationBar.onPrimaryColorChanged(); });
return tab; return tab;
} }
......
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