Here's a video demo of using custom async proc's with Fidget to animate a UI: https://youtu.be/nRFoflV3MI0 and the example code.
It turns out the required user code is as simple as creating an async proc:
var progressBar: float = 0.02
var ticks: Future[void] = emptyFuture() ## Create an completed "empty" future
proc ticker() {.async.} =
## Every tick will increment the progress bar 10% until its done.
for i in 1..10:
await sleepAsync(1_000)
progressBar = 0.1 * i.toFloat()
echo fmt"tick {bar}"
refresh()
Then trigger it in the UI:
rectangle "run animation button":
box barW-80, 0, 40, 40
onClick:
if ticks.finished():
echo "setup new ticker"
ticks = ticker()
else:
echo "ticker already running!"
Enabling that use case required tweaking Fidget and Nim's async/ioselectors on MacOSX. The Fidget changes were just adding an AsyncEvent callback on keyboard/mouse inputs.
Unfortunately on MacOSX it was painfully slow. That's because Nim's AsyncEvent relies on user events in selectors. On MacOSX and BSD user events in the selectors module are emulated using pipes. This cause me headaches before so I created a PR to enable using kqueues's EVFILT_USER. Though it'll need some testing/work the latency is much better at about ~1ms.
Wow, as the maker of Fidget, that is pretty cool.
Thanks! Fidget is awesome.
I would probably not do it through the async system but its cool that you got that to work. I would probably just have a progress variable and call refresh() when some thing happens. But it's neat that you got this to work.
That's essentially what the async animation code does. The UI code itself doesn't do async calls or awaits.
Instead it just creates a new future which runs in the "background". I didn't know it was possible to use async's that way. It works since Fidget calls poll() already. Neat!
The only alternative non-async approach I can see to trigger "things" would be to have Fidget run refresh loops at a fixed rate (e.g. 60 Hz). Maybe only between an animation "start" and "stop". You'd still need one shot timers, user events, etc.