I am trying to figure out how to avoid losing keystrokes that are made in anticipation of the completion of an async operation.
This is in the context of trying to implement a little type-ahead search box for a file browser.
In this specific application, the user will type a query, select a match using the arrow keys and then hit enter to either navigate to a selected folder or open a selected file. If the user navigates to a folder they should immediately be able to type again to query within that folder.
The important thing is that no keystrokes should ever go missing. Hitting down within the type-ahead search box while the file list is loading should still select the right entry once the results appear. Typing after hitting enter on a previous query should start a search from the right place even if the previous query hasn’t finished yet. The user can be assumed to be very familiar with the contents of the filesystem and able to navigate it without visual feedback.
What complicates things is that the filesystem might be slow and or unreliable. It can take a long time to get a result back and during that time the window should continue rendering. It should also be possible to cancel the operation by clicking the cancel or close buttons or, ideally, by hitting Escape.
Running the query on the main thread makes lost keystrokes impossible but breaks interactivity.
I think the ideal would be to add an event controller to the window to capture and buffer keyboard GdkEvents. It would provide methods to begin capture and then to either replay or discard the buffered events. An earlier controller could intercept Escape presses, cancel the operation and discard the buffered events.
Unfortunately I don’t see any way to implement such a thing outside upstream GTK. gdk_display_put_event is rightfully deprecated and, in any case, will shuffle input by putting events at the end of the queue rather than the beginning*.
Is there a more principled way to go about getting this to work? Would there be an appetite for such a controller in upstream GTK?**
*A problem also noted by the webkit devs, although their use case is a bit different. See https://github.com/WebKit/WebKit/pull/8663.
**Only reference I can find to previous discussions is Updates from inside GTK – GTK Development Blog