File size: 6,699 Bytes
9ff4bea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
from __future__ import annotations

import json
from dataclasses import dataclass
from typing import Literal

from typing_extensions import deprecated

# Type definitions based on `crates/top/re_viewer/src/event.rs`
# NOTE: In Python, we need to update both the type definitions
#       and the serialization code, which is in `viewer_event_from_json`.


@dataclass
class ViewerEventBase:
    """Base class for all viewer events."""

    application_id: str
    """The application ID of the recording that triggered the event."""

    recording_id: str
    """The recording ID of the recording that triggered the event."""

    segment_id: str | None
    """The segment ID of the recording that triggered the event, if any."""

    @property
    @deprecated("Use `segment_id` instead.")
    def partition_id(self) -> str | None:
        return self.segment_id


@dataclass
class EntitySelectionItem:
    """An entity selection item, representing a selected entity in the viewer."""

    @property
    def type(self) -> Literal["entity"]:
        return "entity"

    entity_path: str
    """The entity path of the selected entity."""

    instance_id: int | None = None
    """The instance ID of the selected entity, if any."""

    view_name: str | None = None
    """The name of the view containing the selected entity, if any."""

    position: list[float] | None = None
    """The 3D position of the selection, if any."""


@dataclass
class ViewSelectionItem:
    """A view selection item, representing a selected view in the viewer."""

    @property
    def type(self) -> Literal["view"]:
        return "view"

    view_id: str
    """The ID of the selected view."""

    view_name: str
    """The name of the selected view."""


@dataclass
class ContainerSelectionItem:
    """A container selection item, representing a selected container in the viewer."""

    @property
    def type(self) -> Literal["container"]:
        return "container"

    container_id: str
    """The ID of the selected container."""

    container_name: str
    """The name of the selected container."""


SelectionItem = EntitySelectionItem | ViewSelectionItem | ContainerSelectionItem
"""Union type for all possible selection item types."""


@dataclass
class PlayEvent(ViewerEventBase):
    """Event triggered when the viewer starts playing."""

    @property
    def type(self) -> Literal["play"]:
        return "play"


@dataclass
class PauseEvent(ViewerEventBase):
    """Event triggered when the viewer pauses playback."""

    @property
    def type(self) -> Literal["pause"]:
        return "pause"


@dataclass
class TimeUpdateEvent(ViewerEventBase):
    """Event triggered when the current time changes in the viewer."""

    @property
    def type(self) -> Literal["time_update"]:
        return "time_update"

    time: float
    """The new time value."""


@dataclass
class TimelineChangeEvent(ViewerEventBase):
    """Event triggered when the active timeline changes in the viewer."""

    @property
    def type(self) -> Literal["timeline_change"]:
        return "timeline_change"

    timeline: str
    """The name of the new active timeline."""

    time: float
    """The current time value on the new timeline."""


@dataclass
class SelectionChangeEvent(ViewerEventBase):
    """Event triggered when the selection changes in the viewer."""

    @property
    def type(self) -> Literal["selection_change"]:
        return "selection_change"

    items: list[SelectionItem]
    """The list of currently selected items."""


@dataclass
class RecordingOpenEvent(ViewerEventBase):
    """Event triggered when a recording is opened in the viewer."""

    @property
    def type(self) -> Literal["recording_open"]:
        return "recording_open"

    source: str
    """The source of the recording (e.g. a URL or file path)."""

    version: str | None
    """The version of the recording, if available."""


ViewerEvent = PlayEvent | PauseEvent | TimeUpdateEvent | TimelineChangeEvent | SelectionChangeEvent | RecordingOpenEvent
"""Union type for all possible viewer event types."""


def _viewer_event_from_json_str(json_str: str) -> ViewerEvent:
    data = json.loads(json_str)

    event_type: str = data["type"]
    app_id: str = data["application_id"]
    recording_id: str = data["recording_id"]
    segment_id: str | None = data.get("segment_id", None)

    if event_type == "play":
        return PlayEvent(application_id=app_id, recording_id=recording_id, segment_id=segment_id)

    elif event_type == "pause":
        return PauseEvent(application_id=app_id, recording_id=recording_id, segment_id=segment_id)

    elif event_type == "time_update":
        return TimeUpdateEvent(
            application_id=app_id,
            recording_id=recording_id,
            segment_id=segment_id,
            time=data["time"],
        )

    elif event_type == "timeline_change":
        return TimelineChangeEvent(
            application_id=app_id,
            recording_id=recording_id,
            segment_id=segment_id,
            timeline=data["timeline"],
            time=data["time"],
        )

    elif event_type == "selection_change":
        items: list[SelectionItem] = []
        for item in data["items"]:
            if item["type"] == "entity":
                items.append(
                    EntitySelectionItem(
                        entity_path=item["entity_path"],
                        instance_id=item.get("instance_id", None),
                        view_name=item.get("view_name", None),
                        position=item.get("position", None),
                    )
                )
            elif item["type"] == "view":
                items.append(
                    ViewSelectionItem(
                        view_id=item["view_id"],
                        view_name=item["view_name"],
                    )
                )
            elif item["type"] == "container":
                items.append(
                    ContainerSelectionItem(
                        container_id=item["container_id"],
                        container_name=item["container_name"],
                    )
                )

        return SelectionChangeEvent(
            application_id=app_id,
            recording_id=recording_id,
            segment_id=segment_id,
            items=items,
        )

    elif event_type == "recording_open":
        return RecordingOpenEvent(
            application_id=app_id,
            recording_id=recording_id,
            segment_id=segment_id,
            source=data["source"],
            version=data.get("version", None),
        )

    else:
        raise ValueError(f"Unknown event type: '{event_type}'")