cef/tests/cefclient/browser/views_style.cc

130 lines
4.4 KiB
C++
Raw Normal View History

// Copyright (c) 2017 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "tests/cefclient/browser/views_style.h"
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.
2024-03-29 17:48:33 +01:00
#include "include/cef_color_ids.h"
#include "include/views/cef_window.h"
#include "tests/cefclient/browser/main_context.h"
namespace client::views_style {
namespace {
cef_color_t g_background_color = 0;
cef_color_t g_background_hover_color = 0;
cef_color_t g_text_color = 0;
int GetShade(int component) {
return (component < 127) ? component + 75 : component - 75;
}
void MaybeInitialize() {
static bool initialized = false;
2023-01-02 23:59:03 +01:00
if (initialized) {
return;
2023-01-02 23:59:03 +01:00
}
g_background_color = MainContext::Get()->GetBackgroundColor();
if (g_background_color != 0) {
// Use a slightly modified shade of the background color for hover.
g_background_hover_color =
CefColorSetARGB(255, GetShade(CefColorGetR(g_background_color)),
GetShade(CefColorGetG(g_background_color)),
GetShade(CefColorGetB(g_background_color)));
// Invert the background color for text.
g_text_color = CefColorSetARGB(255, 255 - CefColorGetR(g_background_color),
255 - CefColorGetG(g_background_color),
255 - CefColorGetB(g_background_color));
}
initialized = true;
}
} // namespace
bool IsSet() {
MaybeInitialize();
return g_background_color != 0;
}
void ApplyTo(CefRefPtr<CefMenuModel> menu_model) {
2023-01-02 23:59:03 +01:00
if (!IsSet()) {
return;
2023-01-02 23:59:03 +01:00
}
// All text except non-hovered accelerator gets the same color.
menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT, g_text_color);
menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_HOVERED, g_text_color);
menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR,
g_background_hover_color);
menu_model->SetColorAt(-1, CEF_MENU_COLOR_TEXT_ACCELERATOR_HOVERED,
g_text_color);
menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND, g_background_color);
menu_model->SetColorAt(-1, CEF_MENU_COLOR_BACKGROUND_HOVERED,
g_background_hover_color);
// Recursively color sub-menus.
for (size_t i = 0; i < menu_model->GetCount(); ++i) {
2023-01-02 23:59:03 +01:00
if (menu_model->GetTypeAt(i) == MENUITEMTYPE_SUBMENU) {
ApplyTo(menu_model->GetSubMenuAt(i));
2023-01-02 23:59:03 +01:00
}
}
}
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.
2024-03-29 17:48:33 +01:00
void ApplyTo(CefRefPtr<CefWindow> window) {
if (!IsSet()) {
return;
}
// Customize default background color.
window->SetThemeColor(CEF_ColorPrimaryBackground, g_background_color);
// Customize default menu colors.
window->SetThemeColor(CEF_ColorMenuBackground, g_background_color);
window->SetThemeColor(CEF_ColorMenuItemBackgroundHighlighted,
g_background_hover_color);
window->SetThemeColor(CEF_ColorMenuItemBackgroundSelected,
g_background_hover_color);
window->SetThemeColor(CEF_ColorMenuSeparator, g_text_color);
window->SetThemeColor(CEF_ColorMenuItemForeground, g_text_color);
window->SetThemeColor(CEF_ColorMenuItemForegroundHighlighted, g_text_color);
window->SetThemeColor(CEF_ColorMenuItemForegroundSelected, g_text_color);
// Customize default textfield colors.
window->SetThemeColor(CEF_ColorTextfieldBackground, g_background_color);
window->SetThemeColor(CEF_ColorTextfieldOutline, g_text_color);
// Customize default Chrome toolbar colors.
window->SetThemeColor(CEF_ColorToolbar, g_background_color);
window->SetThemeColor(CEF_ColorToolbarText, g_text_color);
window->SetThemeColor(CEF_ColorToolbarButtonIcon, g_text_color);
window->SetThemeColor(CEF_ColorToolbarButtonText, g_text_color);
window->SetThemeColor(CEF_ColorLocationBarBackground, g_background_color);
window->SetThemeColor(CEF_ColorLocationBarBackgroundHovered,
g_background_hover_color);
window->SetThemeColor(CEF_ColorOmniboxText, g_text_color);
}
void OnThemeChanged(CefRefPtr<CefView> view) {
if (!IsSet()) {
return;
}
if (auto button = view->AsButton()) {
if (auto label_button = button->AsLabelButton()) {
// All text except disabled gets the same color.
label_button->SetEnabledTextColors(g_text_color);
label_button->SetTextColor(CEF_BUTTON_STATE_DISABLED,
g_background_hover_color);
}
} else if (auto textfield = view->AsTextfield()) {
textfield->SetTextColor(g_text_color);
}
}
} // namespace client::views_style