gemgui 0.5.1

GUI application library
Documentation
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589


use futures::Future;

use crate::GemGuiError;
use crate::Result;
use crate::Rect;
use crate::event::Event;
use crate::event::MOUSE_CLICK;
use crate::event::MOUSE_DBLCLICK;
use crate::event::MOUSE_DOWN;
use crate::event::MOUSE_MOVE;
use crate::event::MOUSE_UP;
use crate::event::Properties;
use crate::msgsender::MsgSender;
use crate::ui_data::UiData;
use crate::ui_data::UiDataRef;
use crate::ui_ref::UiRef;
use crate::value_to_string;

use super::JSMessageTx;
use super::JSType;

use std::collections::HashMap;
use std::str::FromStr;
use std::time::Duration;

use std::sync::Arc;
use std::sync::Mutex; 

/// Elements values
pub type Values = HashMap<String, String>;

/// Elements collection
pub type Elements = Vec<Element>;

/// Convenience for a properties parameter to express no properties applied.
pub static NONE_PROPERTIES: Option<&[&str]> = None;


/// HTML Element representation
pub struct Element {
    id: String,
    tx: MsgSender,
    ui: UiDataRef,
}

/// Mouse Event types 
pub enum MouseEvent{
    /// mouse button up
    MouseUp,
    /// Mouse button down
    MouseDown,
    /// Mouse move
    MouseMove,
    /// Mouse click (up and down)
    MouseClick,
    /// Mouse double click (Mouse up and down twice)
    MouseDblClick,
}

impl MouseEvent {
    fn as_str(&self) -> &str {
        match self {
            Self::MouseUp  => MOUSE_UP,
            Self::MouseDown  => MOUSE_DOWN,
            Self::MouseMove  => MOUSE_MOVE,
            Self::MouseClick =>  MOUSE_CLICK,
            Self::MouseDblClick =>  MOUSE_DBLCLICK,
        }
    }
} 

impl Clone for Element {    
    fn clone(&self) -> Self {
        let tx = self.tx.clone();
        Element {
            id: self.id.clone(),
            tx,
            ui: self.ui.clone()}
    }
}

impl Element {


    /// Element id
    /// 
    /// # Return
    /// 
    /// Id string
    pub fn id(&self) -> &str {
        &self.id
    }

    /// Unsubscribe event
    /// 
    /// # Arguments
    /// 
    /// `name` - Event name 
    pub fn unsubscribe(&self, name: &str) {
        UiData::remove_subscription(&self.ui, &self.id, name);   
    }


    /// Subscribe event
    /// 
    /// # Arguments
    /// 
    /// `name` - Event name
    /// 
    /// `callback` - Function called on event
    /// 
    /// `properties` - Optional list of properties that are expected to receive on callback. Note that you have to list
    /// all callbacks that you want to receive
    /// 
    /// `throttle` - Minimum time in between consecutive events. If a new event emit in shorter time, it is ignored
    /// 
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// `Event` - Event information
    /// 
    pub fn subscribe_throttled<CB, Str>(&self, name: &str, callback: CB, properties: Option<&[Str]>, throttle: Duration)
    where
        Str: Into<String> + std::clone::Clone,
        CB: FnMut(UiRef, Event) + Send + 'static {
        let callback = Box::new(callback);
        let properties =  match properties {
            Some(p) => p.iter().cloned().map(|v| { JSType::String(v.into())}).collect(),
            None =>  vec!(JSType::from("")),
        };
        UiData::add_subscription(&self.ui, &self.id, name, callback);

        if name != "created" && self.id() != crate::window::MENU_ELEMENT { // created are not subscribed - come when created. MENU_ELEMENT is not HTML
            let throttle = throttle.as_millis().to_string();   
            let msg =  JSMessageTx {
                element: self.id(),
                _type: "event",
                event: Some(name),
                properties: Some(&properties),
                throttle: Some(&throttle),
                ..Default::default()
            };
            self.send(msg);
        }
    }

    /// Subscribe event
    /// 
    /// # Arguments
    /// 
    /// `name` - Event name
    /// 
    /// `callback` - Function called on event
    /// 
    /// `properties` - list of properties that are expected to receive on callback. Note that you have to list
    /// all callbacks that you want to receive
    /// 
    /// 
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// 
    /// `Event` - Event information
    /// 
    pub fn subscribe_properties<CB, Str>(&self, name: &str, callback: CB, properties: &[Str])
    where  Str: Into<String> + std::clone::Clone,
        CB: FnMut(UiRef, Event) + Send + 'static {
            self.subscribe_throttled(name, callback, Some(properties), Duration::from_micros(0u64));

    }
    
    /// Subscribe event
    /// 
    /// # Arguments
    /// 
    /// `name` - Event name
    /// 
    /// `callback` - Function called on event
    /// 
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// 
    /// `Event` - Event information
    pub fn subscribe<CB>(&self, name: &str, callback: CB) 
    where CB: FnMut(UiRef, Event) + Send + 'static {    
        self.subscribe_throttled(name, callback, NONE_PROPERTIES, Duration::from_millis(0u64))
    }

    /// See [subscribe](Self::subscribe)
    pub fn subscribe_async<CB, Fut>(&self, name: &str, async_func: CB)
     where CB: FnOnce(UiRef, Event)-> Fut + Send + Clone + 'static,
     Fut: Future<Output =  ()> + Send +  'static {
        self.subscribe_throttled(name, UiData::as_sync_fn(async_func), NONE_PROPERTIES, Duration::from_millis(0u64))
    }

     /// See [subscribe_properties](Self::subscribe_properties)
    pub fn subscribe_properties_async<CB, Str, Fut>(&self, name: &str, async_func: CB,  properties: &[Str])
    where CB: FnOnce(UiRef, Event)-> Fut + Send + Clone + 'static,
    Fut: Future<Output = ()> + Send +  'static ,
        Str: Into<String> + std::clone::Clone {
       self.subscribe_throttled(name, UiData::as_sync_fn(async_func), Some(properties), Duration::from_millis(0u64))
   }

    /// See [subscribe_throttled](Self::subscribe_throttled)
    pub fn subscribe_throttled_async<CB, Str, Fut>(&self, name: &str, async_func: CB,  properties: &[Str], throttle: Duration)
    where CB: FnOnce(UiRef, Event)-> Fut + Send + Clone + 'static,
    Fut: Future<Output =  ()> + Send +  'static ,
        Str: Into<String> + std::clone::Clone {
       self.subscribe_throttled(name, UiData::as_sync_fn(async_func), Some(properties), throttle)
   }

   /// Subscribe mouse events
   /// 
   /// A Convenience class to subscribe mouse events
   /// 
   /// basically a:
   /// ```no_run 
   /// # use std::time::Duration;
   /// # use futures::Future;
   /// # use crate::gemgui::event::Event;
   /// # use crate::gemgui::ui_ref::UiRef;
   /// # use crate::gemgui::event::MOUSE_CLICK;
   /// # struct Element__ {};
   /// # impl Element__ {
   /// # pub fn subscribe_throttled<CB, Str>(&self, name: &str, callback: CB, properties: Option<&[Str]>, throttle: Duration)
   /// # where
   /// # Str: Into<String> + std::clone::Clone,
   /// # CB: FnMut(UiRef, Event) + Send + 'static {}
   /// # fn foo(&self) { 
   /// # let callback = |_: crate::gemgui::ui_ref::UiRef, _: Event| {};
   /// let properties = vec!("clientX", "clientY");
   /// self.subscribe_throttled(MOUSE_CLICK, callback, Some(&properties), Duration::from_millis(10));
   /// # }
   /// # }
   /// ```
   ///
   /// # Arguments
   /// 
   /// `mouse` - Mouse event 
   /// 
   /// `event` - Callback on mouse event
   /// 
   /// # Callback
   /// 
   /// `UiRef`- Reference to UI
   /// 
   /// `Event` - Event information
   pub fn subscribe_mouse<CB>(&self, mouse: MouseEvent, callback: CB) 
   where CB: FnMut(UiRef, Event) + Send + 'static {
            let properties = vec!("clientX", "clientY");
            /*
            
            Next version suggestion
            possible change is have callback that directly return x y

            let m_cb = move |ui: UiRef, ev: Event| {
                let x = ev.property_str("clientX").unwrap().parse::<i32>().unwrap();
                let y = ev.property_str("clientY").unwrap().parse::<i32>().unwrap();
                callback(ui, x, y, ev.element());
            };*/
            self.subscribe_throttled(mouse.as_str(), callback, Some(&properties), Duration::from_millis(10));
   }

   /// See [subscribe_mouse](Self::subscribe_mouse)
   pub fn subscribe_mouse_async<CB, Fut>(&self, mouse: MouseEvent, async_func: CB)
    where CB: FnOnce(UiRef, Event)-> Fut + Send + Clone + 'static,
    Fut: Future<Output =  ()> + Send +  'static  {
    let properties = vec!("clientX", "clientY");  
    self.subscribe_throttled(mouse.as_str(), UiData::as_sync_fn(async_func), Some(&properties), Duration::from_millis(0u64))
    }

    /// Get HTML content of the element
    /// 
    /// # Return
    /// 
    /// HTML content
    pub async fn html(&self) -> Result<String> {
    let result = self.query("innerHTML", &vec![]).await;
    match result {
        Ok(value) => Ok(String::from(value.as_str().unwrap())),
        Err(e) => Err(e),
    }
}
    

    /// Set inner HTML of the element
    /// 
    /// # Arguments
    /// 
    /// `html`- HTML applied to the element
    /// 
    pub fn set_html(&self, html: &str) {
        let msg =  JSMessageTx {
            element: self.id(),
            _type: "html",
            html: Some(html),
            ..Default::default()
        };
        self.send(msg);
    }

    /// Set element styles
    /// 
    /// # Arguments
    /// 
    /// `style`- Style name
    /// 
    /// `value`- Style value 
    pub fn set_style(&self, style: &str, value: &str) {
        let msg =  JSMessageTx {
            element: self.id(),
            _type: "set_style",
            style: Some(style),
            value: Some(value),
            ..Default::default()
        };
        self.send(msg);
    }


    /// Get element styles
    /// 
    /// # Arguments
    /// 
    /// `filter` -  get only styles that are lists
    /// 
    /// # Return
    /// 
    /// Style - value pairs
    pub async fn styles<Str: AsRef<str>>(&self, filter: &[Str]) -> Result<Values> {
        let plist = filter.iter().map(|s|{String::from(s.as_ref())}).collect();
        let result = self.query("styles", &plist).await;
        match result {
            Ok(value) => {
                match crate::value_to_string_map(value) {
                    Some(v) => {Ok(v)},
                    None => GemGuiError::error("Bad value"),
                }
            },
            Err(e) => Err(e),
        }
    }

    
    /*
    Not working - C++ test was faulty - TODO if needed
    pub fn remove_style<Str>(&self, style: Str) 
    where Str: Into<String> {
        let msg =  JSMessageTx {
            element: self.id.clone(),
            _type: String::from("remove_style"),
            style: Some(style.into()),
            ..Default::default()
        };
        self.send(msg);
    }
    */
    
    /// Apply an attribute
    /// 
    /// For attributes that has no value
    /// 
    /// # Arguments
    /// 
    /// `attr` - attribute name
    pub fn set_attribute_on(&self, attr: &str) {
        self.set_attribute(attr, "");
    }

    /// Set an attribute values
    /// 
    /// 
    /// # Arguments
    /// 
    /// `attr` - attribute name.
    /// 
    /// `value` - attribute value.
    pub fn set_attribute(&self, attr: &str, value: &str) {
        let msg =  JSMessageTx {
            element: self.id(),
            _type: "set_attribute",
            attribute: Some(attr),
            value: Some(value),
            ..Default::default()
        };
        self.send(msg);
    }

    /// Get element attributes
    /// 
    /// 
    /// # Return
    /// 
    /// Attribute - value pairs
    pub async fn attributes(&self) -> Result<Values> {
        let result = self.query("attributes", &vec![]).await;
        match result {
            Ok(value) => {
                match crate::value_to_string_map(value) {
                    Some(v) => {println!("attributes {:#?}", &v); Ok(v)},
                    None => GemGuiError::error("Bad value"),
                }
            },
            Err(e) => Err(e),
        }
    }

    /// Remove an attribute
    /// 
    /// # Arguments
    /// `attr` - attribute name
    pub fn remove_attribute(&self, attr: &str) {
        let msg =  JSMessageTx {
            element: self.id(),
            _type: "remove_attribute",
            attribute: Some(attr),
            ..Default::default()
        };
        self.send(msg);
    }

    /// Get element values
    /// 
    /// # Return
    /// 
    /// name - value pairs
    pub async fn values(&self) -> Result<Values> {
        let result = self.query("value", &vec![]).await;
        match result {
            Ok(value) => {
                match crate::value_to_string_map(value) {
                    Some(v) => {Ok(v)},
                    None => GemGuiError::error("Bad value"),
                }
            },
            Err(e) => Err(e),
        }
    }

    /// Get element child elements
    /// 
    /// # Return
    /// 
    /// children list
    pub async fn children(&self) -> Result<Elements> {
        let result = self.query("children", &vec![]).await;
        match result {
            Ok(value) => UiData::elements_from_values(&self.ui, value, &self.tx),
            Err(e) => Err(e),
        }
    }

    /*
    
    To test etc. enable for next version upon need

        pub async fn tag_name(&self, tag_name: &str) -> Result<Elements, gemguiError> {
        let result = self.query("tag_name", &vec!("tag_name")).await;
        match result {
            Ok(value) => UiData::elements_from_values(&self.ui, value, &self.tx),
            Err(e) => Err(e),
        }
    }
    
    */
    

    /// Remove this element
    pub fn remove(&self) {
        let msg =  JSMessageTx {
            element: self.id(),
            _type: "remove",
            remove: Some(self.id()),
            ..Default::default()
        };
        self.send(msg);
    }

    /// Get element type
    /// 
    /// # Return
    /// 
    /// Element's type tag.
    pub async fn element_type(&self) -> Result<String> {
        let result = self.query("element_type", &vec![]).await;
        match result {
            Ok(tag) => {
                match value_to_string(tag) {
                    Some(v) => Ok(v),
                    None => GemGuiError::error("Bad value"),
                }
            },
            Err(e) => Err(e),
        }
    }



    /// Get element UI rectangle
    /// 
    /// # Return
    /// 
    /// Element's UI rectangle as requested type 
    pub async fn rect<T>(&self) -> Result<Rect<T>>
    where T: FromStr + Clone + Copy {
        let result = self.query("bounding_rect", &vec![]).await;
        let err = |e: &str| GemGuiError::error(format!("Bad value {e}"));
        match result {
            Ok(value) => {
                match crate::value_to_string_map(value) {
                    Some(v) => {
                        if ! (v.contains_key("x") && v.contains_key("y") && v.contains_key("width") && v.contains_key("height")) {
                            return err("N/A");
                        }
                        let x = &v["x"];
                        // values can be fractions, but that is not ok if request is non-float
                        let x = x.split_once('.').unwrap_or((x, "")).0;
                        let x = match x.parse::<T>() {Ok(v) => v, Err(_) => return err(&v["x"])};

                        let y = &v["y"];
                        let y= y.split_once('.').unwrap_or((y, "")).0;
                        let y = match y.parse::<T>() {Ok(v) => v, Err(_) => return err(&v["y"])};

                        let width = &v["width"];
                        let width = width.split_once('.').unwrap_or((width, "")).0;
                        let width = match width.parse::<T>() {Ok(v) => v, Err(_) => return err(&v["width"])};

                        let height = &v["height"];
                        let height = height.split_once('.').unwrap_or((height, "")).0;
                        let height = match height.parse::<T>() {Ok(v) => v, Err(_) => return err(&v["height"])};
                        Ok(Rect {x, y, width, height})
                    },
                    None => err("invalid"),
                }
            },
            Err(e) => Err(e),
        }
    }

    /// UiRef of the element
    /// 
    /// # Return
    /// 
    /// UiRef
    pub fn ui(&self) -> UiRef {
        UiRef::new(self.ui.clone())
    }

    pub (crate) fn call(&self, name: &str, properties: Properties) {
        UiData::call_subscription(&self.ui, &self.id, name, properties);
    }

    pub(crate) fn create(&self, html_element: &str, parent: &Element) {
        let msg =  JSMessageTx {
            element: parent.id(),
            _type: "create",
            new_id: Some(self.id()),
            html_element: Some(html_element),
            ..Default::default()
        };
        self.
        send(msg);
    }
    
    pub(crate) fn construct(id: String, tx: MsgSender, ui: Arc<Mutex<UiData>>) -> Element {
        Element  {
            id,
            tx,
            ui
        }
    }

    pub (crate) fn send_bin(&self, msg: Vec<u8>) {
        self.tx.send_bin(msg);
    }

    pub (crate) fn send(&self, msg: JSMessageTx) {
        let json = serde_json::to_string(&msg).unwrap();
        self.tx.send(json); 
    }

    async fn query(&self, name: &str, query_params: &Vec<String>) -> Result<JSType> {
        UiRef::do_query(&self.ui, &self.id, name, query_params).await
    }

}