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/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"
@@ -21,6 +22,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);
@@ -63,17 +68,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;
}
@@ -147,6 +206,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_) {