From 7aec5a7e62426b8aee615529d7845957f3702b8a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 5 Sep 2023 18:19:53 +0200 Subject: [PATCH] Implement browser window resizing correctly under Mac too While the window was somehow resized on its own under Mac (and only there) after the initial creation, it didn't have the correct size initially if we didn't give it to it, so provide a Mac-specific implementation of wxEVT_SIZE handler too, which fixes this and allows to remove an ugly Mac-specific workaround from the sample. --- include/wx/osx/private/webview_chromium.h | 9 +++++++++ samples/webview/webview.cpp | 9 +-------- src/common/webview_chromium.cpp | 5 ++--- src/osx/webview_chromium.mm | 8 ++++++++ 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/include/wx/osx/private/webview_chromium.h b/include/wx/osx/private/webview_chromium.h index 79fa14f1cb..736b0a1a79 100644 --- a/include/wx/osx/private/webview_chromium.h +++ b/include/wx/osx/private/webview_chromium.h @@ -10,7 +10,16 @@ #ifndef _WX_OSX_PRIVATE_WEBVIEW_CHROMIUM_H_ #define _WX_OSX_PRIVATE_WEBVIEW_CHROMIUM_H_ +wxGCC_WARNING_SUPPRESS(unused-parameter) + +#include "include/cef_base.h" + +wxGCC_WARNING_RESTORE(unused-parameter) + // Called during startup to add CefAppProtocol support to wxNSApplication. void wxWebViewChromium_InitOSX(); +// Called to resize the given NSView to fit its parent. +void wxWebViewChromium_Resize(cef_window_handle_t handle, wxSize size); + #endif // _WX_OSX_PRIVATE_WEBVIEW_CHROMIUM_H_ diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 2cfb7f4e14..4eb4e0553e 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -460,14 +460,7 @@ WebFrame::WebFrame(const wxString& url, bool isMain, wxWebViewWindowFeatures* wi m_browser->RegisterHandler(wxSharedPtr(new AdvancedWebViewHandler())); } #endif - if ( !m_browser->Create(this, wxID_ANY, url, wxDefaultPosition, -#if defined(wxWEBVIEW_SAMPLE_CHROMIUM) && defined(__WXOSX__) - // OSX implementation currently cannot handle the default size - wxSize(800, 600) -#else - wxDefaultSize -#endif - ) ) + if ( !m_browser->Create(this, wxID_ANY, url) ) { wxLogFatalError("Failed to create wxWebView"); } diff --git a/src/common/webview_chromium.cpp b/src/common/webview_chromium.cpp index 64f5a52e7b..9b5de15b1a 100644 --- a/src/common/webview_chromium.cpp +++ b/src/common/webview_chromium.cpp @@ -614,8 +614,6 @@ void wxWebViewChromium::OnSize(wxSizeEvent& event) { event.Skip(); - // Under Mac we don't have to do anything to resize the browser window. -#ifndef __WXOSX__ const auto handle = m_clientHandler ? m_clientHandler->GetWindowHandle() : 0; if ( !handle ) return; @@ -629,8 +627,9 @@ void wxWebViewChromium::OnSize(wxSizeEvent& event) size *= GetDPIScaleFactor(); ::XResizeWindow(wxGetX11Display(), handle, size.x, size.y); +#elif defined(__WXOSX__) + wxWebViewChromium_Resize(handle, size); #endif -#endif // !__WXOSX__ } void wxWebViewChromium::SetPageSource(const wxString& pageSource) diff --git a/src/osx/webview_chromium.mm b/src/osx/webview_chromium.mm index 38cab48bb9..8435fed950 100644 --- a/src/osx/webview_chromium.mm +++ b/src/osx/webview_chromium.mm @@ -13,6 +13,7 @@ #include #include "wx/log.h" +#include "wx/osx/private.h" #include "wx/osx/private/webview_chromium.h" #import "include/cef_application_mac.h" @@ -75,3 +76,10 @@ void wxWebViewChromium_InitOSX() wxLogError("Could not add setHandlingSendEvent impl"); } } + +void wxWebViewChromium_Resize(cef_window_handle_t handle, wxSize size) +{ + auto const view = static_cast(handle); + + [view setFrame:wxToNSRect([view superview], size)]; +}