Skip to content

Commit

Permalink
Update bottom navigation bar color for dark mode and incognito
Browse files Browse the repository at this point in the history
Bug: 1134927
Change-Id: Icd06307a11bdfd218c77f8aaf3ab6e651bb1e978
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2500483
Reviewed-by: Lijin Shen <lazzzis@google.com>
Reviewed-by: Theresa  <twellington@chromium.org>
Commit-Queue: Sinan Sahin <sinansahin@google.com>
Cr-Commit-Position: refs/heads/master@{#822038}
  • Loading branch information
fsinan authored and Commit Bot committed Oct 29, 2020
1 parent aa74ca7 commit 645f677
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.ViewGroup;
import android.view.Window;
Expand All @@ -25,7 +24,6 @@
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior;
import org.chromium.chrome.browser.compositor.layouts.OverviewModeBehavior.OverviewModeObserver;
import org.chromium.chrome.browser.device.DeviceClassManager;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.tabmodel.EmptyTabModelSelectorObserver;
import org.chromium.chrome.browser.tabmodel.TabModel;
import org.chromium.chrome.browser.tabmodel.TabModelSelector;
Expand Down Expand Up @@ -53,7 +51,7 @@ class TabbedNavigationBarColorController implements VrModeObserver {
private @Nullable OverviewModeObserver mOverviewModeObserver;
private CallbackController mCallbackController = new CallbackController();

private boolean mUseLightNavigation;
private boolean mForceDarkNavigationBarColor;
private boolean mOverviewModeHiding;
private float mNavigationBarScrimFraction;

Expand All @@ -74,16 +72,14 @@ class TabbedNavigationBarColorController implements VrModeObserver {
mResources = mRootView.getResources();
mDefaultScrimColor = ApiCompatibilityUtils.getColor(mResources, R.color.black_alpha_65);

// If we're not using a light navigation bar, it will always be black so there's no need
// to register observers and manipulate coloring.
// If we're not using a light navigation bar, it will always be the same dark color so
// there's no need to register observers and manipulate coloring.
if (!mResources.getBoolean(R.bool.window_light_navigation_bar)) {
mTabModelSelector = null;
mTabModelSelectorObserver = null;
return;
}

mUseLightNavigation = true;

mTabModelSelector = tabModelSelector;
mTabModelSelectorObserver = new EmptyTabModelSelectorObserver() {
@Override
Expand Down Expand Up @@ -155,7 +151,7 @@ public void onOverviewModeFinishedHiding() {
public void onExitVr() {
// The platform ignores the light navigation bar system UI flag when launching an Activity
// in VR mode, so we need to restore it when VR is exited.
UiUtils.setNavigationBarIconColor(mRootView, mUseLightNavigation);
UiUtils.setNavigationBarIconColor(mRootView, !mForceDarkNavigationBarColor);
}

@Override
Expand All @@ -166,57 +162,44 @@ private void updateNavigationBarColor() {
boolean overviewVisible = mOverviewModeBehavior != null
&& mOverviewModeBehavior.overviewVisible() && !mOverviewModeHiding;

boolean useLightNavigation;
if (ChromeFeatureList.isInitialized()
&& (ChromeFeatureList.isEnabled(ChromeFeatureList.HORIZONTAL_TAB_SWITCHER_ANDROID)
|| DeviceClassManager.enableAccessibilityLayout()
|| TabUiFeatureUtilities.isGridTabSwitcherEnabled())) {
useLightNavigation = !mTabModelSelector.isIncognitoSelected();
boolean forceDarkNavigation;
if (DeviceClassManager.enableAccessibilityLayout()
|| TabUiFeatureUtilities.isGridTabSwitcherEnabled()) {
forceDarkNavigation = mTabModelSelector.isIncognitoSelected();
} else {
useLightNavigation = !mTabModelSelector.isIncognitoSelected() || overviewVisible;
forceDarkNavigation = mTabModelSelector.isIncognitoSelected() && !overviewVisible;
}

useLightNavigation &= !UiUtils.isSystemUiThemingDisabled();

if (mUseLightNavigation == useLightNavigation) return;

mUseLightNavigation = useLightNavigation;
forceDarkNavigation &= !UiUtils.isSystemUiThemingDisabled();

mWindow.setNavigationBarColor(useLightNavigation ? ApiCompatibilityUtils.getColor(
mResources, R.color.bottom_system_nav_color)
: Color.BLACK);
if (mForceDarkNavigationBarColor == forceDarkNavigation) return;

setNavigationBarColor(useLightNavigation);
mForceDarkNavigationBarColor = forceDarkNavigation;

UiUtils.setNavigationBarIconColor(mRootView, useLightNavigation);
mWindow.setNavigationBarColor(getNavigationBarColor(mForceDarkNavigationBarColor));
setNavigationBarDividerColor();
UiUtils.setNavigationBarIconColor(mRootView, !mForceDarkNavigationBarColor);
}

@SuppressLint("NewApi")
private void setNavigationBarColor(boolean useLightNavigation) {
private void setNavigationBarDividerColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mWindow.setNavigationBarDividerColor(useLightNavigation
? ApiCompatibilityUtils.getColor(
mResources, R.color.bottom_system_nav_divider_color)
: Color.BLACK);
mWindow.setNavigationBarDividerColor(
getNavigationBarDividerColor(mForceDarkNavigationBarColor));
}
}

/**
* Update the scrim amount on the navigation bar. Note that we only update when the navigation
* bar color is in light mode.
* Update the scrim amount on the navigation bar.
* @param fraction The scrim fraction in range [0, 1].
*/
public void setNavigationBarScrimFraction(float fraction) {
if (!mUseLightNavigation) {
return;
}
mNavigationBarScrimFraction = fraction;
mWindow.setNavigationBarColor(applyCurrentScrimToColor(
ApiCompatibilityUtils.getColor(mResources, R.color.bottom_system_nav_color)));
mWindow.setNavigationBarColor(
applyCurrentScrimToColor(getNavigationBarColor(mForceDarkNavigationBarColor)));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mWindow.setNavigationBarDividerColor(
applyCurrentScrimToColor(ApiCompatibilityUtils.getColor(
mResources, R.color.bottom_system_nav_divider_color)));
mWindow.setNavigationBarDividerColor(applyCurrentScrimToColor(
getNavigationBarDividerColor(mForceDarkNavigationBarColor)));
}

// Adjust the color of navigation bar icons based on color state of the navigation bar.
Expand All @@ -227,6 +210,18 @@ public void setNavigationBarScrimFraction(float fraction) {
}
}

private @ColorInt int getNavigationBarColor(boolean forceDarkNavigationBar) {
return ApiCompatibilityUtils.getColor(mResources,
forceDarkNavigationBar ? R.color.toolbar_background_primary_dark
: R.color.bottom_system_nav_color);
}

private @ColorInt int getNavigationBarDividerColor(boolean forceDarkNavigationBar) {
return ApiCompatibilityUtils.getColor(mResources,
forceDarkNavigationBar ? R.color.hairline_stroke_color_dark
: R.color.bottom_system_nav_divider_color);
}

private @ColorInt int applyCurrentScrimToColor(@ColorInt int color) {
// Apply a color overlay.
float scrimColorAlpha = (mDefaultScrimColor >>> 24) / 255f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Color;
import android.content.res.Resources;
import android.os.Build;
import android.support.test.InstrumentationRegistry;
import android.view.Window;
Expand Down Expand Up @@ -47,9 +47,11 @@ public class TabbedNavigationBarColorControllerTest {
public void setUp() throws InterruptedException {
mActivityTestRule.startMainActivityOnBlankPage();
mWindow = mActivityTestRule.getActivity().getWindow();
mLightNavigationColor = ApiCompatibilityUtils.getColor(
mActivityTestRule.getActivity().getResources(), R.color.bottom_system_nav_color);
mDarkNavigationColor = Color.BLACK;
final Resources resources = mActivityTestRule.getActivity().getResources();
mLightNavigationColor =
ApiCompatibilityUtils.getColor(resources, R.color.default_bg_color_light);
mDarkNavigationColor =
ApiCompatibilityUtils.getColor(resources, R.color.default_bg_color_dark_elev_3);
}

@Test
Expand Down Expand Up @@ -80,8 +82,8 @@ public void testToggleIncognito() {
ChromeTabUtils.newTabFromMenu(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), true, true);

assertEquals("Navigation bar should be black on incognito tabs.", mDarkNavigationColor,
mWindow.getNavigationBarColor());
assertEquals("Navigation bar should be dark_elev_3 on incognito tabs.",
mDarkNavigationColor, mWindow.getNavigationBarColor());

ChromeTabUtils.newTabFromMenu(InstrumentationRegistry.getInstrumentation(),
mActivityTestRule.getActivity(), false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,4 @@
<color name="control_highlight_color">@color/default_control_color_highlight_dark</color>

<color name="text_highlight_color">@color/highlight_color_on_light_text</color>

<!-- Navigation bar colors -->
<color name="bottom_system_nav_color">@android:color/black</color>
<color name="bottom_system_nav_divider_color">@android:color/black</color>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
<color name="default_favicon_background_color">@color/default_control_color_normal_light</color>

<!-- Navigation bar colors -->
<color name="bottom_system_nav_color">@color/modern_white</color>
<color name="bottom_system_nav_divider_color">@color/black_alpha_12</color>
<color name="bottom_system_nav_color">@color/toolbar_background_primary</color>
<color name="bottom_system_nav_divider_color">@color/hairline_stroke_color</color>
</resources>

0 comments on commit 645f677

Please sign in to comment.