aplayer_wasmbind/options/
mod.rs

1use crate::create_aplayer;
2use serde_derive::{Deserialize, Serialize};
3use wasm_bindgen::{prelude::*, JsValue};
4use web_sys::Element;
5
6pub fn build_aplayer(e: &Element, o: &APlayerOptions) {
7    create_aplayer(e, &JsValue::from_serde(o).unwrap())
8}
9
10#[wasm_bindgen]
11#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
12pub struct APlayerAudio {
13    #[wasm_bindgen(skip)]
14    pub name: String,
15    #[wasm_bindgen(skip)]
16    pub artist: String,
17    #[wasm_bindgen(skip)]
18    pub url: String,
19    #[wasm_bindgen(skip)]
20    pub cover: String,
21    #[wasm_bindgen(skip)]
22    pub lrc: Option<String>,
23}
24
25#[wasm_bindgen]
26#[derive(Clone, Debug, Deserialize, Serialize)]
27pub struct APlayerOptions {
28    pub autoplay: bool,
29    pub volume: f32,
30    pub mutex: bool,
31    #[serde(rename = "listFolded")]
32    pub list_folded: bool,
33    #[serde(rename = "listMaxHeight")]
34    pub list_max_height: u32,
35
36    fixed: bool,
37    mini: bool,
38
39    #[serde(rename = "theme")]
40    theme_color: String,
41    #[serde(rename = "loop")]
42    audio_loop: String,
43    #[serde(rename = "order")]
44    audio_order: String,
45    preload: String,
46    #[serde(rename = "lrcType")]
47    lrc_type: u32,
48    audio: Vec<APlayerAudio>,
49    #[serde(rename = "storageName")]
50    storage_name: String,
51}
52
53impl Default for APlayerOptions {
54    fn default() -> Self {
55        Self {
56            autoplay: false,
57            volume: 0.7,
58            mutex: true,
59            list_folded: false,
60            list_max_height: 90,
61            fixed: false,
62            mini: false,
63            theme_color: "#b7daff".to_string(),
64            audio_loop: "all".to_string(),
65            audio_order: "list".to_string(),
66            preload: "auto".to_string(),
67            lrc_type: 3,
68            audio: vec![],
69            storage_name: "aplayer-setting".to_string(),
70        }
71    }
72}
73
74impl APlayerOptions {
75    pub fn fixed_player() -> Self {
76        Self { fixed: true, ..APlayerOptions::default() }
77    }
78    pub fn mini_player() -> Self {
79        Self { mini: true, ..APlayerOptions::default() }
80    }
81}
82
83impl APlayerOptions {
84    pub fn set_audio_list(&mut self, list: &[APlayerAudio]) -> bool {
85        let mut audios = Vec::with_capacity(list.len());
86        for a in list {
87            audios.push(APlayerAudio::from(a.clone()))
88        }
89        self.audio = audios;
90        true
91    }
92
93    pub fn set_audio_loop(&mut self, cfg: &str) -> bool {
94        match cfg {
95            "all" => self.audio_loop = String::from("all"),
96            "one" => self.audio_loop = String::from("one"),
97            "none" => self.audio_loop = String::from("none"),
98            _ => return false,
99        }
100        true
101    }
102    pub fn set_audio_order(&mut self) -> bool {
103        unimplemented!()
104    }
105    pub fn set_preload(&mut self) -> bool {
106        unimplemented!()
107    }
108}
109
110impl APlayerAudio {
111    pub fn new(name: &str, artist: &str, url: &str, cover: &str, lrc: Option<&str>) -> Self {
112        Self {
113            name: name.to_string(),
114            artist: artist.to_string(),
115            url: url.to_string(),
116            cover: cover.to_string(),
117            lrc: lrc.map(ToString::to_string),
118        }
119    }
120}