Skip to content

Instantly share code, notes, and snippets.

@mtkennerly
Created November 11, 2022 12:40
Show Gist options
  • Save mtkennerly/c604cc74f2087d4827f9c8513ee0447f to your computer and use it in GitHub Desktop.
Save mtkennerly/c604cc74f2087d4827f9c8513ee0447f to your computer and use it in GitHub Desktop.
Iced 0.5.2 ignores some commands in Command::batch if they finish too fast
[package]
name = "repro"
version = "0.1.0"
authors = ["mtkennerly <mtkennerly@gmail.com>"]
edition = "2021"
[dependencies]
iced = "0.5.2"
# iced = { version = "0.5.2", features = ["default_system_font", "glow"] }
# iced = { git = "https://github.com/hecrj/iced" }
use iced::{
executor, Application, Command, Element, Theme,
widget::{Button, Column, 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,
}
impl Application for App {
type Executor = executor::Default;
type Message = Message;
type Flags = ();
type Theme = Theme;
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(&self) -> Element<Message> {
Column::new()
.push(Row::new().push(
Button::new(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()).unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment