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.