diff --git a/UI/Gtk/Application.cpp b/UI/Gtk/Application.cpp index 39edd7380e..8b8f627cee 100644 --- a/UI/Gtk/Application.cpp +++ b/UI/Gtk/Application.cpp @@ -232,23 +232,26 @@ void Application::display_error_dialog(StringView error_message) const Dialogs::show_error(m_active_window->gtk_window(), error_message); } -// GDK4 only provides an async clipboard API. Spin a nested event loop to read synchronously. +// GDK4 only provides an async clipboard API. Spin the event loop until we get a response. static Optional read_clipboard_text_sync() { auto* clipboard = gdk_display_get_clipboard(gdk_display_get_default()); - Optional result; - Core::EventLoop nested_loop; + struct ClipboardReadTextResult { + bool done { false }; + Optional text; + } result; gdk_clipboard_read_text_async(clipboard, nullptr, [](GObject* source, GAsyncResult* async_result, gpointer user_data) { - auto* result_ptr = static_cast*>(user_data); + auto* result_ptr = static_cast(user_data); g_autofree char* text = gdk_clipboard_read_text_finish(GDK_CLIPBOARD(source), async_result, nullptr); if (text) - *result_ptr = ByteString(text); - Core::EventLoop::current().quit(0); }, &result); + result_ptr->text = ByteString(text); - nested_loop.exec(); - return result; + result_ptr->done = true; }, &result); + + Core::EventLoop::current().spin_until([&] { return result.done; }); + return result.text; } Utf16String Application::clipboard_text(ClipboardType) const