views: Support themes for Alloy BrowserView in Chrome Window (see #3681)

To test:
- Run `cefclient --enable-chrome-runtime --use-alloy-style
                 --use-chrome-style-window [--background-color=green]`
- OS and Chrome theme changes behave as expected.
This commit is contained in:
Marshall Greenblatt 2024-05-01 17:56:30 -04:00
parent 91ba12468b
commit fae4a6f0bd
6 changed files with 193 additions and 90 deletions

View File

@ -9,6 +9,7 @@
#include "libcef/browser/views/window_view.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
@ -22,6 +23,10 @@
ChromeBrowserFrame::ChromeBrowserFrame(CefWindowView* window_view)
: window_view_(window_view) {}
ChromeBrowserFrame::~ChromeBrowserFrame() {
DCHECK(associated_profiles_.empty());
}
void ChromeBrowserFrame::Init(BrowserView* browser_view,
std::unique_ptr<Browser> browser) {
DCHECK(browser_view);
@ -64,17 +69,71 @@ void ChromeBrowserFrame::Initialized() {
#endif
}
void ChromeBrowserFrame::AddAssociatedProfile(Profile* /*profile*/) {
// Calls ThemeChanged().
UserChangedTheme(BrowserThemeChangeType::kBrowserTheme);
void ChromeBrowserFrame::AddAssociatedProfile(Profile* profile) {
DCHECK(profile);
// Always call ThemeChanged() when the Chrome style BrowserView is added.
bool call_theme_changed =
browser_view_ && browser_view_->GetProfile() == profile;
ProfileMap::iterator it = associated_profiles_.find(profile);
if (it != associated_profiles_.end()) {
// Another instance of a known Profile.
(it->second)++;
} else {
auto* current_profile = GetThemeProfile();
associated_profiles_.insert(std::make_pair(profile, 1));
if (auto* theme_service = ThemeServiceFactory::GetForProfile(profile)) {
theme_service->AddObserver(this);
}
// Potentially switching to a different theme.
call_theme_changed |= GetThemeProfile() != current_profile;
}
if (call_theme_changed) {
// Calls ThemeChanged().
UserChangedTheme(BrowserThemeChangeType::kBrowserTheme);
}
}
void ChromeBrowserFrame::RemoveAssociatedProfile(Profile* /*profile*/) {}
void ChromeBrowserFrame::RemoveAssociatedProfile(Profile* profile) {
DCHECK(profile);
ProfileMap::iterator it = associated_profiles_.find(profile);
if (it == associated_profiles_.end()) {
DCHECK(false); // Not reached.
return;
}
if (--(it->second) > 0) {
// More instances of the Profile exist.
return;
}
auto* current_profile = GetThemeProfile();
associated_profiles_.erase(it);
if (auto* theme_service = ThemeServiceFactory::GetForProfile(profile)) {
theme_service->RemoveObserver(this);
}
auto* new_profile = GetThemeProfile();
if (new_profile != current_profile) {
// Switching to a different theme.
NotifyThemeColorsChanged(/*chrome_theme=*/!!new_profile);
}
}
Profile* ChromeBrowserFrame::GetThemeProfile() const {
// Always prefer the Browser Profile, if any.
if (browser_view_) {
return browser_view_->GetProfile();
}
if (!associated_profiles_.empty()) {
return associated_profiles_.begin()->first;
}
return nullptr;
}
@ -148,6 +207,31 @@ void ChromeBrowserFrame::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
native_theme_change_ = false;
}
ui::ColorProviderKey ChromeBrowserFrame::GetColorProviderKey() const {
if (browser_view_) {
// Use the default Browser implementation.
return BrowserFrame::GetColorProviderKey();
}
const auto& widget_key = Widget::GetColorProviderKey();
if (auto* profile = GetThemeProfile()) {
return CefWidget::GetColorProviderKey(widget_key, profile);
}
return widget_key;
}
void ChromeBrowserFrame::OnThemeChanged() {
if (browser_view_) {
// Ignore these notifications if we have a Browser.
return;
}
// When the Chrome theme changes, the NativeTheme may also change.
SelectNativeTheme();
NotifyThemeColorsChanged(/*chrome_theme=*/true);
}
void ChromeBrowserFrame::OnColorProviderCacheResetMissed() {
// Ignore calls during Widget::Init().
if (!initialized_) {

View File

@ -6,10 +6,13 @@
#define CEF_LIBCEF_BROWSER_CHROME_VIEWS_CHROME_BROWSER_FRAME_H_
#pragma once
#include <map>
#include "libcef/browser/views/color_provider_tracker.h"
#include "libcef/browser/views/widget.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/themes/theme_service_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_frame.h"
@ -96,9 +99,11 @@ class CefWindowView;
// CefWindowView::CreateWidget() when the Chrome runtime is enabled.
class ChromeBrowserFrame : public BrowserFrame,
public CefWidget,
public CefColorProviderTracker::Observer {
public CefColorProviderTracker::Observer,
public ThemeServiceObserver {
public:
explicit ChromeBrowserFrame(CefWindowView* window_view);
~ChromeBrowserFrame() override;
ChromeBrowserFrame(const ChromeBrowserFrame&) = delete;
ChromeBrowserFrame& operator=(const ChromeBrowserFrame&) = delete;
@ -131,6 +136,10 @@ class ChromeBrowserFrame : public BrowserFrame,
// ui::NativeThemeObserver methods:
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
ui::ColorProviderKey GetColorProviderKey() const override;
// ThemeServiceObserver methods:
void OnThemeChanged() override;
BrowserView* browser_view() const { return browser_view_; }
@ -146,6 +155,10 @@ class ChromeBrowserFrame : public BrowserFrame,
bool initialized_ = false;
bool native_theme_change_ = false;
// Map of Profile* to count.
using ProfileMap = std::map<Profile*, size_t>;
ProfileMap associated_profiles_;
CefColorProviderTracker color_provider_tracker_{this};
base::WeakPtrFactory<ChromeBrowserFrame> weak_ptr_factory_{this};

View File

@ -9,6 +9,27 @@
#include "libcef/browser/views/widget_impl.h"
#include "libcef/browser/views/window_impl.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/themes/theme_service.h"
#include "chrome/browser/themes/theme_service_factory.h"
namespace {
ui::ColorProviderKey::SchemeVariant GetSchemeVariant(
ui::mojom::BrowserColorVariant color_variant) {
using BCV = ui::mojom::BrowserColorVariant;
using SV = ui::ColorProviderKey::SchemeVariant;
static constexpr auto kSchemeVariantMap = base::MakeFixedFlatMap<BCV, SV>({
{BCV::kTonalSpot, SV::kTonalSpot},
{BCV::kNeutral, SV::kNeutral},
{BCV::kVibrant, SV::kVibrant},
{BCV::kExpressive, SV::kExpressive},
});
return kSchemeVariantMap.at(color_variant);
}
} // namespace
// static
CefWidget* CefWidget::Create(CefWindowView* window_view) {
if (window_view->IsChromeStyle()) {
@ -30,3 +51,60 @@ CefWidget* CefWidget::GetForWidget(views::Widget* widget) {
}
return nullptr;
}
// static
ui::ColorProviderKey CefWidget::GetColorProviderKey(
const ui::ColorProviderKey& widget_key,
Profile* profile) {
// Based on BrowserFrame::GetColorProviderKey.
auto key = widget_key;
const auto* theme_service = ThemeServiceFactory::GetForProfile(profile);
CHECK(theme_service);
// color_mode.
[&key, theme_service]() {
const auto browser_color_scheme = theme_service->GetBrowserColorScheme();
if (browser_color_scheme != ThemeService::BrowserColorScheme::kSystem) {
key.color_mode =
browser_color_scheme == ThemeService::BrowserColorScheme::kLight
? ui::ColorProviderKey::ColorMode::kLight
: ui::ColorProviderKey::ColorMode::kDark;
}
}();
// user_color.
// Device theme retains the user_color from `Widget`.
if (!theme_service->UsingDeviceTheme()) {
if (theme_service->UsingAutogeneratedTheme()) {
key.user_color = theme_service->GetAutogeneratedThemeColor();
} else if (auto user_color = theme_service->GetUserColor()) {
key.user_color = user_color;
}
}
// user_color_source.
if (theme_service->UsingDeviceTheme()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kAccent;
} else if (theme_service->GetIsGrayscale()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kGrayscale;
} else if (theme_service->GetIsBaseline()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kBaseline;
} else {
CHECK(key.user_color.has_value());
key.user_color_source = ui::ColorProviderKey::UserColorSource::kAccent;
}
// scheme_variant.
ui::mojom::BrowserColorVariant color_variant =
theme_service->GetBrowserColorVariant();
if (!theme_service->UsingDeviceTheme() &&
color_variant != ui::mojom::BrowserColorVariant::kSystem) {
key.scheme_variant = GetSchemeVariant(color_variant);
}
// frame_type.
key.frame_type = ui::ColorProviderKey::FrameType::kNative;
return key;
}

View File

@ -6,6 +6,8 @@
#define CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_
#pragma once
#include "ui/color/color_provider_key.h"
class CefWindowView;
class Profile;
@ -42,7 +44,6 @@ class CefWidget {
// Track all Profiles associated with this Widget. Called from
// CefBrowserViewImpl::AddedToWidget and DisassociateFromWidget.
// |profile| is only used with the Alloy runtime.
virtual void AddAssociatedProfile(Profile* profile) = 0;
virtual void RemoveAssociatedProfile(Profile* profile) = 0;
@ -59,6 +60,10 @@ class CefWidget {
protected:
virtual ~CefWidget() = default;
static ui::ColorProviderKey GetColorProviderKey(
const ui::ColorProviderKey& widget_key,
Profile* profile);
};
#endif // CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_

View File

@ -16,23 +16,6 @@
#include "ui/linux/linux_ui.h"
#endif
namespace {
ui::ColorProviderKey::SchemeVariant GetSchemeVariant(
ui::mojom::BrowserColorVariant color_variant) {
using BCV = ui::mojom::BrowserColorVariant;
using SV = ui::ColorProviderKey::SchemeVariant;
static constexpr auto kSchemeVariantMap = base::MakeFixedFlatMap<BCV, SV>({
{BCV::kTonalSpot, SV::kTonalSpot},
{BCV::kNeutral, SV::kNeutral},
{BCV::kVibrant, SV::kVibrant},
{BCV::kExpressive, SV::kExpressive},
});
return kSchemeVariantMap.at(color_variant);
}
} // namespace
CefWidgetImpl::CefWidgetImpl(CefWindowView* window_view)
: window_view_(window_view) {}
@ -157,62 +140,11 @@ void CefWidgetImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
}
ui::ColorProviderKey CefWidgetImpl::GetColorProviderKey() const {
auto* profile = GetThemeProfile();
if (!profile) {
return Widget::GetColorProviderKey();
const auto& widget_key = Widget::GetColorProviderKey();
if (auto* profile = GetThemeProfile()) {
return CefWidget::GetColorProviderKey(widget_key, profile);
}
// Based on BrowserFrame::GetColorProviderKey.
auto key = Widget::GetColorProviderKey();
const auto* theme_service = ThemeServiceFactory::GetForProfile(profile);
CHECK(theme_service);
// color_mode.
[&key, theme_service]() {
const auto browser_color_scheme = theme_service->GetBrowserColorScheme();
if (browser_color_scheme != ThemeService::BrowserColorScheme::kSystem) {
key.color_mode =
browser_color_scheme == ThemeService::BrowserColorScheme::kLight
? ui::ColorProviderKey::ColorMode::kLight
: ui::ColorProviderKey::ColorMode::kDark;
}
}();
// user_color.
// Device theme retains the user_color from `Widget`.
if (!theme_service->UsingDeviceTheme()) {
if (theme_service->UsingAutogeneratedTheme()) {
key.user_color = theme_service->GetAutogeneratedThemeColor();
} else if (auto user_color = theme_service->GetUserColor()) {
key.user_color = user_color;
}
}
// user_color_source.
if (theme_service->UsingDeviceTheme()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kAccent;
} else if (theme_service->GetIsGrayscale()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kGrayscale;
} else if (theme_service->GetIsBaseline()) {
key.user_color_source = ui::ColorProviderKey::UserColorSource::kBaseline;
} else {
CHECK(key.user_color.has_value());
key.user_color_source = ui::ColorProviderKey::UserColorSource::kAccent;
}
// scheme_variant.
ui::mojom::BrowserColorVariant color_variant =
theme_service->GetBrowserColorVariant();
if (!theme_service->UsingDeviceTheme() &&
color_variant != ui::mojom::BrowserColorVariant::kSystem) {
key.scheme_variant = GetSchemeVariant(color_variant);
}
// frame_type.
key.frame_type = ui::ColorProviderKey::FrameType::kNative;
return key;
return widget_key;
}
void CefWidgetImpl::OnThemeChanged() {

View File

@ -233,7 +233,7 @@ index 0ccfe39eb5696..c9424316b6d14 100644
return gfx::Rect();
}
diff --git chrome/browser/ui/views/frame/browser_frame.cc chrome/browser/ui/views/frame/browser_frame.cc
index 8dd3620ba2720..ab3a92fd7ebe4 100644
index 8dd3620ba2720..e3bac8207de24 100644
--- chrome/browser/ui/views/frame/browser_frame.cc
+++ chrome/browser/ui/views/frame/browser_frame.cc
@@ -114,15 +114,23 @@ ui::ColorProviderKey::SchemeVariant GetSchemeVariant(
@ -332,16 +332,7 @@ index 8dd3620ba2720..ab3a92fd7ebe4 100644
chrome::SaveWindowWorkspace(browser_view_->browser(), GetWorkspace());
chrome::SaveWindowVisibleOnAllWorkspaces(browser_view_->browser(),
IsVisibleOnAllWorkspaces());
@@ -483,6 +515,8 @@ void BrowserFrame::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
ui::ColorProviderKey BrowserFrame::GetColorProviderKey() const {
auto key = Widget::GetColorProviderKey();
+ if (!browser_view_)
+ return key;
key.app_controller = browser_view_->browser()->app_controller();
@@ -572,6 +606,13 @@ void BrowserFrame::SelectNativeTheme() {
@@ -572,6 +604,13 @@ void BrowserFrame::SelectNativeTheme() {
return;
}
@ -355,7 +346,7 @@ index 8dd3620ba2720..ab3a92fd7ebe4 100644
// Ignore the system theme for web apps with window-controls-overlay as the
// display_override so the web contents can blend with the overlay by using
// the developer-provided theme color for a better experience. Context:
@@ -637,5 +678,8 @@ bool BrowserFrame::RegenerateFrameOnThemeChange(
@@ -637,5 +676,8 @@ bool BrowserFrame::RegenerateFrameOnThemeChange(
}
bool BrowserFrame::IsIncognitoBrowser() const {