Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/window/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F, T>(&self, f: F) -> Option<T>
where
F: FnOnce(raw_window_handle::WindowHandle<'_>) -> T;
}

impl WindowIdExt for WindowId {
Expand Down Expand Up @@ -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<F, T>(&self, f: F) -> Option<T>
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.
Expand Down
Loading