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
use egui::NumExt as _;

use re_entity_db::TimesPerTimeline;
use re_log_types::TimeType;
use re_ui::{list_item, UiExt as _};

use re_viewer_context::{Looping, PlayState, TimeControl};

#[derive(serde::Deserialize, serde::Serialize, Default)]
pub struct TimeControlUi;

impl TimeControlUi {
    #[allow(clippy::unused_self)]
    pub fn timeline_selector_ui(
        &mut self,
        time_control: &mut TimeControl,
        times_per_timeline: &TimesPerTimeline,
        ui: &mut egui::Ui,
    ) {
        time_control.select_a_valid_timeline(times_per_timeline);

        ui.scope(|ui| {
            ui.spacing_mut().button_padding += egui::Vec2::new(2.0, 0.0);
            ui.visuals_mut().widgets.active.expansion = 0.0;
            ui.visuals_mut().widgets.hovered.expansion = 0.0;
            ui.visuals_mut().widgets.open.expansion = 0.0;

            egui::ComboBox::from_id_salt("timeline")
                .selected_text(time_control.timeline().name().as_str())
                .show_ui(ui, |ui| {
                    for timeline in times_per_timeline.timelines() {
                        if ui
                            .selectable_label(
                                timeline == time_control.timeline(),
                                timeline.name().as_str(),
                            )
                            .clicked()
                        {
                            time_control.set_timeline(*timeline);
                        }
                    }
                })
                .response
                .on_hover_ui(|ui| {
                    list_item::list_item_scope(ui, "tooltip", |ui| {
                        ui.markdown_ui(
                            r"
Select timeline.

Each piece of logged data is associated with one or more timelines.

The logging SDK always creates two timelines for you:
* `log_tick` - a sequence timeline with the sequence number of the log call
* `log_time` - a temporal timeline with the time of the log call

You can also define your own timelines, e.g. for sensor time or camera frame number.
"
                            .trim(),
                        );

                        ui.re_hyperlink(
                            "Full documentation",
                            "https://rerun.io/docs/concepts/timelines",
                        );
                    });
                })
        });
    }

    #[allow(clippy::unused_self)]
    pub fn fps_ui(&mut self, time_control: &mut TimeControl, ui: &mut egui::Ui) {
        if time_control.time_type() == TimeType::Sequence {
            if let Some(mut fps) = time_control.fps() {
                ui.scope(|ui| {
                    ui.spacing_mut().interact_size -= egui::Vec2::new(0., 4.);

                    ui.add(
                        egui::DragValue::new(&mut fps)
                            .suffix(" FPS")
                            .speed(1)
                            .range(0.0..=f32::INFINITY),
                    )
                    .on_hover_text("Frames per second");
                });
                time_control.set_fps(fps);
            }
        }
    }

    pub fn play_pause_ui(
        &mut self,
        time_control: &mut TimeControl,
        times_per_timeline: &TimesPerTimeline,
        ui: &mut egui::Ui,
    ) {
        ui.horizontal(|ui| {
            ui.spacing_mut().item_spacing.x = 5.0; // from figma
            self.play_button_ui(time_control, ui, times_per_timeline);
            self.follow_button_ui(time_control, ui, times_per_timeline);
            self.pause_button_ui(time_control, ui);
            self.step_time_button_ui(time_control, ui, times_per_timeline);
            self.loop_button_ui(time_control, ui);
        });
    }

    #[allow(clippy::unused_self)]
    fn play_button_ui(
        &mut self,
        time_control: &mut TimeControl,
        ui: &mut egui::Ui,
        times_per_timeline: &TimesPerTimeline,
    ) {
        let is_playing = time_control.play_state() == PlayState::Playing;
        if ui
            .large_button_selected(&re_ui::icons::PLAY, is_playing)
            .on_hover_text(format!("Play.{}", toggle_playback_text(ui.ctx())))
            .clicked()
        {
            time_control.set_play_state(times_per_timeline, PlayState::Playing);
        }
    }

    #[allow(clippy::unused_self)]
    fn follow_button_ui(
        &mut self,
        time_control: &mut TimeControl,
        ui: &mut egui::Ui,
        times_per_timeline: &TimesPerTimeline,
    ) {
        let is_following = time_control.play_state() == PlayState::Following;
        if ui
            .large_button_selected(&re_ui::icons::FOLLOW, is_following)
            .on_hover_text(format!(
                "Follow latest data.{}",
                toggle_playback_text(ui.ctx())
            ))
            .clicked()
        {
            time_control.set_play_state(times_per_timeline, PlayState::Following);
        }
    }

    #[allow(clippy::unused_self)]
    fn pause_button_ui(&mut self, time_control: &mut TimeControl, ui: &mut egui::Ui) {
        let is_paused = time_control.play_state() == PlayState::Paused;
        if ui
            .large_button_selected(&re_ui::icons::PAUSE, is_paused)
            .on_hover_text(format!("Pause.{}", toggle_playback_text(ui.ctx())))
            .clicked()
        {
            time_control.pause();
        }
    }

    #[allow(clippy::unused_self)]
    fn step_time_button_ui(
        &mut self,
        time_control: &mut TimeControl,
        ui: &mut egui::Ui,
        times_per_timeline: &TimesPerTimeline,
    ) {
        if ui
            .large_button(&re_ui::icons::ARROW_LEFT)
            .on_hover_text("Step back to previous time with any new data (left arrow)")
            .clicked()
        {
            time_control.step_time_back(times_per_timeline);
        }

        if ui
            .large_button(&re_ui::icons::ARROW_RIGHT)
            .on_hover_text("Step forwards to next time with any new data (right arrow)")
            .clicked()
        {
            time_control.step_time_fwd(times_per_timeline);
        }
    }

    #[allow(clippy::unused_self)]
    fn loop_button_ui(&mut self, time_control: &mut TimeControl, ui: &mut egui::Ui) {
        let icon = &re_ui::icons::LOOP;

        ui.scope(|ui| {
            // Loop-button cycles between states:
            match time_control.looping() {
                Looping::Off => {
                    if ui
                        .large_button_selected(icon, false)
                        .on_hover_text("Looping is off")
                        .clicked()
                    {
                        time_control.set_looping(Looping::All);
                    }
                }
                Looping::All => {
                    ui.visuals_mut().selection.bg_fill =
                        re_ui::DesignTokens::loop_everything_color();
                    if ui
                        .large_button_selected(icon, true)
                        .on_hover_text("Looping entire recording")
                        .clicked()
                    {
                        time_control.set_looping(Looping::Selection);
                    }
                }
                Looping::Selection => {
                    // ui.visuals_mut().selection.bg_fill = re_ui::ReUi::loop_selection_color(); // we have one color for the button, and a slightly different shade of it for the actual selection :/
                    #[allow(clippy::collapsible_else_if)]
                    if ui
                        .large_button_selected(icon, true)
                        .on_hover_text("Looping selection")
                        .clicked()
                    {
                        time_control.set_looping(Looping::Off);
                    }
                }
            }
        });
    }

    #[allow(clippy::unused_self)]
    pub fn playback_speed_ui(&mut self, time_control: &mut TimeControl, ui: &mut egui::Ui) {
        let mut speed = time_control.speed();
        let drag_speed = (speed * 0.02).at_least(0.01);
        ui.scope(|ui| {
            ui.spacing_mut().interact_size -= egui::Vec2::new(0., 4.);
            ui.add(
                egui::DragValue::new(&mut speed)
                    .speed(drag_speed)
                    .suffix("x"),
            )
            .on_hover_text("Playback speed");
        });

        time_control.set_speed(speed);
    }
}

fn toggle_playback_text(egui_ctx: &egui::Context) -> String {
    if let Some(shortcut) = re_ui::UICommand::PlaybackTogglePlayPause.kb_shortcut() {
        format!(" Toggle with {}", egui_ctx.format_shortcut(&shortcut))
    } else {
        Default::default()
    }
}