webxr_api/
events.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 euclid::RigidTransform3D;
6use profile_traits::generic_callback::GenericCallback as ProfileGenericCallback;
7use serde::{Deserialize, Serialize};
8
9use crate::{
10    ApiSpace, BaseSpace, Frame, InputFrame, InputId, InputSource, SelectEvent, SelectKind,
11};
12
13#[derive(Clone, Debug, Serialize, Deserialize)]
14#[expect(clippy::large_enum_variant)]
15pub enum Event {
16    /// Input source connected
17    AddInput(InputSource),
18    /// Input source disconnected
19    RemoveInput(InputId),
20    /// Input updated (this is a disconnect+reconnect)
21    UpdateInput(InputId, InputSource),
22    /// Session ended by device
23    SessionEnd,
24    /// Session focused/blurred/etc
25    VisibilityChange(Visibility),
26    /// Selection started / ended
27    Select(InputId, SelectKind, SelectEvent, Frame),
28    /// Input from an input source has changed
29    InputChanged(InputId, InputFrame),
30    /// Reference space has changed
31    ReferenceSpaceChanged(BaseSpace, RigidTransform3D<f32, ApiSpace, ApiSpace>),
32}
33
34#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
35pub enum Visibility {
36    /// Session fully displayed to user
37    Visible,
38    /// Session still visible, but is not the primary focus
39    VisibleBlurred,
40    /// Session not visible
41    Hidden,
42}
43
44/// Convenience structure for buffering up events
45/// when no event callback has been set
46pub enum EventBuffer {
47    Buffered(Vec<Event>),
48    Sink(ProfileGenericCallback<Event>),
49}
50
51impl Default for EventBuffer {
52    fn default() -> Self {
53        EventBuffer::Buffered(vec![])
54    }
55}
56
57impl EventBuffer {
58    pub fn callback(&mut self, event: Event) {
59        match *self {
60            EventBuffer::Buffered(ref mut events) => events.push(event),
61            EventBuffer::Sink(ref dest) => {
62                let _ = dest.send(event);
63            },
64        }
65    }
66
67    pub fn upgrade(&mut self, dest: ProfileGenericCallback<Event>) {
68        if let EventBuffer::Buffered(ref mut events) = *self {
69            for event in events.drain(..) {
70                let _ = dest.send(event);
71            }
72        }
73        *self = EventBuffer::Sink(dest)
74    }
75}