use dom_struct::dom_struct;
use crate::dom::audiotrack::AudioTrack;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::AudioTrackListBinding::AudioTrackListMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::refcounted::Trusted;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::eventtarget::EventTarget;
use crate::dom::htmlmediaelement::HTMLMediaElement;
use crate::dom::window::Window;
use crate::script_runtime::CanGc;
use crate::task_source::TaskSource;
#[dom_struct]
pub struct AudioTrackList {
eventtarget: EventTarget,
tracks: DomRefCell<Vec<Dom<AudioTrack>>>,
media_element: Option<Dom<HTMLMediaElement>>,
}
impl AudioTrackList {
pub fn new_inherited(
tracks: &[&AudioTrack],
media_element: Option<&HTMLMediaElement>,
) -> AudioTrackList {
AudioTrackList {
eventtarget: EventTarget::new_inherited(),
tracks: DomRefCell::new(tracks.iter().map(|track| Dom::from_ref(&**track)).collect()),
media_element: media_element.map(Dom::from_ref),
}
}
pub fn new(
window: &Window,
tracks: &[&AudioTrack],
media_element: Option<&HTMLMediaElement>,
) -> DomRoot<AudioTrackList> {
reflect_dom_object(
Box::new(AudioTrackList::new_inherited(tracks, media_element)),
window,
)
}
pub fn len(&self) -> usize {
self.tracks.borrow().len()
}
pub fn find(&self, track: &AudioTrack) -> Option<usize> {
self.tracks.borrow().iter().position(|t| &**t == track)
}
pub fn item(&self, idx: usize) -> Option<DomRoot<AudioTrack>> {
self.tracks
.borrow()
.get(idx)
.map(|track| DomRoot::from_ref(&**track))
}
pub fn enabled_index(&self) -> Option<usize> {
self.tracks
.borrow()
.iter()
.position(|track| track.enabled())
}
pub fn set_enabled(&self, idx: usize, value: bool) {
let track = match self.item(idx) {
Some(t) => t,
None => return,
};
if track.enabled() == value {
return;
}
track.set_enabled(value);
if let Some(media_element) = self.media_element.as_ref() {
media_element.set_audio_track(idx, value);
}
let global = &self.global();
let this = Trusted::new(self);
let (source, canceller) = global
.as_window()
.task_manager()
.media_element_task_source_with_canceller();
let _ = source.queue_with_canceller(
task!(media_track_change: move || {
let this = this.root();
this.upcast::<EventTarget>().fire_event(atom!("change"), CanGc::note());
}),
&canceller,
);
}
pub fn add(&self, track: &AudioTrack) {
self.tracks.borrow_mut().push(Dom::from_ref(track));
track.add_track_list(self);
}
pub fn clear(&self) {
self.tracks
.borrow()
.iter()
.for_each(|t| t.remove_track_list());
self.tracks.borrow_mut().clear();
}
}
impl AudioTrackListMethods for AudioTrackList {
fn Length(&self) -> u32 {
self.len() as u32
}
fn IndexedGetter(&self, idx: u32) -> Option<DomRoot<AudioTrack>> {
self.item(idx as usize)
}
fn GetTrackById(&self, id: DOMString) -> Option<DomRoot<AudioTrack>> {
self.tracks
.borrow()
.iter()
.find(|track| track.id() == id)
.map(|track| DomRoot::from_ref(&**track))
}
event_handler!(change, GetOnchange, SetOnchange);
event_handler!(addtrack, GetOnaddtrack, SetOnaddtrack);
event_handler!(removetrack, GetOnremovetrack, SetOnremovetrack);
}