diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 8b78801a..9a373a30 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -24,6 +24,7 @@ pub use iced_futures as futures; pub use task::Task; pub use user_interface::UserInterface; +pub use window::Window; use crate::futures::futures::channel::oneshot; diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 057acb8f..a2d8b339 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -12,7 +12,7 @@ use crate::task::{self, Task}; pub use raw_window_handle; -use raw_window_handle::WindowHandle; +use raw_window_handle::{HasDisplayHandle, HasWindowHandle}; /// An operation to be performed on some window. pub enum Action { @@ -145,8 +145,8 @@ pub enum Action { /// said, it's usually in the same ballpark as on Windows. SetIcon(Id, Icon), - /// Runs the closure with the native window handle of the window with the given [`Id`]. - RunWithHandle(Id, Box) + Send>), + /// Runs the closure with a reference to the [`Window`] with the given [`Id`]. + Run(Id, Box), /// Screenshot the viewport of the window. Screenshot(Id, oneshot::Sender), @@ -182,6 +182,13 @@ pub enum Action { RelayoutAll, } +/// A window managed by iced. +/// +/// It implements both [`HasWindowHandle`] and [`HasDisplayHandle`]. +pub trait Window: HasWindowHandle + HasDisplayHandle {} + +impl Window for T where T: HasWindowHandle + HasDisplayHandle {} + /// Subscribes to the frames of the window of the running application. /// /// The resulting [`Subscription`] will produce items at a rate equal to the @@ -442,18 +449,18 @@ pub fn set_icon(id: Id, icon: Icon) -> Task { task::effect(crate::Action::Window(Action::SetIcon(id, icon))) } -/// Runs the given callback with the native window handle for the window with the given id. +/// Runs the given callback with a reference to the [`Window`] with the given [`Id`]. /// /// Note that if the window closes before this call is processed the callback will not be run. -pub fn run_with_handle( +pub fn run( id: Id, - f: impl FnOnce(WindowHandle<'_>) -> T + Send + 'static, + f: impl FnOnce(&dyn Window) -> T + Send + 'static, ) -> Task where T: Send + 'static, { task::oneshot(move |channel| { - crate::Action::Window(Action::RunWithHandle( + crate::Action::Window(Action::Run( id, Box::new(move |handle| { let _ = channel.send(f(handle)); diff --git a/winit/src/lib.rs b/winit/src/lib.rs index be819d3e..be7b7d26 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -1613,13 +1613,8 @@ fn run_action<'a, P, C>( let _ = channel.send(window.raw.id().into()); } } - window::Action::RunWithHandle(id, f) => { - use window::raw_window_handle::HasWindowHandle; - - if let Some(handle) = window_manager - .get_mut(id) - .and_then(|window| window.raw.window_handle().ok()) - { + window::Action::Run(id, f) => { + if let Some(handle) = window_manager.get_mut(id) { f(handle); } } diff --git a/winit/src/window.rs b/winit/src/window.rs index fe3929a2..43660496 100644 --- a/winit/src/window.rs +++ b/winit/src/window.rs @@ -17,6 +17,7 @@ use crate::core::{ }; use crate::graphics::Compositor; use crate::program::{self, Program}; +use crate::runtime::window::raw_window_handle; use winit::dpi::{LogicalPosition, LogicalSize}; use winit::monitor::MonitorHandle; @@ -299,6 +300,36 @@ where } } +impl raw_window_handle::HasWindowHandle for Window +where + P: Program, + C: Compositor, +{ + fn window_handle( + &self, + ) -> Result< + raw_window_handle::WindowHandle<'_>, + raw_window_handle::HandleError, + > { + self.raw.window_handle() + } +} + +impl raw_window_handle::HasDisplayHandle for Window +where + P: Program, + C: Compositor, +{ + fn display_handle( + &self, + ) -> Result< + raw_window_handle::DisplayHandle<'_>, + raw_window_handle::HandleError, + > { + self.raw.display_handle() + } +} + struct Preedit where Renderer: text::Renderer,