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
use egui::RichText;

use re_ui::{UICommand, UiExt as _};
use re_viewer_context::{Item, ItemCollection, SelectionHistory};
use re_viewport_blueprint::ViewportBlueprint;

// ---

#[derive(Default, serde::Deserialize, serde::Serialize)]
#[serde(default)]
pub struct SelectionHistoryUi {}

impl SelectionHistoryUi {
    pub(crate) fn selection_ui(
        &mut self,
        ui: &mut egui::Ui,
        blueprint: &ViewportBlueprint,
        history: &mut SelectionHistory,
    ) -> Option<ItemCollection> {
        let next = self.next_button_ui(ui, blueprint, history);
        let prev = self.prev_button_ui(ui, blueprint, history);
        prev.or(next)
    }

    fn prev_button_ui(
        &mut self,
        ui: &mut egui::Ui,
        blueprint: &ViewportBlueprint,
        history: &mut SelectionHistory,
    ) -> Option<ItemCollection> {
        // undo selection
        if let Some(previous) = history.previous() {
            let response = ui
                .small_icon_button(&re_ui::icons::ARROW_LEFT)
                .on_hover_text(format!(
                    "Go to previous selection{}:\n\
                {}\n\
                \n\
                Right-click for more.",
                    UICommand::SelectionPrevious.format_shortcut_tooltip_suffix(ui.ctx()),
                    selection_to_string(blueprint, &previous.selection),
                ));

            let mut return_current = false;
            response.context_menu(|ui| {
                // undo: newest on top, oldest on bottom
                let cur = history.current;
                for i in (0..history.current).rev() {
                    self.history_item_ui(blueprint, ui, i, history);
                }
                return_current = cur != history.current;
            });
            if return_current {
                return history.current().map(|sel| sel.selection);
            }

            // TODO(cmc): using the keyboard shortcut should highlight the associated
            // button or something (but then again it, it'd make more sense to do that
            // at the egui level rather than specifically here).
            if response.clicked() {
                return history.select_previous();
            }
        } else {
            ui.add_enabled_ui(false, |ui| {
                ui.small_icon_button(&re_ui::icons::ARROW_LEFT)
                    .on_disabled_hover_text("No past selections found");
            });
        }

        None
    }

    fn next_button_ui(
        &mut self,
        ui: &mut egui::Ui,
        blueprint: &ViewportBlueprint,
        history: &mut SelectionHistory,
    ) -> Option<ItemCollection> {
        // redo selection
        if let Some(next) = history.next() {
            let response = ui
                .small_icon_button(&re_ui::icons::ARROW_RIGHT)
                .on_hover_text(format!(
                    "Go to next selection{}:\n\
                {}\n\
                \n\
                Right-click for more.",
                    UICommand::SelectionNext.format_shortcut_tooltip_suffix(ui.ctx()),
                    selection_to_string(blueprint, &next.selection),
                ));

            let mut return_current = false;
            response.context_menu(|ui| {
                // redo: oldest on top, most recent on bottom
                let cur = history.current;
                for i in (history.current + 1)..history.stack.len() {
                    self.history_item_ui(blueprint, ui, i, history);
                }
                return_current = cur != history.current;
            });
            if return_current {
                return history.current().map(|sel| sel.selection);
            }

            // TODO(cmc): using the keyboard shortcut should highlight the associated
            // button or something (but then again it, it'd make more sense to do that
            // at the egui level rather than specifically here).
            if response.clicked() {
                return history.select_next();
            }
        } else {
            ui.add_enabled_ui(false, |ui| {
                ui.small_icon_button(&re_ui::icons::ARROW_RIGHT)
                    .on_disabled_hover_text("No future selections found");
            });
        }

        None
    }

    #[allow(clippy::unused_self)]
    fn history_item_ui(
        &mut self,
        blueprint: &ViewportBlueprint,
        ui: &mut egui::Ui,
        index: usize,
        history: &mut SelectionHistory,
    ) {
        if let Some(sel) = history.stack.get(index) {
            ui.horizontal(|ui| {
                {
                    // borrow checker workaround
                    let sel = selection_to_string(blueprint, sel);
                    if ui
                        .selectable_value(&mut history.current, index, sel)
                        .clicked()
                    {
                        ui.close_menu();
                    }
                }
                if sel.iter_items().count() == 1 {
                    if let Some(item) = sel.iter_items().next() {
                        item_kind_ui(ui, item);
                    }
                }
            });
        }
    }
}

// Different kinds of selections can share the same path in practice! We need to
// differentiate those in the UI to avoid confusion.
fn item_kind_ui(ui: &mut egui::Ui, sel: &Item) {
    ui.weak(RichText::new(format!("({})", sel.kind())));
}

fn selection_to_string(blueprint: &ViewportBlueprint, selection: &ItemCollection) -> String {
    debug_assert!(
        !selection.is_empty(),
        "History should never contain empty selections."
    );
    if selection.len() == 1 {
        if let Some(item) = selection.iter_items().next() {
            item_to_string(blueprint, item)
        } else {
            // All items got removed or weren't there to begin with.
            debug_assert!(
                selection.iter_space_context().next().is_some(),
                "History should never keep selections that have both an empty item & context list."
            );
            "<space context>".to_owned()
        }
    } else if let Some(kind) = selection.are_all_items_same_kind() {
        format!("{}x {}s", selection.len(), kind)
    } else {
        "<multiple selections>".to_owned()
    }
}

fn item_to_string(blueprint: &ViewportBlueprint, item: &Item) -> String {
    match item {
        Item::AppId(app_id) => app_id.to_string(),
        Item::DataSource(data_source) => data_source.to_string(),
        Item::StoreId(store_id) => store_id.to_string(),
        Item::SpaceView(space_view_id) => {
            // TODO(#4678): unnamed space views should have their label formatted accordingly (subdued)
            if let Some(space_view) = blueprint.view(space_view_id) {
                space_view.display_name_or_default().as_ref().to_owned()
            } else {
                "<removed space view>".to_owned()
            }
        }
        Item::InstancePath(instance_path) => instance_path.to_string(),
        Item::DataResult(space_view_id, instance_path) => {
            // TODO(#4678): unnamed space views should have their label formatted accordingly (subdued)
            let space_view_display_name = if let Some(space_view) = blueprint.view(space_view_id) {
                space_view.display_name_or_default().as_ref().to_owned()
            } else {
                "<removed space view>".to_owned()
            };

            format!("{instance_path} in {space_view_display_name}")
        }
        Item::ComponentPath(path) => {
            format!("{}:{}", path.entity_path, path.component_name.short_name(),)
        }
        Item::Container(container_id) => {
            // TODO(#4678): unnamed container should have their label formatted accordingly (subdued)
            if let Some(container) = blueprint.container(container_id) {
                container.display_name_or_default().as_ref().to_owned()
            } else {
                "<removed container>".to_owned()
            }
        }
    }
}