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 b92749a58a
commit 7328b9e40d
6 changed files with 192 additions and 90 deletions

View File

@ -8,6 +8,7 @@
#include "cef/libcef/browser/thread_util.h" #include "cef/libcef/browser/thread_util.h"
#include "cef/libcef/browser/views/window_view.h" #include "cef/libcef/browser/views/window_view.h"
#include "chrome/browser/themes/theme_service.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.h"
#include "chrome/browser/ui/browser_commands.h" #include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/views/frame/browser_view.h" #include "chrome/browser/ui/views/frame/browser_view.h"
@ -21,6 +22,10 @@
ChromeBrowserFrame::ChromeBrowserFrame(CefWindowView* window_view) ChromeBrowserFrame::ChromeBrowserFrame(CefWindowView* window_view)
: window_view_(window_view) {} : window_view_(window_view) {}
ChromeBrowserFrame::~ChromeBrowserFrame() {
DCHECK(associated_profiles_.empty());
}
void ChromeBrowserFrame::Init(BrowserView* browser_view, void ChromeBrowserFrame::Init(BrowserView* browser_view,
std::unique_ptr<Browser> browser) { std::unique_ptr<Browser> browser) {
DCHECK(browser_view); DCHECK(browser_view);
@ -63,17 +68,71 @@ void ChromeBrowserFrame::Initialized() {
#endif #endif
} }
void ChromeBrowserFrame::AddAssociatedProfile(Profile* /*profile*/) { void ChromeBrowserFrame::AddAssociatedProfile(Profile* profile) {
// Calls ThemeChanged(). DCHECK(profile);
UserChangedTheme(BrowserThemeChangeType::kBrowserTheme);
// 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 { Profile* ChromeBrowserFrame::GetThemeProfile() const {
// Always prefer the Browser Profile, if any.
if (browser_view_) { if (browser_view_) {
return browser_view_->GetProfile(); return browser_view_->GetProfile();
} }
if (!associated_profiles_.empty()) {
return associated_profiles_.begin()->first;
}
return nullptr; return nullptr;
} }
@ -147,6 +206,31 @@ void ChromeBrowserFrame::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
native_theme_change_ = false; 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() { void ChromeBrowserFrame::OnColorProviderCacheResetMissed() {
// Ignore calls during Widget::Init(). // Ignore calls during Widget::Init().
if (!initialized_) { if (!initialized_) {

View File

@ -6,9 +6,12 @@
#define CEF_LIBCEF_BROWSER_CHROME_VIEWS_CHROME_BROWSER_FRAME_H_ #define CEF_LIBCEF_BROWSER_CHROME_VIEWS_CHROME_BROWSER_FRAME_H_
#pragma once #pragma once
#include <map>
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
#include "cef/libcef/browser/views/color_provider_tracker.h" #include "cef/libcef/browser/views/color_provider_tracker.h"
#include "cef/libcef/browser/views/widget.h" #include "cef/libcef/browser/views/widget.h"
#include "chrome/browser/themes/theme_service_observer.h"
#include "chrome/browser/ui/browser.h" #include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/frame/browser_frame.h" #include "chrome/browser/ui/views/frame/browser_frame.h"
@ -95,9 +98,11 @@ class CefWindowView;
// CefWindowView::CreateWidget() when the Chrome runtime is enabled. // CefWindowView::CreateWidget() when the Chrome runtime is enabled.
class ChromeBrowserFrame : public BrowserFrame, class ChromeBrowserFrame : public BrowserFrame,
public CefWidget, public CefWidget,
public CefColorProviderTracker::Observer { public CefColorProviderTracker::Observer,
public ThemeServiceObserver {
public: public:
explicit ChromeBrowserFrame(CefWindowView* window_view); explicit ChromeBrowserFrame(CefWindowView* window_view);
~ChromeBrowserFrame() override;
ChromeBrowserFrame(const ChromeBrowserFrame&) = delete; ChromeBrowserFrame(const ChromeBrowserFrame&) = delete;
ChromeBrowserFrame& operator=(const ChromeBrowserFrame&) = delete; ChromeBrowserFrame& operator=(const ChromeBrowserFrame&) = delete;
@ -130,6 +135,10 @@ class ChromeBrowserFrame : public BrowserFrame,
// ui::NativeThemeObserver methods: // ui::NativeThemeObserver methods:
void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override; void OnNativeThemeUpdated(ui::NativeTheme* observed_theme) override;
ui::ColorProviderKey GetColorProviderKey() const override;
// ThemeServiceObserver methods:
void OnThemeChanged() override;
BrowserView* browser_view() const { return browser_view_; } BrowserView* browser_view() const { return browser_view_; }
@ -145,6 +154,10 @@ class ChromeBrowserFrame : public BrowserFrame,
bool initialized_ = false; bool initialized_ = false;
bool native_theme_change_ = 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}; CefColorProviderTracker color_provider_tracker_{this};
base::WeakPtrFactory<ChromeBrowserFrame> weak_ptr_factory_{this}; base::WeakPtrFactory<ChromeBrowserFrame> weak_ptr_factory_{this};

View File

@ -8,6 +8,26 @@
#include "cef/libcef/browser/views/view_util.h" #include "cef/libcef/browser/views/view_util.h"
#include "cef/libcef/browser/views/widget_impl.h" #include "cef/libcef/browser/views/widget_impl.h"
#include "cef/libcef/browser/views/window_impl.h" #include "cef/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 // static
CefWidget* CefWidget::Create(CefWindowView* window_view) { CefWidget* CefWidget::Create(CefWindowView* window_view) {
@ -30,3 +50,60 @@ CefWidget* CefWidget::GetForWidget(views::Widget* widget) {
} }
return nullptr; 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_ #define CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_
#pragma once #pragma once
#include "ui/color/color_provider_key.h"
class CefWindowView; class CefWindowView;
class Profile; class Profile;
@ -42,7 +44,6 @@ class CefWidget {
// Track all Profiles associated with this Widget. Called from // Track all Profiles associated with this Widget. Called from
// CefBrowserViewImpl::AddedToWidget and DisassociateFromWidget. // CefBrowserViewImpl::AddedToWidget and DisassociateFromWidget.
// |profile| is only used with the Alloy runtime.
virtual void AddAssociatedProfile(Profile* profile) = 0; virtual void AddAssociatedProfile(Profile* profile) = 0;
virtual void RemoveAssociatedProfile(Profile* profile) = 0; virtual void RemoveAssociatedProfile(Profile* profile) = 0;
@ -59,6 +60,10 @@ class CefWidget {
protected: protected:
virtual ~CefWidget() = default; virtual ~CefWidget() = default;
static ui::ColorProviderKey GetColorProviderKey(
const ui::ColorProviderKey& widget_key,
Profile* profile);
}; };
#endif // CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_ #endif // CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_

View File

@ -15,23 +15,6 @@
#include "ui/linux/linux_ui.h" #include "ui/linux/linux_ui.h"
#endif #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) CefWidgetImpl::CefWidgetImpl(CefWindowView* window_view)
: window_view_(window_view) {} : window_view_(window_view) {}
@ -156,62 +139,11 @@ void CefWidgetImpl::OnNativeThemeUpdated(ui::NativeTheme* observed_theme) {
} }
ui::ColorProviderKey CefWidgetImpl::GetColorProviderKey() const { ui::ColorProviderKey CefWidgetImpl::GetColorProviderKey() const {
auto* profile = GetThemeProfile(); const auto& widget_key = Widget::GetColorProviderKey();
if (!profile) { if (auto* profile = GetThemeProfile()) {
return Widget::GetColorProviderKey(); return CefWidget::GetColorProviderKey(widget_key, profile);
} }
return widget_key;
// 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;
} }
void CefWidgetImpl::OnThemeChanged() { void CefWidgetImpl::OnThemeChanged() {

View File

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