Skip to main content

i_slint_core/
timers.rs

1// Copyright © SixtyFPS GmbH <info@slint.dev>
2// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0
3
4// cSpell: ignore singleshot
5
6/*!
7    Support for timers.
8
9    Timers are just a bunch of callbacks sorted by expiry date.
10*/
11
12#![warn(missing_docs)]
13use alloc::boxed::Box;
14use alloc::vec::Vec;
15use core::{
16    cell::{Cell, RefCell},
17    num::NonZeroUsize,
18};
19
20use crate::animations::Instant;
21
22type TimerCallback = Box<dyn FnMut()>;
23type SingleShotTimerCallback = Box<dyn FnOnce()>;
24
25/// The TimerMode specifies what should happen after the timer fired.
26///
27/// Used by the [`Timer::start()`] function.
28#[derive(Copy, Clone)]
29#[repr(u8)]
30#[non_exhaustive]
31pub enum TimerMode {
32    /// A SingleShot timer is fired only once.
33    SingleShot,
34    /// A Repeated timer is fired repeatedly until it is stopped or dropped.
35    Repeated,
36}
37
38/// Timer is a handle to the timer system that triggers a callback after a specified
39/// period of time.
40///
41/// Use [`Timer::start()`] to create a timer that repeatedly triggers a callback, or
42/// [`Timer::single_shot`] to trigger a callback only once.
43///
44/// The timer will automatically stop when dropped. You must keep the Timer object
45/// around for as long as you want the timer to keep firing.
46///
47/// Timers can only be used in the thread that runs the Slint event loop. They don't
48/// fire if used in another thread.
49///
50/// ## Example
51/// ```rust,no_run
52/// # i_slint_backend_testing::init_no_event_loop();
53/// use slint::{Timer, TimerMode};
54/// let timer = Timer::default();
55/// timer.start(TimerMode::Repeated, std::time::Duration::from_millis(200), move || {
56///    println!("This will be printed every 200ms.");
57/// });
58/// // ... more initialization ...
59/// slint::run_event_loop();
60/// ```
61#[derive(Default)]
62pub struct Timer {
63    id: Cell<Option<NonZeroUsize>>,
64    /// The timer cannot be moved between treads
65    _phantom: core::marker::PhantomData<*mut ()>,
66}
67
68impl Timer {
69    /// Starts the timer with the given mode and interval, in order for the callback to called when the
70    /// timer fires. If the timer has been started previously, then it will be restarted, no matter if
71    /// it has already been fired or not.
72    ///
73    /// Arguments:
74    /// * `mode`: The timer mode to apply, i.e. whether to repeatedly fire the timer or just once.
75    /// * `interval`: The duration from now until when the timer should fire the first time, and subsequently
76    ///   for repeated [`Repeated`](TimerMode::Repeated) timers.
77    /// * `callback`: The function to call when the time has been reached or exceeded.
78    pub fn start(
79        &self,
80        mode: TimerMode,
81        interval: core::time::Duration,
82        callback: impl FnMut() + 'static,
83    ) {
84        let _ = CURRENT_TIMERS.try_with(|timers| {
85            let mut timers = timers.borrow_mut();
86            let id = timers.start_or_restart_timer(
87                self.id(),
88                mode,
89                interval,
90                CallbackVariant::MultiFire(Box::new(callback)),
91            );
92            self.set_id(Some(id));
93        });
94    }
95
96    /// Starts the timer with the duration and the callback to called when the
97    /// timer fires. It is fired only once and then deleted.
98    ///
99    /// Arguments:
100    /// * `duration`: The duration from now until when the timer should fire.
101    /// * `callback`: The function to call when the time has been reached or exceeded.
102    ///
103    /// ## Example
104    /// ```rust
105    /// # i_slint_backend_testing::init_no_event_loop();
106    /// use slint::Timer;
107    /// Timer::single_shot(std::time::Duration::from_millis(200), move || {
108    ///    println!("This will be printed after 200ms.");
109    /// });
110    /// ```
111    pub fn single_shot(duration: core::time::Duration, callback: impl FnOnce() + 'static) {
112        let _ = CURRENT_TIMERS.try_with(|timers| {
113            let mut timers = timers.borrow_mut();
114            timers.start_or_restart_timer(
115                None,
116                TimerMode::SingleShot,
117                duration,
118                CallbackVariant::SingleShot(Box::new(callback)),
119            );
120        });
121    }
122
123    /// Stops the previously started timer. Does nothing if the timer has never been started.
124    pub fn stop(&self) {
125        if let Some(id) = self.id() {
126            let _ = CURRENT_TIMERS.try_with(|timers| {
127                timers.borrow_mut().deactivate_timer(id);
128            });
129        }
130    }
131
132    /// Restarts the timer. If the timer was previously started by calling [`Self::start()`]
133    /// with a duration and callback, then the time when the callback will be next invoked
134    /// is re-calculated to be in the specified duration relative to when this function is called.
135    ///
136    /// Does nothing if the timer was never started.
137    pub fn restart(&self) {
138        if let Some(id) = self.id() {
139            let _ = CURRENT_TIMERS.try_with(|timers| {
140                timers.borrow_mut().deactivate_timer(id);
141                timers.borrow_mut().activate_timer(id);
142            });
143        }
144    }
145
146    /// Returns true if the timer is running; false otherwise.
147    pub fn running(&self) -> bool {
148        self.id()
149            .and_then(|timer_id| {
150                CURRENT_TIMERS.try_with(|timers| timers.borrow().timers[timer_id].running).ok()
151            })
152            .unwrap_or(false)
153    }
154
155    /// Change the duration of timer. If the timer was is running (see [`Self::running()`]),
156    /// then the time when the callback will be next invoked is re-calculated to be in the
157    /// specified duration relative to when this function is called.
158    ///
159    /// Arguments:
160    /// * `interval`: The duration from now until when the timer should fire. And the period of that timer
161    ///   for [`Repeated`](TimerMode::Repeated) timers.
162    pub fn set_interval(&self, interval: core::time::Duration) {
163        if let Some(id) = self.id() {
164            let _ = CURRENT_TIMERS.try_with(|timers| {
165                timers.borrow_mut().set_interval(id, interval);
166            });
167        }
168    }
169
170    /// Returns the interval of the timer. If the timer was never started, the returned duration is 0ms.
171    pub fn interval(&self) -> core::time::Duration {
172        self.id()
173            .and_then(|timer_id| {
174                CURRENT_TIMERS.try_with(|timers| timers.borrow().timers[timer_id].duration).ok()
175            })
176            .unwrap_or_default()
177    }
178
179    fn id(&self) -> Option<usize> {
180        self.id.get().map(|v| usize::from(v) - 1)
181    }
182
183    fn set_id(&self, id: Option<usize>) {
184        self.id.set(id.and_then(|v| NonZeroUsize::new(v + 1)));
185    }
186}
187
188impl Drop for Timer {
189    fn drop(&mut self) {
190        if let Some(id) = self.id() {
191            let _ = CURRENT_TIMERS.try_with(|timers| {
192                #[cfg(target_os = "android")]
193                if timers.borrow().timers.is_empty() {
194                    // There seems to be a bug in android thread_local where try_with recreates the already thread local.
195                    // But we are called from the drop of another thread local, just ignore the drop then
196                    return;
197                }
198                let callback = timers.borrow_mut().remove_timer(id);
199                // drop the callback without having CURRENT_TIMERS borrowed
200                drop(callback);
201            });
202        }
203    }
204}
205
206enum CallbackVariant {
207    Empty,
208    MultiFire(TimerCallback),
209    SingleShot(SingleShotTimerCallback),
210}
211
212struct TimerData {
213    duration: core::time::Duration,
214    mode: TimerMode,
215    running: bool,
216    /// Set to true when it is removed when the callback is still running
217    removed: bool,
218    /// true if it is in the cached the active_timers list in the maybe_activate_timers stack
219    being_activated: bool,
220
221    callback: CallbackVariant,
222}
223
224#[derive(Clone, Copy)]
225struct ActiveTimer {
226    id: usize,
227    timeout: Instant,
228}
229
230/// TimerList provides the interface to the event loop for activating times and
231/// determining the nearest timeout.
232#[derive(Default)]
233pub struct TimerList {
234    timers: slab::Slab<TimerData>,
235    active_timers: Vec<ActiveTimer>,
236    /// If a callback is currently running, this is the id of the currently running callback
237    callback_active: Option<usize>,
238}
239
240impl TimerList {
241    /// Returns the timeout of the timer that should fire the soonest, or None if there
242    /// is no timer active.
243    pub fn next_timeout() -> Option<Instant> {
244        CURRENT_TIMERS.with(|timers| {
245            timers
246                .borrow()
247                .active_timers
248                .first()
249                .map(|first_active_timer| first_active_timer.timeout)
250        })
251    }
252
253    /// Activates any expired timers by calling their callback function. Returns true if any timers were
254    /// activated; false otherwise.
255    pub fn maybe_activate_timers(now: Instant) -> bool {
256        // Shortcut: Is there any timer worth activating?
257        if TimerList::next_timeout().map(|timeout| now < timeout).unwrap_or(false) {
258            return false;
259        }
260
261        CURRENT_TIMERS.with(|timers| {
262            assert!(timers.borrow().callback_active.is_none(), "Recursion in timer code");
263
264            // Re-register all timers that expired but are repeating, as well as all that haven't expired yet. This is
265            // done in one shot to ensure a consistent state by the time the callbacks are invoked.
266            let expired_timers = {
267                let mut timers = timers.borrow_mut();
268
269                // Empty active_timers and rebuild it, to preserve insertion order across expired and not expired timers.
270                let mut active_timers = core::mem::take(&mut timers.active_timers);
271
272                let expired_vs_remaining_timers_partition_point =
273                    active_timers.partition_point(|active_timer| active_timer.timeout <= now);
274
275                let (expired_timers, timers_not_activated_this_time) =
276                    active_timers.split_at(expired_vs_remaining_timers_partition_point);
277
278                for expired_timer in expired_timers {
279                    let timer = &mut timers.timers[expired_timer.id];
280                    assert!(!timer.being_activated);
281                    timer.being_activated = true;
282
283                    if matches!(timers.timers[expired_timer.id].mode, TimerMode::Repeated) {
284                        timers.activate_timer(expired_timer.id);
285                    } else {
286                        timers.timers[expired_timer.id].running = false;
287                    }
288                }
289
290                for future_timer in timers_not_activated_this_time.iter() {
291                    timers.register_active_timer(*future_timer);
292                }
293
294                // turn `expired_timers` slice into a truncated vec.
295                active_timers.truncate(expired_vs_remaining_timers_partition_point);
296                active_timers
297            };
298
299            let any_activated = !expired_timers.is_empty();
300
301            for active_timer in expired_timers.into_iter() {
302                let mut callback = {
303                    let mut timers = timers.borrow_mut();
304
305                    timers.callback_active = Some(active_timer.id);
306
307                    // have to release the borrow on `timers` before invoking the callback,
308                    // so here we temporarily move the callback out of its permanent place
309                    core::mem::replace(
310                        &mut timers.timers[active_timer.id].callback,
311                        CallbackVariant::Empty,
312                    )
313                };
314
315                match callback {
316                    CallbackVariant::Empty => (),
317                    CallbackVariant::MultiFire(ref mut cb) => cb(),
318                    CallbackVariant::SingleShot(cb) => {
319                        cb();
320                        timers.borrow_mut().callback_active = None;
321                        timers.borrow_mut().timers.remove(active_timer.id);
322                        continue;
323                    }
324                };
325
326                let mut timers = timers.borrow_mut();
327
328                let callback_register = &mut timers.timers[active_timer.id].callback;
329
330                // only emplace back the callback if its permanent store is still Empty:
331                // if not, it means the invoked callback has restarted its own timer with a new callback
332                if matches!(callback_register, CallbackVariant::Empty) {
333                    *callback_register = callback;
334                }
335
336                timers.callback_active = None;
337                let t = &mut timers.timers[active_timer.id];
338                if t.removed {
339                    timers.timers.remove(active_timer.id);
340                } else {
341                    t.being_activated = false;
342                }
343            }
344            any_activated
345        })
346    }
347
348    fn start_or_restart_timer(
349        &mut self,
350        id: Option<usize>,
351        mode: TimerMode,
352        duration: core::time::Duration,
353        callback: CallbackVariant,
354    ) -> usize {
355        let mut timer_data = TimerData {
356            duration,
357            mode,
358            running: false,
359            removed: false,
360            callback,
361            being_activated: false,
362        };
363        let inactive_timer_id = if let Some(id) = id {
364            self.deactivate_timer(id);
365            timer_data.being_activated = self.timers[id].being_activated;
366            self.timers[id] = timer_data;
367            id
368        } else {
369            self.timers.insert(timer_data)
370        };
371        self.activate_timer(inactive_timer_id);
372        inactive_timer_id
373    }
374
375    fn deactivate_timer(&mut self, id: usize) {
376        let mut i = 0;
377        while i < self.active_timers.len() {
378            if self.active_timers[i].id == id {
379                self.active_timers.remove(i);
380                self.timers[id].running = false;
381                debug_assert!(!self.active_timers.iter().any(|t| t.id == id));
382                break;
383            } else {
384                i += 1;
385            }
386        }
387    }
388
389    fn activate_timer(&mut self, id: usize) {
390        self.register_active_timer(ActiveTimer {
391            id,
392            timeout: Instant::now() + self.timers[id].duration,
393        });
394    }
395
396    fn register_active_timer(&mut self, new_active_timer: ActiveTimer) {
397        debug_assert!(!self.active_timers.iter().any(|t| t.id == new_active_timer.id));
398        let insertion_index = self
399            .active_timers
400            .partition_point(|existing_timer| existing_timer.timeout < new_active_timer.timeout);
401        self.active_timers.insert(insertion_index, new_active_timer);
402        self.timers[new_active_timer.id].running = true;
403    }
404
405    fn remove_timer(&mut self, id: usize) -> CallbackVariant {
406        self.deactivate_timer(id);
407        let t = &mut self.timers[id];
408        if t.being_activated {
409            t.removed = true;
410            CallbackVariant::Empty
411        } else {
412            self.timers.remove(id).callback
413        }
414    }
415
416    fn set_interval(&mut self, id: usize, duration: core::time::Duration) {
417        let timer = &self.timers[id];
418        if timer.running {
419            self.deactivate_timer(id);
420            self.timers[id].duration = duration;
421            self.activate_timer(id);
422        } else {
423            self.timers[id].duration = duration;
424        }
425    }
426}
427
428crate::thread_local!(static CURRENT_TIMERS : RefCell<TimerList> = RefCell::default());
429
430#[cfg(feature = "ffi")]
431pub(crate) mod ffi {
432    #![allow(unsafe_code)]
433
434    use super::*;
435    use core::ffi::c_void;
436
437    struct WrapFn {
438        callback: extern "C" fn(*mut c_void),
439        user_data: *mut c_void,
440        drop_user_data: Option<extern "C" fn(*mut c_void)>,
441    }
442
443    impl Drop for WrapFn {
444        fn drop(&mut self) {
445            if let Some(x) = self.drop_user_data {
446                x(self.user_data)
447            }
448        }
449    }
450
451    impl WrapFn {
452        fn call(&self) {
453            (self.callback)(self.user_data)
454        }
455    }
456
457    /// Start a timer with the given mode, duration in millisecond and callback. A timer id may be provided (first argument).
458    /// A value of -1 for the timer id means a new timer is to be allocated.
459    /// The (new) timer id is returned.
460    /// The timer MUST be destroyed with slint_timer_destroy.
461    #[unsafe(no_mangle)]
462    pub extern "C" fn slint_timer_start(
463        id: usize,
464        mode: TimerMode,
465        duration: u64,
466        callback: extern "C" fn(*mut c_void),
467        user_data: *mut c_void,
468        drop_user_data: Option<extern "C" fn(*mut c_void)>,
469    ) -> usize {
470        let wrap = WrapFn { callback, user_data, drop_user_data };
471        let timer = Timer::default();
472        if id != 0 {
473            timer.id.set(NonZeroUsize::new(id));
474        }
475        if duration > i64::MAX as u64 {
476            // negative duration? stop the timer
477            timer.stop();
478        } else {
479            timer.start(mode, core::time::Duration::from_millis(duration), move || wrap.call());
480        }
481        timer.id.take().map(usize::from).unwrap_or(0)
482    }
483
484    /// Execute a callback with a delay in millisecond
485    #[unsafe(no_mangle)]
486    pub extern "C" fn slint_timer_singleshot(
487        delay: u64,
488        callback: extern "C" fn(*mut c_void),
489        user_data: *mut c_void,
490        drop_user_data: Option<extern "C" fn(*mut c_void)>,
491    ) {
492        let wrap = WrapFn { callback, user_data, drop_user_data };
493        Timer::single_shot(core::time::Duration::from_millis(delay), move || wrap.call());
494    }
495
496    /// Stop a timer and free its raw data
497    #[unsafe(no_mangle)]
498    pub extern "C" fn slint_timer_destroy(id: usize) {
499        if id == 0 {
500            return;
501        }
502        let timer = Timer { id: Cell::new(NonZeroUsize::new(id)), _phantom: Default::default() };
503        drop(timer);
504    }
505
506    /// Stop a timer
507    #[unsafe(no_mangle)]
508    pub extern "C" fn slint_timer_stop(id: usize) {
509        if id == 0 {
510            return;
511        }
512        let timer = Timer { id: Cell::new(NonZeroUsize::new(id)), _phantom: Default::default() };
513        timer.stop();
514        timer.id.take(); // Make sure that dropping the Timer doesn't unregister it. C++ will call destroy() in the destructor.
515    }
516
517    /// Restart a repeated timer
518    #[unsafe(no_mangle)]
519    pub extern "C" fn slint_timer_restart(id: usize) {
520        if id == 0 {
521            return;
522        }
523        let timer = Timer { id: Cell::new(NonZeroUsize::new(id)), _phantom: Default::default() };
524        timer.restart();
525        timer.id.take(); // Make sure that dropping the Timer doesn't unregister it. C++ will call destroy() in the destructor.
526    }
527
528    /// Returns true if the timer is running; false otherwise.
529    #[unsafe(no_mangle)]
530    pub extern "C" fn slint_timer_running(id: usize) -> bool {
531        if id == 0 {
532            return false;
533        }
534        let timer = Timer { id: Cell::new(NonZeroUsize::new(id)), _phantom: Default::default() };
535        let running = timer.running();
536        timer.id.take(); // Make sure that dropping the Timer doesn't unregister it. C++ will call destroy() in the destructor.
537        running
538    }
539
540    /// Returns the interval in milliseconds. 0 when the timer was never started.
541    #[unsafe(no_mangle)]
542    pub extern "C" fn slint_timer_interval(id: usize) -> u64 {
543        if id == 0 {
544            return 0;
545        }
546        let timer = Timer { id: Cell::new(NonZeroUsize::new(id)), _phantom: Default::default() };
547        let val = timer.interval().as_millis() as u64;
548        timer.id.take(); // Make sure that dropping the Timer doesn't unregister it. C++ will call destroy() in the destructor.
549        val
550    }
551}
552
553/**
554```rust
555i_slint_backend_testing::init_no_event_loop();
556use slint::{Timer, TimerMode};
557use std::{rc::Rc, cell::RefCell, time::Duration};
558#[derive(Default)]
559struct SharedState {
560    timer_200: Timer,
561    timer_200_called: usize,
562    timer_500: Timer,
563    timer_500_called: usize,
564    timer_once: Timer,
565    timer_once_called: usize,
566}
567let state = Rc::new(RefCell::new(SharedState::default()));
568// Note: state will be leaked because of circular dependencies: don't do that in production
569let state_ = state.clone();
570state.borrow_mut().timer_200.start(TimerMode::Repeated, Duration::from_millis(200), move || {
571    state_.borrow_mut().timer_200_called += 1;
572});
573let state_ = state.clone();
574state.borrow_mut().timer_once.start(TimerMode::Repeated, Duration::from_millis(300), move || {
575    state_.borrow_mut().timer_once_called += 1;
576    state_.borrow().timer_once.stop();
577});
578let state_ = state.clone();
579state.borrow_mut().timer_500.start(TimerMode::Repeated, Duration::from_millis(500), move || {
580    state_.borrow_mut().timer_500_called += 1;
581});
582slint::platform::update_timers_and_animations();
583i_slint_backend_testing::mock_elapsed_time(100);
584assert_eq!(state.borrow().timer_200_called, 0);
585assert_eq!(state.borrow().timer_once_called, 0);
586assert_eq!(state.borrow().timer_500_called, 0);
587i_slint_backend_testing::mock_elapsed_time(100);
588assert_eq!(state.borrow().timer_200_called, 1);
589assert_eq!(state.borrow().timer_once_called, 0);
590assert_eq!(state.borrow().timer_500_called, 0);
591i_slint_backend_testing::mock_elapsed_time(100);
592assert_eq!(state.borrow().timer_200_called, 1);
593assert_eq!(state.borrow().timer_once_called, 1);
594assert_eq!(state.borrow().timer_500_called, 0);
595i_slint_backend_testing::mock_elapsed_time(200); // total: 500
596assert_eq!(state.borrow().timer_200_called, 2);
597assert_eq!(state.borrow().timer_once_called, 1);
598assert_eq!(state.borrow().timer_500_called, 1);
599for _ in 0..10 {
600    i_slint_backend_testing::mock_elapsed_time(100);
601}
602// total: 1500
603assert_eq!(state.borrow().timer_200_called, 7);
604assert_eq!(state.borrow().timer_once_called, 1);
605assert_eq!(state.borrow().timer_500_called, 3);
606state.borrow().timer_once.restart();
607state.borrow().timer_200.restart();
608state.borrow().timer_500.stop();
609slint::platform::update_timers_and_animations();
610i_slint_backend_testing::mock_elapsed_time(100);
611assert_eq!(state.borrow().timer_200_called, 7);
612assert_eq!(state.borrow().timer_once_called, 1);
613assert_eq!(state.borrow().timer_500_called, 3);
614slint::platform::update_timers_and_animations();
615i_slint_backend_testing::mock_elapsed_time(100);
616assert_eq!(state.borrow().timer_200_called, 8);
617assert_eq!(state.borrow().timer_once_called, 1);
618assert_eq!(state.borrow().timer_500_called, 3);
619slint::platform::update_timers_and_animations();
620i_slint_backend_testing::mock_elapsed_time(100);
621assert_eq!(state.borrow().timer_200_called, 8);
622assert_eq!(state.borrow().timer_once_called, 2);
623assert_eq!(state.borrow().timer_500_called, 3);
624slint::platform::update_timers_and_animations();
625i_slint_backend_testing::mock_elapsed_time(1000);
626slint::platform::update_timers_and_animations();
627slint::platform::update_timers_and_animations();
628// Despite 1000ms have passed, the 200 timer is only called once because we didn't call update_timers_and_animations in between
629assert_eq!(state.borrow().timer_200_called, 9);
630assert_eq!(state.borrow().timer_once_called, 2);
631assert_eq!(state.borrow().timer_500_called, 3);
632let state_ = state.clone();
633state.borrow().timer_200.start(TimerMode::SingleShot, Duration::from_millis(200), move || {
634    state_.borrow_mut().timer_200_called += 1;
635});
636for _ in 0..5 {
637    i_slint_backend_testing::mock_elapsed_time(75);
638}
639assert_eq!(state.borrow().timer_200_called, 10);
640assert_eq!(state.borrow().timer_once_called, 2);
641assert_eq!(state.borrow().timer_500_called, 3);
642state.borrow().timer_200.restart();
643for _ in 0..5 {
644    i_slint_backend_testing::mock_elapsed_time(75);
645}
646assert_eq!(state.borrow().timer_200_called, 11);
647assert_eq!(state.borrow().timer_once_called, 2);
648assert_eq!(state.borrow().timer_500_called, 3);
649
650// Test re-starting from a callback
651let state_ = state.clone();
652state.borrow_mut().timer_500.start(TimerMode::Repeated, Duration::from_millis(500), move || {
653    state_.borrow_mut().timer_500_called += 1;
654    let state__ = state_.clone();
655    state_.borrow_mut().timer_500.start(TimerMode::Repeated, Duration::from_millis(500), move || {
656        state__.borrow_mut().timer_500_called += 1000;
657    });
658    let state__ = state_.clone();
659    state_.borrow_mut().timer_200.start(TimerMode::Repeated, Duration::from_millis(200), move || {
660        state__.borrow_mut().timer_200_called += 1000;
661    });
662});
663for _ in 0..20 {
664    i_slint_backend_testing::mock_elapsed_time(100);
665}
666assert_eq!(state.borrow().timer_200_called, 7011);
667assert_eq!(state.borrow().timer_once_called, 2);
668assert_eq!(state.borrow().timer_500_called, 3004);
669
670// Test set interval
671let state_ = state.clone();
672state.borrow_mut().timer_200.start(TimerMode::Repeated, Duration::from_millis(200), move || {
673    state_.borrow_mut().timer_200_called += 1;
674});
675let state_ = state.clone();
676state.borrow_mut().timer_once.start(TimerMode::Repeated, Duration::from_millis(300), move || {
677    state_.borrow_mut().timer_once_called += 1;
678    state_.borrow().timer_once.stop();
679});
680let state_ = state.clone();
681state.borrow_mut().timer_500.start(TimerMode::Repeated, Duration::from_millis(500), move || {
682    state_.borrow_mut().timer_500_called += 1;
683});
684
685let state_ = state.clone();
686slint::platform::update_timers_and_animations();
687for _ in 0..5 {
688    i_slint_backend_testing::mock_elapsed_time(100);
689}
690slint::platform::update_timers_and_animations();
691assert_eq!(state.borrow().timer_200_called, 7013);
692assert_eq!(state.borrow().timer_once_called, 3);
693assert_eq!(state.borrow().timer_500_called, 3005);
694
695for _ in 0..20 {
696    state.borrow().timer_200.set_interval(Duration::from_millis(200 * 2));
697    state.borrow().timer_once.set_interval(Duration::from_millis(300 * 2));
698    state.borrow().timer_500.set_interval(Duration::from_millis(500 * 2));
699
700    assert_eq!(state.borrow().timer_200_called, 7013);
701    assert_eq!(state.borrow().timer_once_called, 3);
702    assert_eq!(state.borrow().timer_500_called, 3005);
703
704    i_slint_backend_testing::mock_elapsed_time(100);
705}
706
707slint::platform::update_timers_and_animations();
708for _ in 0..9 {
709    i_slint_backend_testing::mock_elapsed_time(100);
710}
711slint::platform::update_timers_and_animations();
712assert_eq!(state.borrow().timer_200_called, 7015);
713assert_eq!(state.borrow().timer_once_called, 3);
714assert_eq!(state.borrow().timer_500_called, 3006);
715
716state.borrow().timer_200.stop();
717state.borrow().timer_500.stop();
718
719state.borrow_mut().timer_once.restart();
720for _ in 0..4 {
721    i_slint_backend_testing::mock_elapsed_time(100);
722}
723assert_eq!(state.borrow().timer_once_called, 3);
724for _ in 0..4 {
725    i_slint_backend_testing::mock_elapsed_time(100);
726}
727assert_eq!(state.borrow().timer_once_called, 4);
728
729state.borrow_mut().timer_once.stop();
730i_slint_backend_testing::mock_elapsed_time(1000);
731
732assert_eq!(state.borrow().timer_200_called, 7015);
733assert_eq!(state.borrow().timer_once_called, 4);
734assert_eq!(state.borrow().timer_500_called, 3006);
735```
736 */
737#[cfg(doctest)]
738const _TIMER_TESTS: () = ();
739
740/**
741 * Test that deleting an active timer from a timer event works.
742```rust
743// There is a 200 ms timer that increase variable1
744// after 500ms, that timer is destroyed by a single shot timer,
745// and a new new timer  increase variable2
746i_slint_backend_testing::init_no_event_loop();
747use slint::{Timer, TimerMode};
748use std::{rc::Rc, cell::RefCell, time::Duration};
749#[derive(Default)]
750struct SharedState {
751    repeated_timer: Timer,
752    variable1: usize,
753    variable2: usize,
754}
755let state = Rc::new(RefCell::new(SharedState::default()));
756// Note: state will be leaked because of circular dependencies: don't do that in production
757let state_ = state.clone();
758state.borrow_mut().repeated_timer.start(TimerMode::Repeated, Duration::from_millis(200), move || {
759    state_.borrow_mut().variable1 += 1;
760});
761let state_ = state.clone();
762Timer::single_shot(Duration::from_millis(500), move || {
763    state_.borrow_mut().repeated_timer = Default::default();
764    let state = state_.clone();
765    state_.borrow_mut().repeated_timer.start(TimerMode::Repeated, Duration::from_millis(200), move || {
766        state.borrow_mut().variable2 += 1;
767    })
768} );
769i_slint_backend_testing::mock_elapsed_time(10);
770assert_eq!(state.borrow().variable1, 0);
771assert_eq!(state.borrow().variable2, 0);
772i_slint_backend_testing::mock_elapsed_time(200);
773assert_eq!(state.borrow().variable1, 1);
774assert_eq!(state.borrow().variable2, 0);
775i_slint_backend_testing::mock_elapsed_time(200);
776assert_eq!(state.borrow().variable1, 2);
777assert_eq!(state.borrow().variable2, 0);
778i_slint_backend_testing::mock_elapsed_time(100);
779// More than 500ms have elapsed, the single shot timer should have been activated, but that has no effect on variable 1 and 2
780// This should just restart the timer so that the next change should happen 200ms from now
781assert_eq!(state.borrow().variable1, 2);
782assert_eq!(state.borrow().variable2, 0);
783i_slint_backend_testing::mock_elapsed_time(110);
784assert_eq!(state.borrow().variable1, 2);
785assert_eq!(state.borrow().variable2, 0);
786i_slint_backend_testing::mock_elapsed_time(100);
787assert_eq!(state.borrow().variable1, 2);
788assert_eq!(state.borrow().variable2, 1);
789i_slint_backend_testing::mock_elapsed_time(100);
790assert_eq!(state.borrow().variable1, 2);
791assert_eq!(state.borrow().variable2, 1);
792i_slint_backend_testing::mock_elapsed_time(100);
793assert_eq!(state.borrow().variable1, 2);
794assert_eq!(state.borrow().variable2, 2);
795```
796 */
797#[cfg(doctest)]
798const _BUG3019: () = ();
799
800/**
801 * Test that starting a singleshot timer works
802```rust
803// There is a 200 ms singleshot timer that increase variable1
804i_slint_backend_testing::init_no_event_loop();
805use slint::{Timer, TimerMode};
806use std::{rc::Rc, cell::RefCell, time::Duration};
807#[derive(Default)]
808struct SharedState {
809    variable1: usize,
810}
811let state = Rc::new(RefCell::new(SharedState::default()));
812let state_ = state.clone();
813let timer = Timer::default();
814
815timer.start(TimerMode::SingleShot, Duration::from_millis(200), move || {
816    state_.borrow_mut().variable1 += 1;
817});
818
819// Singleshot timer set up and run...
820assert!(timer.running());
821i_slint_backend_testing::mock_elapsed_time(10);
822assert!(timer.running());
823assert_eq!(state.borrow().variable1, 0);
824i_slint_backend_testing::mock_elapsed_time(200);
825assert_eq!(state.borrow().variable1, 1);
826assert!(!timer.running());
827i_slint_backend_testing::mock_elapsed_time(200);
828assert_eq!(state.borrow().variable1, 1); // It's singleshot, it only triggers once!
829assert!(!timer.running());
830
831// Restart a previously set up singleshot timer
832timer.restart();
833assert!(timer.running());
834assert_eq!(state.borrow().variable1, 1);
835i_slint_backend_testing::mock_elapsed_time(200);
836assert_eq!(state.borrow().variable1, 2);
837assert!(!timer.running());
838i_slint_backend_testing::mock_elapsed_time(200);
839assert_eq!(state.borrow().variable1, 2); // It's singleshot, it only triggers once!
840assert!(!timer.running());
841
842// Stop a non-running singleshot timer
843timer.stop();
844assert!(!timer.running());
845assert_eq!(state.borrow().variable1, 2);
846i_slint_backend_testing::mock_elapsed_time(200);
847assert_eq!(state.borrow().variable1, 2);
848assert!(!timer.running());
849i_slint_backend_testing::mock_elapsed_time(200);
850assert_eq!(state.borrow().variable1, 2); // It's singleshot, it only triggers once!
851assert!(!timer.running());
852
853// Stop a running singleshot timer
854timer.restart();
855assert!(timer.running());
856assert_eq!(state.borrow().variable1, 2);
857i_slint_backend_testing::mock_elapsed_time(10);
858timer.stop();
859assert!(!timer.running());
860i_slint_backend_testing::mock_elapsed_time(200);
861assert_eq!(state.borrow().variable1, 2);
862assert!(!timer.running());
863i_slint_backend_testing::mock_elapsed_time(200);
864assert_eq!(state.borrow().variable1, 2); // It's singleshot, it only triggers once!
865assert!(!timer.running());
866
867// set_interval on a non-running singleshot timer
868timer.set_interval(Duration::from_millis(300));
869assert!(!timer.running());
870i_slint_backend_testing::mock_elapsed_time(1000);
871assert_eq!(state.borrow().variable1, 2);
872assert!(!timer.running());
873timer.restart();
874assert!(timer.running());
875i_slint_backend_testing::mock_elapsed_time(200);
876assert_eq!(state.borrow().variable1, 2);
877assert!(timer.running());
878i_slint_backend_testing::mock_elapsed_time(200);
879assert_eq!(state.borrow().variable1, 3);
880assert!(!timer.running());
881i_slint_backend_testing::mock_elapsed_time(300);
882assert_eq!(state.borrow().variable1, 3); // It's singleshot, it only triggers once!
883assert!(!timer.running());
884
885// set_interval on a running singleshot timer
886timer.restart();
887assert!(timer.running());
888assert_eq!(state.borrow().variable1, 3);
889i_slint_backend_testing::mock_elapsed_time(290);
890timer.set_interval(Duration::from_millis(400));
891assert!(timer.running());
892i_slint_backend_testing::mock_elapsed_time(200);
893assert_eq!(state.borrow().variable1, 3);
894assert!(timer.running());
895i_slint_backend_testing::mock_elapsed_time(250);
896assert_eq!(state.borrow().variable1, 4);
897assert!(!timer.running());
898i_slint_backend_testing::mock_elapsed_time(400);
899assert_eq!(state.borrow().variable1, 4); // It's singleshot, it only triggers once!
900assert!(!timer.running());
901```
902 */
903#[cfg(doctest)]
904const _SINGLESHOT_START: () = ();
905
906/**
907 * Test that it's possible to start a new timer from within Drop of a timer's closure.
908 * This may happen when a timer's closure is dropped, that closure holds the last reference
909 * to a component, that component is destroyed, and the accesskit code schedules a reload_tree
910 * via a single shot.
911```rust
912i_slint_backend_testing::init_no_event_loop();
913use slint::{Timer, TimerMode};
914use std::{rc::Rc, cell::Cell, time::Duration};
915#[derive(Default)]
916struct CapturedInClosure {
917    last_fired: Option<Rc<Cell<bool>>>,
918}
919impl Drop for CapturedInClosure {
920    fn drop(&mut self) {
921        if let Some(last_fired) = self.last_fired.as_ref().cloned() {
922            Timer::single_shot(Duration::from_millis(100), move || last_fired.set(true));
923        }
924    }
925}
926
927let last_fired = Rc::new(Cell::new(false));
928
929let mut cap_in_clos = CapturedInClosure::default();
930
931let timer_to_stop = Timer::default();
932timer_to_stop.start(TimerMode::Repeated, Duration::from_millis(100), {
933    let last_fired = last_fired.clone();
934    move || {
935    cap_in_clos.last_fired = Some(last_fired.clone());
936}});
937
938assert_eq!(last_fired.get(), false);
939i_slint_backend_testing::mock_elapsed_time(110);
940assert_eq!(last_fired.get(), false);
941drop(timer_to_stop);
942
943i_slint_backend_testing::mock_elapsed_time(110);
944assert_eq!(last_fired.get(), true);
945```
946 */
947#[cfg(doctest)]
948const _TIMER_CLOSURE_DROP_STARTS_NEW_TIMER: () = ();
949
950/**
951 * Test that it's possible to set a timer's interval from within the callback.
952```rust
953i_slint_backend_testing::init_no_event_loop();
954use slint::{Timer, TimerMode};
955use std::{rc::Rc, cell::RefCell, time::Duration};
956#[derive(Default)]
957struct SharedState {
958    // Note: state will be leaked because of circular dependencies: don't do that in production
959    timer: Timer,
960    variable1: usize,
961}
962let state = Rc::new(RefCell::new(SharedState::default()));
963let state_ = state.clone();
964state.borrow().timer.start(TimerMode::Repeated, Duration::from_millis(200), move || {
965    state_.borrow_mut().variable1 += 1;
966    let variable1 = state_.borrow().variable1;
967    if variable1 == 2 {
968        state_.borrow().timer.set_interval(Duration::from_millis(500));
969    } else if variable1 == 3 {
970        state_.borrow().timer.set_interval(Duration::from_millis(100));
971    }
972});
973
974assert!(state.borrow().timer.running());
975i_slint_backend_testing::mock_elapsed_time(10);
976assert!(state.borrow().timer.running());
977assert_eq!(state.borrow().variable1, 0);
978i_slint_backend_testing::mock_elapsed_time(200);
979assert_eq!(state.borrow().variable1, 1); // fired
980assert!(state.borrow().timer.running());
981i_slint_backend_testing::mock_elapsed_time(180);
982assert_eq!(state.borrow().variable1, 1);
983assert!(state.borrow().timer.running());
984i_slint_backend_testing::mock_elapsed_time(30);
985assert_eq!(state.borrow().variable1, 2); // fired
986assert!(state.borrow().timer.running());
987// now the timer interval should be 500
988i_slint_backend_testing::mock_elapsed_time(480);
989assert_eq!(state.borrow().variable1, 2);
990assert!(state.borrow().timer.running());
991i_slint_backend_testing::mock_elapsed_time(30);
992assert_eq!(state.borrow().variable1, 3); // fired
993assert!(state.borrow().timer.running());
994// now the timer interval should be 100
995i_slint_backend_testing::mock_elapsed_time(100);
996assert_eq!(state.borrow().variable1, 4); // fired
997assert!(state.borrow().timer.running());
998i_slint_backend_testing::mock_elapsed_time(100);
999assert_eq!(state.borrow().variable1, 5); // fired
1000assert!(state.borrow().timer.running());
1001```
1002 */
1003#[cfg(doctest)]
1004const _BUG6141_SET_INTERVAL_FROM_CALLBACK: () = ();
1005
1006/**
1007 * Test that a timer can't be activated twice.
1008```rust
1009i_slint_backend_testing::init_no_event_loop();
1010use slint::{Timer, TimerMode};
1011use std::{rc::Rc, cell::Cell, time::Duration};
1012
1013let later_timer_expiration_count = Rc::new(Cell::new(0));
1014
1015let sooner_timer = Timer::default();
1016let later_timer = Rc::new(Timer::default());
1017later_timer.start(TimerMode::SingleShot, Duration::from_millis(500), {
1018    let later_timer_expiration_count = later_timer_expiration_count.clone();
1019    move || {
1020        later_timer_expiration_count.set(later_timer_expiration_count.get() + 1);
1021    }
1022});
1023
1024sooner_timer.start(TimerMode::SingleShot, Duration::from_millis(100), {
1025    let later_timer = later_timer.clone();
1026    let later_timer_expiration_count = later_timer_expiration_count.clone();
1027    move || {
1028    later_timer.start(TimerMode::SingleShot, Duration::from_millis(600), {
1029        let later_timer_expiration_count = later_timer_expiration_count.clone();
1030        move || {
1031            later_timer_expiration_count.set(later_timer_expiration_count.get() + 1);
1032        }
1033    });
1034}});
1035
1036assert_eq!(later_timer_expiration_count.get(), 0);
1037i_slint_backend_testing::mock_elapsed_time(110);
1038assert_eq!(later_timer_expiration_count.get(), 0);
1039i_slint_backend_testing::mock_elapsed_time(400);
1040assert_eq!(later_timer_expiration_count.get(), 0);
1041i_slint_backend_testing::mock_elapsed_time(800);
1042assert_eq!(later_timer_expiration_count.get(), 1);
1043```
1044 */
1045#[cfg(doctest)]
1046const _DOUBLY_REGISTER_ACTIVE_TIMER: () = ();
1047
1048/**
1049 * Test that a timer can't be activated twice.
1050```rust
1051i_slint_backend_testing::init_no_event_loop();
1052use slint::{Timer, TimerMode};
1053use std::{rc::Rc, cell::Cell, time::Duration};
1054
1055let later_timer_expiration_count = Rc::new(Cell::new(0));
1056
1057let sooner_timer = Timer::default();
1058let later_timer = Rc::new(Timer::default());
1059later_timer.start(TimerMode::Repeated, Duration::from_millis(110), {
1060    let later_timer_expiration_count = later_timer_expiration_count.clone();
1061    move || {
1062        later_timer_expiration_count.set(later_timer_expiration_count.get() + 1);
1063    }
1064});
1065
1066sooner_timer.start(TimerMode::SingleShot, Duration::from_millis(100), {
1067    let later_timer = later_timer.clone();
1068    let later_timer_expiration_count = later_timer_expiration_count.clone();
1069    move || {
1070    later_timer.start(TimerMode::Repeated, Duration::from_millis(110), {
1071        let later_timer_expiration_count = later_timer_expiration_count.clone();
1072        move || {
1073            later_timer_expiration_count.set(later_timer_expiration_count.get() + 1);
1074        }
1075    });
1076}});
1077
1078assert_eq!(later_timer_expiration_count.get(), 0);
1079i_slint_backend_testing::mock_elapsed_time(120);
1080assert_eq!(later_timer_expiration_count.get(), 1);
1081```
1082 */
1083#[cfg(doctest)]
1084const _DOUBLY_REGISTER_ACTIVE_TIMER_2: () = ();
1085
1086/**
1087 * Test that a timer that's being activated can be restarted and dropped in one go.
1088```rust
1089i_slint_backend_testing::init_no_event_loop();
1090use slint::{Timer, TimerMode};
1091use std::{cell::RefCell, rc::Rc, time::Duration};
1092
1093let destructive_timer = Rc::new(RefCell::new(Some(Timer::default())));
1094
1095destructive_timer.borrow().as_ref().unwrap().start(TimerMode::Repeated, Duration::from_millis(110), {
1096    let destructive_timer = destructive_timer.clone();
1097    move || {
1098        // start() used to reset the `being_activated` flag...
1099        destructive_timer.borrow().as_ref().unwrap().start(TimerMode::Repeated, Duration::from_millis(110), || {});
1100        // ... which would make this drop remove the timer from the timer list altogether and continued processing
1101        // of the timer would panic as the id isn't valid anymore.
1102        drop(destructive_timer.take());
1103    }
1104});
1105
1106drop(destructive_timer);
1107i_slint_backend_testing::mock_elapsed_time(120);
1108```
1109 */
1110#[cfg(doctest)]
1111const _RESTART_TIMER_BEING_ACTIVATED: () = ();
1112
1113/**
1114 * Test that a future timer can be stopped from the activation callback of an earlier timer.
1115```rust
1116i_slint_backend_testing::init_no_event_loop();
1117use slint::{Timer, TimerMode};
1118use std::{rc::Rc, cell::Cell, time::Duration};
1119
1120let later_timer_expiration_count = Rc::new(Cell::new(0));
1121
1122let sooner_timer = Timer::default();
1123let later_timer = Rc::new(Timer::default());
1124later_timer.start(TimerMode::SingleShot, Duration::from_millis(500), {
1125    let later_timer_expiration_count = later_timer_expiration_count.clone();
1126    move || {
1127        later_timer_expiration_count.set(later_timer_expiration_count.get() + 1);
1128    }
1129});
1130
1131sooner_timer.start(TimerMode::SingleShot, Duration::from_millis(100), {
1132    let later_timer = later_timer.clone();
1133    let later_timer_expiration_count = later_timer_expiration_count.clone();
1134    move || {
1135        later_timer.stop();
1136    }
1137});
1138
1139assert_eq!(later_timer_expiration_count.get(), 0);
1140assert!(later_timer.running());
1141i_slint_backend_testing::mock_elapsed_time(110);
1142assert_eq!(later_timer_expiration_count.get(), 0);
1143assert!(!later_timer.running());
1144i_slint_backend_testing::mock_elapsed_time(800);
1145assert_eq!(later_timer_expiration_count.get(), 0);
1146assert!(!later_timer.running());
1147i_slint_backend_testing::mock_elapsed_time(800);
1148i_slint_backend_testing::mock_elapsed_time(800);
1149assert_eq!(later_timer_expiration_count.get(), 0);
1150```
1151 */
1152#[cfg(doctest)]
1153const _STOP_FUTURE_TIMER_DURING_ACTIVATION_OF_EARLIER: () = ();
1154
1155/**
1156 * Test for issue #8897
1157```rust
1158use slint::TimerMode;
1159static DROP_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
1160static CALL1_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
1161static CALL2_COUNT: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0);
1162std::thread::spawn(move || {
1163    struct StartTimerInDrop{};
1164    impl Drop for StartTimerInDrop {
1165        fn drop(&mut self) {
1166            DROP_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
1167            slint::Timer::single_shot(std::time::Duration::from_millis(100), move || {
1168                println!("Timer fired");
1169            });
1170            let timer = slint::Timer::default();
1171            timer.start(TimerMode::Repeated, std::time::Duration::from_millis(100), move || {
1172                println!("fired");
1173            });
1174            timer.restart();
1175            timer.stop();
1176        }
1177    }
1178
1179    thread_local! { static START_TIMER_IN_DROP: StartTimerInDrop = StartTimerInDrop {}; }
1180    let timer = START_TIMER_IN_DROP.with(|_| { });
1181    thread_local! { static TIMER2: slint::Timer = slint::Timer::default(); }
1182    TIMER2.with(|timer| {
1183        timer.start(TimerMode::Repeated, std::time::Duration::from_millis(100), move || {
1184            CALL2_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
1185        });
1186    });
1187
1188
1189    i_slint_backend_testing::init_no_event_loop();
1190    slint::Timer::single_shot(std::time::Duration::from_millis(100), move || {
1191            CALL1_COUNT.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
1192    });
1193    i_slint_backend_testing::mock_elapsed_time(50);
1194
1195    assert_eq!(CALL1_COUNT.load(std::sync::atomic::Ordering::SeqCst), 0);
1196    assert_eq!(CALL2_COUNT.load(std::sync::atomic::Ordering::SeqCst), 0);
1197    i_slint_backend_testing::mock_elapsed_time(60);
1198    assert_eq!(CALL1_COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
1199    assert_eq!(CALL2_COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
1200    i_slint_backend_testing::mock_elapsed_time(60);
1201}).join().unwrap();
1202assert_eq!(DROP_COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
1203assert_eq!(CALL1_COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
1204assert_eq!(CALL2_COUNT.load(std::sync::atomic::Ordering::SeqCst), 1);
1205```
1206 */
1207#[cfg(doctest)]
1208const _TIMER_AT_EXIT: () = ();