From dce09c9b2ac216e2bbcf7e3aebb25f910096b8b8 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 22 Apr 2024 17:43:14 +0200 Subject: [PATCH] Avoid crash on startup in webvew sample under Wayland The workaround for wxWebViewChromium added in f5e2af9a28 (Make wxWebViewChromium work with wxGTK3 and X11, 2023-09-02) broke wxWebViewWebKit under Wayland as calling wxYield() inside wxEVT_WEBVIEW_CREATED handler results in Error flushing display: Resource temporarily unavailable error from GTK and immediate application exit. This is almost certainly a GTK bug and should be fixed there, see https://gitlab.gnome.org/GNOME/gtk/-/issues/124, but for now make the workaround even uglier to avoid it. --- samples/webview/webview.cpp | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/samples/webview/webview.cpp b/samples/webview/webview.cpp index 41db3eb718..be0df35e6f 100644 --- a/samples/webview/webview.cpp +++ b/samples/webview/webview.cpp @@ -555,8 +555,11 @@ WebFrame::WebFrame(const wxString& url, int flags, wxWebViewWindowFeatures* wind wxWebView::GetBackendVersionInfo(backend).ToString()); // Chromium backend can't be used immediately after creation, so wait - // until the browser is created before calling GetUserAgent(). - m_browser->Bind(wxEVT_WEBVIEW_CREATED, [this](wxWebViewEvent& event) { + // until the browser is created before calling GetUserAgent(), but we + // can't do it unconditionally neither as doing it with WebViewGTK + // triggers https://gitlab.gnome.org/GNOME/gtk/-/issues/124 and just + // kills the sample. + const auto initShow = [this](){ wxLogMessage("Web view created, user agent is \"%s\"", m_browser->GetUserAgent()); // We need to synchronize this call with GetUserAgent() one, as @@ -565,9 +568,22 @@ WebFrame::WebFrame(const wxString& url, int flags, wxWebViewWindowFeatures* wind // order and we'd get the wrong user agent string back. if (!m_browser->AddScriptMessageHandler("wx")) wxLogError("Could not add script message handler"); + }; - event.Skip(); - }); +#if wxUSE_WEBVIEW_CHROMIUM + if ( backend == wxWebViewBackendChromium ) + { + m_browser->Bind(wxEVT_WEBVIEW_CREATED, [&initShow](wxWebViewEvent& event) { + initShow(); + + event.Skip(); + }); + } + else +#endif // wxUSE_WEBVIEW_CHROMIUM + { + initShow(); + } #ifndef __WXMAC__ //We register the wxfs:// protocol for testing purposes