Skip to content

Rework event handling to be based on a trait #3432

@madsmtm

Description

@madsmtm

Part of #3367, opening to discuss separately.

Winit needs some way for the user to synchronously respond to events with a value.

Prominent examples include:

To do this, we propose allowing the user to implement a trait, where each method is a callback that will be called when a certain event happens. Roughly:

// API
pub trait ApplicationHandler {
    fn new_events(&mut self, event_loop: ActiveEventLoop<'_>, start_cause: StartCause);

    fn resized(
        &mut self,
        event_loop: ActiveEventLoop<'_>,
        window_id: WindowId,
        size: PhysicalSize<u32>
    ) -> bool;

    // ... Further events
}

// User code
struct App {
    window: Option<Window>,
}
impl Application for App { ... }

fn main() {
    event_loop.run(App { window: None })?;
}

Feel free to update this code-block once we narrow down the actual API.


Identified problems to which we need some sort of solution:

Implementation plan:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions