views: mac: Support dynamic resize of title bar height (see #3189)

This is intended for usage with frameless windows that show the standard window
buttons, where resizing the title bar height changes the button offset. Returning a
different value from CefWindowDelegate::GetTitlebarHeight and forcing a resize of
the NSWindow's theme frame (see ViewsWindow::NudgeWindow) will update the
title bar height.

To test:
1. Run `cefclient --use-views --hide-frame --show-window-buttons --url=http://tests/window`
2. Enter a new value for title bar height and click the "Set Titlebar Height" button
This commit is contained in:
Nik Pavlov
2023-03-16 17:19:50 +00:00
committed by Marshall Greenblatt
parent 4eb0508671
commit 491d2383ef
19 changed files with 156 additions and 62 deletions

View File

@@ -6,15 +6,18 @@
#define CEF_LIBCEF_BROWSER_VIEWS_NATIVE_WIDGET_MAC_H_
#pragma once
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "include/internal/cef_ptr.h"
#include "ui/views/widget/native_widget_mac.h"
class CefWindow;
class CefWindowDelegate;
class CefNativeWidgetMac : public views::NativeWidgetMac {
public:
CefNativeWidgetMac(views::internal::NativeWidgetDelegate* delegate,
bool is_frameless,
bool with_window_buttons,
absl::optional<float> title_bar_height);
CefRefPtr<CefWindow> window,
CefWindowDelegate* window_delegate);
~CefNativeWidgetMac() override = default;
CefNativeWidgetMac(const CefNativeWidgetMac&) = delete;
@@ -29,9 +32,8 @@ class CefNativeWidgetMac : public views::NativeWidgetMac {
float* titlebar_height) override;
private:
const bool is_frameless_;
const bool with_window_buttons_;
const absl::optional<float> title_bar_height_;
const CefRefPtr<CefWindow> window_;
CefWindowDelegate* const window_delegate_;
};
#endif // CEF_LIBCEF_BROWSER_VIEWS_NATIVE_WIDGET_MAC_H_

View File

@@ -4,17 +4,17 @@
#include "libcef/browser/views/native_widget_mac.h"
#include "include/views/cef_window.h"
#include "include/views/cef_window_delegate.h"
#include "libcef/browser/views/ns_window.h"
CefNativeWidgetMac::CefNativeWidgetMac(
views::internal::NativeWidgetDelegate* delegate,
bool is_frameless,
bool with_window_buttons,
absl::optional<float> title_bar_height)
CefRefPtr<CefWindow> window,
CefWindowDelegate* window_delegate)
: views::NativeWidgetMac(delegate),
is_frameless_(is_frameless),
with_window_buttons_(with_window_buttons),
title_bar_height_(title_bar_height) {}
window_(window),
window_delegate_(window_delegate) {}
NativeWidgetMacNSWindow* CefNativeWidgetMac::CreateNSWindow(
const remote_cocoa::mojom::CreateWindowParams* params) {
@@ -22,15 +22,18 @@ NativeWidgetMacNSWindow* CefNativeWidgetMac::CreateNSWindow(
NSWindowStyleMaskTitled | NSWindowStyleMaskMiniaturizable |
NSWindowStyleMaskClosable | NSWindowStyleMaskResizable |
NSWindowStyleMaskTexturedBackground;
auto window = [[CefNSWindow alloc] initWithStyle:style_mask
isFrameless:is_frameless_];
if (is_frameless_) {
bool is_frameless = window_delegate_->IsFrameless(window_);
auto window = [[CefNSWindow alloc] initWithStyle:style_mask
isFrameless:is_frameless];
if (is_frameless) {
[window setTitlebarAppearsTransparent:YES];
[window setTitleVisibility:NSWindowTitleHidden];
}
if (!with_window_buttons_) {
if (!window_delegate_->WithStandardWindowButtons(window_)) {
[[window standardWindowButton:NSWindowCloseButton] setHidden:YES];
[[window standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
[[window standardWindowButton:NSWindowZoomButton] setHidden:YES];
@@ -42,9 +45,8 @@ NativeWidgetMacNSWindow* CefNativeWidgetMac::CreateNSWindow(
void CefNativeWidgetMac::GetWindowFrameTitlebarHeight(
bool* override_titlebar_height,
float* titlebar_height) {
if (title_bar_height_) {
if (window_delegate_->GetTitlebarHeight(window_, titlebar_height)) {
*override_titlebar_height = true;
*titlebar_height = title_bar_height_.value();
} else {
views::NativeWidgetMac::GetWindowFrameTitlebarHeight(
override_titlebar_height, titlebar_height);

View File

@@ -9,7 +9,6 @@
#include "include/views/cef_view.h"
#include "include/views/cef_window.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/views/view.h"
@@ -29,6 +28,8 @@ class NativeWidgetDelegate;
}
} // namespace views
class CefWindowDelegate;
#define CEF_REQUIRE_VALID_RETURN(ret) \
if (!ParentClass::IsValid()) \
return ret;
@@ -148,9 +149,8 @@ CefWindowHandle GetWindowHandle(gfx::NativeWindow window);
views::NativeWidget* CreateNativeWidget(
views::internal::NativeWidgetDelegate* delegate,
bool is_frameless,
bool with_window_buttons,
absl::optional<float> title_bar_height);
CefRefPtr<CefWindow> window,
CefWindowDelegate* window_delegate);
} // namespace view_util

View File

@@ -44,9 +44,8 @@ CefWindowHandle GetWindowHandle(gfx::NativeWindow window) {
views::NativeWidget* CreateNativeWidget(
views::internal::NativeWidgetDelegate* delegate,
bool is_frameless,
bool with_window_buttons,
absl::optional<float> title_bar_height) {
CefRefPtr<CefWindow> window,
CefWindowDelegate* window_delegate) {
return nullptr;
}

View File

@@ -47,10 +47,8 @@ CefWindowHandle GetWindowHandle(gfx::NativeWindow window) {
views::NativeWidget* CreateNativeWidget(
views::internal::NativeWidgetDelegate* delegate,
bool is_frameless,
bool with_window_buttons,
absl::optional<float> title_bar_height) {
return new CefNativeWidgetMac(delegate, is_frameless, with_window_buttons,
title_bar_height);
CefRefPtr<CefWindow> window,
CefWindowDelegate* window_delegate) {
return new CefNativeWidgetMac(delegate, window, window_delegate);
}
} // namespace view_util

View File

@@ -313,13 +313,8 @@ void CefWindowView::CreateWidget(gfx::AcceleratedWidget parent_widget) {
} else {
is_frameless_ = cef_delegate()->IsFrameless(cef_window);
const bool with_standard_buttons =
cef_delegate()->WithStandardWindowButtons(cef_window);
const auto title_bar_height = GetTitlebarHeight(cef_window);
params.native_widget = view_util::CreateNativeWidget(
widget, is_frameless_, with_standard_buttons, title_bar_height);
params.native_widget =
view_util::CreateNativeWidget(widget, cef_window, cef_delegate());
can_resize = cef_delegate()->CanResize(cef_window);
@@ -666,14 +661,3 @@ views::NonClientFrameView* CefWindowView::GetNonClientFrameView() const {
}
return widget->non_client_view()->frame_view();
}
absl::optional<float> CefWindowView::GetTitlebarHeight(
const CefRefPtr<CefWindow>& window) const {
float title_bar_height = 0;
const bool has_title_bar_height =
cef_delegate()->GetTitlebarHeight(window, &title_bar_height);
if (has_title_bar_height) {
return title_bar_height;
}
return absl::nullopt;
}

View File

@@ -14,7 +14,6 @@
#include "libcef/browser/views/overlay_view_host.h"
#include "libcef/browser/views/panel_view.h"
#include "third_party/abseil-cpp/absl/types/optional.h"
#include "third_party/skia/include/core/SkRegion.h"
#include "ui/display/display.h"
#include "ui/views/widget/widget_delegate.h"
@@ -120,9 +119,6 @@ class CefWindowView
void MoveOverlaysIfNecessary();
absl::optional<float> GetTitlebarHeight(
const CefRefPtr<CefWindow>& window) const;
// Not owned by this object.
Delegate* window_delegate_;