1use alloc::collections::BTreeMap;
2use core::cell::RefCell;
3use embassy_sync::blocking_mutex::{Mutex, raw::CriticalSectionRawMutex};
4
5#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
7pub struct LayerId(pub u8);
8
9#[derive(PartialEq, Eq, PartialOrd, Ord)]
11pub struct LayerManagerEntry(u64);
12
13static LAYER_MANAGER_MAP: Mutex<CriticalSectionRawMutex, RefCell<BTreeMap<u64, LayerId>>> =
14 Mutex::new(RefCell::new(BTreeMap::new()));
15
16#[derive(Clone, Copy, Default)]
22#[non_exhaustive]
23pub struct LayerManager {}
24
25impl LayerManager {
26 pub fn new() -> Self {
28 Self {}
29 }
30
31 pub fn push(&self, layer: LayerId) -> LayerManagerEntry {
33 LAYER_MANAGER_MAP.lock(|map| {
34 let mut map = map.borrow_mut();
35 let new_id = match map.last_key_value() {
36 Some((last_id, _)) => last_id + 1,
37 None => 0,
38 };
39 assert!(!map.contains_key(&new_id));
40 map.insert(new_id, layer);
41 LayerManagerEntry(new_id)
42 })
43 }
44
45 pub fn remove(&self, entry: LayerManagerEntry) -> LayerId {
47 LAYER_MANAGER_MAP.lock(|map| map.borrow_mut().remove(&entry.0).unwrap())
48 }
49
50 pub fn active(&self) -> LayerId {
52 LAYER_MANAGER_MAP.lock(|map| match map.borrow().last_key_value() {
53 Some((_, layer)) => *layer,
54 None => LayerId(0),
55 })
56 }
57}