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
//! This module contains Yew's implementation of a service which can be used to
//! request frame rendering

use crate::callback::Callback;
use crate::services::Task;
use cfg_if::cfg_if;
use cfg_match::cfg_match;
use std::fmt;
cfg_if! {
    if #[cfg(feature = "std_web")] {
        #[allow(unused_imports)]
        use stdweb::{_js_impl, js};
        use stdweb::unstable::TryInto;
        use stdweb::Value;
    } else if #[cfg(feature = "web_sys")] {
        use crate::utils;
        use wasm_bindgen::closure::Closure;
        use wasm_bindgen::{JsCast, JsValue};
    }
}

/// A handle to cancel a render task.
#[must_use]
pub struct RenderTask(
    #[cfg(feature = "std_web")] Value,
    #[cfg(feature = "web_sys")] RenderTaskInner,
);

#[cfg(feature = "web_sys")]
struct RenderTaskInner {
    render_id: i32,
    #[allow(dead_code)]
    callback: Closure<dyn Fn(JsValue)>,
}

impl fmt::Debug for RenderTask {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("RenderTask")
    }
}

/// A service to request animation frames.
#[derive(Default, Debug)]
pub struct RenderService {}

impl RenderService {
    /// Request animation frame. Callback will be notified when frame should be rendered.
    pub fn request_animation_frame(callback: Callback<f64>) -> RenderTask {
        let callback = move |#[cfg(feature = "std_web")] v,
                             #[cfg(feature = "web_sys")] v: JsValue| {
            let time: f64 = cfg_match! {
                feature = "std_web" => ({
                    match v {
                        Value::Number(n) => n.try_into().unwrap(),
                        _ => 0.0,
                    }
                }),
                feature = "web_sys" => v.as_f64().unwrap_or(0.),
            };
            callback.emit(time);
        };
        let handle = cfg_match! {
            feature = "std_web" => js! {
                var callback = @{callback};
                var action = function(time) {
                    callback(time);
                    callback.drop();
                };
                return {
                    render_id: requestAnimationFrame(action),
                    callback: callback,
                };
            },
            feature = "web_sys" => ({
                let callback = Closure::wrap(Box::new(callback) as Box<dyn Fn(JsValue)>);
                let render_id = utils::window().request_animation_frame(callback.as_ref().unchecked_ref()).unwrap();
                RenderTaskInner {
                    render_id,
                    callback,
                }
            }),
        };
        RenderTask(handle)
    }
}

impl Task for RenderTask {
    fn is_active(&self) -> bool {
        true
    }
}

impl Drop for RenderTask {
    fn drop(&mut self) {
        if self.is_active() {
            cfg_match! {
                feature = "std_web" => ({
                    let handle = &self.0;
                    js! { @(no_return)
                        var handle = @{handle};
                        cancelAnimationFrame(handle.render_id);
                        handle.callback.drop();
                    }
                }),
                feature = "web_sys" => utils::window().cancel_animation_frame(self.0.render_id).unwrap(),
            }
        }
    }
}