Skip to content

Commit

Permalink
views: Support themes for Alloy BrowserView in Chrome Window (see #3681)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
magreenblatt committed May 3, 2024
1 parent 91ba124 commit fae4a6f
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 90 deletions.
92 changes: 88 additions & 4 deletions libcef/browser/chrome/views/chrome_browser_frame.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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_) {
Expand Down
15 changes: 14 additions & 1 deletion libcef/browser/chrome/views/chrome_browser_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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_; }

Expand All @@ -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};
Expand Down
78 changes: 78 additions & 0 deletions libcef/browser/views/widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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;
}
7 changes: 6 additions & 1 deletion libcef/browser/views/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#define CEF_LIBCEF_BROWSER_VIEWS_WIDGET_H_
#pragma once

#include "ui/color/color_provider_key.h"

class CefWindowView;
class Profile;

Expand Down Expand Up @@ -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;

Expand All @@ -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_
76 changes: 4 additions & 72 deletions libcef/browser/views/widget_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down Expand Up @@ -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() {
Expand Down
Loading

0 comments on commit fae4a6f

Please sign in to comment.