winit icon indicating copy to clipboard operation
winit copied to clipboard

Add `cursor_position` getter API

Open amrbashir opened this issue 2 years ago • 14 comments

  • [x] Tested on all platforms changed
    • [X] Windows
    • [x] macOS
    • [X] X11
  • [X] Added an entry to CHANGELOG.md if knowledge of this change could be valuable to users
  • [X] Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • [X] Created or updated an example program if it would help users understand this functionality
  • [X] Updated feature matrix, if new features were added or implemented

amrbashir avatar Jan 24 '23 16:01 amrbashir

What's the motivation for getting the cursor position with the new function over getting it via an event ?

filnet avatar Jan 24 '23 17:01 filnet

This is useful for positioning the window on a particular monitor when creating the window, for example, when making a spotlight-like app, where you want to show the window on the monitor the has the cursor, then I would need to:

  1. get the cursor location
  2. get the monitor based on the cursor location
  3. center the app on the monitor

This PR is for 1 and I will be opening another PR for 2, later today or tomorrow.

amrbashir avatar Jan 24 '23 17:01 amrbashir

get the cursor location get the monitor based on the cursor location

This won't work on macOS/Wayland for sure?

kchibisov avatar Jan 24 '23 19:01 kchibisov

Wayland yes, macOS should be possible, no?

amrbashir avatar Jan 24 '23 19:01 amrbashir

for macOS there is no direct API to get a monitor based on a point but we can iterate over available monitors and check if the point is contained within one of them

amrbashir avatar Jan 24 '23 19:01 amrbashir

Wayland yes, macOS should be possible, no?

On Wayland certainly not. Since you know only about the cursor over your window. You don't even have control were to open a window, so use-case you're describing is very specific and can be achieved on X11 and maybe Windows.

kchibisov avatar Jan 24 '23 19:01 kchibisov

And in my experience normal window managers will open a new window where your cursor is if you don't explicitly specify the output you want(like on Wayland it's done by majority).

kchibisov avatar Jan 24 '23 19:01 kchibisov

yeah mostly you don't need to manually position your window but there is use-cases where you want to control which monitor to spawn the window on, for example: spotlight-like apps, widgets, overlays...etc

amrbashir avatar Jan 24 '23 19:01 amrbashir

But you already have all in-place for that? You could just report current_monitor on the event_loop or such?

kchibisov avatar Jan 24 '23 20:01 kchibisov

On Wayland certainly not. Since you know only about the cursor over your window. You don't even have control were to open a window, so use-case you're describing is very specific and can be achieved on X11 and maybe Windows.

You should be able to implement a centered "spotlight-like" app like this on Wayland with layer-shell (in compositors that support it), but yeah, you couldn't implement it this way.

ids1024 avatar Jan 24 '23 20:01 ids1024

This is useful for positioning the window on a particular monitor when creating the window, for example, when making a spotlight-like app, where you want to show the window on the monitor the has the cursor, then I would need to:

1. get the cursor location

2. get the monitor based on the cursor location

3. center the app on the monitor

This PR is for 1 and I will be opening another PR for 2, later today or tomorrow.

May be you can keep 1 private for 2 ?

filnet avatar Jan 30 '23 16:01 filnet

May be you can keep 1 private for 2 ?

2 has been rejected in https://github.com/rust-windowing/winit/pull/2649 so this API is no longer connected to 2 directly

amrbashir avatar Jan 30 '23 16:01 amrbashir

As I mentioned earlier, the "spotlight-like app" use case on Wayland would be served by layer shell (https://github.com/rust-windowing/winit/issues/2582). I think what would be ideal for that is if either winit or an extension crate could provide an API that can use layer shell, or position things more manually on X/win32/etc. to provide as portable a solution as possible for this particular use case.

I would generally agree with the authors of the base Wayland protocol that applications placing windows at specific spots on screen is generally a bad practice outside of specialized things like this (though Winit already has set_outer_position).

https://github.com/rust-windowing/winit/issues/2160 is relevant, wrt including things in winit vs a separate crate.

ids1024 avatar Jan 30 '23 17:01 ids1024

Seems like a couple of tests are failing on nightly but should be unrelated to this PR

amrbashir avatar Mar 26 '24 14:03 amrbashir