Created
July 7, 2020 02:12
-
-
Save mtkennerly/b513e7fe89c735e5a5df672c503404d7 to your computer and use it in GitHub Desktop.
Iced ignores some commands in Command::batch if they finish too fast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[package] | |
name = "repro" | |
version = "0.1.0" | |
authors = ["mtkennerly <mtkennerly@gmail.com>"] | |
edition = "2018" | |
[dependencies] | |
# iced = "0.1.1" | |
iced = { git = "https://github.com/hecrj/iced" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use iced::{ | |
button, executor, Application, Button, Column, Command, Element, ProgressBar, Row, Text, | |
}; | |
const TOTAL_STEPS: usize = 20_000; | |
#[derive(Debug, Clone)] | |
enum Message { | |
Start, | |
Step { output: usize }, | |
Complete, | |
} | |
#[derive(Default)] | |
struct App { | |
current: f32, | |
max: f32, | |
start_button: button::State, | |
} | |
impl Application for App { | |
type Executor = executor::Default; | |
type Message = Message; | |
type Flags = (); | |
fn new(_flags: ()) -> (Self, Command<Message>) { | |
(Self::default(), Command::none()) | |
} | |
fn title(&self) -> String { | |
"Repro".to_string() | |
} | |
fn update(&mut self, message: Message) -> Command<Message> { | |
match message { | |
Message::Start => { | |
self.current = 0.0; | |
self.max = TOTAL_STEPS as f32; | |
let mut commands = Vec::<Command<Message>>::new(); | |
for i in 0..TOTAL_STEPS { | |
commands.push(Command::perform( | |
async move { | |
// It works if you uncomment this line: | |
// std::thread::sleep(std::time::Duration::from_millis(1)); | |
i | |
}, | |
move |output| Message::Step { output }, | |
)); | |
} | |
Command::batch(commands) | |
} | |
Message::Step { output } => { | |
println!("Step {}", output); | |
self.current += 1.0; | |
if self.current >= self.max { | |
Command::perform(async move {}, |_| Message::Complete) | |
} else { | |
Command::none() | |
} | |
} | |
Message::Complete => { | |
println!("Complete"); | |
self.current = 0.0; | |
self.max = 0.0; | |
Command::none() | |
} | |
} | |
} | |
fn view(&mut self) -> Element<Message> { | |
Column::new() | |
.push(Row::new().push( | |
Button::new(&mut self.start_button, Text::new("Start")).on_press(Message::Start), | |
)) | |
.push(Row::new().push(Text::new(format!( | |
"Progress: {} of {}", | |
self.current, self.max | |
)))) | |
.push(ProgressBar::new(0.0..=self.max, self.current)) | |
.into() | |
} | |
} | |
fn main() { | |
App::run(iced::Settings::default()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment