Skip to main content

script/dom/webvtt/
vttregion.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 js::context::JSContext;
9use js::rust::HandleObject;
10use script_bindings::cell::DomRefCell;
11use script_bindings::domstring::TracedDOMString;
12use script_bindings::reflector::{Reflector, reflect_dom_object_with_proto};
13
14use crate::dom::bindings::codegen::Bindings::VTTRegionBinding::{ScrollSetting, VTTRegionMethods};
15use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
16use crate::dom::bindings::num::Finite;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::window::Window;
20
21#[dom_struct]
22pub(crate) struct VTTRegion {
23    reflector_: Reflector,
24    id: DomRefCell<TracedDOMString>,
25    width: Cell<f64>,
26    lines: Cell<u32>,
27    region_anchor_x: Cell<f64>,
28    region_anchor_y: Cell<f64>,
29    viewport_anchor_x: Cell<f64>,
30    viewport_anchor_y: Cell<f64>,
31    scroll: Cell<ScrollSetting>,
32}
33
34impl VTTRegion {
35    fn new_inherited() -> Self {
36        VTTRegion {
37            reflector_: Reflector::new(),
38            id: DomRefCell::new(DOMString::default().traced()),
39            width: Cell::new(100_f64),
40            lines: Cell::new(3),
41            region_anchor_x: Cell::new(0_f64),
42            region_anchor_y: Cell::new(100_f64),
43            viewport_anchor_x: Cell::new(0_f64),
44            viewport_anchor_y: Cell::new(100_f64),
45            scroll: Cell::new(Default::default()),
46        }
47    }
48
49    fn new(cx: &mut JSContext, window: &Window, proto: Option<HandleObject>) -> DomRoot<Self> {
50        reflect_dom_object_with_proto(cx, Box::new(Self::new_inherited()), window, proto)
51    }
52}
53
54impl VTTRegionMethods<crate::DomTypeHolder> for VTTRegion {
55    /// <https://w3c.github.io/webvtt/#dom-vttregion-vttregion>
56    fn Constructor(
57        cx: &mut JSContext,
58        window: &Window,
59        proto: Option<HandleObject>,
60    ) -> Fallible<DomRoot<Self>> {
61        Ok(VTTRegion::new(cx, window, proto))
62    }
63
64    /// <https://w3c.github.io/webvtt/#dom-vttregion-id>
65    fn Id(&self) -> DOMString {
66        self.id.borrow().clone().root()
67    }
68
69    /// <https://w3c.github.io/webvtt/#dom-vttregion-id>
70    fn SetId(&self, value: DOMString) {
71        *self.id.borrow_mut() = value.traced();
72    }
73
74    /// <https://w3c.github.io/webvtt/#dom-vttregion-width>
75    fn Width(&self) -> Finite<f64> {
76        Finite::wrap(self.width.get())
77    }
78
79    /// <https://w3c.github.io/webvtt/#dom-vttregion-width>
80    fn SetWidth(&self, value: Finite<f64>) -> ErrorResult {
81        if *value < 0_f64 || *value > 100_f64 {
82            return Err(Error::IndexSize(None));
83        }
84
85        self.width.set(*value);
86        Ok(())
87    }
88
89    /// <https://w3c.github.io/webvtt/#dom-vttregion-lines>
90    fn Lines(&self) -> u32 {
91        self.lines.get()
92    }
93
94    /// <https://w3c.github.io/webvtt/#dom-vttregion-lines>
95    fn SetLines(&self, value: u32) -> ErrorResult {
96        self.lines.set(value);
97        Ok(())
98    }
99
100    /// <https://w3c.github.io/webvtt/#dom-vttregion-regionanchorx>
101    fn RegionAnchorX(&self) -> Finite<f64> {
102        Finite::wrap(self.region_anchor_x.get())
103    }
104
105    /// <https://w3c.github.io/webvtt/#dom-vttregion-regionanchorx>
106    fn SetRegionAnchorX(&self, value: Finite<f64>) -> ErrorResult {
107        if *value < 0_f64 || *value > 100_f64 {
108            return Err(Error::IndexSize(None));
109        }
110
111        self.region_anchor_x.set(*value);
112        Ok(())
113    }
114
115    /// <https://w3c.github.io/webvtt/#dom-vttregion-regionanchory>
116    fn RegionAnchorY(&self) -> Finite<f64> {
117        Finite::wrap(self.region_anchor_y.get())
118    }
119
120    /// <https://w3c.github.io/webvtt/#dom-vttregion-regionanchory>
121    fn SetRegionAnchorY(&self, value: Finite<f64>) -> ErrorResult {
122        if *value < 0_f64 || *value > 100_f64 {
123            return Err(Error::IndexSize(None));
124        }
125
126        self.region_anchor_y.set(*value);
127        Ok(())
128    }
129
130    /// <https://w3c.github.io/webvtt/#dom-vttregion-viewportanchorx>
131    fn ViewportAnchorX(&self) -> Finite<f64> {
132        Finite::wrap(self.viewport_anchor_x.get())
133    }
134
135    /// <https://w3c.github.io/webvtt/#dom-vttregion-viewportanchorx>
136    fn SetViewportAnchorX(&self, value: Finite<f64>) -> ErrorResult {
137        if *value < 0_f64 || *value > 100_f64 {
138            return Err(Error::IndexSize(None));
139        }
140
141        self.viewport_anchor_x.set(*value);
142        Ok(())
143    }
144
145    /// <https://w3c.github.io/webvtt/#dom-vttregion-viewportanchory>
146    fn ViewportAnchorY(&self) -> Finite<f64> {
147        Finite::wrap(self.viewport_anchor_y.get())
148    }
149
150    /// <https://w3c.github.io/webvtt/#dom-vttregion-viewportanchory>
151    fn SetViewportAnchorY(&self, value: Finite<f64>) -> ErrorResult {
152        if *value < 0_f64 || *value > 100_f64 {
153            return Err(Error::IndexSize(None));
154        }
155
156        self.viewport_anchor_y.set(*value);
157        Ok(())
158    }
159
160    /// <https://w3c.github.io/webvtt/#dom-vttregion-scroll>
161    fn Scroll(&self) -> ScrollSetting {
162        self.scroll.get()
163    }
164
165    /// <https://w3c.github.io/webvtt/#dom-vttregion-scroll>
166    fn SetScroll(&self, value: ScrollSetting) {
167        self.scroll.set(value);
168    }
169}