mogwai 0.7.1

The minimal, obvious, graphical, widget application interface.
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
//! # `web-sys` view implementation
//!
//! This module provides an implementation of [`View`] for [`web-sys`] types,
//! allowing for the creation and manipulation of DOM nodes in a browser environment.
//!
//! ## Key Components
//!
//! - **ViewChild**: Implements the [`ViewChild`] trait for `web-sys` types, enabling them to be
//!   appended to views.
//!
//! - **ViewParent**: Implements the [`ViewParent`] trait for `web-sys` types, providing methods
//!   for managing child nodes within a view.
//!
//! - **ViewProperties**: Implements the [`ViewProperties`] trait for various web elements, allowing
//!   for the manipulation of attributes and styles.
//!
//! - **ViewEventListener**: Implements the [`ViewEventListener`] trait for [`EventListener`], enabling
//!   asynchronous event handling.
//!
//! - **Extension traits**: [`WebElement`] and [`WebEvent`] make it easy to specialize on web views.
use std::{cell::RefCell, ops::Deref, rc::Rc, task::Waker};

use event::EventListener;
use wasm_bindgen::{JsValue, UnwrapThrowExt, prelude::Closure};
use web_sys::wasm_bindgen::JsCast;

use crate::{prelude::*, sync::Global};
pub mod event;

pub mod prelude {
    //! Re-export of the common prelude with browser specific extras.
    pub use super::{Web, WebElement, WebEvent, event::*};
    pub use crate::prelude::*;
    pub extern crate wasm_bindgen;
    pub extern crate wasm_bindgen_futures;
    pub extern crate web_sys;
}

impl<T: AsRef<web_sys::EventTarget>> ViewEventTarget<Web> for T {
    fn listen(
        &self,
        event_name: impl Into<std::borrow::Cow<'static, str>>,
    ) -> <Web as View>::EventListener {
        EventListener::new(self, event_name)
    }
}

macro_rules! node_impl {
    ($ty:ident) => {
        impl ViewChild<Web> for web_sys::$ty {
            fn as_append_arg(
                &self,
            ) -> AppendArg<Web, impl Iterator<Item = std::borrow::Cow<'_, web_sys::Node>>> {
                AppendArg::new(std::iter::once(std::borrow::Cow::Borrowed(self.as_ref())))
            }
        }

        impl ViewParent<Web> for web_sys::$ty {
            fn append_node(&self, node: std::borrow::Cow<'_, <Web as View>::Node>) {
                web_sys::Node::append_child(self, node.as_ref()).unwrap_throw();
            }

            fn remove_node(&self, node: std::borrow::Cow<'_, <Web as View>::Node>) {
                web_sys::Node::remove_child(self, node.as_ref()).unwrap_throw();
            }

            fn replace_node(
                &self,
                new_node: std::borrow::Cow<'_, <Web as View>::Node>,
                old_node: std::borrow::Cow<'_, <Web as View>::Node>,
            ) {
                web_sys::Node::replace_child(self, new_node.as_ref(), old_node.as_ref())
                    .unwrap_throw();
            }

            fn insert_node_before(
                &self,
                new_node: std::borrow::Cow<'_, <Web as View>::Node>,
                before_node: Option<std::borrow::Cow<'_, <Web as View>::Node>>,
            ) {
                web_sys::Node::insert_before(self, new_node.as_ref(), before_node.as_deref())
                    .unwrap_throw();
            }
        }
    };
}

node_impl!(Text);
node_impl!(Node);
node_impl!(Element);

impl ViewProperties for web_sys::Element {
    fn set_property(&self, key: impl AsRef<str>, value: impl AsRef<str>) {
        let _ = self.set_attribute(key.as_ref(), value.as_ref());
    }

    fn has_property(&self, key: impl AsRef<str>) -> bool {
        self.has_attribute(key.as_ref())
    }

    fn get_property(&self, key: impl AsRef<str>) -> Option<Str> {
        self.get_attribute(key.as_ref()).map(|s| s.into())
    }

    fn remove_property(&self, key: impl AsRef<str>) {
        let _ = self.remove_attribute(key.as_ref());
    }

    fn set_style(&self, key: impl AsRef<str>, value: impl AsRef<str>) {
        if let Some(el) = self.dyn_ref::<web_sys::HtmlElement>() {
            let style = el.style();
            let _ = style.set_property(key.as_ref(), value.as_ref());
        }
    }

    fn remove_style(&self, key: impl AsRef<str>) {
        if let Some(el) = self.dyn_ref::<web_sys::HtmlElement>() {
            let style = el.style();
            let _ = style.remove_property(key.as_ref());
        }
    }
}

impl ViewText for web_sys::Text {
    fn new(text: impl AsRef<str>) -> Self {
        web_sys::Text::new_with_data(text.as_ref()).unwrap()
    }

    fn set_text(&self, text: impl AsRef<str>) {
        self.set_data(text.as_ref());
    }

    fn get_text(&self) -> Str {
        self.data().into()
    }
}

impl ViewEventListener<Web> for EventListener {
    fn next(&self) -> impl Future<Output = web_sys::Event> {
        self.next()
    }

    fn on_window(event_name: impl Into<Str>) -> EventListener {
        EventListener::new(window(), event_name)
    }

    fn on_document(event_name: impl Into<Str>) -> EventListener {
        EventListener::new(document(), event_name)
    }
}

impl ViewElement for web_sys::Element {
    type View = Web;

    fn new(name: impl AsRef<str>) -> Self {
        DOCUMENT
            .create_element(name.as_ref())
            .unwrap_throw()
            .dyn_into()
            .unwrap()
    }

    fn new_namespace(name: impl AsRef<str>, ns: impl AsRef<str>) -> Self {
        DOCUMENT
            .create_element_ns(Some(ns.as_ref()), name.as_ref())
            .unwrap_throw()
            .dyn_into()
            .unwrap()
    }
}

impl ViewEvent for web_sys::Event {
    type View = Web;
}

#[derive(Clone, Copy)]
pub struct Web;

impl View for Web {
    type Element = web_sys::Element;
    type Text = web_sys::Text;
    type Node = web_sys::Node;
    type EventListener = EventListener;
    type Event = web_sys::Event;
}

static WINDOW: Global<web_sys::Window> = Global::new(|| web_sys::window().unwrap_throw());
static DOCUMENT: Global<web_sys::Document> = Global::new(|| WINDOW.document().unwrap_throw());

/// Return the DOM [`web_sys::Window`].
/// #### Panics
/// Panics when the window cannot be returned.
pub fn window() -> &'static web_sys::Window {
    WINDOW.deref()
}

/// Returns the global document object [`web_sys::Document`]
///
/// #### Panics
/// Panics on non-wasm32 or when the document cannot be returned.
pub fn document() -> &'static web_sys::Document {
    DOCUMENT.deref()
}

/// Return the global document's body object.
///
/// ## Panics
/// Panics on wasm32 if the body cannot be returned.
pub fn body() -> web_sys::HtmlElement {
    DOCUMENT
        .body()
        .expect_throw("document does not have a body")
}

fn req_animation_frame(f: &Closure<dyn FnMut(JsValue)>) {
    WINDOW
        .request_animation_frame(f.as_ref().unchecked_ref())
        .expect_throw("should register `requestAnimationFrame` OK");
}

/// Sets a static rust closure to be called with `window.requestAnimationFrame`.
///
/// The static rust closure takes one parameter which is
/// a timestamp representing the number of milliseconds since the application's
/// load. See <https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp>
/// for more info.
pub fn request_animation_frame(mut f: impl FnMut(JsValue) + 'static) {
    let wrapper = Rc::new(RefCell::new(None));
    let callback = Box::new({
        let wrapper = wrapper.clone();
        move |jsval| {
            f(jsval);
            wrapper.borrow_mut().take();
        }
    }) as Box<dyn FnMut(JsValue)>;
    let closure: Closure<dyn FnMut(JsValue)> = Closure::wrap(callback);
    *wrapper.borrow_mut() = Some(closure);
    req_animation_frame(wrapper.borrow().as_ref().unwrap_throw());
}

#[derive(Clone, Default)]
#[expect(clippy::type_complexity, reason = "not too complex")]
struct NextFrame {
    closure: Rc<RefCell<Option<Closure<dyn FnMut(JsValue)>>>>,
    ts: Rc<RefCell<Option<f64>>>,
    waker: Rc<RefCell<Option<Waker>>>,
}

/// Creates a future that will resolve on the next animation frame.
///
/// The future's output is a timestamp representing the number of
/// milliseconds since the application's load.
/// See <https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp>
/// for more info.
pub fn next_animation_frame() -> impl Future<Output = f64> {
    // https://rustwasm.github.io/wasm-bindgen/examples/request-animation-frame.html#srclibrs
    let frame = NextFrame::default();

    *frame.closure.borrow_mut() = Some(Closure::wrap(Box::new({
        let frame = frame.clone();
        move |ts_val: JsValue| {
            *frame.ts.borrow_mut() = Some(ts_val.as_f64().unwrap_or(0.0));
            if let Some(waker) = frame.waker.borrow_mut().take() {
                waker.wake();
            }
        }
    }) as Box<dyn FnMut(JsValue)>));

    req_animation_frame(frame.closure.borrow().as_ref().unwrap_throw());

    frame
}

impl Future for NextFrame {
    type Output = f64;

    fn poll(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Self::Output> {
        if let Some(ts) = self.ts.borrow_mut().take() {
            std::task::Poll::Ready(ts)
        } else {
            *self.waker.borrow_mut() = Some(cx.waker().clone());
            std::task::Poll::Pending
        }
    }
}

/// Marker trait for specializing generic view elements to [`web_sys::Element`] and friends.
pub trait WebElement: ViewElement {
    /// Attempt to cast the element.
    ///
    /// If successful, run the given function on the result of the cast.
    fn dyn_el<T: JsCast, X>(&self, f: impl FnOnce(&T) -> X) -> Option<X> {
        let opt_x = self.when_element::<Web, _>(|el: &web_sys::Element| -> Option<X> {
            let el = el.dyn_ref::<T>()?;
            let x = f(el);
            Some(x)
        });
        opt_x.flatten()
    }
}

impl<T: ViewElement> WebElement for T {}

/// Marker trait for specializing generic view events to [`web_sys::Event`] and friends.
pub trait WebEvent: ViewEvent {
    /// Attempt to cast the element.
    ///
    /// If successful, run the given function on the result of the cast.
    fn dyn_ev<T: JsCast, X>(&self, f: impl FnOnce(&T) -> X) -> Option<X> {
        let opt_x = self.when_event::<Web, _>(|el: &web_sys::Event| -> Option<X> {
            let el = el.dyn_ref::<T>()?;
            let x = f(el);
            Some(x)
        });
        opt_x.flatten()
    }
}

impl<T: ViewEvent> WebEvent for T {}

#[cfg(test)]
mod test {
    use crate::{self as mogwai, proxy::Proxy, ssr::Ssr};
    use mogwai::web::prelude::*;

    #[test]
    /// ```compile_fail
    /// fn rsx_proxy_on_outermost_block_is_compiler_error() {
    ///     fn view<V: View>() {
    ///         let proxy = Proxy::<Web, ()>::default();
    ///         rsx! {
    ///             {proxy(() => "Erroring text node.".into_text::<Web>())}
    ///         }
    ///     }
    /// }
    /// ```
    fn rsx_doc_proxy_on_outermost_block_is_compiler_error() {}

    #[test]
    #[allow(dead_code)]
    fn rsx_unique_names() {
        struct MyView<V: View> {
            wrapper: V::Element,
        }

        fn view<V: View>() -> MyView<V> {
            rsx! {
                let wrapper = main() {
                    div() {
                        "Text one."
                        "Text two."
                        "Text three."
                        p() {
                            "Inside p one."
                        }
                        p() {
                            "Inside p two."
                        }
                    }
                }
            }
            MyView { wrapper }
        }
    }

    #[test]
    #[allow(dead_code)]
    fn rsx_block_nesting() {
        struct MyView<V: View> {
            wrapper: V::Element,
            child: MyChild<V>,
            text: V::Text,
            proxy: Proxy<Str>,
        }

        #[derive(ViewChild)]
        struct MyChild<V: View> {
            #[child]
            wrapper: V::Element,
        }

        fn view<V: View>() -> MyView<V> {
            rsx! {
                let wrapper = p() {
                    "Here lies davey jones."
                }
            }

            let child = MyChild { wrapper };

            let mut proxy = Proxy::<Str>::default();

            rsx! {
                let wrapper = div(id = "wrapper") {
                    // You can nest view structs
                    let child = {child}

                    "Constant text can live inline."

                    // You can use Rust expressions inside a block...
                    let text = {
                        "But Rust expressions in a block must evaluate to some kind of node.".into_text::<V>()
                    }

                    ul() {
                        li() {
                            "And you can use `Proxy` to insert a variable number of nodes..."
                        }
                        { proxy(s => s.into_text::<V>()) }
                        li() {
                            "That updates every time a new value is set on the `Proxy`"
                        }
                    }
                }
            }

            MyView {
                wrapper,
                child,
                text,
                proxy,
            }
        }
    }

    #[test]
    fn view_cast() {
        struct MyView<V: View> {
            wrapper: V::Element,
        }

        fn create_view<V: View>() -> MyView<V> {
            rsx! {
                let wrapper = div() {
                    a() {
                        "Hello"
                    }
                }
            }

            MyView { wrapper }
        }

        let view = create_view::<Ssr>();
        let cast = view.wrapper.when_element::<Web, _>(|el| Some(el.clone()));
        assert!(cast.is_none());
        let cast = view.wrapper.when_element::<Ssr, _>(|el| Some(el.clone()));
        assert!(cast.is_some());
    }

    #[test]
    #[allow(dead_code)]
    fn rsx_proxy_attribute() {
        fn view<V: View>() {
            let mut proxy = Proxy::<String>::default();

            rsx! {
                let wrapper = div() {
                    fieldset(
                        id = "blah",
                        style:display = proxy(s => s)
                    ) {}
                }
            }
        }
    }
}