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