views: Add support for OS and Chrome themes (fixes #3610, fixes #3671)

Controls now respect OS and Chrome themes by default for both Alloy
and Chrome runtimes. Chrome themes (mode and colors) can be configured
using the new CefRequestContext::SetChromeColorScheme method. Individual
theme colors can be overridden using the new CefWindowDelegate::
OnThemeColorsChanged and CefWindow::SetThemeColor methods.

The `--force-light-mode` and `--force-dark-mode` command-line flags are
now respected on all platforms as an override for the OS theme.

The current Chrome theme, if any, will take precedence over the OS theme
when determining light/dark status. On Windows and MacOS the titlebar
color will also be updated to match the light/dark theme.

Testable as follows:
- Run: `cefclient --enable-chrome-runtime` OR
       `cefclient --use-views --persist-user-preferences --cache-path=...`
  - App launches with default OS light/dark theme colors.
  - Change OS dark/light theme under system settings. Notice that theme
    colors change as expected.
  - Right click, select items from the new Theme sub-menu. Notice that
    theme colors behave as expected.
  - Exit and relaunch the app. Notice that the last-used theme colors are
    applied on app restart.
- Add `--background-color=green` to above command-line.
  - Perform the same actions as above. Notice that all controls start
    and remain green throughout (except some icons with Chrome runtime).
- Add `--force-light-mode` or `--force-dark-mode` to above command-line.
  - Perform the same actions as above. Notice that OS dark/light theme
    changes are ignored, but Chrome theme changes work as expected.
This commit is contained in:
Marshall Greenblatt
2024-03-29 12:48:33 -04:00
parent 29c21f58e8
commit f60476b848
97 changed files with 2976 additions and 206 deletions

View File

@ -384,7 +384,7 @@ index 2f0fe9d22667c..f50fe7bf75df3 100644
+#endif
}
diff --git chrome/browser/chrome_content_browser_client.cc chrome/browser/chrome_content_browser_client.cc
index 66b50cf0fd99b..f2e2cc47a513e 100644
index 66b50cf0fd99b..4b868cdf4c3ee 100644
--- chrome/browser/chrome_content_browser_client.cc
+++ chrome/browser/chrome_content_browser_client.cc
@@ -47,6 +47,7 @@
@ -416,7 +416,33 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
// static
void ChromeContentBrowserClient::RegisterLocalStatePrefs(
PrefRegistrySimple* registry) {
@@ -4375,9 +4383,11 @@ void ChromeContentBrowserClient::BrowserURLHandlerCreated(
@@ -3627,9 +3635,24 @@ bool UpdatePreferredColorScheme(WebPreferences* web_prefs,
: blink::mojom::PreferredColorScheme::kLight;
}
#else
+ auto preferred_color_scheme = native_theme->GetPreferredColorScheme();
+
+ auto* profile = Profile::FromBrowserContext(
+ web_contents->GetBrowserContext());
+ const auto* theme_service = ThemeServiceFactory::GetForProfile(profile);
+
+ const auto browser_color_scheme = theme_service->GetBrowserColorScheme();
+ if (browser_color_scheme != ThemeService::BrowserColorScheme::kSystem) {
+ // Override the native theme.
+ preferred_color_scheme =
+ browser_color_scheme == ThemeService::BrowserColorScheme::kLight
+ ? ui::NativeTheme::PreferredColorScheme::kLight
+ : ui::NativeTheme::PreferredColorScheme::kDark;
+ }
+
// Update based on native theme scheme.
web_prefs->preferred_color_scheme =
- ToBlinkPreferredColorScheme(native_theme->GetPreferredColorScheme());
+ ToBlinkPreferredColorScheme(preferred_color_scheme);
#endif // BUILDFLAG(IS_ANDROID)
// Reauth WebUI doesn't support dark mode yet because it shares the dialog
@@ -4375,9 +4398,11 @@ void ChromeContentBrowserClient::BrowserURLHandlerCreated(
&search::HandleNewTabURLReverseRewrite);
#endif // BUILDFLAG(IS_ANDROID)
@ -428,7 +454,7 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
}
base::FilePath ChromeContentBrowserClient::GetDefaultDownloadDirectory() {
@@ -6484,7 +6494,7 @@ void ChromeContentBrowserClient::OnNetworkServiceCreated(
@@ -6484,7 +6509,7 @@ void ChromeContentBrowserClient::OnNetworkServiceCreated(
#endif
}
@ -437,7 +463,7 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
content::BrowserContext* context,
bool in_memory,
const base::FilePath& relative_partition_path,
@@ -6502,6 +6512,8 @@ void ChromeContentBrowserClient::ConfigureNetworkContextParams(
@@ -6502,6 +6527,8 @@ void ChromeContentBrowserClient::ConfigureNetworkContextParams(
network_context_params->user_agent = GetUserAgentBasedOnPolicy(context);
network_context_params->accept_language = GetApplicationLocale();
}
@ -446,7 +472,7 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
}
std::vector<base::FilePath>
@@ -7627,10 +7639,10 @@ void ChromeContentBrowserClient::OnKeepaliveRequestStarted(
@@ -7627,10 +7654,10 @@ void ChromeContentBrowserClient::OnKeepaliveRequestStarted(
const auto now = base::TimeTicks::Now();
const auto timeout = GetKeepaliveTimerTimeout(context);
keepalive_deadline_ = std::max(keepalive_deadline_, now + timeout);
@ -459,7 +485,7 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
FROM_HERE, keepalive_deadline_ - now,
base::BindOnce(
&ChromeContentBrowserClient::OnKeepaliveTimerFired,
@@ -7649,7 +7661,8 @@ void ChromeContentBrowserClient::OnKeepaliveRequestFinished() {
@@ -7649,7 +7676,8 @@ void ChromeContentBrowserClient::OnKeepaliveRequestFinished() {
--num_keepalive_requests_;
if (num_keepalive_requests_ == 0) {
DVLOG(1) << "Stopping the keepalive timer";
@ -469,7 +495,7 @@ index 66b50cf0fd99b..f2e2cc47a513e 100644
// This deletes the keep alive handle attached to the timer function and
// unblock the shutdown sequence.
}
@@ -7789,7 +7802,7 @@ void ChromeContentBrowserClient::OnKeepaliveTimerFired(
@@ -7789,7 +7817,7 @@ void ChromeContentBrowserClient::OnKeepaliveTimerFired(
const auto now = base::TimeTicks::Now();
const auto then = keepalive_deadline_;
if (now < then) {