mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-02-22 23:19:09 +01:00
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.
95 lines
3.4 KiB
C++
95 lines
3.4 KiB
C++
// Copyright 2016 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.
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|
|
#define CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|
|
#pragma once
|
|
|
|
#include "include/views/cef_button_delegate.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
|
#include "libcef/browser/views/view_view.h"
|
|
|
|
#include "base/logging.h"
|
|
#include "ui/gfx/color_utils.h"
|
|
#include "ui/views/animation/ink_drop.h"
|
|
#include "ui/views/controls/button/button.h"
|
|
|
|
// Helpers for template boiler-plate.
|
|
#define CEF_BUTTON_VIEW_T CEF_VIEW_VIEW_T
|
|
#define CEF_BUTTON_VIEW_A CEF_VIEW_VIEW_A
|
|
#define CEF_BUTTON_VIEW_D CefButtonView<CEF_BUTTON_VIEW_A>
|
|
|
|
// Template for implementing views::Button-derived classes. The
|
|
// views::Button-derived type passed to this template must extend
|
|
// views::ButtonListener (for example, see LabelButtonEx from
|
|
// basic_label_button_view.h). See comments in view_impl.h for a usage overview.
|
|
CEF_BUTTON_VIEW_T class CefButtonView : public CEF_VIEW_VIEW_D {
|
|
public:
|
|
using ParentClass = CEF_VIEW_VIEW_D;
|
|
|
|
// |cef_delegate| may be nullptr.
|
|
template <typename... Args>
|
|
explicit CefButtonView(CefViewDelegateClass* cef_delegate, Args... args)
|
|
: ParentClass(cef_delegate, args...) {}
|
|
|
|
// Returns the CefButton associated with this view. See comments on
|
|
// CefViewView::GetCefView.
|
|
CefRefPtr<CefButton> GetCefButton() const {
|
|
CefRefPtr<CefButton> button = ParentClass::GetCefView()->AsButton();
|
|
DCHECK(button);
|
|
return button;
|
|
}
|
|
|
|
// views::View methods:
|
|
void OnThemeChanged() override;
|
|
|
|
// views::Button methods:
|
|
void StateChanged(views::Button::ButtonState old_state) override;
|
|
|
|
// LabelButtonEx methods:
|
|
void ButtonPressed(const ui::Event& event) override;
|
|
};
|
|
|
|
CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::OnThemeChanged() {
|
|
ParentClass::OnThemeChanged();
|
|
|
|
auto* inkdrop = views::InkDrop::Get(this);
|
|
if (inkdrop->ink_drop_mode() != views::InkDropHost::InkDropMode::OFF) {
|
|
// Never returns an empty value.
|
|
const auto& color =
|
|
view_util::GetBackgroundColor(this, /*allow_transparent=*/false);
|
|
inkdrop->SetBaseColor(color_utils::BlendTowardMaxContrast(*color, 0x61));
|
|
}
|
|
}
|
|
|
|
CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::StateChanged(
|
|
views::Button::ButtonState old_state) {
|
|
ParentClass::StateChanged(old_state);
|
|
if (ParentClass::cef_delegate()) {
|
|
ParentClass::cef_delegate()->OnButtonStateChanged(GetCefButton());
|
|
}
|
|
}
|
|
|
|
CEF_BUTTON_VIEW_T void CEF_BUTTON_VIEW_D::ButtonPressed(
|
|
const ui::Event& event) {
|
|
// Callback may trigger new animation state.
|
|
if (ParentClass::cef_delegate()) {
|
|
ParentClass::cef_delegate()->OnButtonPressed(GetCefButton());
|
|
}
|
|
if (views::InkDrop::Get(this)->ink_drop_mode() !=
|
|
views::InkDropHost::InkDropMode::OFF &&
|
|
!ParentClass::IsFocusable() &&
|
|
ParentClass::GetState() != views::Button::STATE_PRESSED) {
|
|
// Ink drop state does not get reset properly on click when the button is
|
|
// non-focusable. Reset the ink drop state here if the state has not been
|
|
// explicitly set to pressed by the OnButtonPressed callback calling
|
|
// SetState (which also sets the ink drop state).
|
|
views::InkDrop::Get(this)->AnimateToState(
|
|
views::InkDropState::HIDDEN, ui::LocatedEvent::FromIfValid(&event));
|
|
}
|
|
}
|
|
|
|
#endif // CEF_LIBCEF_BROWSER_VIEWS_BUTTON_VIEW_H_
|