From 7899e5a44d24541b2de8e4774f5c59c82c6b23d7 Mon Sep 17 00:00:00 2001 From: ZendyKim Date: Sun, 21 Jun 2026 21:39:53 +0900 Subject: [PATCH] feat: add WindowIdExt::with_window_handle for native window handle access MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds a backend-neutral way to get a window's native handle from a WindowId, for embedders that attach native children (e.g. a wry child webview via build_as_child) to a Floem window. with_window_handle(|handle| ...) runs a closure with the window's raw_window_handle::WindowHandle (raw-window-handle is already a dependency) and returns None if there's no live OS window. It wraps the existing internal with_window and mirrors the other WindowIdExt accessors (scale, is_visible, ...), so it does not expose Floem's windowing-backend (winit) types — matching the design goal discussed in #505. WindowHandle impls HasWindowHandle, so it plugs straight into APIs like wry's WebViewBuilder::build_as_child(&handle). --- src/window/id.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/window/id.rs b/src/window/id.rs index 191984bcb..e93af788d 100644 --- a/src/window/id.rs +++ b/src/window/id.rs @@ -216,6 +216,18 @@ pub trait WindowIdExt: WindowIdExtSealed { /// Get the dots-per-inch scaling of this window or 1.0 if the platform does not /// support it (Android). fn scale(&self) -> f64; + + /// Run a closure with this window's native [`raw_window_handle::WindowHandle`], returning the + /// closure's result, or `None` if there is no live OS window for this id (or the platform could + /// not produce a handle). + /// + /// This is a backend-neutral handle (`raw-window-handle` is a crate Floem already depends on), + /// so embedders can integrate native children — e.g. attaching a child webview via `wry`'s + /// `build_as_child` — against the parent window without Floem exposing its windowing-backend + /// types. The handle is only valid for the duration of `f`. + fn with_window_handle(&self, f: F) -> Option + where + F: FnOnce(raw_window_handle::WindowHandle<'_>) -> T; } impl WindowIdExt for WindowId { @@ -279,6 +291,17 @@ impl WindowIdExt for WindowId { fn scale(&self) -> f64 { with_window(self, |window| window.scale_factor()).unwrap_or(1.0) } + + fn with_window_handle(&self, f: F) -> Option + where + F: FnOnce(raw_window_handle::WindowHandle<'_>) -> T, + { + use raw_window_handle::HasWindowHandle; + // `Window: HasWindowHandle` (Floem already relies on this internally to build its render + // surface); hand the closure the resulting `WindowHandle`, which itself impls + // `HasWindowHandle` for use with e.g. `wry`'s `build_as_child`. + with_window(self, move |window| window.window_handle().ok().map(f)).flatten() + } } /// Called by `ApplicationHandle` at the end of the event loop callback.