slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.5k stars 600 forks source link

Separating provision of `Timer` callback and starting it #5898

Open Enyium opened 2 months ago

Enyium commented 2 months ago

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 an App struct that holds a Slint window component along with other data that I need for my app's business. Its new() function returns a type very similar to Rc<RefCell<App>> (including borrow() and borrow_mut(); for the sake of this issue, the concrete type deviation is irrelevant). new() sets up callbacks by cloning the Rc and passing the clones into closures that call borrow() or borrow_mut() to call through to App's methods, so they have nice &self or &mut self parameters. In some App methods, I need to start timers and must, as it stands, redefine their callbacks there everytime, because Timer::start() works that way. Of course, I can't move &self or &mut self into the timer closure; so I have to pass an extra Rc clone into the App methods that start Timers and do closure definition work there that should happen in new().

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 dedicated App methods that are called on Timer expiration, just like with all other callbacks. I would need a single Timer 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:

ogoffart commented 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.

Enyium commented 2 months ago

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.