Open Enyium opened 2 months ago
For reference, in C++, there is a constructor that takes an interval and callback: https://releases.slint.dev/1.7.2/docs/cpp/api/structslint_1_1timer#_CPPv4I_NSt9invocableEEN5slint5Timer5TimerENSt6chrono12millisecondsE1F (but it will then be running) We could have also a rust consturctor function that creates a started timer.
We could have also a rust consturctor function that creates a started timer.
If it created a started Timer
, I'd have to stop()
it right away. Seems like a quirk to me.
I use Rust.
The way the
Timer
API is structured, I must do some noisy extra work to be able to access what's needed in its callback. I have anApp
struct that holds a Slint window component along with other data that I need for my app's business. Itsnew()
function returns a type very similar toRc<RefCell<App>>
(includingborrow()
andborrow_mut()
; for the sake of this issue, the concrete type deviation is irrelevant).new()
sets up callbacks by cloning theRc
and passing the clones into closures that callborrow()
orborrow_mut()
to call through toApp
's methods, so they have nice&self
or&mut self
parameters. In someApp
methods, I need to start timers and must, as it stands, redefine their callbacks there everytime, becauseTimer::start()
works that way. Of course, I can't move&self
or&mut self
into the timer closure; so I have to pass an extraRc
clone into theApp
methods that startTimer
s and do closure definition work there that should happen innew()
.I wish
Timer
would allow to pass its callback once on creation, and then provide the means to start, restart and stop the timer at will. I would then just have dedicatedApp
methods that are called onTimer
expiration, just like with all other callbacks. I would need a singleTimer
method to start or restart it, no matter whether it was started before (in my app, certain code should run after user actions subsided for a while, which means constant restarts on user actions).If this accepted: While you're at it, breaking
Timer
's API:set_interval()
could be namedrestart_with_interval()
.TimerMode
, maybe withrestart_with_details()
with parameters forTimerMode
and interval."one-shot timer"
yields more results than"single-shot timer"
and is shorter, which could justify adopting the first term in the crate (Rust's API guidelines say, "contractions of compound words count as one word: use [...]Stdin
rather thanStdIn
", which may mean that it should beoneshot
rather thanone_shot
, which would be written with a hyphen in a natural-language context).