summaryrefslogtreecommitdiff
path: root/src/state/mod.rs
blob: 5efd7709137a24de1bcdf867cd97f935f17d1371 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use std::sync::{RwLock, RwLockReadGuard, RwLockWriteGuard};

use mlua::{UserData, UserDataFields};


pub mod sheetview;

#[derive(Debug, Default)]
pub struct GlobalState {
    pub sheetview: sheetview::SheetViewState,
}

static GLOBAL_STATE: RwLock<GlobalState> = RwLock::new(GlobalState::new());
const DUMMY_STATE: GlobalState = GlobalState::new();

impl GlobalState {
    const fn new() -> Self {
        Self {
            sheetview: sheetview::SheetViewState::new()
        }
    }

    pub fn instance() -> RwLockReadGuard<'static, Self> {
        GLOBAL_STATE.read().unwrap()
    }

    pub fn instance_mut() -> RwLockWriteGuard<'static, Self> {
        GLOBAL_STATE.write().unwrap()
    }
}

impl UserData for GlobalState {
    fn add_fields<'lua, F: UserDataFields<'lua, Self>>(fields: &mut F) {
        fields.add_field_function_get("sheetview", |_, _| Ok(DUMMY_STATE.sheetview))
    }
}