Merge pull request #2718 from flakes/new-rfd-and-windowhandles
Replace `window::run_with_handle` with a more powerful `window::run`
This commit is contained in:
commit
87eea88463
8 changed files with 364 additions and 485 deletions
757
Cargo.lock
generated
757
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -12,4 +12,4 @@ iced.features = ["highlighter", "tokio", "debug"]
|
|||
tokio.workspace = true
|
||||
tokio.features = ["fs"]
|
||||
|
||||
rfd = "0.13"
|
||||
rfd.workspace = true
|
||||
|
|
|
|||
|
|
@ -4,7 +4,8 @@ use iced::widget::{
|
|||
button, center_x, column, container, operation, pick_list, row, space,
|
||||
text, text_editor, toggler, tooltip,
|
||||
};
|
||||
use iced::{Center, Element, Fill, Font, Task, Theme};
|
||||
use iced::window;
|
||||
use iced::{Center, Element, Fill, Font, Task, Theme, Window};
|
||||
|
||||
use std::ffi;
|
||||
use std::io;
|
||||
|
|
@ -97,7 +98,10 @@ impl Editor {
|
|||
} else {
|
||||
self.is_loading = true;
|
||||
|
||||
Task::perform(open_file(), Message::FileOpened)
|
||||
window::oldest()
|
||||
.and_then(|id| window::run(id, open_file))
|
||||
.then(Task::future)
|
||||
.map(Message::FileOpened)
|
||||
}
|
||||
}
|
||||
Message::FileOpened(result) => {
|
||||
|
|
@ -246,14 +250,19 @@ pub enum Error {
|
|||
IoError(io::ErrorKind),
|
||||
}
|
||||
|
||||
async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
|
||||
let picked_file = rfd::AsyncFileDialog::new()
|
||||
fn open_file(
|
||||
window: &dyn Window,
|
||||
) -> impl Future<Output = Result<(PathBuf, Arc<String>), Error>> + use<> {
|
||||
let dialog = rfd::AsyncFileDialog::new()
|
||||
.set_title("Open a text file...")
|
||||
.pick_file()
|
||||
.await
|
||||
.ok_or(Error::DialogClosed)?;
|
||||
.set_parent(&window);
|
||||
|
||||
load_file(picked_file).await
|
||||
async move {
|
||||
let picked_file =
|
||||
dialog.pick_file().await.ok_or(Error::DialogClosed)?;
|
||||
|
||||
load_file(picked_file).await
|
||||
}
|
||||
}
|
||||
|
||||
async fn load_file(
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
|
|
|
|||
|
|
@ -647,6 +647,7 @@ pub use font::Font;
|
|||
pub use program::Program;
|
||||
pub use renderer::Renderer;
|
||||
pub use task::Task;
|
||||
pub use window::Window;
|
||||
|
||||
#[doc(inline)]
|
||||
pub use application::application;
|
||||
|
|
|
|||
|
|
@ -1613,14 +1613,9 @@ 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())
|
||||
{
|
||||
f(handle);
|
||||
window::Action::Run(id, f) => {
|
||||
if let Some(window) = window_manager.get_mut(id) {
|
||||
f(window);
|
||||
}
|
||||
}
|
||||
window::Action::Screenshot(id, channel) => {
|
||||
|
|
|
|||
|
|
@ -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