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
//! The main Rerun drop-down menu found in the top panel.

use egui::NumExt as _;

use re_ui::UICommand;
use re_viewer_context::StoreContext;

use crate::App;

const SPACING: f32 = 12.0;

impl App {
    pub fn rerun_menu_button_ui(
        &mut self,
        frame: &eframe::Frame,
        _store_context: Option<&StoreContext<'_>>,
        ui: &mut egui::Ui,
    ) {
        // let desired_icon_height = ui.max_rect().height() - 2.0 * ui.spacing_mut().button_padding.y;
        let desired_icon_height = ui.max_rect().height() - 4.0; // TODO(emilk): figure out this fudge
        let desired_icon_height = desired_icon_height.at_most(28.0); // figma size 2023-02-03

        let image = re_ui::icons::RERUN_MENU
            .as_image()
            .max_height(desired_icon_height);

        ui.menu_image_button(image, |ui| {
            ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend); // no wrapping: make as wide as needed

            ui.menu_button("About", |ui| self.about_rerun_ui(frame, ui));

            ui.add_space(SPACING);

            UICommand::ToggleCommandPalette.menu_button_ui(ui, &self.command_sender);

            ui.add_space(SPACING);

            UICommand::Open.menu_button_ui(ui, &self.command_sender);
            UICommand::Import.menu_button_ui(ui, &self.command_sender);

            self.save_buttons_ui(ui, _store_context);

            UICommand::SaveBlueprint.menu_button_ui(ui, &self.command_sender);

            UICommand::CloseCurrentRecording.menu_button_ui(ui, &self.command_sender);

            ui.add_space(SPACING);

            #[cfg(not(target_arch = "wasm32"))]
            {
                // On the web the browser controls the zoom
                let zoom_factor = ui.ctx().zoom_factor();
                ui.weak(format!("Current zoom: {:.0}%", zoom_factor * 100.0))
                    .on_hover_text(
                        "The UI zoom level on top of the operating system's default value",
                    );
                UICommand::ZoomIn.menu_button_ui(ui, &self.command_sender);
                UICommand::ZoomOut.menu_button_ui(ui, &self.command_sender);
                ui.add_enabled_ui(zoom_factor != 1.0, |ui| {
                    UICommand::ZoomReset.menu_button_ui(ui, &self.command_sender)
                });

                UICommand::ToggleFullscreen.menu_button_ui(ui, &self.command_sender);

                ui.add_space(SPACING);
            }

            {
                UICommand::ResetViewer.menu_button_ui(ui, &self.command_sender);

                #[cfg(not(target_arch = "wasm32"))]
                UICommand::OpenProfiler.menu_button_ui(ui, &self.command_sender);

                UICommand::ToggleMemoryPanel.menu_button_ui(ui, &self.command_sender);
                UICommand::ToggleChunkStoreBrowser.menu_button_ui(ui, &self.command_sender);

                #[cfg(debug_assertions)]
                UICommand::ToggleEguiDebugPanel.menu_button_ui(ui, &self.command_sender);
            }

            ui.add_space(SPACING);

            UICommand::Settings.menu_button_ui(ui, &self.command_sender);

            #[cfg(target_arch = "wasm32")]
            backend_menu_ui(&self.command_sender, ui, frame);

            #[cfg(debug_assertions)]
            ui.menu_button("Debug", |ui| {
                ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);
                debug_menu_options_ui(ui, &mut self.state.app_options, &self.command_sender);

                ui.label("egui debug options:");
                ui.weak(format!(
                    "pixels_per_point: {:?}",
                    ui.ctx().pixels_per_point()
                ));
                egui_debug_options_ui(ui);
            });

            ui.add_space(SPACING);

            UICommand::OpenWebHelp.menu_button_ui(ui, &self.command_sender);
            UICommand::OpenRerunDiscord.menu_button_ui(ui, &self.command_sender);

            #[cfg(not(target_arch = "wasm32"))]
            {
                ui.add_space(SPACING);
                UICommand::Quit.menu_button_ui(ui, &self.command_sender);
            }
        });
    }

    fn about_rerun_ui(&self, frame: &eframe::Frame, ui: &mut egui::Ui) {
        let re_build_info::BuildInfo {
            crate_name,
            features,
            version,
            rustc_version,
            llvm_version,
            git_hash,
            git_branch: _,
            is_in_rerun_workspace: _,
            target_triple,
            datetime,
        } = *self.build_info();

        ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend);

        let git_hash_suffix = if git_hash.is_empty() {
            String::new()
        } else {
            let short_git_hash = &git_hash[..std::cmp::min(git_hash.len(), 7)];
            format!("({short_git_hash})")
        };

        let mut label = format!(
            "{crate_name} {version} {git_hash_suffix}\n\
            {target_triple}"
        );

        // It is really the features of `rerun-cli` (the `rerun` binary) that are interesting.
        // For the web-viewer we get `crate_name: "re_viewer"` here, which is much less interesting.
        if crate_name == "rerun-cli" && !features.is_empty() {
            label += &format!("\n{crate_name} features: {features}");
        }

        if !rustc_version.is_empty() {
            label += &format!("\nrustc {rustc_version}");
            if !llvm_version.is_empty() {
                label += &format!(", LLVM {llvm_version}");
            }
        }

        if !datetime.is_empty() {
            label += &format!("\nbuilt {datetime}");
        }

        ui.label(label);

        if let Some(render_state) = frame.wgpu_render_state() {
            render_state_ui(ui, render_state);
        }
    }

    fn save_buttons_ui(&mut self, ui: &mut egui::Ui, store_ctx: Option<&StoreContext<'_>>) {
        use re_ui::UICommandSender;

        let file_save_in_progress = self.background_tasks.is_file_save_in_progress();

        let save_recording_button = UICommand::SaveRecording.menu_button(ui.ctx());
        let save_selection_button = UICommand::SaveRecordingSelection.menu_button(ui.ctx());

        if file_save_in_progress {
            ui.add_enabled_ui(false, |ui| {
                ui.horizontal(|ui| {
                    ui.add(save_recording_button);
                    ui.spinner();
                });
                ui.horizontal(|ui| {
                    ui.add(save_selection_button);
                    ui.spinner();
                });
            });
        } else {
            let entity_db_is_nonempty = store_ctx.map_or(false, |ctx| !ctx.recording.is_empty());
            ui.add_enabled_ui(entity_db_is_nonempty, |ui| {
                if ui
                    .add(save_recording_button)
                    .on_hover_text("Save all data to a Rerun data file (.rrd)")
                    .clicked()
                {
                    ui.close_menu();
                    self.command_sender.send_ui(UICommand::SaveRecording);
                }

                // We need to know the loop selection _before_ we can even display the
                // button, as this will determine whether its grayed out or not!
                // TODO(cmc): In practice the loop (green) selection is always there
                // at the moment so…
                let loop_selection = self.state.loop_selection(store_ctx);

                if ui
                    .add_enabled(loop_selection.is_some(), save_selection_button)
                    .on_hover_text(
                        "Save data for the current loop selection to a Rerun data file (.rrd)",
                    )
                    .clicked()
                {
                    ui.close_menu();
                    self.command_sender
                        .send_ui(UICommand::SaveRecordingSelection);
                }
            });
        }
    }
}

fn render_state_ui(ui: &mut egui::Ui, render_state: &egui_wgpu::RenderState) {
    let wgpu_adapter_details_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
        let info = &adapter.get_info();

        let wgpu::AdapterInfo {
            name,
            vendor,
            device,
            device_type,
            driver,
            driver_info,
            backend,
        } = &info;

        // Example values:
        // > name: "llvmpipe (LLVM 16.0.6, 256 bits)", device_type: Cpu, backend: Vulkan, driver: "llvmpipe", driver_info: "Mesa 23.1.6-arch1.4 (LLVM 16.0.6)"
        // > name: "Apple M1 Pro", device_type: IntegratedGpu, backend: Metal, driver: "", driver_info: ""
        // > name: "ANGLE (Apple, Apple M1 Pro, OpenGL 4.1)", device_type: IntegratedGpu, backend: Gl, driver: "", driver_info: ""

        egui::Grid::new("adapter_info").show(ui, |ui| {
            ui.label("Backend");
            ui.label(backend.to_str()); // TODO(wgpu#5170): Use std::fmt::Display for backend.
            ui.end_row();

            ui.label("Device Type");
            ui.label(format!("{device_type:?}"));
            ui.end_row();

            if !name.is_empty() {
                ui.label("Name");
                ui.label(format!("{name:?}"));
                ui.end_row();
            }
            if !driver.is_empty() {
                ui.label("Driver");
                ui.label(format!("{driver:?}"));
                ui.end_row();
            }
            if !driver_info.is_empty() {
                ui.label("Driver info");
                ui.label(format!("{driver_info:?}"));
                ui.end_row();
            }
            if *vendor != 0 {
                // TODO(emilk): decode using https://github.com/gfx-rs/wgpu/blob/767ac03245ee937d3dc552edc13fe7ab0a860eec/wgpu-hal/src/auxil/mod.rs#L7
                ui.label("Vendor");
                ui.label(format!("0x{vendor:04X}"));
                ui.end_row();
            }
            if *device != 0 {
                ui.label("Device");
                ui.label(format!("0x{device:02X}"));
                ui.end_row();
            }
        });
    };

    let wgpu_adapter_ui = |ui: &mut egui::Ui, adapter: &eframe::wgpu::Adapter| {
        let info = &adapter.get_info();
        // TODO(wgpu#5170): Use std::fmt::Display for backend.
        ui.label(info.backend.to_str()).on_hover_ui(|ui| {
            wgpu_adapter_details_ui(ui, adapter);
        });
    };

    egui::Grid::new("wgpu_info").num_columns(2).show(ui, |ui| {
        ui.label("Rendering backend:");
        wgpu_adapter_ui(ui, &render_state.adapter);
        ui.end_row();

        #[cfg(not(target_arch = "wasm32"))]
        if 1 < render_state.available_adapters.len() {
            ui.label("Other rendering backends:");
            ui.vertical(|ui| {
                for adapter in &*render_state.available_adapters {
                    if adapter.get_info() != render_state.adapter.get_info() {
                        wgpu_adapter_ui(ui, adapter);
                    }
                }
            });
            ui.end_row();
        }
    });
}

/// Adapter switching UI.
// Only implemented for web so far. For native it's less well defined since the application may be
// embedded in another application that reads arguments differently.
#[cfg(target_arch = "wasm32")]
fn backend_menu_ui(
    command_sender: &re_viewer_context::CommandSender,
    ui: &mut egui::Ui,
    frame: &eframe::Frame,
) {
    if let Some(backend) = frame
        .wgpu_render_state()
        .map(|state| state.adapter.get_info().backend)
    {
        if backend == wgpu::Backend::BrowserWebGpu {
            UICommand::RestartWithWebGl.menu_button_ui(ui, command_sender);
        } else {
            UICommand::RestartWithWebGpu.menu_button_ui(ui, command_sender);
        }
    }
}

#[cfg(debug_assertions)]
fn egui_debug_options_ui(ui: &mut egui::Ui) {
    use re_ui::UiExt as _;

    let mut debug = ui.style().debug;
    let mut any_clicked = false;

    any_clicked |= ui
        .re_checkbox(&mut debug.debug_on_hover, "Ui debug on hover")
        .on_hover_text("However over widgets to see their rectangles")
        .changed();
    any_clicked |= ui
        .re_checkbox(&mut debug.show_expand_width, "Show expand width")
        .on_hover_text("Show which widgets make their parent wider")
        .changed();
    any_clicked |= ui
        .re_checkbox(&mut debug.show_expand_height, "Show expand height")
        .on_hover_text("Show which widgets make their parent higher")
        .changed();
    any_clicked |= ui
        .re_checkbox(&mut debug.show_resize, "Show resize")
        .changed();
    any_clicked |= ui
        .re_checkbox(
            &mut debug.show_interactive_widgets,
            "Show interactive widgets",
        )
        .on_hover_text("Show an overlay on all interactive widgets")
        .changed();

    if any_clicked {
        let mut style = (*ui.ctx().style()).clone();
        style.debug = debug;
        ui.ctx().set_style(style);
    }
}

#[cfg(debug_assertions)]
use re_viewer_context::CommandSender;

#[cfg(debug_assertions)]
fn debug_menu_options_ui(
    ui: &mut egui::Ui,
    app_options: &mut re_viewer_context::AppOptions,
    command_sender: &CommandSender,
) {
    use re_ui::UiExt as _;

    #[cfg(not(target_arch = "wasm32"))]
    {
        if ui.button("Mobile size").clicked() {
            // let size = egui::vec2(375.0, 812.0); // iPhone 12 mini
            let size = egui::vec2(375.0, 667.0); //  iPhone SE 2nd gen
            ui.ctx()
                .send_viewport_cmd(egui::ViewportCommand::Fullscreen(false));
            ui.ctx()
                .send_viewport_cmd(egui::ViewportCommand::InnerSize(size));
            ui.close_menu();
        }
    }

    if ui.button("Log something at INFO level").clicked() {
        re_log::info!("Logging some info");
    }

    UICommand::ToggleBlueprintInspectionPanel.menu_button_ui(ui, command_sender);

    ui.horizontal(|ui| {
        ui.label("Blueprint GC:");
        ui.re_radio_value(&mut app_options.blueprint_gc, true, "Enabled");
        ui.re_radio_value(&mut app_options.blueprint_gc, false, "Disabled");
    });

    ui.re_checkbox(
        &mut app_options.show_picking_debug_overlay,
        "Picking Debug Overlay",
    )
    .on_hover_text(
        "Show a debug overlay that renders the picking layer information using the \
        `debug_overlay.wgsl` shader.",
    );

    ui.menu_button("Crash", |ui| {
        #[allow(clippy::manual_assert)]
        if ui.button("panic!").clicked() {
            panic!("Intentional panic");
        }

        if ui.button("panic! during unwind").clicked() {
            struct PanicOnDrop {}

            impl Drop for PanicOnDrop {
                fn drop(&mut self) {
                    panic!("Second intentional panic in Drop::drop");
                }
            }

            let _this_will_panic_when_dropped = PanicOnDrop {};
            panic!("First intentional panic");
        }

        if ui.button("SEGFAULT").clicked() {
            // Taken from https://github.com/EmbarkStudios/crash-handling/blob/065f3dd9c1c318630e539375165cf74961b44bcc/sadness-generator/src/lib.rs

            /// This is the fixed address used to generate a segfault. It's possible that
            /// this address can be mapped and writable by the your process in which case a
            /// crash may not occur
            #[cfg(target_pointer_width = "64")]
            pub const SEGFAULT_ADDRESS: u64 = u32::MAX as u64 + 0x42;
            #[cfg(target_pointer_width = "32")]
            pub const SEGFAULT_ADDRESS: u32 = 0x42;

            let bad_ptr: *mut u8 = SEGFAULT_ADDRESS as _;
            #[allow(unsafe_code)]
            // SAFETY: this is not safe. We are _trying_ to crash.
            unsafe {
                std::ptr::write_volatile(bad_ptr, 1);
            }
        }

        if ui.button("Stack overflow").clicked() {
            // Taken from https://github.com/EmbarkStudios/crash-handling/blob/065f3dd9c1c318630e539375165cf74961b44bcc/sadness-generator/src/lib.rs
            fn recurse(data: u64) -> u64 {
                let mut buff = [0u8; 256];
                buff[..9].copy_from_slice(b"junk data");

                let mut result = data;
                for c in buff {
                    result += c as u64;
                }

                if result == 0 {
                    result
                } else {
                    recurse(result) + 1
                }
            }

            recurse(42);
        }
    });
}