Skip to main content

script/dom/css/
fontfacesetloadevent.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 dom_struct::dom_struct;
6use js::context::JSContext;
7use js::rust::{HandleObject, MutableHandleValue};
8use script_bindings::reflector::reflect_dom_object_with_proto;
9use stylo_atoms::Atom;
10
11use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
12use crate::dom::bindings::codegen::Bindings::FontFaceSetBinding::{
13    FontFaceSetLoadEventInit, FontFaceSetLoadEventMethods,
14};
15use crate::dom::bindings::frozenarray::CachedFrozenArray;
16use crate::dom::bindings::inheritance::Castable;
17use crate::dom::bindings::root::{Dom, DomRoot};
18use crate::dom::bindings::str::DOMString;
19use crate::dom::event::Event;
20use crate::dom::fontface::FontFace;
21use crate::dom::window::Window;
22
23/// <https://drafts.csswg.org/css-font-loading/#fontfacesetloadevent>
24#[dom_struct]
25pub(crate) struct FontFaceSetLoadEvent {
26    event: Event,
27
28    /// The fonts that this event is about.
29    fontfaces: Vec<Dom<FontFace>>,
30
31    /// The JavaScript version of `fontfaces`, made once and then reused.
32    #[ignore_malloc_size_of = "mozjs"]
33    frozen_fontfaces: CachedFrozenArray,
34}
35
36impl FontFaceSetLoadEvent {
37    fn new_inherited(fontfaces: &[DomRoot<FontFace>]) -> FontFaceSetLoadEvent {
38        FontFaceSetLoadEvent {
39            event: Event::new_inherited(),
40            fontfaces: fontfaces
41                .iter()
42                .map(|fontface| Dom::from_ref(&**fontface))
43                .collect(),
44            frozen_fontfaces: CachedFrozenArray::new(),
45        }
46    }
47
48    fn new_with_proto(
49        cx: &mut JSContext,
50        window: &Window,
51        proto: Option<HandleObject>,
52        type_: Atom,
53        bubbles: bool,
54        cancelable: bool,
55        fontfaces: &[DomRoot<FontFace>],
56    ) -> DomRoot<FontFaceSetLoadEvent> {
57        let event = reflect_dom_object_with_proto(
58            cx,
59            Box::new(FontFaceSetLoadEvent::new_inherited(fontfaces)),
60            window,
61            proto,
62        );
63        event
64            .upcast::<Event>()
65            .init_event(type_, bubbles, cancelable);
66        event
67    }
68}
69
70impl FontFaceSetLoadEventMethods<crate::DomTypeHolder> for FontFaceSetLoadEvent {
71    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfacesetloadevent-fontfacesetloadevent>
72    fn Constructor(
73        cx: &mut JSContext,
74        window: &Window,
75        proto: Option<HandleObject>,
76        type_: DOMString,
77        init: &FontFaceSetLoadEventInit,
78    ) -> DomRoot<FontFaceSetLoadEvent> {
79        let fontfaces: Vec<DomRoot<FontFace>> = init
80            .fontfaces
81            .iter()
82            .map(|fontface| DomRoot::from_ref(&**fontface))
83            .collect();
84        FontFaceSetLoadEvent::new_with_proto(
85            cx,
86            window,
87            proto,
88            Atom::from(type_),
89            init.parent.bubbles,
90            init.parent.cancelable,
91            &fontfaces,
92        )
93    }
94
95    /// <https://drafts.csswg.org/css-font-loading/#dom-fontfacesetloadevent-fontfaces>
96    fn Fontfaces(&self, cx: &mut JSContext, retval: MutableHandleValue) {
97        self.frozen_fontfaces.get_or_init(
98            cx,
99            || {
100                self.fontfaces
101                    .iter()
102                    .map(|fontface| DomRoot::from_ref(&**fontface))
103                    .collect()
104            },
105            retval,
106        );
107    }
108
109    /// <https://dom.spec.whatwg.org/#dom-event-istrusted>
110    fn IsTrusted(&self) -> bool {
111        self.event.IsTrusted()
112    }
113}