Re-implement against latest iced master. Rename FetchNativeHandle.

This commit is contained in:
dtzxporter 2024-01-19 14:48:14 -05:00 committed by Héctor Ramón Jiménez
parent 6f97b62457
commit 7105992228
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
5 changed files with 47 additions and 0 deletions

View file

@ -20,3 +20,4 @@ iced_futures.workspace = true
iced_futures.features = ["thread-pool"] iced_futures.features = ["thread-pool"]
thiserror.workspace = true thiserror.workspace = true
raw-window-handle.workspace = true

View file

@ -15,6 +15,8 @@ use crate::core::{Point, Size};
use crate::futures::event; use crate::futures::event;
use crate::futures::Subscription; use crate::futures::Subscription;
use raw_window_handle::WindowHandle;
/// Subscribes to the frames of the window of the running application. /// Subscribes to the frames of the window of the running application.
/// ///
/// The resulting [`Subscription`] will produce items at a rate equal to the /// The resulting [`Subscription`] will produce items at a rate equal to the
@ -170,6 +172,19 @@ pub fn change_icon<Message>(id: Id, icon: Icon) -> Command<Message> {
Command::single(command::Action::Window(Action::ChangeIcon(id, icon))) Command::single(command::Action::Window(Action::ChangeIcon(id, icon)))
} }
/// Requests access to 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 fetch_native_handle<Message>(
id: Id,
f: impl FnOnce(&WindowHandle<'_>) -> Message + 'static,
) -> Command<Message> {
Command::single(command::Action::Window(Action::FetchNativeHandle(
id,
Box::new(f),
)))
}
/// Captures a [`Screenshot`] from the window. /// Captures a [`Screenshot`] from the window.
pub fn screenshot<Message>( pub fn screenshot<Message>(
id: Id, id: Id,

View file

@ -3,6 +3,8 @@ use crate::core::{Point, Size};
use crate::futures::MaybeSend; use crate::futures::MaybeSend;
use crate::window::Screenshot; use crate::window::Screenshot;
use raw_window_handle::WindowHandle;
use std::fmt; use std::fmt;
/// An operation to be performed on some window. /// An operation to be performed on some window.
@ -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 /// - **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. /// said, it's usually in the same ballpark as on Windows.
ChangeIcon(Id, Icon), ChangeIcon(Id, Icon),
/// Requests access to the windows native handle.
FetchNativeHandle(Id, Box<dyn FnOnce(&WindowHandle<'_>) -> T + 'static>),
/// Screenshot the viewport of the window. /// Screenshot the viewport of the window.
Screenshot(Id, Box<dyn FnOnce(Screenshot) -> T + 'static>), Screenshot(Id, Box<dyn FnOnce(Screenshot) -> T + 'static>),
} }
@ -141,6 +145,9 @@ impl<T> Action<T> {
Action::FetchId(id, Box::new(move |s| f(o(s)))) Action::FetchId(id, Box::new(move |s| f(o(s))))
} }
Self::ChangeIcon(id, icon) => Action::ChangeIcon(id, icon), Self::ChangeIcon(id, icon) => Action::ChangeIcon(id, icon),
Self::FetchNativeHandle(id, o) => {
Action::FetchNativeHandle(id, Box::new(move |s| f(o(s))))
}
Self::Screenshot(id, tag) => Action::Screenshot( Self::Screenshot(id, tag) => Action::Screenshot(
id, id,
Box::new(move |screenshot| f(tag(screenshot))), Box::new(move |screenshot| f(tag(screenshot))),
@ -197,6 +204,9 @@ impl<T> fmt::Debug for Action<T> {
Self::ChangeIcon(id, _icon) => { Self::ChangeIcon(id, _icon) => {
write!(f, "Action::ChangeIcon({id:?})") write!(f, "Action::ChangeIcon({id:?})")
} }
Self::FetchNativeHandle(id, _) => {
write!(f, "Action::RequestNativeHandle({id:?})")
}
Self::Screenshot(id, _) => write!(f, "Action::Screenshot({id:?})"), Self::Screenshot(id, _) => write!(f, "Action::Screenshot({id:?})"),
} }
} }

View file

@ -23,6 +23,8 @@ use crate::{Clipboard, Error, Proxy, Settings};
use futures::channel::mpsc; use futures::channel::mpsc;
use winit::raw_window_handle::HasWindowHandle;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::sync::Arc; use std::sync::Arc;
@ -783,6 +785,13 @@ pub fn run_command<A, C, E>(
.send_event(tag(window.id().into())) .send_event(tag(window.id().into()))
.expect("Send message to event loop"); .expect("Send message to event loop");
} }
window::Action::FetchNativeHandle(_id, tag) => {
proxy
.send_event(tag(&window
.window_handle()
.expect("Missing window handle")))
.expect("Send message to event loop");
}
window::Action::Screenshot(_id, tag) => { window::Action::Screenshot(_id, tag) => {
let bytes = compositor.screenshot( let bytes = compositor.screenshot(
renderer, renderer,

View file

@ -22,6 +22,8 @@ use crate::runtime::Debug;
use crate::style::application::StyleSheet; use crate::style::application::StyleSheet;
use crate::{Clipboard, Error, Proxy, Settings}; use crate::{Clipboard, Error, Proxy, Settings};
use winit::raw_window_handle::HasWindowHandle;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem::ManuallyDrop; use std::mem::ManuallyDrop;
use std::sync::Arc; use std::sync::Arc;
@ -1037,6 +1039,16 @@ fn run_command<A, C, E>(
.expect("Event loop doesn't exist."); .expect("Event loop doesn't exist.");
} }
} }
window::Action::FetchNativeHandle(id, tag) => {
if let Some(window) = window_manager.get_mut(id) {
proxy
.send_event(tag(&window
.raw
.window_handle()
.expect("Missing window handle.")))
.expect("Event loop doesn't exist.");
}
}
window::Action::Screenshot(id, tag) => { window::Action::Screenshot(id, tag) => {
if let Some(window) = window_manager.get_mut(id) { if let Some(window) = window_manager.get_mut(id) {
let bytes = compositor.screenshot( let bytes = compositor.screenshot(