summaryrefslogtreecommitdiffstats
path: root/native/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-01-12 04:35:41 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-01-12 04:35:41 +0100
commit0b86c4a299d384cafca31206eac8c94f1123518d (patch)
tree6101ee28b8f030963bcc0f0cc7c51e67076fcf72 /native/src
parentc649ec8cf7066eb190193ff499c0ecccbca76796 (diff)
downloadiced-0b86c4a299d384cafca31206eac8c94f1123518d.tar.gz
iced-0b86c4a299d384cafca31206eac8c94f1123518d.tar.bz2
iced-0b86c4a299d384cafca31206eac8c94f1123518d.zip
Implement `window::frames` subscription
... and use it in the `solar_system` example :tada:
Diffstat (limited to 'native/src')
-rw-r--r--native/src/subscription.rs27
-rw-r--r--native/src/window.rs15
2 files changed, 41 insertions, 1 deletions
diff --git a/native/src/subscription.rs b/native/src/subscription.rs
index 980a8116..4c0d80a7 100644
--- a/native/src/subscription.rs
+++ b/native/src/subscription.rs
@@ -59,8 +59,11 @@ pub fn events_with<Message>(
where
Message: 'static + MaybeSend,
{
+ #[derive(Hash)]
+ struct EventsWith;
+
Subscription::from_recipe(Runner {
- id: f,
+ id: (EventsWith, f),
spawn: move |events| {
use futures::future;
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
/// given [`Stream`].
///
diff --git a/native/src/window.rs b/native/src/window.rs
index 1b97e655..4bccc471 100644
--- a/native/src/window.rs
+++ b/native/src/window.rs
@@ -8,3 +8,18 @@ pub use action::Action;
pub use event::Event;
pub use mode::Mode;
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,
+ })
+}