mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
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:
@@ -8,6 +8,26 @@
|
||||
#include "cef/libcef/browser/views/view_util.h"
|
||||
#include "cef/libcef/browser/views/widget_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
|
||||
CefWidget* CefWidget::Create(CefWindowView* window_view) {
|
||||
@@ -30,3 +50,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;
|
||||
}
|
||||
|
@@ -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_
|
||||
|
@@ -15,23 +15,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) {}
|
||||
|
||||
@@ -156,62 +139,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() {
|
||||
|
Reference in New Issue
Block a user