Provide new Window trait to window::run
Co-authored-by: flakes <cxxjoe@gmail.com>
This commit is contained in:
parent
2bbc7385e0
commit
bbba0942f8
4 changed files with 48 additions and 14 deletions
|
|
@ -24,6 +24,7 @@ pub use iced_futures as futures;
|
|||
|
||||
pub use task::Task;
|
||||
pub use user_interface::UserInterface;
|
||||
pub use window::Window;
|
||||
|
||||
use crate::futures::futures::channel::oneshot;
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ use crate::task::{self, Task};
|
|||
|
||||
pub use raw_window_handle;
|
||||
|
||||
use raw_window_handle::WindowHandle;
|
||||
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
|
||||
|
||||
/// An operation to be performed on some window.
|
||||
pub enum Action {
|
||||
|
|
@ -145,8 +145,8 @@ pub enum Action {
|
|||
/// said, it's usually in the same ballpark as on Windows.
|
||||
SetIcon(Id, Icon),
|
||||
|
||||
/// Runs the closure with the native window handle of the window with the given [`Id`].
|
||||
RunWithHandle(Id, Box<dyn FnOnce(WindowHandle<'_>) + Send>),
|
||||
/// Runs the closure with a reference to the [`Window`] with the given [`Id`].
|
||||
Run(Id, Box<dyn FnOnce(&dyn Window) + Send>),
|
||||
|
||||
/// Screenshot the viewport of the window.
|
||||
Screenshot(Id, oneshot::Sender<Screenshot>),
|
||||
|
|
@ -182,6 +182,13 @@ pub enum Action {
|
|||
RelayoutAll,
|
||||
}
|
||||
|
||||
/// A window managed by iced.
|
||||
///
|
||||
/// It implements both [`HasWindowHandle`] and [`HasDisplayHandle`].
|
||||
pub trait Window: HasWindowHandle + HasDisplayHandle {}
|
||||
|
||||
impl<T> Window for T where T: HasWindowHandle + HasDisplayHandle {}
|
||||
|
||||
/// Subscribes to the frames of the window of the running application.
|
||||
///
|
||||
/// The resulting [`Subscription`] will produce items at a rate equal to the
|
||||
|
|
@ -442,18 +449,18 @@ pub fn set_icon<T>(id: Id, icon: Icon) -> Task<T> {
|
|||
task::effect(crate::Action::Window(Action::SetIcon(id, icon)))
|
||||
}
|
||||
|
||||
/// Runs the given callback with the native window handle for the window with the given id.
|
||||
/// Runs the given callback with a reference to 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<T>(
|
||||
pub fn run<T>(
|
||||
id: Id,
|
||||
f: impl FnOnce(WindowHandle<'_>) -> T + Send + 'static,
|
||||
f: impl FnOnce(&dyn Window) -> T + Send + 'static,
|
||||
) -> Task<T>
|
||||
where
|
||||
T: Send + 'static,
|
||||
{
|
||||
task::oneshot(move |channel| {
|
||||
crate::Action::Window(Action::RunWithHandle(
|
||||
crate::Action::Window(Action::Run(
|
||||
id,
|
||||
Box::new(move |handle| {
|
||||
let _ = channel.send(f(handle));
|
||||
|
|
|
|||
|
|
@ -1613,13 +1613,8 @@ fn run_action<'a, P, C>(
|
|||
let _ = channel.send(window.raw.id().into());
|
||||
}
|
||||
}
|
||||
window::Action::RunWithHandle(id, f) => {
|
||||
use window::raw_window_handle::HasWindowHandle;
|
||||
|
||||
if let Some(handle) = window_manager
|
||||
.get_mut(id)
|
||||
.and_then(|window| window.raw.window_handle().ok())
|
||||
{
|
||||
window::Action::Run(id, f) => {
|
||||
if let Some(handle) = window_manager.get_mut(id) {
|
||||
f(handle);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ use crate::core::{
|
|||
};
|
||||
use crate::graphics::Compositor;
|
||||
use crate::program::{self, Program};
|
||||
use crate::runtime::window::raw_window_handle;
|
||||
|
||||
use winit::dpi::{LogicalPosition, LogicalSize};
|
||||
use winit::monitor::MonitorHandle;
|
||||
|
|
@ -299,6 +300,36 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<P, C> raw_window_handle::HasWindowHandle for Window<P, C>
|
||||
where
|
||||
P: Program,
|
||||
C: Compositor<Renderer = P::Renderer>,
|
||||
{
|
||||
fn window_handle(
|
||||
&self,
|
||||
) -> Result<
|
||||
raw_window_handle::WindowHandle<'_>,
|
||||
raw_window_handle::HandleError,
|
||||
> {
|
||||
self.raw.window_handle()
|
||||
}
|
||||
}
|
||||
|
||||
impl<P, C> raw_window_handle::HasDisplayHandle for Window<P, C>
|
||||
where
|
||||
P: Program,
|
||||
C: Compositor<Renderer = P::Renderer>,
|
||||
{
|
||||
fn display_handle(
|
||||
&self,
|
||||
) -> Result<
|
||||
raw_window_handle::DisplayHandle<'_>,
|
||||
raw_window_handle::HandleError,
|
||||
> {
|
||||
self.raw.display_handle()
|
||||
}
|
||||
}
|
||||
|
||||
struct Preedit<Renderer>
|
||||
where
|
||||
Renderer: text::Renderer,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue