by dan-robertson · 6y ago · 0 comments
What API alternatives are there to callbacks for widgets that may signal multiple events? One object would be to pass some implementation of some “Events” trait which would have methods to be called for each event kind. This is basically callbacks (I don’t think rust offers a way to make inline trait implementations and I don’t know if there’s a way to implicitly have default implementations that do nothing). This feels similar to in Java where you might subclass Button (I think. It’s been a while since I looked at any Java at all, let alone gui code).
One way to deal with events is a more imgui-like api where you write e.g.
if (make_button("click me")) {
/* just been clicked */
}
But I don’t see how this would work for lots of events (one option would be to have the return value be e.g. ButtonEvent option where the ButtonEvent type is an enum of all possible events a button may signal but there isn’t sub typing for variants so you either need lots of similar types for different controls and lots of pointless conversions or the types say that a control may signal events which it in fact never does. Also it may be possible to have multiple events hit a widget in a frame (eg mouseEnter, mouseMove; focus, mouseDown; mouseUp, click) so the option should be a sequence and you lose the ability to use “if let” when making the controls). A second issue is that this seems likely to add a frame of latency in many cases (eg if you have a wizard with a “next” buttons then you get the mouse event for the button being clicked by rerendering the dram as-is, and then on the next render you get to do the next page).Perhaps I should just look at the code to see what Druid and crochet do.
We don't have the one frame latency, as it's not imgui under the hood. (There's a bug in the Crochet prototype on this, but it's known and will be fixed).
Hope this helps clear things up a bit.
These gestures seems not internal to the widgets, but also mutate the widgets state in one way or another and need some abstraction to handle the common behavior.
I did quite a lot of different attempts to GUI Holy Grail in last 20 years ( https://sciter.com/10-years-road-to-sciter/ ) . I am pretty confident that DOM and DOM events is the most flexible (and so robust) foundation so far.