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
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
pub(crate) mod server;
mod utils;

use crate::Menu;
use crate::Result;

use core::fmt;
use std::sync::Arc;
use std::sync::Mutex;

use std::process::Command;

use std::time::Duration;

use futures::Future;
use tokio::sync::mpsc;
use tokio::sync::watch;

/// TimerId to identify a timer. Unique on Ui lifetime.
pub type TimerId = u32;

/// Subscription callback type.
pub type SubscribeCallback = dyn FnMut(UiRef, Event) + Send + 'static;

/// Time callback type.
pub type TimerCallback = dyn FnMut(UiRef, TimerId) + Send + 'static;

//pub type AsyncTimerCallback = dyn FnMut(UiRef, TimerId) ->
// (dyn std::future::Future<Output = ()> + Send + 'static) + Send + 'static;

use crate::Filemap;
use crate::GemGuiError;
use crate::JSMessageRx;

use crate::default_error;
use crate::event::Event;
use crate::event::Properties;
use crate::element::Element;

use crate::ui_data::UiData;
use crate::ui_data::UiDataRef;
use crate::ui_data::State;
use crate::ui_ref::UiRef;


pub (crate) type ChannelReceiver<T> = tokio::sync::mpsc::Receiver<T>;
pub (crate) type ChannelSender<T> = tokio::sync::mpsc::Sender<T>;

// internal resources that are added with external
include!(concat!(env!("OUT_DIR"), "/generated.rs"));

// batch commands
pub (crate) static BATCH_BEGIN: &str = "batch_begin";
pub (crate) static BATCH_END: &str = "batch_end";  
pub (crate) static CLOSE_REQUEST: &str = "close_request";


// ##known issues:## 
// * Sync callbacks are FnMut, but are limited with Send + 'static
// ** It would be nice to get rid of those: Send to let use Rc + RefCell etc non-Send stuff,
// 'static to ui's lifetime would even let use ref's. However async_to_fn internal functions seems 
// to make that tricky and probably async should be be wrapped in sync handlers.  (that also
// has a extra task spawn so probably a real design flaw)
// Next versions
// * Port reserve
// ** Maybe not the best way to do this as free and use wont be
// atomic and hence use of port may fail



/// Target enum
 pub enum Target {
    /// Blank Opens the linked document in a new window or tab
    Blank,
    /// Same Opens the linked document in the same frame as it was clicked (this is default). 
    Same,
    /// Parent Opens the linked document in the parent frame.
    Parent,
    /// Top Opens the linked document in the full body of the window.
    Top,
    /// Framename Opens the linked document in the named iframe
    FrameName(String),	
}

/// Py Ui Flags See [Gui::set_python_gui]
pub mod py_ui_flags {
    /// See pywebview documentation 
    pub const NORESIZE : u32 = 0x1;
     /// See pywebview documentation 
    pub const FULLSCREEN : u32 = 0x2;
    /// See pywebview documentation 
    pub const HIDDEN : u32 = 0x4;
    /// See pywebview documentation 
    pub const FRAMELESS : u32 = 0x8;
    /// See pywebview documentation 
    pub const MINIMIZED : u32 = 0x10;
    /// See pywebview documentation 
    pub const ONTOP : u32 = 0x20;
    /// See pywebview documentation 
    pub const CONFIRMCLOSE : u32 = 0x40;
    /// See pywebview documentation 
    pub const TEXTSELECT : u32 = 0x80;
    /// See pywebview documentation 
    pub const EASYDRAG : u32 = 0x100;
    /// See pywebview documentation 
    pub const TRANSPARENT : u32 = 0x200;
}


impl Target {
    pub (crate) fn value(&self) -> &str {
        match self {
            Self::Blank => "_blank",
            Self::Same => "_self",	
            Self::Parent => "_parent",	
            Self::Top => "_top",	
            Self::FrameName(value) => value,	
        }
    }
}

pub (crate) mod private {
    use crate::ui_data::UiDataRef;
    pub trait UserInterface {    
        fn ui(&self) -> &UiDataRef;
    }
}

/// UI interface
pub trait Ui : private::UserInterface {

    /// Root element
    /// HTML really does not have a root element, but this helps to refer &lt;body&gt; level
    fn root(&self) -> Element {
        UiData::root(self.ui())
    }

    /// Execute Javascript on UI environment
    /// 
    /// # Arguments
    /// 
    /// `eval`- Javascript code executed in UI document context. 
    fn eval(&self, eval: &str) {
        UiData::eval(self.ui(), eval)
    }

    /// Start a batch
    /// Batches let collect multiple commands as singe request. That may speed up their processing and 
    /// avoid UI flickering on complex UI modification sequences.
    fn batch_begin(&self) {
        UiData::batch_begin(self.ui())
    }

    /// End a batch
    /// The collected batch is immediately sent to UI.
    fn batch_end(&self) {
        UiData::batch_end(self.ui())
    }

    /// Set logging
    /// 
    /// # Arguments
    /// 
    /// `logging` - true starts logging, false stops UI logging
    fn set_logging(&self, logging: bool) {
        UiData::set_logging(self.ui(), logging)
    }
 
    /// Send a message to UI that is bounced back as a log
    /// 
    /// # Arguments
    /// 
    /// `msg` - message
    fn debug(&self, msg: &str) {
        UiData::debug(self.ui(), msg)
    }
    
         
    /// Show a alert dialog with a message
    /// 
    /// # Arguments
    /// 
    /// `msg` - message
    fn alert(&self, msg: &str) {
        UiData::alert(self.ui(), msg)
    }
    
    /// Open an page on UI
    /// 
    /// Note If target page wont contain gemgui.rs the page cannot be accessed.
    /// 
    /// # Arguments
    /// 
    /// `url` - message
    /// 
    /// `target` - target where url is opened
    fn open(&self, url: &str, target: Target) {
        UiData::open(self.ui(), url, target)
    }

    /// Get an application resource
    /// 
    /// # Arguments
    /// 
    /// ´resource_name´ - name of the resource
    fn resource(&self, resource_name: &str) -> Option<Box<[u8]>> {
        UiData::resource(self.ui(), resource_name)
    }

    /// Add a file content as an application resources
    ///
    /// # Arguments
    /// 
    /// `path` - path to file
    /// 
    /// # Return
    /// 
    /// On success name of the resource 
    fn add_resource<PathStr>(&self, path: PathStr) -> Result<String>
    where PathStr: AsRef<std::path::Path> {
        UiData::add_file(self.ui(), path)
    }

    /// Exit event loop
    fn exit(&self) {
        UiData::exit(self.ui());
    }

    /// Instantiate an element
    /// It is expected that element is defined in HTML or added by `add_element`, see [UiRef::add_element_async] or [UiRef::add_element_async] how to create a non-exiting
    /// element. Please note that this function always success event there is no such element as this creates a light weight
    /// Rust side struct. You have to call some query function to get error or call some function to get an error callback. 
    /// 
    /// # Arguments
    /// 
    /// `id` - refer to HTML id
    /// 
    fn element(&self, id: &str) -> Element {
        UiData::element(self.ui(), id)
    }

    /// Cancel timer
    /// 
    /// # Arguments
    /// 
    /// `id` - Timer id.
    fn cancel_timer(&self, id: TimerId) -> Result<()> {
        UiData::cancel_timer(self.ui(), id)
    }

    /// One shot timer
    /// 
    /// # Arguments
    /// 
    /// `Duration` - timer timeouts
    /// 
    /// `callback` - Callback function called on timeout
    ///     
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// 
    /// `TimerId` - id of request timer
    ///     
    /// # Return
    /// 
    /// TimerId
    fn after<CB>(&self, after: Duration, callback: CB) -> TimerId
    where CB: FnMut(UiRef, TimerId) + Send + 'static {
        UiData::after(self.ui(), after, callback)
    }


    /// One shot timer
    /// 
    /// See [after](Self::after)
    fn after_async<CB, Fut>(&self, after: Duration, async_func: CB) -> TimerId
    where CB: FnOnce(UiRef, TimerId)-> Fut + Send + Clone + 'static,
    Fut: Future<Output = ()>  + Send + 'static {
        UiData::after_async(self.ui(), after, async_func)
    }
   
    /// Periodic timer
    /// 
    /// See [periodic](Self::periodic)
    fn periodic<CB>(&self, period: Duration, callback: CB) -> TimerId
    where CB: FnMut(UiRef, TimerId) + Send + 'static {
        UiData::periodic(self.ui(), period, callback)   
    }

    /// Periodic timer
    /// 
    /// # Arguments
    /// 
    /// `Duration` - timer timeouts
    /// 
    /// `callback` - Callback function called on timeout
    /// 
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// 
    /// `TimerId` - id of request timer
    ///  
    /// # Return
    /// 
    /// TimerId
    fn periodic_async<CB, Fut>(&self, period: Duration, async_func: CB) -> TimerId
    where CB: FnOnce(UiRef, TimerId)-> Fut + Send + Clone + 'static,
    Fut: Future<Output = ()>  + Send + 'static {
        UiData::periodic_async(self.ui(), period, async_func)
    }

}


/// Ui instance

pub struct Gui  {
    ui: UiDataRef,
    index_html : String,
    subscription_receiver: ChannelReceiver<String>,
    timer_receiver:  ChannelReceiver<TimerId>,
    start_cmd: Option<(String, Vec<String>)>,
    server: server::WSServer,
    on_start_cb: Option<Box<dyn FnMut(UiRef)>>,
    on_start_notifee: watch::Sender<State>,
    on_reload_cb: Option<Box<dyn FnMut(UiRef)>>,
    on_error_cb: Option<Box<dyn FnMut(UiRef, String)>>,
}

impl fmt::Debug for Gui {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

        let ui = self.ui.lock().unwrap();
        let empty: (String, Vec<String>) = ("".to_string(), vec!());
        let (cmd, params) = if self.start_cmd.is_some() {self.start_cmd.as_ref().unwrap()} else {&empty};
        f.debug_struct("Gui")
         .field("ui", &ui)
         .field("server", &self.server)
         .field("index_html", &self.index_html)
         .field("start_cmd", &cmd)
         .field("start_params", &params)
         .finish()
    }
}


impl private::UserInterface for Gui {
    fn ui(&self) -> &UiDataRef {
        &self.ui
    }
}


impl Ui for Gui { }


impl Gui {
    /// Create a UI

    pub fn new(user_map : Filemap, index_html: &str, port: u16) -> Result<Self> {
        if ! port_scanner::local_port_available(port) {
            return GemGuiError::error(format!("Port {port} is not available"));
        }
        let mut filemap = user_map;
        for resource in RESOURCES {
            let res = base64::decode(resource.1).unwrap();
            let key =  resource.0.to_string();
            if filemap.contains_key(&key) {
                eprintln!("Warning: {:#?} already in resources", &key);
            }
            filemap.insert(key, res);
        }
        
        if ! filemap.contains_key(index_html) {
            return GemGuiError::error(format!("Error {index_html}, not found"));
        }

        let filemap = Arc::new(Mutex::new(filemap));
  
        let (subscription_sender, subscription_receiver) = mpsc::channel(32);
        let (timer_sender, timer_receiver) = mpsc::channel(32);
        let server = server::new(filemap.clone(), port, subscription_sender.clone());

        let(start_notifee, start_notify) = watch::channel(State::Init);

        let ui = UiData::new(filemap,
             server.sender(),
            timer_sender,
            start_notify,
            subscription_sender,
    
    );

       let start_cmd = None;

        Ok(Gui{
            ui: Arc::new(Mutex::new(ui)),
            index_html: index_html.to_string(),
            subscription_receiver,
            timer_receiver,
            start_cmd,
            server,
            on_start_cb: None,
            on_start_notifee: start_notifee,
            on_reload_cb: None,
            on_error_cb: Some(Box::new(|ui, err_msg| {default_error(ui, err_msg)})),
        })
    }

    /// URL of Ui
    pub fn address(&self) -> String {
        // currently only localhost - but remote UI in future easily possible!
        format!("http://127.0.0.1:{}/{}", self.server.port(), self.index_html)
    }

    async fn run_process(cmd: (String, Vec<String>)) -> Result<bool> {
   
        let output = Command::new(&cmd.0)
            .args(&cmd.1)
            .spawn();

        // we wait a moment so try_wait knows if spawn has really succeed
        tokio::time::sleep(Duration::from_millis(1500)).await;

        match output {
            Ok(mut child) => {
                match child.try_wait() {
                    Ok(status) => match status {
                        None => Ok(true),
                        Some(err) => {
                            if err.code().unwrap_or(0) != 0 {
                                eprintln!("Spawned process {} not running {err}", cmd.0);
                                Ok(false)
                            } else {
                                Ok(true)    // OSX uses 'open' app to spawn browser, hence it may have ended, we just rely on error code
                            }
                        },
                    },
                    Err(err) => GemGuiError::error(format!("Spawn process failed: {err}")),
                }
            }, // here we get handle to spawned UI - not used now as exit is done nicely
            Err(e) => {
                if cmd.1.is_empty() {
                    GemGuiError::error(format!("Error while spawning call:'{}' error:{} - URL is missing!", cmd.0, e))
                } else {
                    GemGuiError::error(format!("Error while spawning call:'{}' params:'{:#?}' error:{}", cmd.0, cmd.1, e))
                }
            }
        } 
    }


    /// Overrides UI application command line. The default is a OS specific call to system default browser.
    /// 
    /// 
    /// # Arguments
    /// 
    /// `ui_cmd` - command line call
    /// 
    /// `ui_cmp_params`- list of parameters
    pub fn set_gui_command_line<Str: Into<String> + Clone>(&mut self, cmd: &str, params: &[Str]) {
        let params = params.iter().map(move |v| v.clone().into()).collect();
        self.start_cmd = if !cmd.is_empty() {Some((cmd.to_string(), params))} else {None};
    }


    /// Set python (pywebview) UI
    /// 
    /// # Arguments
    /// `title` - window title
    /// `width` - window width
    /// `height` - window height
    /// `python_parameters` - parameters passed to pywebview e.g "{"debug", true}"
    /// `flags` - See [py_ui_flags]
    pub fn set_python_gui<OptionalMenu>(&mut self,
        title: &str,
        width:u32,
        height: u32,
        python_parameters: &[(&str, &str)],
        flags: u32,
        menu: OptionalMenu) -> bool 
        where OptionalMenu: Into<Option<Menu>>{
            let py = utils::python3();
            if py.is_none() {
                return false;
            }

            let mut py_pa = Vec::new();

            //if let Some(python_parameters) = python_parameters {
                for (k, v) in python_parameters.iter() {
                    py_pa.push(format!("{k}={v}"));
                }
            //}

            let py_src = RESOURCES.iter().find(|r| r.0 == "pyclient.py").unwrap().1;
            let py_src = base64::decode(py_src).unwrap();
            let py_src = String::from_utf8_lossy(&py_src);

            let mut params = vec!(
                "-c".to_string(),
                format!("{py_src}"),
                format!("--gempyre-url={}", self.address()),
                format!("--gempyre-width={width}"),
                format!("--gempyre-height={height}"),
                format!("--gempyre-title={title}"),
                format!("--gempyre-extra={}", py_pa.join(";")), 
                format!("--gempyre-flags={flags}"));

            let menu = menu.into();
            if menu.is_some() {
                params.push(format!("--gempyre-menu={}", menu.unwrap().to_string()));
            }    

            let path = py.unwrap().to_str().unwrap().to_string();

            self.set_gui_command_line(&path, &params);
            true
        }

    fn default_start_cmd(&self) -> Result<(String, Vec<String>)> {
        let start_cmd = utils::html_file_launch_cmd();
        if start_cmd.is_none() {
            return GemGuiError::error("Cannot find a default application");
        }
        let mut start_cmd = start_cmd.unwrap();
        start_cmd.1.push(self.address());
        Ok(start_cmd)
    }


    /// Start event loop
    pub async fn run(&mut self) -> Result<()> {
        static DEFAULT_ERROR: &str = "Cannot fallback to default";

        let default_cmd = self.default_start_cmd();

        let cmd = match &self.start_cmd {
            Some(v) => v.clone(),
            None => default_cmd.clone().expect(DEFAULT_ERROR),
        };

        let on_start = move |_| async move {
            let success = match Self::run_process(cmd.clone()).await {
                Ok(success) => {
                    if ! success {
                        let default_cmd = default_cmd.expect(DEFAULT_ERROR);
                        if cmd.0 != default_cmd.0.clone() {
                            let default_ok = Self::run_process(default_cmd).await.unwrap_or_else(|e| panic!("{e}"));
                            eprintln!("Requested UI failed, falling back to default: {default_ok}");
                        }
                    }
                true
                },
                Err(err) => panic!("{err}"),
            };
            success
        };

        //let on_start = move |_| {Self::run_process(cmd)};


        let server_wait = self.start_server(on_start).await;
        if server_wait.is_none() {
            return GemGuiError::error("Starting server failed");
        }
        let server_wait = server_wait.unwrap();
        tokio::pin!(server_wait); // see https://tokio.rs/tokio/tutorial/select

        // wait here
        loop {
            tokio::select! {
                // wait server close
                _ =  &mut server_wait => {  
                    break;
                },
                 //  wait WS
                Some(msg) = self.subscription_receiver.recv() => {
                match serde_json::from_str::<JSMessageRx>(&msg) {
                        Ok(m) => {
                            match m._type.as_str()  {
                                "keepalive" => {
                                    // println!("keep alive");
                                },
                                "uiready" => UiData::entered(&self.ui),
                                "start_request" => self.start_handler(),
                                "close_request"  => {  // whaaat CLOSE_REQUEST cannot be used!
                                    self.exit(); // send exit to all windows - then go
                                    break; },  
                                "event" => self.event_handler(m),
                                "query" => self.query_handler(&msg),
                                "error" => self.error_handler(&msg),
                                "extension_response" => self.extension_response_handler(&msg),
                                "extensionready" => println!("Extension ready"),
                                _ => panic!("Handler not implemented for {}", m._type)
                            }
                        }
                        Err(e) => {
                            eprintln!("Invalid response {e}");
                        }
                    }
                },
                // wait timer 
                Some(timer_msg) = self.timer_receiver.recv() => {
                    self.timer_handler(timer_msg)
                },
            
            }
        }
        Ok(())
    }

    fn error_handler(&mut self, msg: &str) {
          match &mut self.on_error_cb {
            Some(f) => f(UiRef::new(self.ui.clone()), msg.to_string()),
            None => (),
        }
        eprintln!("Ui Error {msg:#?}")
    }

    fn timer_handler(&self, timer_id: u32) {
        let handler = self.get_timer_callback(&timer_id);
        if handler.is_none() {
            eprintln!("Handler not found for {timer_id}");
            return;
        }
        let rc = handler.unwrap();
        let mut fun = rc.lock().unwrap();
        fun(UiRef::new(self.ui.clone()), timer_id); 
    }

    fn event_handler(&self, msg: JSMessageRx) {
        let event_name = &msg.event.unwrap();
        let element = msg.element.unwrap();
        let handler = self.get_subscribe_callback(&element, event_name);
        if handler.is_none() {
            eprintln!("Handler not found at {} for {}", &element, event_name);
            return;
        } 
        let mut prop = Properties::new();
        for (k, v) in msg.properties.unwrap().iter() {
            let key = k.clone();
            if v.is_string() {
                prop.insert(key, v.as_str().unwrap().to_string());
            } else {
                prop.insert(key, v.to_string());
            }        
        }
        let rc = handler.unwrap();
        let mut fun = rc.lock().unwrap();
        fun(UiRef::new(self.ui.clone()), Event::new(self.ui.clone(), element, prop));
    }

    fn query_handler(&mut self, raw: &str) {
        let mut js: serde_json::Value = serde_json::from_str(raw).unwrap();
        let query_value = String::from(js["query_value"].as_str().unwrap()); // otherwise we cannot take later as mutable
        let query_id = js["query_id"].as_str().unwrap();
        let tx = UiData::get_query_sender(&mut self.ui, query_id);
        match tx {
            Some(r) => {
                let value = js[query_value].take();
                r.send(value).unwrap_or_else(|e| {panic!("Cannot send query: {e}")});
            },
            None =>  {
                eprintln!("No query listener for {query_id}");
            }
        };
    }

    fn extension_response_handler(&mut self, raw: &str) {
        let mut js: serde_json::Value = serde_json::from_str(raw).unwrap();
        let extension_call = String::from(js["extension_call"].as_str().unwrap()); // otherwise we cannot take later as mutable
        let extension_id = js["extension_id"].as_str().unwrap();
        let tx = UiData::get_query_sender(&mut self.ui, extension_id);
        match tx {
            Some(r) => {
                let value = js[extension_call].take();
                r.send(value).unwrap_or_else(|e| {panic!("Cannot send extension: {e}")});
            },
            None =>  {
                eprintln!("No extension listener for {extension_id}");
            }
        };
       
    }


    fn start_handler(&mut self) {
        if ! UiData::is_started(&self.ui) {
            UiData::set_started(&self.ui);
            match &mut self.on_start_cb {
                Some(cb) => cb(UiRef::new(self.ui.clone())),
                None => (),
            };
            self.on_start_notifee.send(State::Running).unwrap_or_else(|_| panic!("Cannot set ready"));
        }
    }

    fn get_subscribe_callback(&self, id: &str, event_name: &str) -> Option<Arc<Mutex<SubscribeCallback>>> {
        let ui = self.ui.lock().unwrap();
        let map = ui.elements.get(id)?;
        let value = map.get(event_name)?;
        Some(value.clone())
    }

    fn get_timer_callback(&self, id: &TimerId) -> Option<Arc<Mutex<TimerCallback>>> {
        let ui = self.ui.lock().unwrap();
        let val = ui.timers.get(id)?;
        Some(val.0.clone())
    }

    async fn start_server<F, Fut>(&self, on_start: F) -> Option<tokio::task::JoinHandle<()>>
    where F: FnOnce(u16) -> Fut + Send + 'static,
        Fut: Future<Output = bool> + Send + 'static {
        self.server.run(on_start).await
    }

    

    /// Set callback called when UI is ready
    /// 
    /// # Arguments
    /// 
    /// `callback` - Callback function to be called when UI is ready and data can be accessed.
    /// 
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    pub fn on_start<CB>(&mut self, callback: CB)
    where CB: FnMut(UiRef) + Send + Clone + 'static {
        self.on_start_cb = Some(Box::new(callback));
    }

    /// Set callback called when UI is ready
    /// 
    /// See [on_start](Self::on_start)
    pub fn on_start_async<CB, Fut>(&mut self, callback: CB) 
    where CB: FnOnce(UiRef)-> Fut + Send + Clone + 'static,
        Fut: Future<Output = ()>  + Send + 'static {
        let cb = UiData::as_sync_monad(callback);
        self.on_start_cb = Some(Box::new(cb));
    }

    /// Set callback called when UI is reloaded
    /// 
    /// # Arguments
    /// 
    /// `callback` - Callback function to handle UI reload.
    ///    
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI 
    pub fn on_reload<CB>(&mut self, callback: CB)
    where CB: FnMut(UiRef) + Send + 'static {
        self.on_reload_cb = Some(Box::new(callback));
    }

    /// Set callback called when UI is reloaded
    /// 
    /// See [on_reload](Self::on_reload)
    pub fn on_reload_async<CB, Fut>(&mut self, callback: CB) 
    where CB: FnOnce(UiRef)-> Fut + Send + Clone + 'static,
        Fut: Future<Output = ()>  + Send + 'static {
        let cb = UiData::as_sync_monad(callback);
        self.on_reload_cb = Some(Box::new(cb));
    }

    /// Set callback called on UI error
    /// 
    /// Default [default_error] print error and do exit.
    /// 
    /// # Arguments
    /// 
    /// `callback` - Callback function to handle UI error.
    ///   
    /// # Callback
    /// 
    /// `UiRef`- Reference to UI
    /// 
    /// `String` - error string
    /// 
    pub fn on_error<CB>(&mut self, callback: CB)
    where CB: FnMut(UiRef, String) + Send + 'static {
        self.on_error_cb = Some(Box::new(callback));
    }

    /// Set callback called on UI error
    /// 
    /// See [on_error](Self::on_error)
    pub fn on_error_async<CB, Fut>(&mut self, callback: CB) 
    where CB: FnOnce(UiRef, String)-> Fut + Send + Clone + 'static,
    Fut: Future<Output = ()>  + Send + 'static {
        self.on_error(UiData::as_sync_fn(callback))
    }

    
    


}