diff --git a/CHANGELOG.md b/CHANGELOG.md index 28c2fcaac6..c62ab53117 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Cut functionality for `TextEditor`. [#2215](https://github.com/iced-rs/iced/pull/2215) - Disabled support for `Checkbox`. [#2109](https://github.com/iced-rs/iced/pull/2109) - `skip_taskbar` window setting for Windows. [#2211](https://github.com/iced-rs/iced/pull/2211) -- `fetch_maximized` and `fetch_minimized` window commands. [#2189](https://github.com/iced-rs/iced/pull/2189) +- `fetch_maximized` and `fetch_minimized` commands in `window`. [#2189](https://github.com/iced-rs/iced/pull/2189) +- `run_with_handle` command in `window`. [#2200](https://github.com/iced-rs/iced/pull/2200) - `text_shaping` method for `Tooltip`. [#2172](https://github.com/iced-rs/iced/pull/2172) - `hovered` styling for `Svg` widget. [#2163](https://github.com/iced-rs/iced/pull/2163) - Customizable style for `TextEditor`. [#2159](https://github.com/iced-rs/iced/pull/2159) diff --git a/Cargo.toml b/Cargo.toml index c9dee6b75d..b1fa863491 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -156,7 +156,7 @@ xxhash-rust = { version = "0.8", features = ["xxh3"] } unicode-segmentation = "1.0" wasm-bindgen-futures = "0.4" wasm-timer = "0.2" -web-sys = "0.3" +web-sys = "=0.3.67" web-time = "0.2" wgpu = "0.19" winapi = "0.3" diff --git a/runtime/Cargo.toml b/runtime/Cargo.toml index 8089d545a1..3a47a971ae 100644 --- a/runtime/Cargo.toml +++ b/runtime/Cargo.toml @@ -20,3 +20,4 @@ iced_futures.workspace = true iced_futures.features = ["thread-pool"] thiserror.workspace = true +raw-window-handle.workspace = true diff --git a/runtime/src/window.rs b/runtime/src/window.rs index 2136d64dcf..4d97d5eec9 100644 --- a/runtime/src/window.rs +++ b/runtime/src/window.rs @@ -15,6 +15,10 @@ use crate::core::{Point, Size}; use crate::futures::event; use crate::futures::Subscription; +pub use raw_window_handle; + +use raw_window_handle::WindowHandle; + /// Subscribes to the frames of the window of the running application. /// /// The resulting [`Subscription`] will produce items at a rate equal to the @@ -170,6 +174,19 @@ pub fn change_icon(id: Id, icon: Icon) -> Command { Command::single(command::Action::Window(Action::ChangeIcon(id, icon))) } +/// Runs the given callback with the native window handle for 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( + id: Id, + f: impl FnOnce(&WindowHandle<'_>) -> Message + 'static, +) -> Command { + Command::single(command::Action::Window(Action::RunWithHandle( + id, + Box::new(f), + ))) +} + /// Captures a [`Screenshot`] from the window. pub fn screenshot( id: Id, diff --git a/runtime/src/window/action.rs b/runtime/src/window/action.rs index 8b5325692c..86d5852880 100644 --- a/runtime/src/window/action.rs +++ b/runtime/src/window/action.rs @@ -3,6 +3,8 @@ use crate::core::{Point, Size}; use crate::futures::MaybeSend; use crate::window::Screenshot; +use raw_window_handle::WindowHandle; + use std::fmt; /// An operation to be performed on some window. @@ -96,6 +98,8 @@ pub enum Action { /// - **X11:** Has no universal guidelines for icon sizes, so you're at the whims of the WM. That /// said, it's usually in the same ballpark as on Windows. ChangeIcon(Id, Icon), + /// Runs the closure with the native window handle of the window with the given [`Id`]. + RunWithHandle(Id, Box) -> T + 'static>), /// Screenshot the viewport of the window. Screenshot(Id, Box T + 'static>), } @@ -141,6 +145,9 @@ impl Action { Action::FetchId(id, Box::new(move |s| f(o(s)))) } Self::ChangeIcon(id, icon) => Action::ChangeIcon(id, icon), + Self::RunWithHandle(id, o) => { + Action::RunWithHandle(id, Box::new(move |s| f(o(s)))) + } Self::Screenshot(id, tag) => Action::Screenshot( id, Box::new(move |screenshot| f(tag(screenshot))), @@ -197,6 +204,9 @@ impl fmt::Debug for Action { Self::ChangeIcon(id, _icon) => { write!(f, "Action::ChangeIcon({id:?})") } + Self::RunWithHandle(id, _) => { + write!(f, "Action::RunWithHandle({id:?})") + } Self::Screenshot(id, _) => write!(f, "Action::Screenshot({id:?})"), } } diff --git a/winit/src/application.rs b/winit/src/application.rs index 24c98d46dc..6a17683463 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -783,6 +783,16 @@ pub fn run_command( .send_event(tag(window.id().into())) .expect("Send message to event loop"); } + window::Action::RunWithHandle(_id, tag) => { + use window::raw_window_handle::HasWindowHandle; + + if let Ok(handle) = window.window_handle() { + proxy + .send_event(tag(&handle)) + .expect("Send message to event loop"); + } + } + window::Action::Screenshot(_id, tag) => { let bytes = compositor.screenshot( renderer, diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 662adf5b43..23b2f3c4cd 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -998,7 +998,7 @@ fn run_command( proxy .send_event(tag(mode)) - .expect("Event loop doesn't exist."); + .expect("Send message to event loop"); } } window::Action::ToggleMaximize(id) => { @@ -1034,7 +1034,19 @@ fn run_command( if let Some(window) = window_manager.get_mut(id) { proxy .send_event(tag(window.raw.id().into())) - .expect("Event loop doesn't exist."); + .expect("Send message to event loop"); + } + } + window::Action::RunWithHandle(id, tag) => { + use window::raw_window_handle::HasWindowHandle; + + if let Some(handle) = window_manager + .get_mut(id) + .and_then(|window| window.raw.window_handle().ok()) + { + proxy + .send_event(tag(&handle)) + .expect("Send message to event loop"); } } window::Action::Screenshot(id, tag) => {