Commit b1784091 authored by Leonard Grey's avatar Leonard Grey Committed by Commit Bot

Dark mode: colors for sync error states

Bug: 915465
Change-Id: I2712459903140725c2c75dfdf0026013961ea964
Reviewed-on: https://chromium-review.googlesource.com/c/1391952
Commit-Queue: Leonard Grey <lgrey@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629148}
parent b745c9dd
......@@ -24,6 +24,7 @@
#include "chrome/browser/ui/view_ids.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/profiles/incognito_window_count_view.h"
#include "chrome/browser/ui/views/toolbar/toolbar_ink_drop_util.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/generated_resources.h"
#include "services/identity/public/cpp/identity_manager.h"
......@@ -133,10 +134,15 @@ void AvatarToolbarButton::UpdateText() {
}
}
} else if (sync_state == SyncState::kError) {
color = gfx::kGoogleRed600;
color =
AdjustHighlightColorForContrast(gfx::kGoogleRed300, gfx::kGoogleRed600,
gfx::kGoogleRed050, gfx::kGoogleRed900);
text = l10n_util::GetStringUTF16(IDS_AVATAR_BUTTON_SYNC_ERROR);
} else if (sync_state == SyncState::kPaused) {
color = gfx::kGoogleBlue600;
color = AdjustHighlightColorForContrast(
gfx::kGoogleBlue300, gfx::kGoogleBlue600, gfx::kGoogleBlue050,
gfx::kGoogleBlue900);
text = l10n_util::GetStringUTF16(IDS_AVATAR_BUTTON_SYNC_PAUSED);
}
......@@ -169,6 +175,10 @@ void AvatarToolbarButton::OnThemeChanged() {
UpdateText();
}
void AvatarToolbarButton::AddedToWidget() {
UpdateText();
}
void AvatarToolbarButton::OnAvatarErrorChanged() {
UpdateIcon();
UpdateText();
......@@ -395,3 +405,33 @@ void AvatarToolbarButton::SetInsets() {
SetLayoutInsetDelta(layout_insets);
}
SkColor AvatarToolbarButton::AdjustHighlightColorForContrast(
SkColor desired_dark_color,
SkColor desired_light_color,
SkColor dark_extreme,
SkColor light_extreme) const {
const ui::ThemeProvider* theme_provider = GetThemeProvider();
if (!theme_provider)
return desired_light_color;
SkColor toolbar_color =
GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR);
SkColor contrasting_color = color_utils::PickContrastingColor(
desired_dark_color, desired_light_color, toolbar_color);
SkColor limit =
contrasting_color == desired_dark_color ? dark_extreme : light_extreme;
// Setting highlight color will set the text to the highlight color, and the
// background to the same color with a low alpha. This means that our target
// contrast is between the text (the highlight color) and a blend of the
// highlight color and the toolbar color.
SkColor base_color = color_utils::AlphaBlend(contrasting_color, toolbar_color,
kToolbarButtonBackgroundAlpha);
// Add a fudge factor to the minimum contrast ratio since we'll actually be
// blending with the adjusted color.
SkAlpha blend_alpha = color_utils::GetBlendValueWithMinimumContrast(
contrasting_color, limit, base_color,
color_utils::kMinimumReadableContrastRatio * 1.05);
return color_utils::AlphaBlend(limit, contrasting_color, blend_alpha);
}
......@@ -34,11 +34,14 @@ class AvatarToolbarButton : public ToolbarButton,
void UpdateText();
private:
FRIEND_TEST_ALL_PREFIXES(AvatarToolbarButtonTest,
HighlightMeetsMinimumContrast);
enum class SyncState { kNormal, kPaused, kError };
// ToolbarButton:
void NotifyClick(const ui::Event& event) override;
void OnThemeChanged() override;
void AddedToWidget() override;
// AvatarButtonErrorControllerDelegate:
void OnAvatarErrorChanged() override;
......@@ -78,6 +81,18 @@ class AvatarToolbarButton : public ToolbarButton,
void SetInsets();
// Chooses from |desired_dark_color| and |desired_light_color| based on
// whether the toolbar background is dark or light.
//
// If the resulting color will achieve sufficient contrast,
// returns it. Otherwise, blends it towards |dark_extreme| if it's light, or
// |dark_extreme| if it's dark until minimum contrast is achieved, and returns
// the result.
SkColor AdjustHighlightColorForContrast(SkColor desired_dark_color,
SkColor desired_light_color,
SkColor dark_extreme,
SkColor light_extreme) const;
Browser* const browser_;
Profile* const profile_;
......
// Copyright 2019 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.
#include "chrome/browser/ui/views/profiles/avatar_toolbar_button.h"
#include "chrome/browser/themes/theme_properties.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/test_with_browser_view.h"
#include "ui/base/theme_provider.h"
#include "ui/gfx/color_utils.h"
#include "ui/views/widget/widget.h"
class AvatarToolbarButtonTest : public TestWithBrowserView {};
// CrOS only shows the avatar button for incognito/guest.
#if !defined(OS_CHROMEOS)
TEST_F(AvatarToolbarButtonTest, HighlightMeetsMinimumContrast) {
auto button = std::make_unique<AvatarToolbarButton>(browser());
button->set_owned_by_client();
browser_view()->GetWidget()->GetContentsView()->AddChildView(button.get());
SkColor toolbar_color =
button->GetThemeProvider()->GetColor(ThemeProperties::COLOR_TOOLBAR);
SkColor highlight_color = SkColorSetRGB(0xFE, 0x00, 0x00);
DCHECK_LT(color_utils::GetContrastRatio(highlight_color, toolbar_color),
color_utils::kMinimumReadableContrastRatio);
SkColor result = button->AdjustHighlightColorForContrast(
highlight_color, highlight_color, SK_ColorBLACK, SK_ColorWHITE);
EXPECT_GT(color_utils::GetContrastRatio(result, toolbar_color),
color_utils::kMinimumReadableContrastRatio);
}
#endif
......@@ -4497,6 +4497,7 @@ test("unit_tests") {
"../browser/ui/views/payments/payment_request_item_list_unittest.cc",
"../browser/ui/views/payments/validating_textfield_unittest.cc",
"../browser/ui/views/payments/view_stack_unittest.cc",
"../browser/ui/views/profiles/avatar_toolbar_button_unittest.cc",
"../browser/ui/views/relaunch_notification/relaunch_notification_controller_unittest.cc",
"../browser/ui/views/relaunch_notification/relaunch_required_timer_internal_unittest.cc",
"../browser/ui/views/relaunch_notification/wall_clock_timer_unittest.cc",
......
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