Skip to content

Commit edf09d7

Browse files
committed
Allow fetching the raw window handle of a window by it's iced::window::Id.
1 parent 90332ac commit edf09d7

File tree

6 files changed

+31
-0
lines changed

6 files changed

+31
-0
lines changed

runtime/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ iced_core.workspace = true
1919
iced_futures.workspace = true
2020
iced_futures.features = ["thread-pool"]
2121

22+
raw-window-handle.workspace = true
2223
thiserror.workspace = true

runtime/src/window.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ use crate::core::{Point, Size};
1515
use crate::futures::event;
1616
use crate::futures::Subscription;
1717

18+
use raw_window_handle::RawWindowHandle;
19+
1820
/// Subscribes to the frames of the window of the running application.
1921
///
2022
/// The resulting [`Subscription`] will produce items at a rate equal to the
@@ -165,6 +167,11 @@ pub fn fetch_id<Message>(
165167
Command::single(command::Action::Window(Action::FetchId(id, Box::new(f))))
166168
}
167169

170+
/// Fetches the raw window handle for the specified window.
171+
pub fn fetch_handle<Message>(id: Id, f: impl FnOnce(RawWindowHandle) -> Message + 'static) -> Command<Message> {
172+
Command::single(command::Action::Window(Action::FetchHandle(id, Box::new(f))))
173+
}
174+
168175
/// Changes the [`Icon`] of the window.
169176
pub fn change_icon<Message>(id: Id, icon: Icon) -> Command<Message> {
170177
Command::single(command::Action::Window(Action::ChangeIcon(id, icon)))

runtime/src/window/action.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::core::{Point, Size};
33
use crate::futures::MaybeSend;
44
use crate::window::Screenshot;
55

6+
use raw_window_handle::RawWindowHandle;
7+
68
use std::fmt;
79

810
/// An operation to be performed on some window.
@@ -81,6 +83,8 @@ pub enum Action<T> {
8183
ChangeLevel(Id, Level),
8284
/// Fetch the raw identifier unique to the window.
8385
FetchId(Id, Box<dyn FnOnce(u64) -> T + 'static>),
86+
/// Fetch the raw window handle.
87+
FetchHandle(Id, Box<dyn FnOnce(RawWindowHandle) -> T + 'static>),
8488
/// Change the window [`Icon`].
8589
///
8690
/// On Windows and X11, this is typically the small icon in the top-left
@@ -140,6 +144,9 @@ impl<T> Action<T> {
140144
Self::FetchId(id, o) => {
141145
Action::FetchId(id, Box::new(move |s| f(o(s))))
142146
}
147+
Self::FetchHandle(id, o) => {
148+
Action::FetchHandle(id, Box::new(move |s| f(o(s))))
149+
}
143150
Self::ChangeIcon(id, icon) => Action::ChangeIcon(id, icon),
144151
Self::Screenshot(id, tag) => Action::Screenshot(
145152
id,
@@ -194,6 +201,7 @@ impl<T> fmt::Debug for Action<T> {
194201
write!(f, "Action::ChangeLevel({id:?}, {level:?})")
195202
}
196203
Self::FetchId(id, _) => write!(f, "Action::FetchId({id:?})"),
204+
Self::FetchHandle(id, _) => write!(f, "Action::FetchHandle({id:?})"),
197205
Self::ChangeIcon(id, _icon) => {
198206
write!(f, "Action::ChangeIcon({id:?})")
199207
}

winit/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ thiserror.workspace = true
3131
tracing.workspace = true
3232
window_clipboard.workspace = true
3333
winit.workspace = true
34+
raw-window-handle.workspace = true
3435

3536
sysinfo.workspace = true
3637
sysinfo.optional = true

winit/src/application.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ use futures::channel::mpsc;
2525

2626
use std::mem::ManuallyDrop;
2727

28+
use raw_window_handle::HasRawWindowHandle;
29+
2830
/// An interactive, native cross-platform application.
2931
///
3032
/// This trait is the main entrypoint of Iced. Once implemented, you can run
@@ -807,6 +809,9 @@ pub fn run_command<A, C, E>(
807809
.send_event(tag(window.id().into()))
808810
.expect("Send message to event loop");
809811
}
812+
window::Action::FetchHandle(_id, tag) => proxy
813+
.send_event(tag(window.raw_window_handle()))
814+
.expect("Send message to event loop"),
810815
window::Action::Screenshot(_id, tag) => {
811816
let bytes = compositor.screenshot(
812817
renderer,

winit/src/multi_window.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ use std::collections::HashMap;
2626
use std::mem::ManuallyDrop;
2727
use std::time::Instant;
2828

29+
use raw_window_handle::HasRawWindowHandle;
30+
2931
/// An interactive, native, cross-platform, multi-windowed application.
3032
///
3133
/// This trait is the main entrypoint of multi-window Iced. Once implemented, you can run
@@ -1039,6 +1041,13 @@ fn run_command<A, C, E>(
10391041
.expect("Event loop doesn't exist.");
10401042
}
10411043
}
1044+
window::Action::FetchHandle(id, tag) => {
1045+
if let Some(window) = window_manager.get_mut(id) {
1046+
proxy
1047+
.send_event(tag(window.raw.raw_window_handle()))
1048+
.expect("Event loop doesn't exist.")
1049+
}
1050+
}
10421051
window::Action::Screenshot(id, tag) => {
10431052
if let Some(window) = window_manager.get_mut(id) {
10441053
let bytes = compositor.screenshot(

0 commit comments

Comments
 (0)