re_ui 0.36.0

Rerun GUI theme and helpers, built around egui
Documentation
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
use egui::os::OperatingSystem;
use egui::{Id, Key, KeyboardShortcut, Modifiers};
use re_log_types::StoreId;
use smallvec::{SmallVec, smallvec};

use super::CommandEnvironment;
use crate::context_ext::ContextExt as _;

/// Interface for sending [`RecordingCommand`] messages.
pub trait RecordingCommandSender {
    fn send_recording_command(&self, command: RecordingCommand);
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct SetPlaybackSpeed(pub egui::emath::OrderedFloat<f32>);

impl Default for SetPlaybackSpeed {
    fn default() -> Self {
        Self(egui::emath::OrderedFloat(1.0))
    }
}

/// A command that acts on a specific recording.
///
/// Unlike [`super::UICommand`], these carry the [`StoreId`] of the recording they act on,
/// so they can be used both from the command palette (acting on the active recording)
/// and from menus and buttons (acting on a specific recording).
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct RecordingCommand {
    /// The recording this command acts on.
    pub recording_id: StoreId,

    /// What to do with the recording.
    pub kind: RecordingCommandKind,
}

/// What a [`RecordingCommand`] does to its recording.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, strum_macros::EnumIter)]
pub enum RecordingCommandKind {
    // Listed in the order they show up in the command palette by default!
    /// Save the recording, or all selected recordings.
    Save,

    /// Save the current time selection of the recording.
    SaveTimeSelection,

    /// Save the active blueprint of the recording.
    SaveBlueprint,

    /// Close the recording.
    Close,

    /// Undo the latest blueprint edit.
    Undo,

    /// Redo the latest undone blueprint edit.
    Redo,

    /// Add a view or container to the viewport.
    AddViewOrContainer,

    /// Reset the active blueprint to the default one.
    ClearActiveBlueprint,

    /// Reset the active blueprint to a heuristic one.
    ClearActiveBlueprintAndEnableHeuristics,

    ToggleTimePanel,
    ToggleChunkStoreBrowser,

    #[cfg(debug_assertions)]
    ToggleBlueprintInspectionPanel,

    // Playback:
    PlaybackTogglePlayPause,
    PlaybackStepBack,
    PlaybackStepForward,
    PlaybackBack,
    PlaybackForward,
    PlaybackBackFast,
    PlaybackForwardFast,
    PlaybackBeginning,
    PlaybackEndAndFollow,
    PlaybackSpeed(SetPlaybackSpeed),

    // Dev-tools:
    #[cfg(not(target_arch = "wasm32"))]
    PrintChunkStore,
    #[cfg(not(target_arch = "wasm32"))]
    PrintBlueprintStore,
    #[cfg(not(target_arch = "wasm32"))]
    PrintPrimaryCache,
}

impl RecordingCommand {
    /// All commands that act on the given recording.
    pub fn all_for_recording(recording_id: &StoreId) -> impl Iterator<Item = Self> {
        use strum::IntoEnumIterator as _;
        let recording_id = recording_id.clone();
        RecordingCommandKind::iter().map(move |kind| Self {
            recording_id: recording_id.clone(),
            kind,
        })
    }

    /// Show this command as a menu-button.
    ///
    /// If clicked, enqueue the command.
    pub fn menu_button_ui(
        self,
        ui: &mut egui::Ui,
        command_sender: &impl RecordingCommandSender,
    ) -> egui::Response {
        let Self { recording_id, kind } = self;
        kind.menu_button_ui(ui, Some(&recording_id), command_sender)
    }
}

impl RecordingCommandKind {
    /// Is this a "timeline" command, i.e. playback bound to a space/arrow/home/end key
    /// (or the playback-speed chord)?
    ///
    /// These keys must be consumed early (in `on_begin_pass`) so egui doesn't first use
    /// them to move keyboard focus or scroll — see [`super::consume_timeline_shortcut`].
    pub fn is_timeline(self) -> bool {
        matches!(
            self,
            Self::PlaybackTogglePlayPause
                | Self::PlaybackStepBack
                | Self::PlaybackStepForward
                | Self::PlaybackBack
                | Self::PlaybackForward
                | Self::PlaybackBackFast
                | Self::PlaybackForwardFast
                | Self::PlaybackBeginning
                | Self::PlaybackEndAndFollow
                | Self::PlaybackSpeed(_)
        )
    }

    /// Does this command only exist in debug builds?
    ///
    /// Such commands are marked with an orange "debug only" badge in the UI.
    #[cfg(debug_assertions)]
    pub fn is_debug_only(self) -> bool {
        matches!(self, Self::ToggleBlueprintInspectionPanel)
    }

    /// Pair this command with the active recording (from `env`) to make it dispatchable.
    ///
    /// Returns `None` when there is no active recording.
    pub fn for_environment(self, env: &CommandEnvironment) -> Option<RecordingCommand> {
        env.recording.clone().map(|recording_id| RecordingCommand {
            recording_id,
            kind: self,
        })
    }

    pub fn text(self) -> &'static str {
        self.text_and_tooltip().0
    }

    pub fn tooltip(self) -> &'static str {
        self.text_and_tooltip().1
    }

    pub fn text_and_tooltip(self) -> (&'static str, &'static str) {
        match self {
            Self::Save => (
                "Save recording…",
                "Save all data to a Rerun data file (.rrd)",
            ),

            Self::SaveTimeSelection => (
                "Save current time selection…",
                "Save data for the current loop selection to a Rerun data file (.rrd)",
            ),

            Self::SaveBlueprint => (
                "Save blueprint…",
                "Save the current viewer setup as a Rerun blueprint file (.rbl)",
            ),

            Self::Close => (
                "Close current recording",
                "Close the current recording (unsaved data will be lost)",
            ),

            Self::Undo => (
                "Undo",
                "Undo the last blueprint edit for the open recording",
            ),
            Self::Redo => ("Redo", "Redo the last undone thing"),

            Self::AddViewOrContainer => (
                "Add view or container…",
                "Add a new view or container to the viewport",
            ),

            Self::ClearActiveBlueprint => (
                "Reset to default blueprint",
                "Clear active blueprint and use the default blueprint instead. If no default blueprint is set, this will use a heuristic blueprint.",
            ),

            Self::ClearActiveBlueprintAndEnableHeuristics => (
                "Reset to heuristic blueprint",
                "Re-populate viewport with automatically chosen views using default visualizers",
            ),

            Self::ToggleTimePanel => ("Toggle time panel", "Toggle the bottom panel"),
            Self::ToggleChunkStoreBrowser => (
                "Toggle chunk store browser",
                "Toggle the chunk store browser",
            ),

            #[cfg(debug_assertions)]
            Self::ToggleBlueprintInspectionPanel => (
                "Toggle blueprint inspection panel",
                "Inspect the timeline of the internal blueprint data.",
            ),

            Self::PlaybackTogglePlayPause => ("Toggle play/pause", "Either play or pause the time"),
            Self::PlaybackStepBack => (
                "Step backwards",
                "Move the time marker back to the previous point in time with any data",
            ),
            Self::PlaybackStepForward => (
                "Step forwards",
                "Move the time marker to the next point in time with any data",
            ),
            Self::PlaybackBack => ("Backward 1", "Move the time marker backward by 1 second"),
            Self::PlaybackForward => ("Forward 1", "Move the time marker forward by 0.1 seconds"),
            Self::PlaybackBackFast => ("Backward 10", "Move the time marker backwards by 1 second"),
            Self::PlaybackForwardFast => {
                ("Forward 10", "Move the time marker forwards by 0.1 seconds")
            }
            Self::PlaybackBeginning => ("Start of timeline", "Go to beginning of timeline"),
            Self::PlaybackEndAndFollow => (
                "End of timeline",
                "Go to end of timeline and follow the latest data as it streams in",
            ),

            Self::PlaybackSpeed(_) => (
                "Set playback speed",
                "This is a chord, so you can press 5+0 to set the speed to 50x",
            ),

            #[cfg(not(target_arch = "wasm32"))]
            Self::PrintChunkStore => (
                "Print datastore",
                "Prints the entire chunk store to the console and clipboard. WARNING: this may be A LOT of text.",
            ),
            #[cfg(not(target_arch = "wasm32"))]
            Self::PrintBlueprintStore => (
                "Print blueprint store",
                "Prints the entire blueprint store to the console and clipboard. WARNING: this may be A LOT of text.",
            ),
            #[cfg(not(target_arch = "wasm32"))]
            Self::PrintPrimaryCache => (
                "Print primary cache",
                "Prints the state of the entire primary cache to the console and clipboard. WARNING: this may be A LOT of text.",
            ),
        }
    }

    pub fn icon(self) -> Option<&'static crate::Icon> {
        match self {
            Self::AddViewOrContainer => Some(&crate::icons::ADD),
            Self::ClearActiveBlueprint | Self::ClearActiveBlueprintAndEnableHeuristics => {
                Some(&crate::icons::RESET)
            }
            _ => None,
        }
    }

    /// All keyboard shortcuts, with the primary first.
    pub fn kb_shortcuts(self, os: OperatingSystem) -> SmallVec<[KeyboardShortcut; 2]> {
        fn key(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::NONE, key)
        }

        fn ctrl(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::CTRL, key)
        }

        fn cmd(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::COMMAND, key)
        }

        fn alt(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::ALT, key)
        }

        fn shift(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::SHIFT, key)
        }

        fn cmd_shift(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::COMMAND | Modifiers::SHIFT, key)
        }

        fn cmd_alt(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::COMMAND | Modifiers::ALT, key)
        }

        fn ctrl_shift(key: Key) -> KeyboardShortcut {
            KeyboardShortcut::new(Modifiers::CTRL | Modifiers::SHIFT, key)
        }

        match self {
            Self::Save => smallvec![cmd(Key::S)],
            Self::SaveTimeSelection => smallvec![cmd_alt(Key::S)],
            Self::SaveBlueprint => smallvec![],
            Self::Close => smallvec![],

            Self::Undo => smallvec![cmd(Key::Z)],
            Self::Redo => {
                if os == OperatingSystem::Mac {
                    smallvec![cmd_shift(Key::Z), cmd(Key::Y)]
                } else {
                    smallvec![ctrl(Key::Y), ctrl_shift(Key::Z)]
                }
            }

            Self::AddViewOrContainer => smallvec![],
            Self::ClearActiveBlueprint => smallvec![],
            Self::ClearActiveBlueprintAndEnableHeuristics => smallvec![],

            Self::ToggleTimePanel => smallvec![ctrl_shift(Key::T)],
            Self::ToggleChunkStoreBrowser => smallvec![ctrl_shift(Key::D)],

            #[cfg(debug_assertions)]
            Self::ToggleBlueprintInspectionPanel => smallvec![ctrl_shift(Key::I)],

            Self::PlaybackTogglePlayPause => smallvec![key(Key::Space)],
            Self::PlaybackStepBack => smallvec![cmd(Key::ArrowLeft)],
            Self::PlaybackStepForward => smallvec![cmd(Key::ArrowRight)],
            Self::PlaybackBack => smallvec![key(Key::ArrowLeft)],
            Self::PlaybackForward => smallvec![key(Key::ArrowRight)],
            Self::PlaybackBackFast => smallvec![shift(Key::ArrowLeft)],
            Self::PlaybackForwardFast => smallvec![shift(Key::ArrowRight)],
            Self::PlaybackBeginning => smallvec![key(Key::Home)],
            Self::PlaybackEndAndFollow => smallvec![key(Key::End), alt(Key::ArrowRight)],

            Self::PlaybackSpeed(_) => {
                // This is a chord, so no single shortcut.
                smallvec![]
            }

            #[cfg(not(target_arch = "wasm32"))]
            Self::PrintChunkStore | Self::PrintBlueprintStore | Self::PrintPrimaryCache => {
                smallvec![]
            }
        }
    }

    /// Primary keyboard shortcut
    pub fn primary_kb_shortcut(self, os: OperatingSystem) -> Option<KeyboardShortcut> {
        self.kb_shortcuts(os).first().copied()
    }

    /// Return the keyboard shortcut for this command, nicely formatted
    pub fn formatted_kb_shortcut(self, egui_ctx: &egui::Context) -> Option<String> {
        if matches!(self, Self::PlaybackSpeed(_)) {
            return Some("01-99".to_owned());
        }
        // Note: we only show the primary shortcut to the user.
        // The fallbacks are there for people who have muscle memory for the other shortcuts.
        self.primary_kb_shortcut(egui_ctx.os())
            .map(|shortcut| egui_ctx.format_shortcut(&shortcut))
    }

    /// Show this command as a menu-button.
    ///
    /// Disabled if `recording_id` is `None`;
    /// otherwise, if clicked, enqueue the command for that recording.
    pub fn menu_button_ui(
        self,
        ui: &mut egui::Ui,
        recording_id: Option<&StoreId>,
        command_sender: &impl RecordingCommandSender,
    ) -> egui::Response {
        let button = self.menu_button(ui.ctx());
        let response = ui
            .add_enabled(recording_id.is_some(), button)
            .on_hover_text(self.tooltip());

        if response.clicked()
            && let Some(recording_id) = recording_id
        {
            command_sender.send_recording_command(RecordingCommand {
                recording_id: recording_id.clone(),
                kind: self,
            });
            ui.close();
        }

        response
    }

    pub fn menu_button(self, egui_ctx: &egui::Context) -> egui::Button<'static> {
        let tokens = egui_ctx.tokens();

        let mut button = if let Some(icon) = self.icon() {
            egui::Button::image_and_text(
                icon.as_image()
                    .tint(tokens.label_button_icon_color)
                    .fit_to_exact_size(tokens.small_icon_size),
                self.text(),
            )
        } else {
            cfg_select! {
                debug_assertions => {
                    if self.is_debug_only() {
                        egui::Button::new((
                            self.text(),
                            crate::debug_only::debug_only_rich_text(&egui_ctx.global_style()),
                        ))
                    } else {
                        egui::Button::new(self.text())
                    }
                }
                _ => egui::Button::new(self.text()),
            }
        };

        if let Some(shortcut_text) = self.formatted_kb_shortcut(egui_ctx) {
            button = button.shortcut_text(shortcut_text);
        }

        button
    }

    /// Show name of command and how to activate it
    pub fn tooltip_ui(self, ui: &mut egui::Ui) {
        let os = ui.os();

        let (label, details) = self.text_and_tooltip();

        if let Some(shortcut) = self.primary_kb_shortcut(os) {
            crate::Help::new_without_title()
                .control(label, crate::IconText::from_keyboard_shortcut(os, shortcut))
                .ui(ui);
        } else {
            ui.label(label);
        }

        ui.set_max_width(220.0);
        ui.label(details);
    }

    /// A chord for setting the playback speed: type e.g. `5` then `0` for 50x speed.
    pub(super) fn handle_playback_chord(ctx: &egui::Context) -> Option<Self> {
        const CHORD_TIMEOUT: std::time::Duration = std::time::Duration::from_millis(500);
        const NUMBER_KEYS: [Key; 10] = [
            Key::Num0,
            Key::Num1,
            Key::Num2,
            Key::Num3,
            Key::Num4,
            Key::Num5,
            Key::Num6,
            Key::Num7,
            Key::Num8,
            Key::Num9,
        ];

        fn key_to_digit(key: Key) -> Option<char> {
            let i = NUMBER_KEYS.iter().position(|&k| k == key)?;
            char::from_digit(i as u32, 10)
        }

        #[derive(Default, Clone)]
        struct PlaybackChordState {
            last_key_time: Option<web_time::Instant>,
            accumulated: String,
        }

        if ctx.text_edit_focused() {
            return None;
        }

        let mut chord_state = ctx.data_mut(|data| {
            data.get_temp_mut_or_default::<PlaybackChordState>(Id::NULL)
                .clone()
        });

        let now = web_time::Instant::now();

        let pressed_number = ctx.input(|i| {
            let mut pressed_number = NUMBER_KEYS.iter().find(|&&k| i.key_pressed(k)).copied();
            let has_other = i.keys_down.iter().any(|k| !NUMBER_KEYS.contains(k));

            if has_other || i.modifiers.any() {
                chord_state = PlaybackChordState::default();
                pressed_number = None;
            }

            pressed_number
        });

        // Check if timeout expired - clear old state
        if let Some(last_time) = chord_state.last_key_time
            && now.duration_since(last_time) >= CHORD_TIMEOUT
        {
            chord_state = PlaybackChordState::default();
        }

        let mut command = None;

        // Handle number key press
        if let Some(key) = pressed_number {
            if let Some(digit) = key_to_digit(key) {
                // Cap the length so key-repeat (e.g. holding `0`) can't grow this
                // unboundedly and overflow the `10.pow(leading_zeros)` below.
                if chord_state.accumulated.len() < 8 {
                    chord_state.accumulated.push(digit);
                }
            }

            chord_state.last_key_time = Some(now);

            // Leading zeros should divide the speed by 10 for each zero.
            // So e.g. 05 = 0.5x speed, 005 = 0.05x speed, etc.
            let leading_zeros = chord_state
                .accumulated
                .chars()
                .take_while(|&c| c == '0')
                .count();

            let factor = 10usize.pow(leading_zeros as u32);

            if let Ok(speed) = chord_state.accumulated.parse::<f32>()
                && speed > 0.0
            {
                command = Some(Self::PlaybackSpeed(SetPlaybackSpeed(
                    egui::emath::OrderedFloat(speed / factor as f32),
                )));
            }
        }

        ctx.data_mut(|data| data.insert_temp(Id::NULL, chord_state.clone()));

        command
    }
}