action.rs 1.74 KB
Newer Older
Dorota Czaplejewicz's avatar
WIP  
Dorota Czaplejewicz committed
1
/*! The symbol object, defining actions that the key can do when activated */
2

Dorota Czaplejewicz's avatar
WIP  
Dorota Czaplejewicz committed
3
use std::ffi::CString;
4

5 6 7
/// Name of the keysym
#[derive(Debug, Clone, PartialEq)]
pub struct KeySym(pub String);
8

9 10
/// Use to switch views
type View = String;
11 12

/// Use to send modified keypresses
13
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
14
pub enum Modifier {
15 16 17
    /// Control and Alt are the only modifiers
    /// which doesn't interfere with levels,
    /// so it's simple to implement as levels are deprecated in squeekboard.
18 19
    Control,
    Alt,
David96's avatar
David96 committed
20
    Mod4,
21 22 23
}

/// Action to perform on the keypress and, in reverse, on keyrelease
24
#[derive(Debug, Clone, PartialEq)]
25
pub enum Action {
26
    /// Switch to this view
27
    SetView(View),
28
    /// Switch to a view and latch
29 30
    LockView {
        lock: View,
31
        /// When unlocked by pressing it or emitting a key
32
        unlock: View,
33
    },
34 35
    /// Hold this modifier for as long as the button is pressed
    ApplyModifier(Modifier),
36 37
    /// Submit some text
    Submit {
38 39
        /// Text to submit with input-method.
        /// If None, then keys are to be submitted instead.
40 41
        text: Option<CString>,
        /// The key events this symbol submits when submitting text is not possible
42
        keys: Vec<KeySym>,
43
    },
44 45
    /// Erase a position behind the cursor
    Erase,
46
    ShowPreferences,
47
}
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

impl Action {
    pub fn is_locked(&self, view_name: &str) -> bool {
        match self {
            Action::LockView { lock, unlock: _ } => lock == view_name,
            _ => false,
        }
    }
    pub fn is_active(&self, view_name: &str) -> bool {
        match self {
            Action::SetView(view) => view == view_name,
            Action::LockView { lock, unlock: _ } => lock == view_name,
            _ => false,
        }
    }
}