views: Use default theme background color for all controls (see #3671)

Add new CefViewDelegate::OnThemeChanged callback for optionally overriding
default theme colors when the current theme changes.
This commit is contained in:
Marshall Greenblatt
2024-03-22 19:51:00 -04:00
parent a13b6dc7f6
commit 19ba8b2b8d
43 changed files with 510 additions and 102 deletions

View File

@@ -124,9 +124,6 @@ MainContextImpl::MainContextImpl(CefRefPtr<CefCommandLine> command_line,
// Parse the background color value.
background_color_ =
ParseColor(command_line_->GetSwitchValue(switches::kBackgroundColor));
} else if (command_line_->HasSwitch("force-dark-mode")) {
// Use black background color by default with dark mode.
background_color_ = ParseColor("black");
}
if (background_color_ == 0 && !use_views_) {

View File

@@ -83,7 +83,6 @@ CefRefPtr<CefMenuModel> ViewsMenuBar::CreateMenuModel(const CefString& label,
CefRefPtr<CefMenuButton> button =
CefMenuButton::CreateMenuButton(this, label);
button->SetID(new_menu_id);
views_style::ApplyTo(button.get());
button->SetInkDropEnabled(true);
// Assign a group ID to allow focus traversal between MenuButtons using the
@@ -283,13 +282,17 @@ void ViewsMenuBar::MenuClosed(CefRefPtr<CefMenuModel> menu_model) {
}
}
void ViewsMenuBar::OnThemeChanged(CefRefPtr<CefView> view) {
// Apply colors when the theme changes.
views_style::ApplyTo(view);
}
void ViewsMenuBar::EnsureMenuPanel() {
if (panel_) {
return;
}
panel_ = CefPanel::CreatePanel(nullptr);
views_style::ApplyTo(panel_);
panel_ = CefPanel::CreatePanel(this);
// Use a horizontal box layout.
CefBoxLayoutSettings top_panel_layout_settings;

View File

@@ -21,7 +21,9 @@ namespace client {
// Implements a menu bar which is composed of CefMenuButtons positioned in a
// row with automatic switching between them via mouse/keyboard. All methods
// must be called on the browser process UI thread.
class ViewsMenuBar : public CefMenuButtonDelegate, public CefMenuModelDelegate {
class ViewsMenuBar : public CefMenuButtonDelegate,
public CefMenuModelDelegate,
public CefPanelDelegate {
public:
// Delegate methods will be called on the browser process UI thread.
class Delegate {
@@ -90,6 +92,9 @@ class ViewsMenuBar : public CefMenuButtonDelegate, public CefMenuModelDelegate {
bool is_rtl) override;
void MenuClosed(CefRefPtr<CefMenuModel> menu_model) override;
// CefViewDelegate methods:
void OnThemeChanged(CefRefPtr<CefView> view) override;
private:
// Creates the menu panel if it doesn't already exist.
void EnsureMenuPanel();

View File

@@ -96,8 +96,7 @@ void ViewsOverlayControls::Initialize(CefRefPtr<CefWindow> window,
// that we can't use a transparent background because subpixel text
// rendering will break.
// See comments on the related DCHECK in Label::PaintText.
panel_ = CefPanel::CreatePanel(nullptr);
views_style::ApplyTo(panel_);
panel_ = CefPanel::CreatePanel(this);
// Use a horizontal box layout.
CefBoxLayoutSettings panel_layout_settings;
@@ -226,11 +225,15 @@ void ViewsOverlayControls::OnButtonPressed(CefRefPtr<CefButton> button) {
}
}
void ViewsOverlayControls::OnThemeChanged(CefRefPtr<CefView> view) {
// Apply colors when the theme changes.
views_style::ApplyTo(view);
}
CefRefPtr<CefLabelButton> ViewsOverlayControls::CreateButton(Command command) {
CefRefPtr<CefLabelButton> button = CefLabelButton::CreateLabelButton(
this, GetLabel(command, window_maximized_));
button->SetID(static_cast<int>(command));
views_style::ApplyTo(button);
button->SetInkDropEnabled(true);
button->SetFocusable(false); // Don't give focus to the button.
return button;

View File

@@ -17,7 +17,7 @@ namespace client {
// Implements window overlay controls that receive absolute positioning on top
// of the browser view. All methods must be called on the browser process UI
// thread.
class ViewsOverlayControls : public CefButtonDelegate {
class ViewsOverlayControls : public CefButtonDelegate, public CefPanelDelegate {
public:
enum class Command {
kMinimize = 1,
@@ -43,6 +43,9 @@ class ViewsOverlayControls : public CefButtonDelegate {
// CefButtonDelegate methods:
void OnButtonPressed(CefRefPtr<CefButton> button) override;
// CefViewDelegate methods:
void OnThemeChanged(CefRefPtr<CefView> view) override;
CefRefPtr<CefLabelButton> CreateButton(Command command);
void MaybeUpdateMaximizeButton();

View File

@@ -48,44 +48,6 @@ bool IsSet() {
return g_background_color != 0;
}
void ApplyBackgroundTo(CefRefPtr<CefView> view) {
if (!IsSet()) {
return;
}
view->SetBackgroundColor(g_background_color);
}
void ApplyTo(CefRefPtr<CefPanel> panel) {
if (!IsSet()) {
return;
}
panel->SetBackgroundColor(g_background_color);
}
void ApplyTo(CefRefPtr<CefLabelButton> label_button) {
if (!IsSet()) {
return;
}
// 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);
label_button->SetBackgroundColor(g_background_color);
}
void ApplyTo(CefRefPtr<CefTextfield> textfield) {
if (!IsSet()) {
return;
}
textfield->SetBackgroundColor(g_background_color);
textfield->SetTextColor(g_text_color);
}
void ApplyTo(CefRefPtr<CefMenuModel> menu_model) {
if (!IsSet()) {
return;
@@ -111,4 +73,23 @@ void ApplyTo(CefRefPtr<CefMenuModel> menu_model) {
}
}
void ApplyTo(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);
}
view->SetBackgroundColor(g_background_color);
}
} // namespace client::views_style

View File

@@ -17,11 +17,8 @@ namespace client::views_style {
bool IsSet();
// Apply style to views objects.
void ApplyBackgroundTo(CefRefPtr<CefView> view);
void ApplyTo(CefRefPtr<CefPanel> panel);
void ApplyTo(CefRefPtr<CefLabelButton> label_button);
void ApplyTo(CefRefPtr<CefTextfield> textfield);
void ApplyTo(CefRefPtr<CefMenuModel> menu_model);
void ApplyTo(CefRefPtr<CefView> view);
} // namespace client::views_style

View File

@@ -1060,6 +1060,11 @@ void ViewsWindow::OnLayoutChanged(CefRefPtr<CefView> view,
}
}
void ViewsWindow::OnThemeChanged(CefRefPtr<CefView> view) {
// Apply colors when the theme changes.
views_style::ApplyTo(view);
}
void ViewsWindow::MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model,
int command_id,
cef_event_flags_t event_flags) {
@@ -1157,7 +1162,6 @@ CefRefPtr<CefLabelButton> ViewsWindow::CreateBrowseButton(
CefRefPtr<CefLabelButton> button =
CefLabelButton::CreateLabelButton(this, label);
button->SetID(id);
views_style::ApplyTo(button.get());
button->SetInkDropEnabled(true);
button->SetEnabled(false); // Disabled by default.
button->SetFocusable(false); // Don't give focus to the button.
@@ -1173,7 +1177,6 @@ CefRefPtr<CefMenuButton> ViewsWindow::CreateMenuButton() {
menu_button_->SetImage(
CEF_BUTTON_STATE_NORMAL,
delegate_->GetImageCache()->GetCachedImage("menu_icon"));
views_style::ApplyTo(menu_button_.get());
menu_button_->SetInkDropEnabled(true);
// Override the default minimum size.
menu_button_->SetMinimumSize(CefSize(0, 0));
@@ -1186,14 +1189,12 @@ CefRefPtr<CefView> ViewsWindow::CreateLocationBar() {
// Chrome will provide a minimal location bar.
location_bar_ = browser_view_->GetChromeToolbar();
DCHECK(location_bar_);
views_style::ApplyBackgroundTo(location_bar_);
}
if (!location_bar_) {
// Create the URL textfield.
CefRefPtr<CefTextfield> url_textfield = CefTextfield::CreateTextfield(this);
url_textfield->SetID(ID_URL_TEXTFIELD);
url_textfield->SetEnabled(false); // Disabled by default.
views_style::ApplyTo(url_textfield);
location_bar_ = url_textfield;
}
return location_bar_;
@@ -1264,7 +1265,6 @@ void ViewsWindow::AddControls() {
panel->AddChildView(extensions_panel_);
panel->AddChildView(menu_button_);
views_style::ApplyTo(panel);
// Allow |location| to grow and fill any remaining space.
panel_layout->SetFlexForView(location_bar_, 1);
@@ -1405,7 +1405,6 @@ void ViewsWindow::UpdateExtensionControls() {
CefMenuButton::CreateMenuButton(this, CefString());
button->SetID(id);
button->SetImage(CEF_BUTTON_STATE_NORMAL, (*it).image_);
views_style::ApplyTo(button.get());
button->SetInkDropEnabled(true);
// Override the default minimum size.
button->SetMinimumSize(CefSize(0, 0));

View File

@@ -202,6 +202,7 @@ class ViewsWindow : public CefBrowserViewDelegate,
void OnWindowChanged(CefRefPtr<CefView> view, bool added) override;
void OnLayoutChanged(CefRefPtr<CefView> view,
const CefRect& new_bounds) override;
void OnThemeChanged(CefRefPtr<CefView> view) override;
// ViewsMenuBar::Delegate methods:
void MenuBarExecuteCommand(CefRefPtr<CefMenuModel> menu_model,

View File

@@ -56,7 +56,6 @@ void CreatePanel(CefRefPtr<CefPanelDelegate> delegate) {
EXPECT_TRUE(panel->IsEnabled());
EXPECT_FALSE(panel->IsFocusable());
EXPECT_FALSE(panel->IsAccessibilityFocusable());
EXPECT_EQ(CefColorSetARGB(255, 255, 255, 255), panel->GetBackgroundColor());
// Verify default Panel state.
EXPECT_TRUE(panel->GetLayout().get());