pyglet / pyglet

pyglet is a cross-platform windowing and multimedia library for Python, for developing games and other visually rich applications.
http://pyglet.org
BSD 3-Clause "New" or "Revised" License
1.89k stars 305 forks source link

on_file_drop_enter event would be really appreciated. #883

Open crunchpaste opened 1 year ago

crunchpaste commented 1 year ago

Is your feature request related to a problem? Please describe. It is great that the Window class received the on_file_dropped event (I believe recently), but i feel the lack of a way to detect if a user is dragging a file kind of limits its usefulness.

Describe the solution you'd like It would be great if there was an event that could be triggered when a user is dragging files (or at least just dragging) to the window, so that the app can show some visual clue that everything is working properly. Currently you can't even use on_mouse_enter as a workaround as that wont work if the user is already dragging the mouse. on_mouse_leave has the same issue.

I believe it is confusing to users to not be able to see any sort of indicator that dragging the file is recognized by the app, and it would, in fact, work.

Maybe there is a workaround I'm not aware of.

benmoran56 commented 1 year ago

This might be doable, but there could be inconsistencies across platforms. Linux is at least possible to detect drag-and-drop entry, but not the entry position (at least not that I could see). Once you release the mouse button, you receive a late on_entry event as well. For Windows as macOS, it needs more research.

pushfoo commented 1 year ago

there could be inconsistencies across platforms

Not just across platforms, but within them: Wayland is well-known for causing problems with drag and drop. Examples I've experienced include:

Flatpak and other sandboxing tools add another layer of complication on top of this. It's worth mentioning these problems briefly in the doc for any such feature.

pushfoo commented 1 year ago

tl;dr It seems possible for not only mac application windows, but individual UI elements within them.

I started looking into UI as part of helping with #931. Although I'll try to finish that ticket, I don't have a mac. This ticket is probably best handled by someone else, so I'm writing down everything I've found for future reference.

There seem to be two relevant root classes for UI elements in the Apple ecosystem: UIView and NSView. We want the NS* API since it's the desktop version according to what I see on the doc and StackOverlow:

  1. NSDraggingInfo.draggingLocation seems to be what we want, but we should double check the coordinate system details

  2. We seem to have at least partially implemented drag support already:

  3. If I understand correctly, we still need to implement NSDraggingView for the cocoa window, of which the wantsPeriodicDraggingUpdates instance method seems relevant

We may need to subscribe to get events as well somehow, but I'm not clear on this. The drag and drop page here is worth reading more closely. Although I may have misunderstood the details, I think we should we should consider refactoring our UI / Window stack to support individual widgets as drag and drop targets, not just the entire window.