# How do I create a window and handle its events with winit?
Source: https://ruststudio.org/community/t/244/how-do-i-create-a-window-and-handle-its-events-with-winit
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, Windows 11, winit 0.30.13; `cargo check` only, the window was not opened. The winit API changes between releases: this is the 0.30 shape.
Category: Help and questions | Kind: FAQ | Tags: winit, window, event-loop, desktop | Views: 0

## Short answer
With winit 0.30 you implement ApplicationHandler on your own struct, create the window inside resumed, handle WindowEvent values in window_event, and pass the struct to EventLoop::run_app. Closing, resizing, scale changes and dropped files arrive as WindowEvent variants. The code passed cargo check on Windows.

## Answer
## Answer
`winit` is the crate that creates windows and delivers events on Windows, macOS and Linux. Several toolkits (egui's eframe, Slint's winit backend, and others) are built on it. You use it directly when you draw the window yourself, for example with a graphics API.

In winit 0.30 you write a struct that implements `ApplicationHandler`. The event loop calls its methods; you create the window in `resumed`, the moment the system says it is ready.

```toml title=Cargo.toml
[dependencies]
winit = "0.30"
```

```rust title=src/main.rs
use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};

#[derive(Default)]
struct App {
    window: Option<Window>,
}

impl ApplicationHandler for App {
    // Windows must be created here: the system says it is now ready for them.
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        let attrs = Window::default_attributes().with_title("winit window");
        self.window = Some(event_loop.create_window(attrs).expect("cannot create the window"));
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => event_loop.exit(),
            WindowEvent::Resized(size) => println!("resized to {}x{} physical pixels", size.width, size.height),
            WindowEvent::ScaleFactorChanged { scale_factor, .. } => println!("scale factor is now {scale_factor}"),
            WindowEvent::DroppedFile(path) => println!("dropped: {}", path.display()),
            WindowEvent::RedrawRequested => { /* draw the frame here */ }
            _ => {}
        }
    }
}

fn main() -> Result<(), winit::error::EventLoopError> {
    let event_loop = EventLoop::new()?;
    event_loop.set_control_flow(ControlFlow::Wait); // sleep until something happens
    event_loop.run_app(&mut App::default())
}
```

This passed `cargo check` with winit 0.30.13 on Windows 11. It was not run.

Notes:

- `ControlFlow::Wait` makes the loop sleep until an event arrives; `ControlFlow::Poll` runs it continuously, which uses a CPU core.
- A window shows nothing by itself. To draw you add a renderer (a graphics API, or a software buffer crate); winit only gives you the window and the events.
- Sizes in `Resized` are in physical pixels. See the thread on high-DPI for the difference between logical and physical sizes.

## Common mistakes
- Following an example from before winit 0.30, which creates the window before the loop starts and passes a closure to `run`. In 0.30 the `ApplicationHandler` style shown here is the one to use; check the version in your `Cargo.toml`.
- Treating `resumed` as "called once". Guard with `if self.window.is_none()` so a second call does not create a second window.
- Doing slow work in `window_event`; it blocks the loop.

## Related questions
### Do I need winit if I use egui or Slint?
No. Those toolkits use it for you.

## Sources
- winit documentation: https://docs.rs/winit/0.30.13/winit/ (read 2026-12-05)
- winit repository: https://github.com/rust-windowing/winit (read 2026-12-05)
