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.workspace = true
|
||||||
tokio.features = ["fs"]
|
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,
|
button, center_x, column, container, operation, pick_list, row, space,
|
||||||
text, text_editor, toggler, tooltip,
|
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::ffi;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
@ -97,7 +98,10 @@ impl Editor {
|
||||||
} else {
|
} else {
|
||||||
self.is_loading = true;
|
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) => {
|
Message::FileOpened(result) => {
|
||||||
|
|
@ -246,14 +250,19 @@ pub enum Error {
|
||||||
IoError(io::ErrorKind),
|
IoError(io::ErrorKind),
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn open_file() -> Result<(PathBuf, Arc<String>), Error> {
|
fn open_file(
|
||||||
let picked_file = rfd::AsyncFileDialog::new()
|
window: &dyn Window,
|
||||||
|
) -> impl Future<Output = Result<(PathBuf, Arc<String>), Error>> + use<> {
|
||||||
|
let dialog = rfd::AsyncFileDialog::new()
|
||||||
.set_title("Open a text file...")
|
.set_title("Open a text file...")
|
||||||
.pick_file()
|
.set_parent(&window);
|
||||||
.await
|
|
||||||
.ok_or(Error::DialogClosed)?;
|
|
||||||
|
|
||||||
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(
|
async fn load_file(
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@ pub use iced_futures as futures;
|
||||||
|
|
||||||
pub use task::Task;
|
pub use task::Task;
|
||||||
pub use user_interface::UserInterface;
|
pub use user_interface::UserInterface;
|
||||||
|
pub use window::Window;
|
||||||
|
|
||||||
use crate::futures::futures::channel::oneshot;
|
use crate::futures::futures::channel::oneshot;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ use crate::task::{self, Task};
|
||||||
|
|
||||||
pub use raw_window_handle;
|
pub use raw_window_handle;
|
||||||
|
|
||||||
use raw_window_handle::WindowHandle;
|
use raw_window_handle::{HasDisplayHandle, HasWindowHandle};
|
||||||
|
|
||||||
/// An operation to be performed on some window.
|
/// An operation to be performed on some window.
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
|
|
@ -145,8 +145,8 @@ pub enum Action {
|
||||||
/// said, it's usually in the same ballpark as on Windows.
|
/// said, it's usually in the same ballpark as on Windows.
|
||||||
SetIcon(Id, Icon),
|
SetIcon(Id, Icon),
|
||||||
|
|
||||||
/// Runs the closure with the native window handle of the window with the given [`Id`].
|
/// Runs the closure with a reference to the [`Window`] with the given [`Id`].
|
||||||
RunWithHandle(Id, Box<dyn FnOnce(WindowHandle<'_>) + Send>),
|
Run(Id, Box<dyn FnOnce(&dyn Window) + Send>),
|
||||||
|
|
||||||
/// Screenshot the viewport of the window.
|
/// Screenshot the viewport of the window.
|
||||||
Screenshot(Id, oneshot::Sender<Screenshot>),
|
Screenshot(Id, oneshot::Sender<Screenshot>),
|
||||||
|
|
@ -182,6 +182,13 @@ pub enum Action {
|
||||||
RelayoutAll,
|
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.
|
/// 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
|
||||||
|
|
@ -442,18 +449,18 @@ pub fn set_icon<T>(id: Id, icon: Icon) -> Task<T> {
|
||||||
task::effect(crate::Action::Window(Action::SetIcon(id, icon)))
|
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.
|
/// 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,
|
id: Id,
|
||||||
f: impl FnOnce(WindowHandle<'_>) -> T + Send + 'static,
|
f: impl FnOnce(&dyn Window) -> T + Send + 'static,
|
||||||
) -> Task<T>
|
) -> Task<T>
|
||||||
where
|
where
|
||||||
T: Send + 'static,
|
T: Send + 'static,
|
||||||
{
|
{
|
||||||
task::oneshot(move |channel| {
|
task::oneshot(move |channel| {
|
||||||
crate::Action::Window(Action::RunWithHandle(
|
crate::Action::Window(Action::Run(
|
||||||
id,
|
id,
|
||||||
Box::new(move |handle| {
|
Box::new(move |handle| {
|
||||||
let _ = channel.send(f(handle));
|
let _ = channel.send(f(handle));
|
||||||
|
|
|
||||||
|
|
@ -647,6 +647,7 @@ pub use font::Font;
|
||||||
pub use program::Program;
|
pub use program::Program;
|
||||||
pub use renderer::Renderer;
|
pub use renderer::Renderer;
|
||||||
pub use task::Task;
|
pub use task::Task;
|
||||||
|
pub use window::Window;
|
||||||
|
|
||||||
#[doc(inline)]
|
#[doc(inline)]
|
||||||
pub use application::application;
|
pub use application::application;
|
||||||
|
|
|
||||||
|
|
@ -1613,14 +1613,9 @@ fn run_action<'a, P, C>(
|
||||||
let _ = channel.send(window.raw.id().into());
|
let _ = channel.send(window.raw.id().into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::RunWithHandle(id, f) => {
|
window::Action::Run(id, f) => {
|
||||||
use window::raw_window_handle::HasWindowHandle;
|
if let Some(window) = window_manager.get_mut(id) {
|
||||||
|
f(window);
|
||||||
if let Some(handle) = window_manager
|
|
||||||
.get_mut(id)
|
|
||||||
.and_then(|window| window.raw.window_handle().ok())
|
|
||||||
{
|
|
||||||
f(handle);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
window::Action::Screenshot(id, channel) => {
|
window::Action::Screenshot(id, channel) => {
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ use crate::core::{
|
||||||
};
|
};
|
||||||
use crate::graphics::Compositor;
|
use crate::graphics::Compositor;
|
||||||
use crate::program::{self, Program};
|
use crate::program::{self, Program};
|
||||||
|
use crate::runtime::window::raw_window_handle;
|
||||||
|
|
||||||
use winit::dpi::{LogicalPosition, LogicalSize};
|
use winit::dpi::{LogicalPosition, LogicalSize};
|
||||||
use winit::monitor::MonitorHandle;
|
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>
|
struct Preedit<Renderer>
|
||||||
where
|
where
|
||||||
Renderer: text::Renderer,
|
Renderer: text::Renderer,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue