use std::cell::Cell;
use dom_struct::dom_struct;
use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::codegen::Bindings::TextTrackBinding::{
TextTrackKind, TextTrackMethods, TextTrackMode,
};
use crate::dom::bindings::error::{Error, ErrorResult};
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject};
use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::eventtarget::EventTarget;
use crate::dom::texttrackcue::TextTrackCue;
use crate::dom::texttrackcuelist::TextTrackCueList;
use crate::dom::texttracklist::TextTrackList;
use crate::dom::window::Window;
#[dom_struct]
pub struct TextTrack {
eventtarget: EventTarget,
kind: TextTrackKind,
label: String,
language: String,
id: String,
mode: Cell<TextTrackMode>,
cue_list: MutNullableDom<TextTrackCueList>,
track_list: DomRefCell<Option<Dom<TextTrackList>>>,
}
impl TextTrack {
pub fn new_inherited(
id: DOMString,
kind: TextTrackKind,
label: DOMString,
language: DOMString,
mode: TextTrackMode,
track_list: Option<&TextTrackList>,
) -> TextTrack {
TextTrack {
eventtarget: EventTarget::new_inherited(),
kind,
label: label.into(),
language: language.into(),
id: id.into(),
mode: Cell::new(mode),
cue_list: Default::default(),
track_list: DomRefCell::new(track_list.map(Dom::from_ref)),
}
}
pub fn new(
window: &Window,
id: DOMString,
kind: TextTrackKind,
label: DOMString,
language: DOMString,
mode: TextTrackMode,
track_list: Option<&TextTrackList>,
) -> DomRoot<TextTrack> {
reflect_dom_object(
Box::new(TextTrack::new_inherited(
id, kind, label, language, mode, track_list,
)),
window,
)
}
pub fn get_cues(&self) -> DomRoot<TextTrackCueList> {
self.cue_list
.or_init(|| TextTrackCueList::new(self.global().as_window(), &[]))
}
pub fn id(&self) -> &str {
&self.id
}
pub fn add_track_list(&self, track_list: &TextTrackList) {
*self.track_list.borrow_mut() = Some(Dom::from_ref(track_list));
}
pub fn remove_track_list(&self) {
*self.track_list.borrow_mut() = None;
}
}
impl TextTrackMethods<crate::DomTypeHolder> for TextTrack {
fn Kind(&self) -> TextTrackKind {
self.kind
}
fn Label(&self) -> DOMString {
DOMString::from(self.label.clone())
}
fn Language(&self) -> DOMString {
DOMString::from(self.language.clone())
}
fn Id(&self) -> DOMString {
DOMString::from(self.id.clone())
}
fn Mode(&self) -> TextTrackMode {
self.mode.get()
}
fn SetMode(&self, value: TextTrackMode) {
self.mode.set(value)
}
fn GetCues(&self) -> Option<DomRoot<TextTrackCueList>> {
match self.Mode() {
TextTrackMode::Disabled => None,
_ => Some(self.get_cues()),
}
}
fn GetActiveCues(&self) -> Option<DomRoot<TextTrackCueList>> {
Some(TextTrackCueList::new(self.global().as_window(), &[]))
}
fn AddCue(&self, cue: &TextTrackCue) -> ErrorResult {
if let Some(old_track) = cue.get_track() {
if old_track.RemoveCue(cue).is_err() {
warn!("Failed to remove cues for the added cue's text track");
}
}
self.get_cues().add(cue);
Ok(())
}
fn RemoveCue(&self, cue: &TextTrackCue) -> ErrorResult {
let cues = self.get_cues();
let index = match cues.find(cue) {
Some(i) => Ok(i),
None => Err(Error::NotFound),
}?;
cues.remove(index);
Ok(())
}
event_handler!(cuechange, GetOncuechange, SetOncuechange);
}