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::{DomRoot, MutNullableDom};
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    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-identifier>
21    id: DomRefCell<DOMString>,
22    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-track>
23    text_track: MutNullableDom<TextTrack>,
24    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-start-time>
25    start_time: Cell<f64>,
26    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-end-time>
27    end_time: Cell<f64>,
28    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-pause-on-exit-flag>
29    pause_on_exit: Cell<bool>,
30    /// <https://html.spec.whatwg.org/multipage/#text-track-cue-active-flag>
31    active: Cell<bool>,
32}
33
34impl TextTrackCue {
35    pub(crate) fn new_inherited(
36        id: DOMString,
37        start_time: f64,
38        end_time: f64,
39        text_track: Option<&TextTrack>,
40    ) -> TextTrackCue {
41        TextTrackCue {
42            eventtarget: EventTarget::new_inherited(),
43            id: DomRefCell::new(id),
44            text_track: MutNullableDom::new(text_track),
45            start_time: Cell::new(start_time),
46            end_time: Cell::new(end_time),
47            pause_on_exit: Cell::new(false),
48            active: Default::default(),
49        }
50    }
51
52    pub(crate) fn id(&self) -> DOMString {
53        self.id.borrow().clone()
54    }
55
56    pub(crate) fn get_text_track(&self) -> Option<DomRoot<TextTrack>> {
57        self.text_track.get()
58    }
59
60    pub(crate) fn set_text_track(&self, text_track: Option<&TextTrack>) {
61        self.text_track.set(text_track);
62    }
63
64    pub(crate) fn start_time(&self) -> f64 {
65        self.start_time.get()
66    }
67
68    pub(crate) fn end_time(&self) -> f64 {
69        self.end_time.get()
70    }
71
72    pub(crate) fn is_active(&self) -> bool {
73        self.active.get()
74    }
75
76    pub(crate) fn set_active(&self, active: bool) {
77        self.active.set(active)
78    }
79}
80
81impl TextTrackCueMethods<crate::DomTypeHolder> for TextTrackCue {
82    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
83    fn Id(&self) -> DOMString {
84        self.id()
85    }
86
87    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-id>
88    fn SetId(&self, value: DOMString) {
89        *self.id.borrow_mut() = value;
90    }
91
92    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-track>
93    fn GetTrack(&self) -> Option<DomRoot<TextTrack>> {
94        self.get_text_track()
95    }
96
97    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
98    fn StartTime(&self) -> Finite<f64> {
99        Finite::wrap(self.start_time.get())
100    }
101
102    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-starttime>
103    fn SetStartTime(&self, value: Finite<f64>) {
104        self.start_time.set(*value);
105    }
106
107    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
108    fn EndTime(&self) -> Finite<f64> {
109        Finite::wrap(self.end_time.get())
110    }
111
112    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-endtime>
113    fn SetEndTime(&self, value: Finite<f64>) {
114        self.end_time.set(*value);
115    }
116
117    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
118    fn PauseOnExit(&self) -> bool {
119        self.pause_on_exit.get()
120    }
121
122    /// <https://html.spec.whatwg.org/multipage/#dom-texttrackcue-pauseonexit>
123    fn SetPauseOnExit(&self, value: bool) {
124        self.pause_on_exit.set(value);
125    }
126
127    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onenter
128    event_handler!(enter, GetOnenter, SetOnenter);
129
130    // https://html.spec.whatwg.org/multipage/#handler-texttrackcue-onexit
131    event_handler!(exit, GetOnexit, SetOnexit);
132}