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
37
38
39
40
use std::sync::Arc;

use tokio::sync::watch;

#[derive(Clone, Copy, PartialEq, Eq)]
pub enum NavigationMode {
    Keyboard,
    NotKeyboard,
}

/// Manages the navigation mode.
#[derive(Clone)]
pub struct NavigatorState {
    _setter: Arc<watch::Sender<NavigationMode>>,
    _getter: watch::Receiver<NavigationMode>,
}

impl NavigatorState {
    pub fn new(mode: NavigationMode) -> Self {
        let (set, get) = watch::channel(mode);
        Self {
            _setter: Arc::new(set),
            _getter: get,
        }
    }

    pub fn getter(&self) -> watch::Receiver<NavigationMode> {
        self._getter.clone()
    }

    pub fn get(&self) -> NavigationMode {
        *self._getter.borrow()
    }

    pub fn set(&self, mode: NavigationMode) {
        if self.get() != mode {
            self._setter.send(mode).unwrap();
        }
    }
}