Skip to content

Commit

Permalink
cap: Implement AccentColor on ChromeOS
Browse files Browse the repository at this point in the history
Wires up ChromeOS's accent color up to blink.

Bug: 1338061
Change-Id: Ifb78e44c96b8c2c59d93bb63f00a44b10effabec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4918373
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Commit-Queue: Giovanni Ortuno Urquidi <ortuno@chromium.org>
Reviewed-by: Joey Arhar <jarhar@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1209966}
  • Loading branch information
Giovanni Ortuño Urquidi authored and Chromium LUCI CQ committed Oct 16, 2023
1 parent 90cdb2a commit 943f590
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 9 deletions.
5 changes: 5 additions & 0 deletions content/browser/theme_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "content/browser/theme_helper.h"

#include "base/no_destructor.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/common/renderer.mojom.h"
#include "ui/color/color_provider_key.h"
Expand Down Expand Up @@ -33,6 +34,10 @@ mojom::UpdateSystemColorInfoParamsPtr MakeUpdateSystemColorInfoParams(
const auto& colors = native_theme->GetSystemColors();
params->colors.insert(colors.begin(), colors.end());

#if BUILDFLAG(IS_CHROMEOS)
params->accent_color = native_theme->user_color();
#endif

// TODO(crbug.com/1251637): We should not be using ColorProviders sourced from
// the global NativeTheme web instance and instead have WebContents instances
// propagate their specific ColorProviders to hosted frames.
Expand Down
3 changes: 3 additions & 0 deletions content/common/renderer.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ struct UpdateSystemColorInfoParams {
// renderer's ColorProviders.
map<SystemThemeColor, uint32> colors;

// Accent color used by the system. Currently only set on ChromeOS.
uint32? accent_color;

// Maps used to reconstruct ColorProvider instances in the renderer.
map<color.mojom.RendererColorId, skia.mojom.SkColor> light_colors;
map<color.mojom.RendererColorId, skia.mojom.SkColor> dark_colors;
Expand Down
11 changes: 8 additions & 3 deletions content/renderer/render_thread_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1565,9 +1565,14 @@ void RenderThreadImpl::UpdateSystemColorInfo(
blink::ColorProvidersChanged();
}

bool did_system_color_info_change =
ui::NativeTheme::GetInstanceForWeb()->UpdateSystemColorInfo(
params->is_dark_mode, params->forced_colors, params->colors);
auto* native_theme = ui::NativeTheme::GetInstanceForWeb();

bool did_system_color_info_change = native_theme->UpdateSystemColorInfo(
params->is_dark_mode, params->forced_colors, params->colors);

did_system_color_info_change |=
native_theme->user_color() != params->accent_color;
native_theme->set_user_color(params->accent_color);

if (did_system_color_info_change) {
// Notify blink of system color info changes. These give blink the
Expand Down
4 changes: 4 additions & 0 deletions third_party/blink/public/platform/web_theme_engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,10 @@ class WebThemeEngine {
return absl::nullopt;
}

virtual absl::optional<SkColor> GetAccentColor() const {
return absl::nullopt;
}

virtual ForcedColors GetForcedColors() const { return ForcedColors::kNone; }
virtual void OverrideForcedColorsTheme(bool is_dark_theme) {}
virtual void SetForcedColors(const blink::ForcedColors forced_colors) {}
Expand Down
26 changes: 26 additions & 0 deletions third_party/blink/renderer/core/layout/layout_theme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,32 @@ void LayoutTheme::UpdateForcedColorsState() {
ForcedColors::kNone;
}

bool LayoutTheme::IsAccentColorCustomized(
mojom::blink::ColorScheme color_scheme) const {
if (!RuntimeEnabledFeatures::CSSSystemAccentColorEnabled()) {
return false;
}

return WebThemeEngineHelper::GetNativeThemeEngine()
->GetAccentColor()
.has_value();
}

Color LayoutTheme::GetSystemAccentColor(
mojom::blink::ColorScheme color_scheme) const {
if (!RuntimeEnabledFeatures::CSSSystemAccentColorEnabled()) {
return Color();
}

// Currently only plumbed through on ChromeOS.
const auto& accent_color =
WebThemeEngineHelper::GetNativeThemeEngine()->GetAccentColor();
if (!accent_color.has_value()) {
return Color();
}
return Color::FromSkColor(accent_color.value());
}

Color LayoutTheme::GetAccentColorOrDefault(
mojom::blink::ColorScheme color_scheme) const {
// This is from the kAccent color from NativeThemeBase::GetControlColor
Expand Down
10 changes: 4 additions & 6 deletions third_party/blink/renderer/core/layout/layout_theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,13 @@ class CORE_EXPORT LayoutTheme : public RefCounted<LayoutTheme> {
virtual void AdjustControlPartStyle(ComputedStyleBuilder&);

virtual bool IsAccentColorCustomized(
mojom::blink::ColorScheme color_scheme) const {
return false;
}
mojom::blink::ColorScheme color_scheme) const;

// GetSystemAccentColor returns transparent unless there is a special value
// from the OS color scheme.
virtual Color GetSystemAccentColor(
mojom::blink::ColorScheme color_scheme) const {
return Color();
}
mojom::blink::ColorScheme color_scheme) const;

// GetAccentColorOrDefault will return GetAccentColor if there is a value from
// the OS, otherwise it will return the default accent color.
Color GetAccentColorOrDefault(mojom::blink::ColorScheme color_scheme) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ absl::optional<SkColor> WebThemeEngineDefault::GetSystemColor(
NativeSystemThemeColor(system_theme_color));
}

absl::optional<SkColor> WebThemeEngineDefault::GetAccentColor() const {
return ui::NativeTheme::GetInstanceForWeb()->user_color();
}

#if BUILDFLAG(IS_WIN)
// static
void WebThemeEngineDefault::cacheScrollBarMetrics(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class WebThemeEngineDefault : public WebThemeEngine {
gfx::Rect NinePatchAperture(Part part) const override;
absl::optional<SkColor> GetSystemColor(
WebThemeEngine::SystemThemeColor system_theme_color) const override;
absl::optional<SkColor> GetAccentColor() const override;
#if BUILDFLAG(IS_WIN)
// Caches the scrollbar metrics. These are retrieved in the browser and passed
// to the renderer in RendererPreferences because the required Windows
Expand Down

0 comments on commit 943f590

Please sign in to comment.