Skip to content

Commit

Permalink
Merge pull request #62 from hecrj/feature/async-actions
Browse files Browse the repository at this point in the history
Async actions
  • Loading branch information
hecrj authored Nov 18, 2019
2 parents 54ffefc + 63dbf07 commit 5adefdf
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 132 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ iced_web = { version = "0.1.0-alpha", path = "web" }

[dev-dependencies]
env_logger = "0.7"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
directories = "2.0"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen = "0.2.51"
7 changes: 7 additions & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,10 @@ edition = "2018"
description = "The essential concepts of Iced"
license = "MIT"
repository = "https://github.com/hecrj/iced"

[features]
# Exposes a future-based `Command` type
command = ["futures"]

[dependencies]
futures = { version = "0.3", optional = true }
49 changes: 49 additions & 0 deletions core/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use futures::future::{BoxFuture, Future, FutureExt};

pub struct Command<T> {
futures: Vec<BoxFuture<'static, T>>,
}

impl<T> Command<T> {
pub fn none() -> Self {
Self {
futures: Vec::new(),
}
}

pub fn perform<A>(
future: impl Future<Output = T> + 'static + Send,
f: impl Fn(T) -> A + 'static + Send,
) -> Command<A> {
Command {
futures: vec![future.map(f).boxed()],
}
}

pub fn batch(commands: impl Iterator<Item = Command<T>>) -> Self {
Self {
futures: commands.flat_map(|command| command.futures).collect(),
}
}

pub fn futures(self) -> Vec<BoxFuture<'static, T>> {
self.futures
}
}

impl<T, A> From<A> for Command<T>
where
A: Future<Output = T> + 'static + Send,
{
fn from(future: A) -> Self {
Self {
futures: vec![future.boxed()],
}
}
}

impl<T> std::fmt::Debug for Command<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Command").finish()
}
}
4 changes: 4 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ pub mod widget;
mod align;
mod background;
mod color;
#[cfg(feature = "command")]
mod command;
mod font;
mod length;
mod point;
Expand All @@ -12,6 +14,8 @@ mod vector;
pub use align::Align;
pub use background::Background;
pub use color::Color;
#[cfg(feature = "command")]
pub use command::Command;
pub use font::Font;
pub use length::Length;
pub use point::Point;
Expand Down
2 changes: 1 addition & 1 deletion core/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ where
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct State {
pub is_focused: bool,
cursor_position: usize,
Expand Down
14 changes: 10 additions & 4 deletions examples/scroll.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use iced::{
button, scrollable, Align, Application, Button, Container, Element, Image,
Length, Scrollable, Text,
button, scrollable, Align, Application, Button, Command, Container,
Element, Image, Length, Scrollable, Text,
};

pub fn main() {
env_logger::init();

Example::default().run()
Example::run()
}

#[derive(Default)]
Expand All @@ -25,16 +25,22 @@ pub enum Message {
impl Application for Example {
type Message = Message;

fn new() -> (Example, Command<Message>) {
(Example::default(), Command::none())
}

fn title(&self) -> String {
String::from("Scroll - Iced")
}

fn update(&mut self, message: Message) {
fn update(&mut self, message: Message) -> Command<Message> {
match message {
Message::AddItem => {
self.item_count += 1;
}
}

Command::none()
}

fn view(&mut self) -> Element<Message> {
Expand Down
Loading

0 comments on commit 5adefdf

Please sign in to comment.