Skip to main content

yew_aplayer/aplayer/
mod.rs

1use aplayer_wasmbind::{build_aplayer, APlayerAudio, APlayerOptions};
2use yew::{prelude::*, Component, ComponentLink, Html, ShouldRender};
3
4#[derive(Properties, Clone, PartialEq)]
5pub struct APlayerProperties {
6    pub audios: Vec<APlayerAudio>,
7    #[prop_or(0.7)]
8    pub volume: f32,
9}
10
11pub struct APlayer {
12    pub props: APlayerProperties,
13}
14
15impl Component for APlayer {
16    type Message = ();
17    type Properties = APlayerProperties;
18
19    fn create(props: Self::Properties, _: ComponentLink<Self>) -> Self {
20        Self { props }
21    }
22
23    fn update(&mut self, _: Self::Message) -> ShouldRender {
24        false
25    }
26
27    fn change(&mut self, props: Self::Properties) -> ShouldRender {
28        match self.props == props {
29            true => false,
30            false => {
31                self.props = props;
32                true
33            }
34        }
35    }
36
37    fn view(&self) -> Html {
38        let t = yew::utils::document().create_element("div").unwrap();
39        build_aplayer(&t, &self.create_aplayer());
40        // Html::VRef(t.first_child().unwrap().into())
41        Html::VRef(t.into())
42    }
43}
44
45impl APlayer {
46    pub fn create_aplayer(&self) -> APlayerOptions {
47        let mut o = APlayerOptions::default();
48        o.volume = self.props.volume;
49        o.set_audio_list(&self.props.audios);
50        return o;
51    }
52}