native_windows_gui/controls/
timer.rs1#![allow(deprecated)]
2
3use crate::controls::ControlHandle;
4use crate::win32::{window_helper as wh, window::build_timer};
5use crate::NwgError;
6use std::cell::RefCell;
7
8const NOT_BOUND: &'static str = "Timer is not yet bound to a winapi object";
9const UNUSABLE_TIMER: &'static str = "Timer parent window was freed";
10const BAD_HANDLE: &'static str = "INTERNAL ERROR: Timer handle is not Timer!";
11
12
13#[deprecated(
50 since = "1.0.11",
51 note = "Use AnimationTimer instead. The winapi timer does not have a constant tick and will call your single threaded from another thread."
52)]
53#[derive(Default)]
54pub struct Timer {
55 pub handle: ControlHandle,
56 interval: RefCell<u32>,
57}
58
59impl Timer {
60
61 pub fn builder() -> TimerBuilder {
62 TimerBuilder {
63 parent: None,
64 interval: 100,
65 stopped: true
66 }
67 }
68
69 pub fn valid(&self) -> bool {
72 if self.handle.blank() { return false; }
73 let (hwnd, _) = self.handle.timer().expect(BAD_HANDLE);
74 wh::window_valid(hwnd)
75 }
76
77 pub fn interval(&self) -> u32 {
79 *self.interval.borrow()
80 }
81
82 pub fn set_interval(&self, i: u32) {
84 *self.interval.borrow_mut() = i;
85 }
86
87 pub fn stop(&self) {
89 if self.handle.blank() { panic!("{}", NOT_BOUND); }
90 if !self.valid() { panic!("{}", UNUSABLE_TIMER); }
91 let (hwnd, id) = self.handle.timer().expect(BAD_HANDLE);
92
93 wh::kill_timer(hwnd, id);
94 }
95
96 pub fn start(&self) {
98 if self.handle.blank() { panic!("{}", NOT_BOUND); }
99 if !self.valid() { panic!("{}", UNUSABLE_TIMER); }
100 let (hwnd, id) = self.handle.timer().expect(BAD_HANDLE);
101
102 wh::start_timer(hwnd, id, self.interval());
103 }
104
105}
106
107impl Drop for Timer {
108 fn drop(&mut self) {
109 self.handle.destroy();
110 }
111}
112
113pub struct TimerBuilder {
114 parent: Option<ControlHandle>,
115 interval: u32,
116 stopped: bool
117}
118
119impl TimerBuilder {
120
121 pub fn interval(mut self, interval: u32) -> TimerBuilder {
122 self.interval = interval;
123 self
124 }
125
126 pub fn stopped(mut self, stop: bool) -> TimerBuilder {
127 self.stopped = stop;
128 self
129 }
130
131 pub fn parent<C: Into<ControlHandle>>(mut self, p: C) -> TimerBuilder {
132 self.parent = Some(p.into());
133 self
134 }
135
136 pub fn build(self, out: &mut Timer) -> Result<(), NwgError> {
137 let parent = match self.parent {
138 Some(p) => match p.hwnd() {
139 Some(handle) => Ok(handle),
140 None => Err(NwgError::control_create("Wrong parent type"))
141 },
142 None => Err(NwgError::no_parent("Timer"))
143 }?;
144
145 *out = Default::default();
146
147 out.handle = unsafe { build_timer(parent, self.interval, self.stopped) };
148 out.set_interval(self.interval);
149
150 Ok(())
151 }
152
153}
154
155impl PartialEq for Timer {
156 fn eq(&self, other: &Self) -> bool {
157 self.handle == other.handle
158 }
159}