Skip to main content

lgui_core/core/input/
action.rs

1use std::borrow::Cow;
2
3pub const POINTER_DOWN_ACTION: &str = "__ui.pointer.down";
4pub const POINTER_DRAG_ACTION: &str = "__ui.pointer.drag";
5pub const POINTER_UP_ACTION: &str = "__ui.pointer.up";
6
7#[derive(Clone, Debug, PartialEq, Eq, Hash)]
8pub struct ActionId(Cow<'static, str>);
9
10impl ActionId {
11    pub fn new(value: &'static str) -> Self {
12        Self(Cow::Borrowed(value))
13    }
14
15    pub fn owned(value: impl Into<String>) -> Self {
16        Self(Cow::Owned(value.into()))
17    }
18
19    pub fn as_str(&self) -> &str {
20        &self.0
21    }
22}
23
24impl From<&'static str> for ActionId {
25    fn from(value: &'static str) -> Self {
26        Self::new(value)
27    }
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct UiAction {
32    pub id: ActionId,
33    pub payload: Option<Cow<'static, str>>,
34}
35
36impl UiAction {
37    pub fn new(id: impl Into<ActionId>) -> Self {
38        Self {
39            id: id.into(),
40            payload: None,
41        }
42    }
43
44    pub fn payload(mut self, payload: impl Into<Cow<'static, str>>) -> Self {
45        self.payload = Some(payload.into());
46        self
47    }
48
49    pub fn id(&self) -> &ActionId {
50        &self.id
51    }
52
53    pub fn payload_value(&self) -> Option<&str> {
54        self.payload.as_deref()
55    }
56}