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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
use raui_core::{
    application::Application, interactive::default_interactions_engine::DefaultInteractionsEngine,
    layout::CoordsMapping,
};
use std::collections::HashMap;

#[derive(Default)]
pub struct ApplicationData {
    pub application: Application,
    pub interactions: DefaultInteractionsEngine,
    pub coords_mapping: CoordsMapping,
}

#[derive(Default)]
pub struct UserInterfaceRes {
    pub(crate) data: HashMap<String, ApplicationData>,
    pub(crate) setup: Option<fn(&mut Application)>,
    pub(crate) last_frame_captured: bool,
    pub pointer_axis_x: String,
    pub pointer_axis_y: String,
    pub pointer_action_trigger: String,
    pub pointer_context_trigger: String,
    pub navigate_accept: String,
    pub navigate_cancel: String,
    pub navigate_up: String,
    pub navigate_down: String,
    pub navigate_left: String,
    pub navigate_right: String,
    pub navigate_prev: String,
    pub navigate_next: String,
    pub text_move_cursor_left: String,
    pub text_move_cursor_right: String,
    pub text_move_cursor_start: String,
    pub text_move_cursor_end: String,
    pub text_delete_left: String,
    pub text_delete_right: String,
}

impl UserInterfaceRes {
    pub fn new(f: fn(&mut Application)) -> Self {
        Self {
            data: Default::default(),
            setup: Some(f),
            last_frame_captured: false,
            pointer_axis_x: Default::default(),
            pointer_axis_y: Default::default(),
            pointer_action_trigger: Default::default(),
            pointer_context_trigger: Default::default(),
            navigate_accept: Default::default(),
            navigate_cancel: Default::default(),
            navigate_up: Default::default(),
            navigate_down: Default::default(),
            navigate_left: Default::default(),
            navigate_right: Default::default(),
            navigate_prev: Default::default(),
            navigate_next: Default::default(),
            text_move_cursor_left: Default::default(),
            text_move_cursor_right: Default::default(),
            text_move_cursor_start: Default::default(),
            text_move_cursor_end: Default::default(),
            text_delete_left: Default::default(),
            text_delete_right: Default::default(),
        }
    }

    pub fn last_frame_captured(&self) -> bool {
        self.last_frame_captured
    }

    pub fn with_pointer_axis(mut self, x: &str, y: &str) -> Self {
        self.pointer_axis_x = x.to_owned();
        self.pointer_axis_y = y.to_owned();
        self
    }

    pub fn with_pointer_trigger(mut self, action: &str, context: &str) -> Self {
        self.pointer_action_trigger = action.to_owned();
        self.pointer_context_trigger = context.to_owned();
        self
    }

    pub fn with_navigation(
        mut self,
        accept: &str,
        cancel: &str,
        up: &str,
        down: &str,
        left: &str,
        right: &str,
        prev: &str,
        next: &str,
    ) -> Self {
        self.navigate_accept = accept.to_owned();
        self.navigate_cancel = cancel.to_owned();
        self.navigate_up = up.to_owned();
        self.navigate_down = down.to_owned();
        self.navigate_left = left.to_owned();
        self.navigate_right = right.to_owned();
        self.navigate_prev = prev.to_owned();
        self.navigate_next = next.to_owned();
        self
    }

    pub fn with_text_move_cursor(
        mut self,
        left: &str,
        right: &str,
        start: &str,
        end: &str,
    ) -> Self {
        self.text_move_cursor_left = left.to_owned();
        self.text_move_cursor_right = right.to_owned();
        self.text_move_cursor_start = start.to_owned();
        self.text_move_cursor_end = end.to_owned();
        self
    }

    pub fn with_text_delete(mut self, left: &str, right: &str) -> Self {
        self.text_delete_left = left.to_owned();
        self.text_delete_right = right.to_owned();
        self
    }

    #[inline]
    pub fn get(&self, app_id: &str) -> Option<&ApplicationData> {
        self.data.get(app_id)
    }

    #[inline]
    pub fn get_mut(&mut self, app_id: &str) -> Option<&mut ApplicationData> {
        self.data.get_mut(app_id)
    }

    #[inline]
    pub fn application(&self, app_id: &str) -> Option<&Application> {
        self.data.get(app_id).map(|item| &item.application)
    }

    #[inline]
    pub fn application_mut(&mut self, app_id: &str) -> Option<&mut Application> {
        self.data.get_mut(app_id).map(|item| &mut item.application)
    }

    #[inline]
    pub fn interactions(&self, app_id: &str) -> Option<&DefaultInteractionsEngine> {
        self.data.get(app_id).map(|item| &item.interactions)
    }

    #[inline]
    pub fn interactions_mut(&mut self, app_id: &str) -> Option<&mut DefaultInteractionsEngine> {
        self.data.get_mut(app_id).map(|item| &mut item.interactions)
    }

    #[inline]
    pub fn coords_mapping(&self, app_id: &str) -> Option<&CoordsMapping> {
        self.data.get(app_id).map(|item| &item.coords_mapping)
    }

    #[inline]
    pub fn coords_mapping_mut(&mut self, app_id: &str) -> Option<&mut CoordsMapping> {
        self.data
            .get_mut(app_id)
            .map(|item| &mut item.coords_mapping)
    }

    pub fn has_layout_widget(&self, app_id: &str, id: &str) -> bool {
        if let Some(item) = self.data.get(app_id) {
            item.application
                .layout_data()
                .items
                .keys()
                .any(|k| k.as_ref() == id)
        } else {
            false
        }
    }
}