Skip to content

macos: create unfocused window #3072

@rparrett

Description

@rparrett

Using macos 13.4.1.
Testing with the main branch.

I'd like to create a single window that begins its life visible but unfocused, never stealing focus.

with_active(false) seems like it should accomplish this, but with that, the new window still appears on top of every other window, reacts to key presses, etc.

Even with .with_window_level(winit::window::WindowLevel::AlwaysOnBottom) and .with_visible(false), the window seems to take focus away from the currently focused window, interrupting typing (but doesn't seem to react to keypresses itself).

Repro:

sleep 5 && cargo run
# now before the window opens, switch back to a text editor
# and attempt to type some things without being interrupted by the new window stealing focus
use simple_logger::SimpleLogger;
use winit::{
    event::{Event, WindowEvent},
    event_loop::EventLoop,
    window::WindowBuilder,
};

fn main() {
    SimpleLogger::new().init().unwrap();

    let event_loop = EventLoop::new();

    let window = WindowBuilder::new()
        .with_active(false)
        .build(&event_loop)
        .unwrap();

    event_loop.run(move |event, _, control_flow| {
        control_flow.set_wait();

        match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                window_id,
            } if window_id == window.id() => control_flow.set_exit(),
            _ => (),
        }
    });
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DS - appkitAffects the AppKit/macOS backendF - questionThere's no such thing as a stupid one

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions