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
use egui::{Key, KeyboardShortcut, Modifiers};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, strum_macros::EnumIter)]
pub enum Command {
#[cfg(not(target_arch = "wasm32"))]
Open,
#[cfg(not(target_arch = "wasm32"))]
Save,
#[cfg(not(target_arch = "wasm32"))]
SaveSelection,
#[cfg(not(target_arch = "wasm32"))]
Quit,
ResetViewer,
#[cfg(not(target_arch = "wasm32"))]
OpenProfiler,
ToggleMemoryPanel,
ToggleBlueprintPanel,
ToggleSelectionPanel,
ToggleTimePanel,
#[cfg(not(target_arch = "wasm32"))]
ToggleFullscreen,
SelectionPrevious,
SelectionNext,
ToggleCommandPalette,
PlaybackTogglePlayPause,
PlaybackStepBack,
PlaybackStepForward,
}
impl Command {
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 {
#[cfg(not(target_arch = "wasm32"))]
Command::Save => ("Save…", "Save all data to a Rerun data file (.rrd)"),
#[cfg(not(target_arch = "wasm32"))]
Command::SaveSelection => (
"Save loop selection…",
"Save data for the current loop selection to a Rerun data file (.rrd)",
),
#[cfg(not(target_arch = "wasm32"))]
Command::Open => ("Open…", "Open a Rerun Data File (.rrd)"),
#[cfg(not(target_arch = "wasm32"))]
Command::Quit => ("Quit", "Close the Rerun Viewer"),
Command::ResetViewer => (
"Reset viewer",
"Reset the viewer to how it looked the first time you ran it",
),
#[cfg(not(target_arch = "wasm32"))]
Command::OpenProfiler => (
"Open profiler",
"Starts a profiler, showing what makes the viewer run slow",
),
Command::ToggleMemoryPanel => (
"Toggle memory panel",
"Investigate what is using up RAM in Rerun Viewer",
),
Command::ToggleBlueprintPanel => ("Toggle blueprint panel", "Toggle the left panel"),
Command::ToggleSelectionPanel => ("Toggle selection panel", "Toggle the right panel"),
Command::ToggleTimePanel => ("Toggle time panel", "Toggle the bottom time panel"),
#[cfg(not(target_arch = "wasm32"))]
Command::ToggleFullscreen => (
"Toggle fullscreen",
"Toggle between windowed and fullscreen viewer",
),
Command::SelectionPrevious => ("Previous selection", "Go to previous selection"),
Command::SelectionNext => ("Next selection", "Go to next selection"),
Command::ToggleCommandPalette => {
("Command palette…", "Toggle the command palette window")
}
Command::PlaybackTogglePlayPause => {
("Toggle play/pause", "Either play or pause the time")
}
Command::PlaybackStepBack => (
"Step time back",
"Move the time marker back to the previous point in time with any data",
),
Command::PlaybackStepForward => (
"Step time forward",
"Move the time marker to the next point in time with any data",
),
}
}
pub fn kb_shortcut(self) -> Option<KeyboardShortcut> {
fn key(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::NONE, key)
}
fn cmd(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::COMMAND, key)
}
#[cfg(not(target_arch = "wasm32"))]
fn cmd_shift(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::COMMAND.plus(Modifiers::SHIFT), key)
}
fn ctrl_shift(key: Key) -> KeyboardShortcut {
KeyboardShortcut::new(Modifiers::CTRL.plus(Modifiers::SHIFT), key)
}
match self {
#[cfg(not(target_arch = "wasm32"))]
Command::Save => Some(cmd(Key::S)),
#[cfg(not(target_arch = "wasm32"))]
Command::SaveSelection => Some(cmd_shift(Key::S)),
#[cfg(not(target_arch = "wasm32"))]
Command::Open => Some(cmd(Key::O)),
#[cfg(all(not(target_arch = "wasm32"), target_os = "windows"))]
Command::Quit => Some(KeyboardShortcut::new(Modifiers::ALT, Key::F4)),
#[cfg(all(not(target_arch = "wasm32"), not(target_os = "windows")))]
Command::Quit => Some(cmd(Key::Q)),
Command::ResetViewer => Some(ctrl_shift(Key::R)),
#[cfg(not(target_arch = "wasm32"))]
Command::OpenProfiler => Some(ctrl_shift(Key::P)),
Command::ToggleMemoryPanel => Some(ctrl_shift(Key::M)),
Command::ToggleBlueprintPanel => Some(ctrl_shift(Key::B)),
Command::ToggleSelectionPanel => Some(ctrl_shift(Key::S)),
Command::ToggleTimePanel => Some(ctrl_shift(Key::T)),
#[cfg(not(target_arch = "wasm32"))]
Command::ToggleFullscreen => Some(key(Key::F11)),
Command::SelectionPrevious => Some(ctrl_shift(Key::ArrowLeft)),
Command::SelectionNext => Some(ctrl_shift(Key::ArrowRight)),
Command::ToggleCommandPalette => Some(cmd(Key::P)),
Command::PlaybackTogglePlayPause => Some(key(Key::Space)),
Command::PlaybackStepBack => Some(key(Key::ArrowLeft)),
Command::PlaybackStepForward => Some(key(Key::ArrowRight)),
}
}
#[must_use = "Returns the Command that was triggered by some keyboard shortcut"]
pub fn listen_for_kb_shortcut(egui_ctx: &egui::Context) -> Option<Command> {
use strum::IntoEnumIterator as _;
let anything_has_focus = egui_ctx.memory(|mem| mem.focus().is_some());
if anything_has_focus {
return None; }
egui_ctx.input_mut(|input| {
for command in Command::iter() {
if let Some(kb_shortcut) = command.kb_shortcut() {
if input.consume_shortcut(&kb_shortcut) {
return Some(command);
}
}
}
None
})
}
pub fn menu_button_ui(
self,
ui: &mut egui::Ui,
pending_commands: &mut Vec<Command>,
) -> egui::Response {
let button = self.menu_button(ui.ctx());
let response = ui.add(button).on_hover_text(self.tooltip());
if response.clicked() {
pending_commands.push(self);
ui.close_menu();
}
response
}
pub fn menu_button(self, egui_ctx: &egui::Context) -> egui::Button {
let mut button = egui::Button::new(self.text());
if let Some(shortcut) = self.kb_shortcut() {
button = button.shortcut_text(egui_ctx.format_shortcut(&shortcut));
}
button
}
pub fn format_shortcut_tooltip_suffix(self, egui_ctx: &egui::Context) -> String {
if let Some(kb_shortcut) = self.kb_shortcut() {
format!(" ({})", egui_ctx.format_shortcut(&kb_shortcut))
} else {
Default::default()
}
}
}