Skip to content

Parallel example (hello_world_par) seems to rely on continous repaint, "Main thread" sub-window ignores input when idle #7828

@schnaader

Description

@schnaader

Describe the bug
When running the parallel hello_world example from "examples\hello_world_par", initially everything looks fine. The two initial background thread sub-windows work and are interactive. However, the "Spawn another thread" button in the "Main thread" sub-window does not react at all (no hover highlight, no click).

After playing around with the code, I found that forcing continous repainting - f.e. with a frame counter - leads to the expected behavior. So my best guess would be that the example relies on continous repainting which isn't guaranteed anymore in the newest versions, regarding this note from the README:

egui only repaints when there is interaction (e.g. mouse movement) or an animation, so if your app is idle, no CPU is wasted.

Code changes needed for workaround:

struct MyApp {
    [...]
    frame_count: u64, // this
}

[...]

impl MyApp {
    fn new() -> Self {
        let threads = Vec::with_capacity(3);
        let (on_done_tx, on_done_rc) = mpsc::sync_channel(0);

        let mut slf = Self {
            threads,
            on_done_tx,
            on_done_rc,
            frame_count: 0, // this
        };

        [...]

impl eframe::App for MyApp {
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        self.frame_count += 1; // this
        
        egui::Window::new("Main thread").show(ui.ctx(), |ui| {
            ui.label(format!("frames: {}", self.frame_count)); // this

            if ui.button("Spawn another thread").clicked() {
                self.spawn_thread();
            }
        });

        [...]

To Reproduce
Steps to reproduce the behavior:

  1. Compile and run "hello_world_par" from the examples in release mode ("cargo run --release").
  2. Interact with the sub-windows "Background thread X" -> works as expected, buttons are clickable and highlighted on hover
  3. Try to interact with the "Spawn another thread" button -> no highlight, not clickable

Expected behavior
I'd expect the "Spawn another thread" button to highlight and spawn additional sub-windows, which doesn't happen without the described "continous repaint" workaround.

Screenshots
Image

(Note the mouse cursor is hovering "Spawn another thread" button, but it isn't highlighted by a white border)

Desktop

  • OS: Reproducable under both Windows 11 and Ubuntu 22.04

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething is broken

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions