2021-02-23 21:08:33 +01:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "libcef/browser/views/view_util.h"
|
|
|
|
|
|
|
|
#include "ui/aura/window.h"
|
|
|
|
#include "ui/aura/window_tree_host.h"
|
2023-04-14 18:58:21 +02:00
|
|
|
#include "ui/views/view_constants_aura.h"
|
2023-02-28 19:34:12 +01:00
|
|
|
#include "ui/views/widget/native_widget.h"
|
|
|
|
#include "ui/views/widget/native_widget_delegate.h"
|
2021-02-23 21:08:33 +01:00
|
|
|
#include "ui/views/widget/widget.h"
|
|
|
|
|
2024-03-29 17:48:33 +01:00
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
#include <dwmapi.h>
|
|
|
|
|
|
|
|
#include "base/win/windows_version.h"
|
|
|
|
#include "ui/views/win/hwnd_util.h"
|
|
|
|
#endif
|
|
|
|
|
2021-02-23 21:08:33 +01:00
|
|
|
namespace view_util {
|
|
|
|
|
|
|
|
gfx::NativeWindow GetNativeWindow(views::Widget* widget) {
|
|
|
|
if (widget) {
|
2023-06-14 10:20:02 +02:00
|
|
|
return widget->GetNativeWindow();
|
2021-02-23 21:08:33 +01:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::NativeView GetNativeView(views::Widget* widget) {
|
2023-06-14 10:20:02 +02:00
|
|
|
if (widget) {
|
|
|
|
return widget->GetNativeView();
|
|
|
|
}
|
|
|
|
return nullptr;
|
2021-02-23 21:08:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CefWindowHandle GetWindowHandle(views::Widget* widget) {
|
|
|
|
// Same implementation as views::HWNDForView() but cross-platform.
|
|
|
|
if (widget) {
|
2022-04-19 19:45:03 +02:00
|
|
|
return GetWindowHandle(widget->GetNativeWindow());
|
|
|
|
}
|
|
|
|
return kNullWindowHandle;
|
|
|
|
}
|
|
|
|
|
|
|
|
CefWindowHandle GetWindowHandle(gfx::NativeWindow window) {
|
|
|
|
// |window| is an aura::Window*.
|
|
|
|
if (window && window->GetRootWindow()) {
|
|
|
|
return window->GetHost()->GetAcceleratedWidget();
|
2021-02-23 21:08:33 +01:00
|
|
|
}
|
|
|
|
return kNullWindowHandle;
|
|
|
|
}
|
|
|
|
|
2023-02-28 19:34:12 +01:00
|
|
|
views::NativeWidget* CreateNativeWidget(
|
|
|
|
views::internal::NativeWidgetDelegate* delegate,
|
2023-03-16 18:19:50 +01:00
|
|
|
CefRefPtr<CefWindow> window,
|
|
|
|
CefWindowDelegate* window_delegate) {
|
2023-02-28 19:34:12 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2023-04-14 18:58:21 +02:00
|
|
|
void SetHostView(views::Widget* widget, views::View* host_view) {
|
|
|
|
widget->GetNativeView()->SetProperty(views::kHostViewKey, host_view);
|
|
|
|
}
|
|
|
|
|
2024-03-29 17:48:33 +01:00
|
|
|
views::View* GetHostView(const views::Widget* widget) {
|
2023-04-14 18:58:21 +02:00
|
|
|
return widget->GetNativeView()->GetProperty(views::kHostViewKey);
|
|
|
|
}
|
|
|
|
|
2024-03-29 17:48:33 +01:00
|
|
|
void UpdateTitlebarTheme(views::Widget* widget) {
|
|
|
|
#if BUILDFLAG(IS_WIN)
|
|
|
|
// Value was 19 prior to Windows 10 20H1, according to
|
|
|
|
// https://stackoverflow.com/a/70693198
|
|
|
|
const DWORD dwAttribute =
|
|
|
|
base::win::GetVersion() >= base::win::Version::WIN10_20H1
|
|
|
|
? DWMWA_USE_IMMERSIVE_DARK_MODE
|
|
|
|
: 19;
|
|
|
|
|
|
|
|
const HWND widget_hwnd = views::HWNDForWidget(widget);
|
|
|
|
|
|
|
|
BOOL has_dark_titlebar = FALSE;
|
|
|
|
DwmGetWindowAttribute(widget_hwnd, dwAttribute, &has_dark_titlebar,
|
|
|
|
sizeof(has_dark_titlebar));
|
|
|
|
|
|
|
|
const BOOL dark_titlebar_enabled = ShouldUseDarkTheme(widget);
|
|
|
|
if (has_dark_titlebar == dark_titlebar_enabled) {
|
|
|
|
// No change required.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// From BrowserFrameViewWin::SetSystemMicaTitlebarAttributes.
|
|
|
|
DwmSetWindowAttribute(widget_hwnd, dwAttribute, &dark_titlebar_enabled,
|
|
|
|
sizeof(dark_titlebar_enabled));
|
|
|
|
|
|
|
|
// Repaint the titlebar if the Widget is visible. None of the usual NC
|
|
|
|
// repaint techniques work with DwmSetWindowAttribute so use a workaround
|
|
|
|
// that nudges the window width.
|
|
|
|
// See https://stackoverflow.com/a/78269906/23991994
|
|
|
|
if (IsWindowVisible(widget_hwnd) && !IsIconic(widget_hwnd)) {
|
|
|
|
RECT rect = {};
|
|
|
|
::GetWindowRect(widget_hwnd, &rect);
|
|
|
|
|
|
|
|
if (IsZoomed(widget_hwnd)) {
|
|
|
|
// Window is currently maximized. Restore and then re-maximize the
|
|
|
|
// window. The restore position is changed temporarily to make the
|
|
|
|
// update less noticeable.
|
|
|
|
WINDOWPLACEMENT placement = {};
|
|
|
|
GetWindowPlacement(widget_hwnd, &placement);
|
|
|
|
|
|
|
|
const RECT oldrect = placement.rcNormalPosition;
|
|
|
|
|
|
|
|
placement.rcNormalPosition = rect;
|
|
|
|
placement.rcNormalPosition.right -= 1;
|
|
|
|
SetWindowPlacement(widget_hwnd, &placement);
|
|
|
|
|
|
|
|
LockWindowUpdate(widget_hwnd);
|
|
|
|
ShowWindow(widget_hwnd, SW_SHOWNORMAL);
|
|
|
|
ShowWindow(widget_hwnd, SW_SHOWMAXIMIZED);
|
|
|
|
LockWindowUpdate(nullptr);
|
|
|
|
|
|
|
|
placement.rcNormalPosition = oldrect;
|
|
|
|
SetWindowPlacement(widget_hwnd, &placement);
|
|
|
|
} else {
|
|
|
|
// Window is currently normal. Change and then restore the window width.
|
|
|
|
// Use Defer functions to make the update less noticeable.
|
|
|
|
HDWP defer = BeginDeferWindowPos(2);
|
|
|
|
DeferWindowPos(defer, widget_hwnd, NULL, 0, 0, rect.right - rect.left - 1,
|
|
|
|
rect.bottom - rect.top,
|
|
|
|
SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
|
|
|
|
DeferWindowPos(defer, widget_hwnd, NULL, 0, 0, rect.right - rect.left,
|
|
|
|
rect.bottom - rect.top,
|
|
|
|
SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
|
|
|
|
LockWindowUpdate(widget_hwnd);
|
|
|
|
EndDeferWindowPos(defer);
|
|
|
|
LockWindowUpdate(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // BUILDFLAG(IS_WIN)
|
|
|
|
}
|
|
|
|
|
2021-02-23 21:08:33 +01:00
|
|
|
} // namespace view_util
|