Skip to content

Wrong cursor position on clicks in top window area, when window is created invisible and maximised on MacOS #4295

Open
@lucasmerlin

Description

@lucasmerlin

Description

This is a odd one:

When creating a window with with_visible(false) (and then later showing it by calling set_visible(true)), the cursor position in the top 20 or so pixels will be wrong during a click, if the window is maximised on an external display. On the built in display it's fine.

Here is a minimal reproduction (for the 0.30.x branch):

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 {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(
            event_loop.create_window(Window::default_attributes().with_visible(false)).unwrap(),
        );
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
        if let Some(window) = &mut self.window {
            window.set_visible(true);
        }
        match event {
            WindowEvent::CloseRequested => {
                event_loop.exit();
            },
            WindowEvent::RedrawRequested => {
                self.window.as_ref().unwrap().request_redraw();
            },
            WindowEvent::CursorMoved { position, .. } => {
                println!("Cursor moved to: {:?}", position);
            },
            _ => (),
        }
    }
}

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);
}

This will log something like this:

Cursor moved to: PhysicalPosition { x: 129.93359375, y: 8.98046875 }
Cursor moved to: PhysicalPosition { x: -1598.06640625, y: -314.01953125 }                        <- this is when I clicked
Cursor moved to: PhysicalPosition { x: 129.93359375, y: 8.98046875 }

macOS version

ProductName:		macOS
ProductVersion:		15.5
BuildVersion:		24F74

Winit version

0.30.x, but the problem also exists for older versions (git bisect in egui let me to a commit from 2022 when the delayed visibility was introduced in egui), which leads me to believe this must be a new MacOS bug? Maybe something about how they handle the menu bar or the cutout, since it's not a problem on the built in retina display?

Adding a log point here seems to confirm that:

fn mouse_motion(&self, event: &NSEvent) {

[src/platform_impl/macos/view.rs:1056:9] &event = NSEvent {
    __superclass: NSEvent: type=LMouseDown loc=(171.258,1427.27) time=882628.7 flags=0 win=0x124308440 winNum=102082 ctxt=0x0 evNum=-13239 click=6 buttonNumber=0 pressure=1 deviceID:0x70000000000006a subtype=NSEventSubtypeTouch,
}
Cursor moved to: PhysicalPosition { x: 171.2578125, y: 12.7265625 }
[src/platform_impl/macos/view.rs:1056:9] &event = NSEvent {
    __superclass: NSEvent: type=MouseMoved loc=(-1556.74,1750.27) time=882628.7 flags=0 win=0x124308440 winNum=102082 ctxt=0x0 evNum=0 click=0 buttonNumber=0 pressure=0 deltaX=0.000000 deltaY=0.000000 deviceID:0x0 subtype=0,
}
Cursor moved to: PhysicalPosition { x: -1556.7421875, y: -310.2734375 }
[src/platform_impl/macos/view.rs:1056:9] &event = NSEvent {
    __superclass: NSEvent: type=LMouseUp loc=(171.258,1427.27) time=882628.8 flags=0 win=0x124308440 winNum=102082 ctxt=0x0 evNum=-13239 click=6 buttonNumber=0 pressure=0 deviceID:0x70000000000006a subtype=NSEventSubtypeTouch,
}
Cursor moved to: PhysicalPosition { x: 171.2578125, y: 12.7265625 }

Related issue where this bug first appeared: rerun-io/rerun#10280

Update:

Apparently even just starting the window in Fullscreen is enough to trigger the issue for me, so the following also reproduces this:

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Fullscreen, Window, WindowId};

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

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(
            event_loop.create_window(Window::default_attributes().with_fullscreen(Some(Fullscreen::Borderless(None)))).unwrap(),
        );
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => {
                event_loop.exit();
            },
            WindowEvent::RedrawRequested => {
                self.window.as_ref().unwrap().request_redraw();
            },
            WindowEvent::CursorMoved { position, .. } => {
                println!("Cursor moved to: {:?}", position);
            },
            _ => (),
        }
    }
}

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    B - bugDang, that shouldn't have happenedDS - macos

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions