Implement window::frames subscription
... and use it in the `solar_system` example 🎉
This commit is contained in:
parent
c649ec8cf7
commit
0b86c4a299
4 changed files with 43 additions and 4 deletions
|
|
@ -9,7 +9,6 @@
|
||||||
use iced::application;
|
use iced::application;
|
||||||
use iced::executor;
|
use iced::executor;
|
||||||
use iced::theme::{self, Theme};
|
use iced::theme::{self, Theme};
|
||||||
use iced::time;
|
|
||||||
use iced::widget::canvas;
|
use iced::widget::canvas;
|
||||||
use iced::widget::canvas::gradient::{self, Gradient};
|
use iced::widget::canvas::gradient::{self, Gradient};
|
||||||
use iced::widget::canvas::stroke::{self, Stroke};
|
use iced::widget::canvas::stroke::{self, Stroke};
|
||||||
|
|
@ -90,7 +89,7 @@ impl Application for SolarSystem {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
time::every(time::Duration::from_millis(10)).map(Message::Tick)
|
window::frames().map(Message::Tick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -59,8 +59,11 @@ pub fn events_with<Message>(
|
||||||
where
|
where
|
||||||
Message: 'static + MaybeSend,
|
Message: 'static + MaybeSend,
|
||||||
{
|
{
|
||||||
|
#[derive(Hash)]
|
||||||
|
struct EventsWith;
|
||||||
|
|
||||||
Subscription::from_recipe(Runner {
|
Subscription::from_recipe(Runner {
|
||||||
id: f,
|
id: (EventsWith, f),
|
||||||
spawn: move |events| {
|
spawn: move |events| {
|
||||||
use futures::future;
|
use futures::future;
|
||||||
use futures::stream::StreamExt;
|
use futures::stream::StreamExt;
|
||||||
|
|
@ -75,6 +78,28 @@ where
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn raw_events<Message>(
|
||||||
|
f: fn(Event, event::Status) -> Option<Message>,
|
||||||
|
) -> Subscription<Message>
|
||||||
|
where
|
||||||
|
Message: 'static + MaybeSend,
|
||||||
|
{
|
||||||
|
#[derive(Hash)]
|
||||||
|
struct RawEvents;
|
||||||
|
|
||||||
|
Subscription::from_recipe(Runner {
|
||||||
|
id: (RawEvents, f),
|
||||||
|
spawn: move |events| {
|
||||||
|
use futures::future;
|
||||||
|
use futures::stream::StreamExt;
|
||||||
|
|
||||||
|
events.filter_map(move |(event, status)| {
|
||||||
|
future::ready(f(event, status))
|
||||||
|
})
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns a [`Subscription`] that will create and asynchronously run the
|
/// Returns a [`Subscription`] that will create and asynchronously run the
|
||||||
/// given [`Stream`].
|
/// given [`Stream`].
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -8,3 +8,18 @@ pub use action::Action;
|
||||||
pub use event::Event;
|
pub use event::Event;
|
||||||
pub use mode::Mode;
|
pub use mode::Mode;
|
||||||
pub use user_attention::UserAttention;
|
pub use user_attention::UserAttention;
|
||||||
|
|
||||||
|
use crate::subscription::{self, Subscription};
|
||||||
|
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
|
/// Subscribes to the frames of the window of the running application.
|
||||||
|
///
|
||||||
|
/// The resulting [`Subscription`] will produce items at a rate equal to the
|
||||||
|
/// framerate of the monitor of said window.
|
||||||
|
pub fn frames() -> Subscription<Instant> {
|
||||||
|
subscription::raw_events(|event, _status| match event {
|
||||||
|
crate::Event::Window(Event::RedrawRequested(at)) => Some(at),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
use crate::command::{self, Command};
|
use crate::command::{self, Command};
|
||||||
use iced_native::window;
|
use iced_native::window;
|
||||||
|
|
||||||
pub use window::{Event, Mode, UserAttention};
|
pub use window::{frames, Event, Mode, UserAttention};
|
||||||
|
|
||||||
/// Closes the current window and exits the application.
|
/// Closes the current window and exits the application.
|
||||||
pub fn close<Message>() -> Command<Message> {
|
pub fn close<Message>() -> Command<Message> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue