script/dom/
dompoint.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 base::id::{DomPointId, DomPointIndex};
6use constellation_traits::DomPoint;
7use dom_struct::dom_struct;
8use js::rust::HandleObject;
9use rustc_hash::FxHashMap;
10
11use crate::dom::bindings::codegen::Bindings::DOMPointBinding::{DOMPointInit, DOMPointMethods};
12use crate::dom::bindings::codegen::Bindings::DOMPointReadOnlyBinding::DOMPointReadOnlyMethods;
13use crate::dom::bindings::error::Fallible;
14use crate::dom::bindings::reflector::reflect_dom_object_with_proto;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::serializable::Serializable;
17use crate::dom::bindings::structuredclone::StructuredData;
18use crate::dom::dompointreadonly::{DOMPointReadOnly, DOMPointWriteMethods};
19use crate::dom::globalscope::GlobalScope;
20use crate::script_runtime::CanGc;
21
22// http://dev.w3.org/fxtf/geometry/Overview.html#dompoint
23#[dom_struct]
24pub(crate) struct DOMPoint {
25    point: DOMPointReadOnly,
26}
27
28impl DOMPoint {
29    fn new_inherited(x: f64, y: f64, z: f64, w: f64) -> DOMPoint {
30        DOMPoint {
31            point: DOMPointReadOnly::new_inherited(x, y, z, w),
32        }
33    }
34
35    pub(crate) fn new(
36        global: &GlobalScope,
37        x: f64,
38        y: f64,
39        z: f64,
40        w: f64,
41        can_gc: CanGc,
42    ) -> DomRoot<DOMPoint> {
43        Self::new_with_proto(global, None, x, y, z, w, can_gc)
44    }
45
46    fn new_with_proto(
47        global: &GlobalScope,
48        proto: Option<HandleObject>,
49        x: f64,
50        y: f64,
51        z: f64,
52        w: f64,
53        can_gc: CanGc,
54    ) -> DomRoot<DOMPoint> {
55        reflect_dom_object_with_proto(
56            Box::new(DOMPoint::new_inherited(x, y, z, w)),
57            global,
58            proto,
59            can_gc,
60        )
61    }
62
63    pub(crate) fn new_from_init(
64        global: &GlobalScope,
65        p: &DOMPointInit,
66        can_gc: CanGc,
67    ) -> DomRoot<DOMPoint> {
68        DOMPoint::new(global, p.x, p.y, p.z, p.w, can_gc)
69    }
70}
71
72impl DOMPointMethods<crate::DomTypeHolder> for DOMPoint {
73    /// <https://drafts.fxtf.org/geometry/#dom-dompointreadonly-dompointreadonly>
74    fn Constructor(
75        global: &GlobalScope,
76        proto: Option<HandleObject>,
77        can_gc: CanGc,
78        x: f64,
79        y: f64,
80        z: f64,
81        w: f64,
82    ) -> Fallible<DomRoot<DOMPoint>> {
83        Ok(DOMPoint::new_with_proto(global, proto, x, y, z, w, can_gc))
84    }
85
86    /// <https://drafts.fxtf.org/geometry/#dom-dompoint-frompoint>
87    fn FromPoint(global: &GlobalScope, init: &DOMPointInit, can_gc: CanGc) -> DomRoot<Self> {
88        Self::new_from_init(global, init, can_gc)
89    }
90
91    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-x>
92    fn X(&self) -> f64 {
93        self.point.X()
94    }
95
96    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-x>
97    fn SetX(&self, value: f64) {
98        self.point.SetX(value);
99    }
100
101    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-y>
102    fn Y(&self) -> f64 {
103        self.point.Y()
104    }
105
106    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-y>
107    fn SetY(&self, value: f64) {
108        self.point.SetY(value);
109    }
110
111    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-z>
112    fn Z(&self) -> f64 {
113        self.point.Z()
114    }
115
116    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-z>
117    fn SetZ(&self, value: f64) {
118        self.point.SetZ(value);
119    }
120
121    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-w>
122    fn W(&self) -> f64 {
123        self.point.W()
124    }
125
126    /// <https://dev.w3.org/fxtf/geometry/Overview.html#dom-dompointreadonly-w>
127    fn SetW(&self, value: f64) {
128        self.point.SetW(value);
129    }
130}
131
132impl Serializable for DOMPoint {
133    type Index = DomPointIndex;
134    type Data = DomPoint;
135
136    fn serialize(&self) -> Result<(DomPointId, Self::Data), ()> {
137        let serialized = DomPoint {
138            x: self.X(),
139            y: self.Y(),
140            z: self.Z(),
141            w: self.W(),
142        };
143        Ok((DomPointId::new(), serialized))
144    }
145
146    fn deserialize(
147        owner: &GlobalScope,
148        serialized: Self::Data,
149        can_gc: CanGc,
150    ) -> Result<DomRoot<Self>, ()>
151    where
152        Self: Sized,
153    {
154        Ok(Self::new(
155            owner,
156            serialized.x,
157            serialized.y,
158            serialized.z,
159            serialized.w,
160            can_gc,
161        ))
162    }
163
164    fn serialized_storage<'a>(
165        data: StructuredData<'a, '_>,
166    ) -> &'a mut Option<FxHashMap<DomPointId, Self::Data>> {
167        match data {
168            StructuredData::Reader(reader) => &mut reader.points,
169            StructuredData::Writer(writer) => &mut writer.points,
170        }
171    }
172}