Skip to content

Commit

Permalink
Merge pull request #2200 from dtzxporter/run-window-callback
Browse files Browse the repository at this point in the history
Provide native window access, via "fetch_native_handle" method on window.
  • Loading branch information
hecrj authored Feb 7, 2024
2 parents 6f97b62 + 2a3778d commit d3619b5
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ iced_futures.workspace = true
iced_futures.features = ["thread-pool"]

thiserror.workspace = true
raw-window-handle.workspace = true
17 changes: 17 additions & 0 deletions runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -170,6 +174,19 @@ pub fn change_icon<Message>(id: Id, icon: Icon) -> Command<Message> {
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<Message>(
id: Id,
f: impl FnOnce(&WindowHandle<'_>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Window(Action::RunWithHandle(
id,
Box::new(f),
)))
}

/// Captures a [`Screenshot`] from the window.
pub fn screenshot<Message>(
id: Id,
Expand Down
10 changes: 10 additions & 0 deletions runtime/src/window/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,6 +98,8 @@ pub enum Action<T> {
/// - **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<dyn FnOnce(&WindowHandle<'_>) -> T + 'static>),
/// Screenshot the viewport of the window.
Screenshot(Id, Box<dyn FnOnce(Screenshot) -> T + 'static>),
}
Expand Down Expand Up @@ -141,6 +145,9 @@ impl<T> Action<T> {
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))),
Expand Down Expand Up @@ -197,6 +204,9 @@ impl<T> fmt::Debug for Action<T> {
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:?})"),
}
}
Expand Down
10 changes: 10 additions & 0 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,16 @@ pub fn run_command<A, C, E>(
.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,
Expand Down
16 changes: 14 additions & 2 deletions winit/src/multi_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ fn run_command<A, C, E>(

proxy
.send_event(tag(mode))
.expect("Event loop doesn't exist.");
.expect("Send message to event loop");
}
}
window::Action::ToggleMaximize(id) => {
Expand Down Expand Up @@ -1034,7 +1034,19 @@ fn run_command<A, C, E>(
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) => {
Expand Down

0 comments on commit d3619b5

Please sign in to comment.