spell_framework/forge.rs
1//! `forge` is a mini submodule which provides alternative method to create and run events
2//! after a certain duration of time. Obvious approach to tackle such events is to use
3//! [Timer](https://docs.slint.dev/latest/docs/slint/reference/timer/). Alternatively,
4//! if you want a more rust facing interface (where timed events are not managed inside
5//! `.slint` files and rather directly created in rust code), you can use
6//! [`Forge`]
7//!
8//! ## Use Cases
9//!
10//! This module can easily be used in creating various timed events which are very common
11//! while making shells. For example, it can be used for retriving:-
12//! - Battery Percentage after certain time.
13//! - Possible PowerProfiles and changes.
14//! - CPU and Temperature Analytics etc.
15
16use std::time::Duration;
17
18use smithay_client_toolkit::reexports::calloop::{
19 EventLoop,
20 timer::{TimeoutAction, Timer},
21};
22
23struct ForgeState;
24
25/// An instance of Forge takes the LoopHandle of your window as input for
26/// instance creation. It is currently not usable because of latency issues. A macro
27/// build is in progress to fix latency issues and provide a more streamlined overview.
28pub struct Forge(EventLoop<'static, ForgeState>);
29impl Default for Forge {
30 /// Create an instance on forge.
31 fn default() -> Self {
32 let event_loop: EventLoop<'static, ForgeState> =
33 EventLoop::try_new().expect("Failed to initialize the event loop!");
34 Forge(event_loop)
35 }
36}
37impl Forge {
38 // // Create an instance on forge.
39 // pub fn new(handle: WinHandle) -> Self {
40 // Forge(handle)
41 // }
42
43 /// Add a recurring event. This function takes [`std::time::Duration`] to specify
44 /// time after which it will be polled again with a callback to run. The callback accepts
45 /// SpellWin instance of loop handle as argument for updating UI.
46 pub fn add_event<F: FnMut() + Send + 'static>(&self, duration: Duration, mut callback: F) {
47 self.0
48 .handle()
49 .insert_source(Timer::from_duration(duration), move |_, _, _| {
50 callback();
51 TimeoutAction::ToDuration(duration)
52 })
53 .unwrap();
54 }
55
56 // pub fn smith(&mut self) -> std::thread::JoinHandle<_> {
57 // let data = ForgeState;
58 // std::thread::spawn(move || {
59 // loop {
60 // self.0.dispatch(Duration::from_secs(1), &mut data);
61 // }
62 // })
63 // }
64}