Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide native window access, via "fetch_native_handle" method on window. #2200

Merged
merged 5 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
Loading