aboutsummaryrefslogtreecommitdiff
path: root/src/animations.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/animations.rs')
-rw-r--r--src/animations.rs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/animations.rs b/src/animations.rs
new file mode 100644
index 0000000..634986b
--- /dev/null
+++ b/src/animations.rs
@@ -0,0 +1,48 @@
1use crate::IndicatorConfig;
2use std::thread;
3use std::time::Duration;
4
5pub struct Animation {
6 duration_per_frame: Duration,
7 frames: Vec<xcb::Arc>,
8 clear: bool,
9}
10
11impl Animation {
12 pub fn circles(
13 config: IndicatorConfig,
14 function: Box<dyn DoubleEndedIterator<Item = u16>>,
15 clear: bool,
16 ) -> Animation {
17 let no_of_frames = config.framerate;
18 let duration_per_frame =
19 Duration::from_millis(config.duration).div_f64(no_of_frames as f64);
20 let frames = function
21 .map(|i| {
22 xcb::Arc::new(
23 ((config.max_size as f32) / (2. * no_of_frames as f32) * i as f32) as i16, // x
24 ((config.max_size as f32) / (2. * no_of_frames as f32) * i as f32) as i16, // y
25 (config.max_size) - (config.max_size / no_of_frames) * i as u16, // width
26 (config.max_size) - (config.max_size / no_of_frames) * i as u16, // height
27 0, // start angle
28 360 << 6, // end angle
29 )
30 })
31 .collect::<Vec<xcb::Arc>>();
32 return Animation {
33 duration_per_frame,
34 frames,
35 clear,
36 };
37 }
38 pub fn play(&self, conn: &xcb::Connection, win: u32, gfx_ctx: u32) {
39 for frame in &self.frames {
40 xcb::poly_arc(&conn, win, gfx_ctx, &[*frame]);
41 conn.flush();
42 if self.clear {
43 xcb::clear_area(&conn, true, win, frame.x(), frame.y(), 2000, 2000);
44 }
45 thread::sleep(self.duration_per_frame);
46 }
47 }
48}