maycoon_core/app/
update.rs

1use bitflags::bitflags;
2use std::sync::Arc;
3use std::sync::atomic::{AtomicU8, Ordering};
4
5bitflags! {
6    /// Update bitflags to define which part of the App should Update.
7    ///
8    /// Possible values:
9    /// - **EVAL** - Re-evaluate the widget tree.
10    /// - **DRAW** - Re-draw the widget tree.
11    /// - **LAYOUT** - Re-layout the widget tree.
12    /// - **FORCE** - Force the App to re-evaluate, re-draw and re-layout the widget tree.
13    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
14    pub struct Update: u8 {
15        /// Re-evaluate the widget tree.
16        const EVAL   = 0b00000001;
17        /// Re-draw the widget tree.
18        const DRAW   = 0b00000010;
19        /// Re-layout the widget tree.
20        const LAYOUT = 0b00000100;
21        /// Force the App to re-evaluate, re-draw and re-layout the widget tree.
22        const FORCE  = 0b00001000;
23        /// Makes the App exit.
24        const EXIT   = 0b00010000;
25    }
26}
27
28/// Manages updates for the application lifecycle.
29///
30/// It's using atomic operations to ensure lockless thread-safety.
31#[derive(Clone, Debug)]
32pub struct UpdateManager {
33    update: Arc<AtomicU8>,
34}
35
36impl UpdateManager {
37    /// Creates a new `UpdateManager`.
38    pub fn new() -> Self {
39        Self {
40            update: Arc::new(AtomicU8::new(0)),
41        }
42    }
43
44    /// Inserts the given `Update` into the `UpdateManager` using bitwise OR.
45    pub fn insert(&self, update: Update) {
46        tracing::debug!("inserting update {update:?}");
47        self.update.fetch_or(update.bits(), Ordering::AcqRel);
48    }
49
50    /// Removes the given `Update` from the `UpdateManager` using bitwise AND.
51    pub fn remove(&self, update: Update) {
52        tracing::debug!("removing update {update:?}");
53        self.update.fetch_and(!update.bits(), Ordering::AcqRel);
54    }
55
56    /// Returns the current `Update` of the `UpdateManager`.
57    pub fn get(&self) -> Update {
58        Update::from_bits(self.update.load(Ordering::Acquire))
59            .expect("failed to decode update bits")
60    }
61
62    /// Sets the current `Update` of the `UpdateManager`.
63    pub fn set(&self, update: Update) {
64        tracing::debug!("setting update {update:?}");
65        self.update.store(update.bits(), Ordering::Release);
66    }
67
68    /// Clears the current `Update` flags of the `UpdateManager`.
69    pub fn clear(&self) {
70        self.update.store(0, Ordering::Release);
71    }
72}
73
74impl Default for UpdateManager {
75    fn default() -> Self {
76        Self::new()
77    }
78}