Skip to main content

script/dom/webvtt/
texttrackcue.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6
7use dom_struct::dom_struct;
8use script_bindings::cell::DomRefCell;
9
10use crate::dom::bindings::codegen::Bindings::TextTrackCueBinding::TextTrackCueMethods;
11use crate::dom::bindings::num::Finite;
12use crate::dom::bindings::root::{Dom, DomRoot};
13use crate::dom::bindings::str::DOMString;
14use crate::dom::eventtarget::EventTarget;
15use crate::dom::texttrack::TextTrack;
16
17#[dom_struct]
18pub(crate) struct TextTrackCue {
19    eventtarget: EventTarget,
20    id: DomRefCell<DOMString>,
21    track: Option<Dom<TextTrack>>,
22    start_time: Cell<f64>,
23    end_time: Cell<f64>,
24    pause_on_exit: Cell<bool>,
25}
26
27impl TextTrackCue {
28    pub(crate) fn new_inherited(
29        id: DOMString,
30        start_time: f64,
31        end_time: f64,
32        track: Option<&TextTrack>,
33    ) -> TextTrackCue {
34        TextTrackCue {
35            eventtarget: EventTarget::new_inherited(),
36            id: DomRefCell::new(id),
37            track: track.map(Dom::from_ref),
38            start_time: Cell::new(start_time),
39            end_time: Cell::new(end_time),
40            pause_on_exit: Cell::new(false),
41        }
42    }
43
44    pub(crate) fn id(&self) -> DOMString {
45        self.id.borrow().clone()
46    }
47
48    pub(crate) fn get_track(&self) -> Option<DomRoot<TextTrack>> {
49        self.track.as_ref().map(|t| DomRoot::from_ref(&**t))
50    }
51}
52
53impl TextTrackCueMethods<crate::DomTypeHolder> for TextTrackCue {
54    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
55    fn Id(&self) -> DOMString {
56        self.id()
57    }
58
59    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
60    fn SetId(&self, value: DOMString) {
61        *self.id.borrow_mut() = value;
62    }
63
64    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-track>
65    fn GetTrack(&self) -> Option<DomRoot<TextTrack>> {
66        self.get_track()
67    }
68
69    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
70    fn StartTime(&self) -> Finite<f64> {
71        Finite::wrap(self.start_time.get())
72    }
73
74    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
75    fn SetStartTime(&self, value: Finite<f64>) {
76        self.start_time.set(*value);
77    }
78
79    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
80    fn EndTime(&self) -> Finite<f64> {
81        Finite::wrap(self.end_time.get())
82    }
83
84    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
85    fn SetEndTime(&self, value: Finite<f64>) {
86        self.end_time.set(*value);
87    }
88
89    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
90    fn PauseOnExit(&self) -> bool {
91        self.pause_on_exit.get()
92    }
93
94    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
95    fn SetPauseOnExit(&self, value: bool) {
96        self.pause_on_exit.set(value);
97    }
98
99    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onenter
100    event_handler!(enter, GetOnenter, SetOnenter);
101
102    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onexit
103    event_handler!(exit, GetOnexit, SetOnexit);
104}