libsdl-org / SDL

Simple Directmedia Layer
https://libsdl.org
zlib License
9.53k stars 1.77k forks source link

Interactive drag and drop api ideas #10448

Open edo9300 opened 1 month ago

edo9300 commented 1 month ago

This is something I wanted to propose to SDL sooner or later, but with the recent drag and drop related prs that are being made (https://github.com/libsdl-org/SDL/pull/10447, https://github.com/libsdl-org/SDL/pull/10435, https://github.com/libsdl-org/SDL/pull/10323), I think it's time to discuss about it. Currently even with the pull request using IDropTarget on Windows, SDL only supports dragging over the whole window, but all the currently implemented "apis" in the various supported backends actually support controlling wether the drag and drop at those coordinates is accepted or not, thus allowing the application to define drop target boundaries with the os also showing the according cursor icons (for example on windows it shows the disabled icon). The issue with dealing with those responses, is that they all require an immediate answer as soon as the drag enter/position event is received, for example on windows, if the drag would not be accepted, pdwEffect would have to be set to DROPEFFECT_NONE, on my own implementation of this drag and drop support for my own program (supporting the same platforms as SDL), I handle this by having the program provide a callback function called with the current coordinates and wether the current operation is a file drag or a text drag, and that will return wether the drop should be accepted or not. I tried to look into SDL if there were already some apis using a similar callback approach to provide an immediate response to an event, skipping the event loop alltogether, but I couldn't find any. I'm creating this issue first without writing any code as I really don't know what the best way to handle this api could could be to respect the way SDL is structured, and I'd receive some feedback regarding an handling that would play nicely with SDL's design. I think the best approach would be the direct callback so that the user can do whatever check the application needs, wether it be as simple as boundary checking, or complex depending on various states of the application, an instead simpler approach that would require no callback that comes to mind would be providing a series of rects to SDL specifying the boundaries of the various drop targets that will then be checked on a drag operation event.

zturtleman commented 1 month ago

For what it's worth, SDL_SetWindowHitTest() uses a callback (running at any time) to check if the cursor is in a custom region for window resizing or window dragging (title bar).

edo9300 commented 1 month ago

For what it's worth, SDL_SetWindowHitTest() uses a callback (running at any time) to check if the cursor is in a custom region for window resizing or window dragging (title bar).

This indeed looks like the function I was looking for. I'd say for drag and drop, an appropriate callback function could be of the like

typedef SDL_bool (SDLCALL *SDL_DropTest)(SDL_Window *win, const SDL_Point *area, SDL_bool is_text, void *data);