Draft first version of event subscriptions 🎉
This commit is contained in:
parent
e92ea48e88
commit
d575f45411
@ -41,6 +41,8 @@ serde_json = "1.0"
|
|||||||
directories = "2.0"
|
directories = "2.0"
|
||||||
reqwest = "0.9"
|
reqwest = "0.9"
|
||||||
rand = "0.7"
|
rand = "0.7"
|
||||||
|
chrono = "0.4"
|
||||||
|
futures = "0.3"
|
||||||
|
|
||||||
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
|
||||||
wasm-bindgen = "0.2.51"
|
wasm-bindgen = "0.2.51"
|
||||||
|
|||||||
@ -10,6 +10,8 @@ repository = "https://github.com/hecrj/iced"
|
|||||||
[features]
|
[features]
|
||||||
# Exposes a future-based `Command` type
|
# Exposes a future-based `Command` type
|
||||||
command = ["futures"]
|
command = ["futures"]
|
||||||
|
# Exposes a future-based `Subscription` type
|
||||||
|
subscription = ["futures"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = { version = "0.3", optional = true }
|
futures = { version = "0.3", optional = true }
|
||||||
|
|||||||
@ -9,7 +9,7 @@
|
|||||||
//! [Iced]: https://github.com/hecrj/iced
|
//! [Iced]: https://github.com/hecrj/iced
|
||||||
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
|
//! [`iced_native`]: https://github.com/hecrj/iced/tree/master/native
|
||||||
//! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web
|
//! [`iced_web`]: https://github.com/hecrj/iced/tree/master/web
|
||||||
#![deny(missing_docs)]
|
//#![deny(missing_docs)]
|
||||||
#![deny(missing_debug_implementations)]
|
#![deny(missing_debug_implementations)]
|
||||||
#![deny(unused_results)]
|
#![deny(unused_results)]
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
@ -38,3 +38,9 @@ mod command;
|
|||||||
|
|
||||||
#[cfg(feature = "command")]
|
#[cfg(feature = "command")]
|
||||||
pub use command::Command;
|
pub use command::Command;
|
||||||
|
|
||||||
|
#[cfg(feature = "subscription")]
|
||||||
|
pub mod subscription;
|
||||||
|
|
||||||
|
#[cfg(feature = "subscription")]
|
||||||
|
pub use subscription::Subscription;
|
||||||
|
|||||||
61
core/src/subscription.rs
Normal file
61
core/src/subscription.rs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
//! Generate events asynchronously for you application.
|
||||||
|
|
||||||
|
/// An event subscription.
|
||||||
|
pub struct Subscription<T> {
|
||||||
|
definitions: Vec<Box<dyn Definition<Message = T>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> Subscription<T> {
|
||||||
|
pub fn none() -> Self {
|
||||||
|
Self {
|
||||||
|
definitions: Vec::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn batch(subscriptions: impl Iterator<Item = Subscription<T>>) -> Self {
|
||||||
|
Self {
|
||||||
|
definitions: subscriptions
|
||||||
|
.flat_map(|subscription| subscription.definitions)
|
||||||
|
.collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn definitions(self) -> Vec<Box<dyn Definition<Message = T>>> {
|
||||||
|
self.definitions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, A> From<A> for Subscription<T>
|
||||||
|
where
|
||||||
|
A: Definition<Message = T> + 'static,
|
||||||
|
{
|
||||||
|
fn from(definition: A) -> Self {
|
||||||
|
Self {
|
||||||
|
definitions: vec![Box::new(definition)],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The definition of an event subscription.
|
||||||
|
pub trait Definition {
|
||||||
|
type Message;
|
||||||
|
|
||||||
|
fn id(&self) -> u64;
|
||||||
|
|
||||||
|
fn stream(
|
||||||
|
&self,
|
||||||
|
) -> (
|
||||||
|
futures::stream::BoxStream<'static, Self::Message>,
|
||||||
|
Box<dyn Handle>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Handle {
|
||||||
|
fn cancel(&mut self);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> std::fmt::Debug for Subscription<T> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
f.debug_struct("Command").finish()
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -8,6 +8,6 @@ license = "MIT"
|
|||||||
repository = "https://github.com/hecrj/iced"
|
repository = "https://github.com/hecrj/iced"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
iced_core = { version = "0.1.0", path = "../core", features = ["command"] }
|
iced_core = { version = "0.1.0", path = "../core", features = ["command", "subscription"] }
|
||||||
twox-hash = "1.5"
|
twox-hash = "1.5"
|
||||||
raw-window-handle = "0.3"
|
raw-window-handle = "0.3"
|
||||||
|
|||||||
@ -52,8 +52,8 @@ mod size;
|
|||||||
mod user_interface;
|
mod user_interface;
|
||||||
|
|
||||||
pub use iced_core::{
|
pub use iced_core::{
|
||||||
Align, Background, Color, Command, Font, HorizontalAlignment, Length,
|
subscription, Align, Background, Color, Command, Font, HorizontalAlignment,
|
||||||
Point, Rectangle, Vector, VerticalAlignment,
|
Length, Point, Rectangle, Subscription, Vector, VerticalAlignment,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub use element::Element;
|
pub use element::Element;
|
||||||
|
|||||||
@ -31,6 +31,7 @@ pub struct Checkbox<Message> {
|
|||||||
on_toggle: Box<dyn Fn(bool) -> Message>,
|
on_toggle: Box<dyn Fn(bool) -> Message>,
|
||||||
label: String,
|
label: String,
|
||||||
label_color: Option<Color>,
|
label_color: Option<Color>,
|
||||||
|
width: Length,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message> Checkbox<Message> {
|
impl<Message> Checkbox<Message> {
|
||||||
@ -53,6 +54,7 @@ impl<Message> Checkbox<Message> {
|
|||||||
on_toggle: Box::new(f),
|
on_toggle: Box::new(f),
|
||||||
label: String::from(label),
|
label: String::from(label),
|
||||||
label_color: None,
|
label_color: None,
|
||||||
|
width: Length::Fill,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,6 +65,14 @@ impl<Message> Checkbox<Message> {
|
|||||||
self.label_color = Some(color.into());
|
self.label_color = Some(color.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Sets the width of the [`Checkbox`].
|
||||||
|
///
|
||||||
|
/// [`Checkbox`]: struct.Checkbox.html
|
||||||
|
pub fn width(mut self, width: Length) -> Self {
|
||||||
|
self.width = width;
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Message, Renderer> Widget<Message, Renderer> for Checkbox<Message>
|
impl<Message, Renderer> Widget<Message, Renderer> for Checkbox<Message>
|
||||||
@ -70,7 +80,7 @@ where
|
|||||||
Renderer: self::Renderer + text::Renderer + row::Renderer,
|
Renderer: self::Renderer + text::Renderer + row::Renderer,
|
||||||
{
|
{
|
||||||
fn width(&self) -> Length {
|
fn width(&self) -> Length {
|
||||||
Length::Fill
|
Length::Shrink
|
||||||
}
|
}
|
||||||
|
|
||||||
fn height(&self) -> Length {
|
fn height(&self) -> Length {
|
||||||
@ -85,6 +95,7 @@ where
|
|||||||
let size = self::Renderer::default_size(renderer);
|
let size = self::Renderer::default_size(renderer);
|
||||||
|
|
||||||
Row::<(), Renderer>::new()
|
Row::<(), Renderer>::new()
|
||||||
|
.width(self.width)
|
||||||
.spacing(15)
|
.spacing(15)
|
||||||
.align_items(Align::Center)
|
.align_items(Align::Center)
|
||||||
.push(
|
.push(
|
||||||
@ -92,7 +103,7 @@ where
|
|||||||
.width(Length::Units(size as u16))
|
.width(Length::Units(size as u16))
|
||||||
.height(Length::Units(size as u16)),
|
.height(Length::Units(size as u16)),
|
||||||
)
|
)
|
||||||
.push(Text::new(&self.label))
|
.push(Text::new(&self.label).width(self.width))
|
||||||
.layout(renderer, limits)
|
.layout(renderer, limits)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::{Command, Element, Settings};
|
use crate::{Command, Element, Settings, Subscription};
|
||||||
|
|
||||||
/// An interactive cross-platform application.
|
/// An interactive cross-platform application.
|
||||||
///
|
///
|
||||||
@ -117,6 +117,11 @@ pub trait Application: Sized {
|
|||||||
/// [`Command`]: struct.Command.html
|
/// [`Command`]: struct.Command.html
|
||||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
||||||
|
|
||||||
|
/// TODO
|
||||||
|
fn subscriptions(&self) -> Subscription<Self::Message> {
|
||||||
|
Subscription::none()
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the widgets to display in the [`Application`].
|
/// Returns the widgets to display in the [`Application`].
|
||||||
///
|
///
|
||||||
/// These widgets can produce __messages__ based on user interaction.
|
/// These widgets can produce __messages__ based on user interaction.
|
||||||
@ -168,6 +173,10 @@ where
|
|||||||
self.0.update(message)
|
self.0.update(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscriptions(&self) -> Subscription<Self::Message> {
|
||||||
|
self.0.subscriptions()
|
||||||
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<'_, Self::Message> {
|
fn view(&mut self) -> Element<'_, Self::Message> {
|
||||||
self.0.view()
|
self.0.view()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
pub use iced_winit::{
|
pub use iced_winit::{
|
||||||
Align, Background, Color, Command, Font, HorizontalAlignment, Length,
|
subscription, Align, Background, Color, Command, Font, HorizontalAlignment,
|
||||||
VerticalAlignment,
|
Length, Subscription, VerticalAlignment,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub mod widget {
|
pub mod widget {
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
use crate::{Application, Command, Element, Settings};
|
use crate::{Application, Command, Element, Settings, Subscription};
|
||||||
|
|
||||||
/// A sandboxed [`Application`].
|
/// A sandboxed [`Application`].
|
||||||
///
|
///
|
||||||
@ -149,6 +149,10 @@ where
|
|||||||
Command::none()
|
Command::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn subscriptions(&self) -> Subscription<T::Message> {
|
||||||
|
Subscription::none()
|
||||||
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<'_, T::Message> {
|
fn view(&mut self) -> Element<'_, T::Message> {
|
||||||
T::view(self)
|
T::view(self)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,10 @@ use crate::{
|
|||||||
conversion,
|
conversion,
|
||||||
input::{keyboard, mouse},
|
input::{keyboard, mouse},
|
||||||
renderer::{Target, Windowed},
|
renderer::{Target, Windowed},
|
||||||
Cache, Command, Container, Debug, Element, Event, Length, MouseCursor,
|
subscription, Cache, Command, Container, Debug, Element, Event, Length,
|
||||||
Settings, UserInterface,
|
MouseCursor, Settings, Subscription, UserInterface,
|
||||||
};
|
};
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
/// An interactive, native cross-platform application.
|
/// An interactive, native cross-platform application.
|
||||||
///
|
///
|
||||||
@ -57,6 +58,9 @@ pub trait Application: Sized {
|
|||||||
/// [`Command`]: struct.Command.html
|
/// [`Command`]: struct.Command.html
|
||||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
||||||
|
|
||||||
|
/// TODO
|
||||||
|
fn subscriptions(&self) -> Subscription<Self::Message>;
|
||||||
|
|
||||||
/// Returns the widgets to display in the [`Application`].
|
/// Returns the widgets to display in the [`Application`].
|
||||||
///
|
///
|
||||||
/// These widgets can produce __messages__ based on user interaction.
|
/// These widgets can produce __messages__ based on user interaction.
|
||||||
@ -89,11 +93,15 @@ pub trait Application: Sized {
|
|||||||
let proxy = event_loop.create_proxy();
|
let proxy = event_loop.create_proxy();
|
||||||
let mut thread_pool =
|
let mut thread_pool =
|
||||||
futures::executor::ThreadPool::new().expect("Create thread pool");
|
futures::executor::ThreadPool::new().expect("Create thread pool");
|
||||||
|
let mut alive_subscriptions = Subscriptions::new();
|
||||||
let mut external_messages = Vec::new();
|
let mut external_messages = Vec::new();
|
||||||
|
|
||||||
let (mut application, init_command) = Self::new();
|
let (mut application, init_command) = Self::new();
|
||||||
spawn(init_command, &mut thread_pool, &proxy);
|
spawn(init_command, &mut thread_pool, &proxy);
|
||||||
|
|
||||||
|
let subscriptions = application.subscriptions();
|
||||||
|
alive_subscriptions.update(subscriptions, &mut thread_pool, &proxy);
|
||||||
|
|
||||||
let mut title = application.title();
|
let mut title = application.title();
|
||||||
|
|
||||||
let window = {
|
let window = {
|
||||||
@ -204,6 +212,13 @@ pub trait Application: Sized {
|
|||||||
debug.update_finished();
|
debug.update_finished();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let subscriptions = application.subscriptions();
|
||||||
|
alive_subscriptions.update(
|
||||||
|
subscriptions,
|
||||||
|
&mut thread_pool,
|
||||||
|
&proxy,
|
||||||
|
);
|
||||||
|
|
||||||
// Update window title
|
// Update window title
|
||||||
let new_title = application.title();
|
let new_title = application.title();
|
||||||
|
|
||||||
@ -404,6 +419,66 @@ fn spawn<Message: Send>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct Subscriptions {
|
||||||
|
alive: HashMap<u64, Box<dyn subscription::Handle>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Subscriptions {
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
alive: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update<Message: Send>(
|
||||||
|
&mut self,
|
||||||
|
subscriptions: Subscription<Message>,
|
||||||
|
thread_pool: &mut futures::executor::ThreadPool,
|
||||||
|
proxy: &winit::event_loop::EventLoopProxy<Message>,
|
||||||
|
) {
|
||||||
|
use futures::stream::StreamExt;
|
||||||
|
|
||||||
|
let definitions = subscriptions.definitions();
|
||||||
|
let mut alive = std::collections::HashSet::new();
|
||||||
|
|
||||||
|
for definition in definitions {
|
||||||
|
let id = definition.id();
|
||||||
|
let _ = alive.insert(id);
|
||||||
|
|
||||||
|
if !self.alive.contains_key(&id) {
|
||||||
|
let (stream, handle) = definition.stream();
|
||||||
|
|
||||||
|
let proxy =
|
||||||
|
std::sync::Arc::new(std::sync::Mutex::new(proxy.clone()));
|
||||||
|
|
||||||
|
let future = stream.for_each(move |message| {
|
||||||
|
proxy
|
||||||
|
.lock()
|
||||||
|
.expect("Acquire event loop proxy lock")
|
||||||
|
.send_event(message)
|
||||||
|
.expect("Send subscription result to event loop");
|
||||||
|
|
||||||
|
futures::future::ready(())
|
||||||
|
});
|
||||||
|
|
||||||
|
thread_pool.spawn_ok(future);
|
||||||
|
|
||||||
|
let _ = self.alive.insert(id, handle);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.alive.retain(|id, handle| {
|
||||||
|
let is_still_alive = alive.contains(&id);
|
||||||
|
|
||||||
|
if !is_still_alive {
|
||||||
|
handle.cancel();
|
||||||
|
}
|
||||||
|
|
||||||
|
is_still_alive
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// As defined in: http://www.unicode.org/faq/private_use.html
|
// As defined in: http://www.unicode.org/faq/private_use.html
|
||||||
// TODO: Remove once https://github.com/rust-windowing/winit/pull/1254 lands
|
// TODO: Remove once https://github.com/rust-windowing/winit/pull/1254 lands
|
||||||
fn is_private_use_character(c: char) -> bool {
|
fn is_private_use_character(c: char) -> bool {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user