Skip to content

Windows: Only one window will be redraw in a multiple window app #4460

@csmoe

Description

@csmoe

Description

use std::collections::HashMap;
use winit::{
    application::ApplicationHandler,
    dpi::LogicalSize,
    event::WindowEvent,
    event_loop::{ActiveEventLoop, ControlFlow, EventLoop},
    window::{Window, WindowAttributes, WindowId},
};

#[derive(Default)]
struct App {
    windows: HashMap<WindowId, Window>,
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let attrs1 = WindowAttributes::default()
            .with_title("1")
            .with_inner_size(LogicalSize::new(400.0, 300.0));
        let window1 = event_loop.create_window(attrs1).unwrap();

        let attrs2 = WindowAttributes::default()
            .with_title("2")
            .with_inner_size(LogicalSize::new(400.0, 300.0));
        let window2 = event_loop.create_window(attrs2).unwrap();

        self.windows.insert(window1.id(), window1);
        self.windows.insert(window2.id(), window2);

        for window in self.windows.values() {
            window.request_redraw();
        }
    }

    fn window_event(
        &mut self,
        event_loop: &ActiveEventLoop,
        window_id: WindowId,
        event: WindowEvent,
    ) {
        match event {
            WindowEvent::CloseRequested => {
                self.windows.remove(&window_id);
                if self.windows.is_empty() {
                    event_loop.exit();
                }
            }
            WindowEvent::RedrawRequested => {
                println!("RedrawRequested for window {:?}", window_id);

                for window in self.windows.values() {
                    window.request_redraw();
                }
            }

            _ => {}
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();

    event_loop.set_control_flow(ControlFlow::Wait);

    let mut app = App::default();
    event_loop.run_app(&mut app).unwrap();
}

When this app is running, I expect the two windows be redraw at the same time, but only one window gets the RedrawRequested event

Image

Windows version

Edition	Windows 11 Pro
Version	24H2
Installed on	‎5/‎15/‎2025
OS build	26100.7462
Experience	Windows Feature Experience Pack 1000.26100.275.0

Winit version

0.30.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugDang, that shouldn't have happenedDS - win32Affects the Win32/Windows backend

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions