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
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
//! A [`Frontend`](gooey_core::Frontend) for `Gooey` that targets web browsers
//! by creating DOM elements using `web-sys` and `wasm-bindgen`.
//!
//! ## [`Window`] implementation
//!
//! The [`Window`] implementation in this frontend is limited by the browser's APIs. Of
//! note:
//!
//! - [`maximized()`](Window::maximized) and [`set_maximized()`](Window::set_maximized) interact with the fullscreen APIs.
//! - [`inner_position()`](Window::inner_position) and [`outer_position()`](Window::outer_position) are equivalent. This also means
//!   that [`set_outer_position()`](Window::set_outer_position) actually sets the inner position.
//! - Controlling the browser window (closing, resizing, moving) is dependent
//!   upon the browser allowing the operation. This generally is only possible
//!   if the window is opened via a javascript API and has no other tabs within
//!   in.

#![forbid(unsafe_code)]
#![warn(
    clippy::cargo,
    // TODO missing_docs,
    clippy::pedantic,
    future_incompatible,
    rust_2018_idioms
)]
#![allow(
    clippy::if_not_else,
    clippy::module_name_repetitions,
    clippy::needless_pass_by_value,
    clippy::multiple_crate_versions, // this is a mess due to winit dependencies and wgpu dependencies not lining up
    clippy::missing_errors_doc, // TODO clippy::missing_errors_doc
    clippy::missing_panics_doc, // TODO clippy::missing_panics_doc
)]
#![cfg_attr(doc, warn(rustdoc::all))]

use std::{
    any::TypeId,
    collections::HashMap,
    convert::TryFrom,
    ops::Deref,
    sync::{
        atomic::{AtomicBool, AtomicU64, Ordering},
        Arc,
    },
    time::Duration,
};

use gooey_core::{
    assets::{self, Configuration, FrontendImage, Image},
    figures::{Point, Size},
    styles::{
        border::Border,
        style_sheet::{Classes, State},
        Alignment, Autofocus, FontFamily, FontSize, Intent, Padding, Style, StyleComponent,
        SystemTheme, TabIndex, VerticalAlignment,
    },
    AnyTransmogrifier, AnyTransmogrifierContext, AnyWidget, AnyWindowBuilder, Callback, Frontend,
    Gooey, NativeTimer, Timer, Transmogrifier, TransmogrifierContext, TransmogrifierState,
    WeakWidgetRegistration, Widget, WidgetId, WidgetRef, WidgetRegistration, Window,
    WindowConfiguration,
};
use parking_lot::Mutex;
use wasm_bindgen::{prelude::*, JsCast};

pub mod utils;

use utils::{
    create_element, set_widget_classes, set_widget_id, widget_css_id, window_element_by_widget_id,
    CssBlockBuilder, CssManager, CssRules,
};
use web_sys::{
    window, ErrorEvent, HtmlElement, HtmlImageElement, KeyboardEvent, MediaQueryListEvent,
};

use crate::utils::window_document;

static INITIALIZED: AtomicBool = AtomicBool::new(false);

/// `WebSys` is a [`Frontend`] implementor that implements Gooey interfaces
/// through `web-sys` in a web browser.
#[derive(Debug, Clone)]
pub struct WebSys {
    pub ui: Gooey<Self>,
    data: Arc<Data>,
}

#[derive(Debug)]
struct Data {
    styles: Vec<CssRules>,
    theme: Mutex<SystemTheme>,
    intent_callbacks: Mutex<HashMap<Intent, Vec<CallbackWidget>>>,
    configuration: Configuration,
    last_image_id: AtomicU64,
}

#[derive(Debug)]
struct CallbackWidget {
    widget: WeakWidgetRegistration,
    callback: Callback,
}

impl WebSys {
    pub fn initialize() {
        if INITIALIZED
            .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
            .is_ok()
        {
            wasm_logger::init(wasm_logger::Config::default());
        }
    }

    #[must_use]
    pub fn new(ui: Gooey<Self>, configuration: Configuration) -> Self {
        Self::initialize();

        let manager = CssManager::shared();
        let mut styles = vec![
            manager.register_rule(
                &CssBlockBuilder::for_id(ui.root_widget().id().id)
                    .with_css_statement("width: 100%")
                    .with_css_statement("height: 100%")
                    .with_css_statement("display: flex")
                    .to_string(),
            ),
            manager.register_rule(
                &CssBlockBuilder::for_css_selector("#gooey")
                    .with_css_statement("margin: 0")
                    .with_css_statement("padding: 0")
                    .to_string(),
            ),
            manager.register_rule(
                &CssBlockBuilder::for_css_selector(".sr-only")
                    .with_css_statement("position: absolute")
                    .with_css_statement("height: 1px")
                    .with_css_statement("width: 1px")
                    .with_css_statement("clip: rect(0 0 0 0)")
                    .with_css_statement("clip-path: inset(100%)")
                    .with_css_statement("overflow: hidden")
                    .with_css_statement("white-space: nowrap")
                    .to_string(),
            ),
        ];

        for rule in ui
            .stylesheet()
            .rules
            .iter()
            .filter(|rule| rule.widget_type_id.is_some())
        {
            if let Some(transmogrifier) =
                ui.transmogrifier_for_type_id(rule.widget_type_id.unwrap())
            {
                for theme in [SystemTheme::Light, SystemTheme::Dark] {
                    let css = transmogrifier.convert_style_to_css(
                        &rule.style,
                        CssBlockBuilder::for_classes_and_rule(
                            &transmogrifier.widget_classes(),
                            rule,
                        )
                        .with_theme(theme),
                    );
                    styles.push(manager.register_rule(&css.to_string()));
                }
            }
        }

        Self {
            ui,
            data: Arc::new(Data {
                styles,
                configuration,
                theme: Mutex::default(),
                intent_callbacks: Mutex::default(),
                last_image_id: AtomicU64::new(0),
            }),
        }
    }

    pub fn install_in_id(&mut self, id: &str, _window_config: WindowConfiguration) {
        // TODO handle window_config
        std::panic::set_hook(Box::new(console_error_panic_hook::hook));
        let window = web_sys::window().unwrap();
        let document = window.document().unwrap();
        let parent = document.get_element_by_id(id).expect("id not found");

        let root_id = id.to_owned();
        // Initialize with light theme
        update_system_theme(&root_id, false, &self.data.theme);

        // TODO This shouldn't leak, it should be stored somewhere, but since
        // wasm-bindgen isn't Send+Sync, we can't store it in `self`. Also,
        // todo: I don't know that the callback is being invoked. My testing has
        // been done limited so far.
        let data = self.data.clone();
        std::mem::forget(
            window
                .match_media("(prefers-color-scheme: dark)")
                .ok()
                .flatten()
                .and_then(move |mql| {
                    update_system_theme(&root_id, mql.matches(), &data.theme);
                    mql.add_listener_with_opt_callback(Some(
                        Closure::wrap(Box::new(move |event: MediaQueryListEvent| {
                            update_system_theme(&root_id, event.matches(), &data.theme);
                        }) as Box<dyn FnMut(_)>)
                        .as_ref()
                        .unchecked_ref(),
                    ))
                    .map(|_| mql)
                    .ok()
                }),
        );

        // Install a keyboard handler in the window for handling enter/escape keypresses
        let data = self.data.clone();
        document.set_onkeydown(
            Closure::wrap(Box::new(move |event: KeyboardEvent| {
                let intent = match event.key().as_str() {
                    "Enter" | "Return" => Intent::Default,
                    "Escape" => Intent::Cancel,
                    _ => return,
                };
                let mut intent_callbacks = data.intent_callbacks.lock();
                if let Some(potential_handlers) = intent_callbacks.get_mut(&intent) {
                    // Widgets report themselves as handlers when they initially
                    // transmogrify. The order of this execution is such that
                    // the earliest widgets should be closer to the root. This
                    // isn't perfect at all, but putting multiple submit buttons
                    // on the same layer should be discouraged.
                    while !potential_handlers.is_empty() {
                        let handler = potential_handlers.last().unwrap();
                        // Check that the widget is still alive.
                        if handler.widget.upgrade().is_some() {
                            handler.callback.invoke(());
                            event.prevent_default();
                            break;
                        }

                        // If the widget is dead, pop it off the queue.
                        potential_handlers.pop();
                    }
                }
            }) as Box<dyn FnMut(KeyboardEvent)>)
            .into_js_value()
            .dyn_ref(),
        );

        self.with_transmogrifier(self.ui.root_widget().id(), |transmogrifier, mut context| {
            if let Some(root_element) = transmogrifier.transmogrify(&mut context) {
                parent.append_child(&root_element).unwrap();
            }
        });
    }

    /// Executes `callback` with the transmogrifier and transmogrifier state as
    /// parameters.
    #[allow(clippy::missing_panics_doc)] // unwrap is guranteed due to get_or_initialize
    pub fn with_transmogrifier<
        TResult,
        C: FnOnce(&'_ dyn AnyWebSysTransmogrifier, AnyTransmogrifierContext<'_, Self>) -> TResult,
    >(
        &self,
        widget_id: &WidgetId,
        callback: C,
    ) -> Option<TResult> {
        self.ui
            .with_transmogrifier(widget_id, self, |transmogrifier, context| {
                callback(transmogrifier.as_ref(), context)
            })
    }

    pub fn register_intent_handler(
        &self,
        intent: Intent,
        widget: &WidgetRegistration,
        callback: Callback,
    ) {
        let mut intent_callbacks = self.data.intent_callbacks.lock();
        let callbacks = intent_callbacks.entry(intent).or_default();
        callbacks.push(CallbackWidget {
            widget: WeakWidgetRegistration::from(widget),
            callback,
        });
    }
}

fn update_system_theme(root_id: &str, dark: bool, theme: &Mutex<SystemTheme>) {
    let (system_theme, active_theme, inactive_theme) = if dark {
        (SystemTheme::Dark, "gooey-dark", "gooey-light")
    } else {
        (SystemTheme::Light, "gooey-light", "gooey-dark")
    };

    let mut theme = theme.lock();
    *theme = system_theme;

    let window = web_sys::window().unwrap();
    let document = window.document().unwrap();
    let parent = document.get_element_by_id(root_id).expect("id not found");
    let class_list = parent.class_list();
    drop(class_list.add_1(active_theme));
    drop(class_list.remove_1(inactive_theme));
}

#[derive(Debug)]
pub struct RegisteredTransmogrifier(pub Box<dyn AnyWebSysTransmogrifier>);

impl AnyWebSysTransmogrifier for RegisteredTransmogrifier {
    fn transmogrify(
        &self,
        context: &mut AnyTransmogrifierContext<'_, WebSys>,
    ) -> Option<web_sys::HtmlElement> {
        self.0.transmogrify(context)
    }

    fn convert_style_to_css(&self, style: &Style, css: CssBlockBuilder) -> CssBlockBuilder {
        self.0.convert_style_to_css(style, css)
    }

    fn widget_classes(&self) -> Classes {
        self.0.widget_classes()
    }
}

impl Deref for RegisteredTransmogrifier {
    type Target = Box<dyn AnyWebSysTransmogrifier>;

    fn deref(&self) -> &'_ Self::Target {
        &self.0
    }
}

impl gooey_core::Frontend for WebSys {
    type AnyTransmogrifier = RegisteredTransmogrifier;
    type Context = Self;

    fn gooey(&self) -> &'_ Gooey<Self> {
        &self.ui
    }

    fn set_widget_has_messages(&self, widget: WidgetId) {
        self.gooey().set_widget_has_messages(widget);
        // If we're not inside of a render
        if !self.gooey().is_managed_code() {
            self.gooey().process_widget_messages(self);
        }
    }

    fn theme(&self) -> SystemTheme {
        let theme = self.data.theme.lock();
        *theme
    }

    fn load_image(&self, image: &Image, completed: Callback<Image>, error: Callback<String>) {
        if let Some(url) = Frontend::asset_url(self, &image.asset) {
            let element = create_element::<HtmlImageElement>("img");
            let image_id = self.data.last_image_id.fetch_add(1, Ordering::SeqCst);
            image.set_data(LoadedImageId(image_id));
            element.set_id(&image.css_id().expect("type error"));
            element.set_onerror(Some(
                &Closure::once_into_js(move |e: ErrorEvent| {
                    error.invoke(e.message());
                })
                .unchecked_into(),
            ));
            let completed_image = image.clone();
            element.set_onload(Some(
                &Closure::once_into_js(move || {
                    completed.invoke(completed_image);
                })
                .unchecked_into(),
            ));
            element.set_src(&url.to_string());

            // Store it in the <head>
            window_document()
                .head()
                .unwrap()
                .append_child(&element)
                .unwrap();
        }
    }

    fn asset_configuration(&self) -> &assets::Configuration {
        &self.data.configuration
    }

    fn schedule_timer(
        &self,
        callback: Callback,
        period: std::time::Duration,
        repeating: bool,
    ) -> Timer {
        let browser = BrowserTimer {
            timeout_id: 0,
            callback,
            period,
            repeating,
        };
        let timer = Timer::from_native(browser);
        schedule_timer(&timer, period);
        timer
    }

    fn window(&self) -> Option<&dyn gooey_core::Window> {
        Some(self)
    }

    fn open(&self, _window: Box<dyn AnyWindowBuilder>) -> bool {
        false
    }
}

fn schedule_timer(timer: &Timer, period: Duration) {
    let weak_timer = timer.downgrade();
    // TODO this should be refactored to reuse the same closure rather than rescheduling it.
    let timeout_id = web_sys::window()
        .unwrap()
        .set_timeout_with_callback_and_timeout_and_arguments_0(
            Closure::once_into_js(move || {
                if let Some(timer) = weak_timer.upgrade() {
                    let reschedule = {
                        let native = timer.native::<BrowserTimer>().unwrap();
                        native.callback.invoke(());

                        native.repeating
                    };

                    if reschedule {
                        schedule_timer(&timer, period);
                    }
                }
            })
            .as_ref()
            .unchecked_ref(),
            i32::try_from(period.as_millis()).unwrap_or(i32::MAX),
        )
        .unwrap();
    let mut native = timer.native::<BrowserTimer>().unwrap();
    native.timeout_id = timeout_id;
}

#[derive(Debug)]
struct BrowserTimer {
    timeout_id: i32,
    callback: Callback,
    period: Duration,
    repeating: bool,
}

impl NativeTimer for BrowserTimer {}

#[derive(Clone, Debug)]
struct LoadedImageId(u64);

impl Drop for LoadedImageId {
    fn drop(&mut self) {
        let css_id = image_css_id(self.0);
        if let Some(element) = web_sys::window()
            .and_then(|w| w.document())
            .and_then(|doc| doc.get_element_by_id(&css_id))
        {
            element.remove();
        } else {
            // This should only happen if an `Image` was passed to a separate
            // thread, which is a no-no for Gooey at the moment.
            log::error!("unable to clean up dropped image: {}", css_id);
        }
    }
}

impl FrontendImage for LoadedImageId {
    fn size(&self) -> Option<gooey_core::figures::Size<u32, gooey_core::Pixels>> {
        let css_id = image_css_id(self.0);
        web_sys::window()
            .and_then(|w| w.document())
            .and_then(|doc| doc.get_element_by_id(&css_id))
            .and_then(|img| img.dyn_into::<HtmlImageElement>().ok())
            .map(|img| Size::new(img.width() as u32, img.height() as u32))
    }
}

pub trait WebSysTransmogrifier: Transmogrifier<WebSys> {
    fn transmogrify(
        &self,
        context: TransmogrifierContext<'_, Self, WebSys>,
    ) -> Option<web_sys::HtmlElement>;

    #[must_use]
    fn initialize_widget_element(
        &self,
        element: &HtmlElement,
        context: &TransmogrifierContext<'_, Self, WebSys>,
    ) -> Option<CssRules> {
        let widget_id = context.registration.id().id;
        set_widget_id(element, widget_id);
        let mut classes = Self::widget_classes();
        if let Some(custom_classes) = context.style.get::<Classes>() {
            classes = classes.merge(custom_classes);
        }
        set_widget_classes(element, &classes);
        let mut rules = None;

        let style_sheet = context.frontend.gooey().stylesheet();
        for theme in [SystemTheme::Light, SystemTheme::Dark] {
            for state in State::permutations() {
                let mut css = CssBlockBuilder::for_id(widget_id)
                    .and_state(&state)
                    .with_theme(theme);

                let effective_style = style_sheet
                    .effective_style_for::<Self::Widget>(context.style().clone(), &state);
                css = self.convert_style_to_css(&effective_style, css);

                let css = css.to_string();
                rules = Some(rules.map_or_else(
                    || CssManager::shared().register_rule(&css),
                    |existing: CssRules| existing.and(&css),
                ));

                if let Some(extra_rules) = self.additional_css_rules(theme, &state, context) {
                    let rules = rules.as_mut().unwrap();
                    rules.extend(extra_rules);
                }
            }
        }

        if context.style.get::<Autofocus>().is_some() {
            web_sys::window()
                .unwrap()
                .set_timeout_with_callback_and_timeout_and_arguments_0(
                    Closure::once_into_js(move || {
                        if let Some(element) = window_element_by_widget_id::<HtmlElement>(widget_id)
                        {
                            drop(element.focus());
                        }
                    })
                    .as_ref()
                    .unchecked_ref(),
                    0,
                )
                .unwrap();
        }

        if let Some(index) = context.style.get::<TabIndex>() {
            // In CSS, tab index is limited to the bounds of an i16. 0 has a
            // special meaning, so we have to add 1 since TabIndex(0) is valid
            // in Gooey.
            let index = index
                .0
                .checked_add(1)
                .and_then(|index| i16::try_from(index).ok())
                .expect("tab index out of bounds");
            element.set_tab_index(i32::from(index));
        } else if <Self::Widget as Widget>::FOCUSABLE {
            element.set_tab_index(0);
        } else {
            element.set_tab_index(-1);
        }

        rules
    }

    #[allow(unused_variables)]
    fn additional_css_rules(
        &self,
        theme: SystemTheme,
        state: &State,
        context: &TransmogrifierContext<'_, Self, WebSys>,
    ) -> Option<CssRules> {
        None
    }

    fn convert_style_to_css(&self, style: &Style, css: CssBlockBuilder) -> CssBlockBuilder {
        self.convert_standard_components_to_css(style, css)
    }

    fn convert_standard_components_to_css(
        &self,
        style: &Style,
        mut css: CssBlockBuilder,
    ) -> CssBlockBuilder {
        css = self
            .convert_font_to_css(
                style,
                self.convert_alignment_to_css(style, self.convert_colors_to_css(style, css)),
            )
            .with_border(&style.get_or_default::<Border>());

        if let Some(padding) = style.get::<Padding>() {
            css = css.with_padding(padding);
        } else {
            css = css.with_css_statement("padding: 0");
        }

        css
    }

    fn convert_colors_to_css(&self, style: &Style, mut css: CssBlockBuilder) -> CssBlockBuilder {
        if let Some(color) = <Self::Widget as Widget>::text_color(style) {
            let color = color
                .themed_color(css.theme.expect("theme is required"))
                .as_css_string();
            css = css.with_css_statement(format!("color: {}", color));
        }
        if let Some(color) = <Self::Widget as Widget>::background_color(style) {
            let color = color
                .themed_color(css.theme.expect("theme is required"))
                .as_css_string();
            css = css.with_css_statement(format!("background-color: {}", color));
        }
        css
    }

    /// Converts [`Alignment`] and [`VerticalAlignment`] components to CSS
    /// rules. Also emits `display: flex` if any alignments are set.
    fn convert_alignment_to_css(&self, style: &Style, mut css: CssBlockBuilder) -> CssBlockBuilder {
        let alignment = style.get::<Alignment>();
        let vertical_alignment = style.get::<VerticalAlignment>();

        if vertical_alignment.is_some() || alignment.is_some() {
            css = css.with_css_statement("display: flex");
        }

        if let Some(alignment) = alignment {
            css = css.with_css_statement(format!(
                "justify-content: {}",
                match alignment {
                    Alignment::Left => "start",
                    Alignment::Center => "center",
                    Alignment::Right => "end",
                },
            ));
            css = css.with_css_statement(format!(
                "text-align: {}",
                match alignment {
                    Alignment::Left => "left",
                    Alignment::Center => "center",
                    Alignment::Right => "right",
                },
            ));
        }
        if let Some(vertical_alignment) = vertical_alignment {
            css = css.with_css_statement(format!(
                "align-items: {}",
                match vertical_alignment {
                    VerticalAlignment::Top => "start",
                    VerticalAlignment::Center => "center",
                    VerticalAlignment::Bottom => "end",
                },
            ));
        }
        css
    }

    fn convert_font_to_css(&self, style: &Style, mut css: CssBlockBuilder) -> CssBlockBuilder {
        if let Some(size) = style.get::<FontSize>() {
            // Accommodate for
            css = css.with_css_statement(format!("font-size: {:.03}pt;", size.get()));
        }

        if let Some(family) = style.get::<FontFamily>() {
            css = css.with_css_statement(format!("font-family: {};", family.0));
        }
        css
    }

    #[must_use]
    fn widget_classes() -> Classes {
        <<Self as Transmogrifier<WebSys>>::Widget as Widget>::classes()
    }
}

pub trait AnyWebSysTransmogrifier: AnyTransmogrifier<WebSys> + Send + Sync {
    fn transmogrify(
        &self,
        context: &mut AnyTransmogrifierContext<'_, WebSys>,
    ) -> Option<web_sys::HtmlElement>;

    #[allow(unused_variables)]
    fn convert_style_to_css(&self, style: &Style, css: CssBlockBuilder) -> CssBlockBuilder;

    fn widget_classes(&self) -> Classes;
}

impl<T> AnyWebSysTransmogrifier for T
where
    T: WebSysTransmogrifier + AnyTransmogrifier<WebSys> + Send + Sync + 'static,
{
    fn transmogrify(
        &self,
        context: &mut AnyTransmogrifierContext<'_, WebSys>,
    ) -> Option<web_sys::HtmlElement> {
        <Self as WebSysTransmogrifier>::transmogrify(
            self,
            TransmogrifierContext::try_from(context).unwrap(),
        )
    }

    fn convert_style_to_css(&self, style: &Style, css: CssBlockBuilder) -> CssBlockBuilder {
        <Self as WebSysTransmogrifier>::convert_style_to_css(self, style, css)
    }

    fn widget_classes(&self) -> Classes {
        <Self as WebSysTransmogrifier>::widget_classes()
    }
}

impl AnyTransmogrifier<WebSys> for RegisteredTransmogrifier {
    fn process_messages(&self, context: AnyTransmogrifierContext<'_, WebSys>) {
        self.0.process_messages(context);
    }

    fn widget_type_id(&self) -> TypeId {
        self.0.widget_type_id()
    }

    fn default_state_for(
        &self,
        widget: &mut dyn AnyWidget,
        registration: &WidgetRegistration,
        frontend: &WebSys,
    ) -> TransmogrifierState {
        self.0.default_state_for(widget, registration, frontend)
    }
}

#[macro_export]
macro_rules! make_browser {
    ($transmogrifier:ident) => {
        impl From<$transmogrifier> for $crate::RegisteredTransmogrifier {
            fn from(transmogrifier: $transmogrifier) -> Self {
                Self(std::boxed::Box::new(transmogrifier))
            }
        }
    };
}

pub struct WidgetClosure;

impl WidgetClosure {
    #[allow(clippy::new_ret_no_self)]
    pub fn new<F: Frontend, W: Widget, C: FnMut() -> <W as Widget>::Event + 'static>(
        widget: WidgetRef<W>,
        mut event_generator: C,
    ) -> Closure<dyn FnMut()> {
        Closure::wrap(Box::new(move || {
            let event = event_generator();
            widget.post_event::<F>(event);
        }) as Box<dyn FnMut()>)
    }
}

pub trait ImageExt {
    fn css_id(&self) -> Option<String>;
}

impl ImageExt for Image {
    fn css_id(&self) -> Option<String> {
        self.map_data(|opt_id| {
            opt_id
                .and_then(|id| id.as_any().downcast_ref::<LoadedImageId>())
                .map(|id| id.0)
        })
        .map(image_css_id)
    }
}

fn image_css_id(id: u64) -> String {
    format!("gooey-img-{}", id)
}
impl Window for WebSys {
    fn set_title(&self, title: &str) {
        let document = window_document();
        document.set_title(title);
    }

    fn inner_size(&self) -> Size<u32, gooey_core::Pixels> {
        let window = window().unwrap();
        let width = window
            .inner_width()
            .ok()
            .and_then(|v| v.as_f64())
            .unwrap_or_default();
        let height = window
            .inner_height()
            .ok()
            .and_then(|v| v.as_f64())
            .unwrap_or_default();

        Size::new(width, height).try_cast().unwrap_or_default()
    }

    fn set_inner_size(&self, new_size: Size<u32, gooey_core::Pixels>) {
        let window = window().unwrap();
        drop(window.resize_to(
            i32::try_from(new_size.width).unwrap(),
            i32::try_from(new_size.height).unwrap(),
        ));
    }

    fn set_outer_position(&self, new_position: Point<i32, gooey_core::Pixels>) {
        let window = window().unwrap();
        drop(window.move_to(new_position.x, new_position.y));
    }

    fn inner_position(&self) -> Point<i32, gooey_core::Pixels> {
        let window = window().unwrap();
        let x = window
            .screen_x()
            .ok()
            .and_then(|v| v.as_f64())
            .unwrap_or_default();
        let y = window
            .screen_y()
            .ok()
            .and_then(|v| v.as_f64())
            .unwrap_or_default();

        Point::new(x, y).try_cast().unwrap_or_default()
    }

    fn set_always_on_top(&self, _always: bool) {}

    fn maximized(&self) -> bool {
        window_document().fullscreen()
    }

    fn set_maximized(&self, maximized: bool) {
        if maximized {
            let body = window_document()
                .get_element_by_id(&widget_css_id(self.ui.root_widget().id().id))
                .unwrap();
            drop(body.request_fullscreen());
        } else {
            window_document().exit_fullscreen();
        }
    }

    fn set_minimized(&self, _minimized: bool) {}

    fn close(&self) {
        drop(window().unwrap().close());
    }
}