cef/libcef/browser/chrome/views/chrome_browser_view.cc

102 lines
3.3 KiB
C++
Raw Normal View History

// Copyright 2021 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 "libcef/browser/chrome/views/chrome_browser_view.h"
#include "libcef/browser/chrome/views/chrome_browser_frame.h"
#include "libcef/browser/views/browser_view_impl.h"
ChromeBrowserView::ChromeBrowserView(CefBrowserViewImpl* cef_browser_view)
: ParentClass(cef_browser_view->delegate()),
cef_browser_view_(cef_browser_view) {}
void ChromeBrowserView::InitBrowser(std::unique_ptr<Browser> browser) {
DCHECK(!web_view_);
// Initialize the BrowserFrame and BrowserView.
auto chrome_widget = static_cast<ChromeBrowserFrame*>(GetWidget());
chrome_widget->Init(this, std::move(browser));
// Retrieve the views::WebView that was created by the above initializations.
web_view_ = cef_browser_view_->web_view();
DCHECK(web_view_);
ParentClass::AddedToWidget();
}
void ChromeBrowserView::Destroyed() {
DCHECK(!destroyed_);
destroyed_ = true;
web_view_ = nullptr;
}
void ChromeBrowserView::ViewHierarchyChanged(
const views::ViewHierarchyChangedDetails& details) {
ParentClass::ViewHierarchyChanged(details);
if (details.is_add && details.child == this) {
gfx::Size size = GetPreferredSize();
if (size.IsEmpty()) {
// No size was provided for this View. Size it to the parent by default
// or, depending on the Layout, the browser may be initially 0x0 size and
// will not display until the parent is next resized (resulting in a call
// to WebView::OnBoundsChanged). For example, this can happen when adding
// this View to a CefWindow with FillLayout and then calling
// CefWindow::Show() without first resizing the CefWindow.
size = details.parent->GetPreferredSize();
2023-01-02 23:59:03 +01:00
if (!size.IsEmpty()) {
SetSize(size);
2023-01-02 23:59:03 +01:00
}
}
}
}
void ChromeBrowserView::AddedToWidget() {
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
// Create the Browser and ChromeBrowserHostImpl.
// Results in a call to InitBrowser which calls ParentClass::AddedToWidget.
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
cef_browser_view_->AddedToWidget();
}
void ChromeBrowserView::RemovedFromWidget() {
ParentClass::RemovedFromWidget();
cef_browser_view_->RemovedFromWidget();
}
void ChromeBrowserView::OnBoundsChanged(const gfx::Rect& previous_bounds) {
ParentClass::OnBoundsChanged(previous_bounds);
cef_browser_view_->OnBoundsChanged();
}
void ChromeBrowserView::OnGestureEvent(ui::GestureEvent* event) {
if (cef_browser_view_->OnGestureEvent(event)) {
return;
}
ParentClass::OnGestureEvent(event);
}
ToolbarView* ChromeBrowserView::OverrideCreateToolbar() {
if (cef_delegate()) {
auto toolbar_type = cef_delegate()->GetChromeToolbarType(cef_browser_view_);
std::optional<ToolbarView::DisplayMode> display_mode;
switch (toolbar_type) {
case CEF_CTT_NORMAL:
display_mode = ToolbarView::DisplayMode::NORMAL;
break;
case CEF_CTT_LOCATION:
display_mode = ToolbarView::DisplayMode::LOCATION;
break;
default:
break;
}
if (display_mode) {
cef_toolbar_ =
CefToolbarViewImpl::Create(nullptr, browser(), this, display_mode);
// Ownership will be taken by BrowserView.
view_util::PassOwnership(cef_toolbar_).release();
return cef_toolbar_->root_view();
}
}
return nullptr;
}