1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//! Timer to periodically execute an action.
use iup_sys;

use Element;
use Guard;

/// A timer which periodically invokes a callback when the time is up.
///
/// # Ownership
///
/// The timer must be manually destroyed, thus for the user safety it returns a guarded object
/// on the `new` constructor.
///
/// Please refer to the crate level documentation of IUP-Rust (the main doc page) for details on
/// ownership of elements.
pub struct Timer(*mut iup_sys::Ihandle);

impl Timer {
    /// Constructs a timer.
    pub fn new() -> Guard<Timer> {
        Guard::new(
            Timer::from_raw(unsafe { iup_sys::IupTimer() })
        )
    }

    /// Gets the set time interval in milliseconds or `None` if not set.
    pub fn time(&self) -> Option<u32> {
        self.attrib_parse("TIME")
    }

    /// Sets the time interval in milliseconds.
    ///
    /// In Windows the minimum value is 10ms.
    pub fn set_time(&mut self, time: u32) -> Self {
        self.set_attrib("TIME", time.to_string())
    }

    /// Starts the timer.
    ///
    /// Does nothing if the TIME attribute is not set i.e. `set_time`.
    ///
    /// If you have multiple threads start the timer in the main thread.
    pub fn run(&mut self) -> Self {
        self.set_attrib("RUN", "YES")
    }

    /// Stops the timer.
    pub fn stop(&mut self) -> Self {
        self.set_attrib("RUN", "NO")
    }

    /// Returns the current timer state.
    pub fn is_running(&self) -> bool {
        self.attrib_bool("RUN").unwrap()
    }
}

impl_element!(Timer, "timer");

/// Called every time the defined time interval is reached.
///
/// To stop the callback from being called simply stop de timer with RUN=NO or `Timer::stop`.
///
/// `CallbackReturn::Close` will be processed.
impl ::callback::ActionCb for Timer {}