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
use std::ops::RangeInclusive;

use re_chunk_store::external::re_chunk::ComponentName;
use re_chunk_store::ChunkStore;
use re_log_types::{EntityPath, ResolvedTimeRange, TimeInt, TimeType, TimeZone, Timeline};
use re_ui::UiExt;
use re_viewer_context::TimeDragValue;

#[derive(Debug)]
pub(crate) enum ChunkListQueryMode {
    LatestAt(TimeInt),
    Range(ResolvedTimeRange),
}

#[derive(Debug, Default)]
pub(crate) enum ChunkListMode {
    #[default]
    /// Show all chunks.
    All,

    /// Only show chunks that are relevant to a given latest-at or range query.
    Query {
        timeline: Timeline,
        entity_path: EntityPath,
        component_name: ComponentName,
        query: ChunkListQueryMode,
    },
}

impl ChunkListMode {
    pub(crate) fn ui(
        &mut self,
        ui: &mut egui::Ui,
        chunk_store: &ChunkStore,
        time_zone: TimeZone,
    ) -> Option<()> {
        let all_timelines = chunk_store.all_timelines();
        let all_entities = chunk_store.all_entities();
        let all_components = chunk_store.all_components();

        let current_timeline = match self {
            Self::All => all_timelines.first().copied()?,
            Self::Query { timeline, .. } => *timeline,
        };
        let current_entity = match self {
            Self::All => all_entities.first().cloned()?,
            Self::Query { entity_path, .. } => entity_path.clone(),
        };
        let current_component = match self {
            Self::All => all_components.first().copied()?,
            Self::Query { component_name, .. } => *component_name,
        };

        ui.horizontal(|ui| {
            ui.selectable_toggle(|ui| {
                if ui
                    .selectable_label(matches!(self, Self::All), "All")
                    .on_hover_text("Display all chunks")
                    .clicked()
                {
                    *self = Self::All;
                }

                if ui
                    .selectable_label(
                        matches!(
                            self,
                            Self::Query {
                                query: ChunkListQueryMode::LatestAt(..),
                                ..
                            }
                        ),
                        "Latest-at",
                    )
                    .on_hover_text("Display chunks relevant to the provided latest-at query")
                    .clicked()
                {
                    *self = Self::Query {
                        timeline: current_timeline,
                        entity_path: current_entity.clone(),
                        component_name: current_component,
                        query: ChunkListQueryMode::LatestAt(TimeInt::MAX),
                    };
                }

                if ui
                    .selectable_label(
                        matches!(
                            self,
                            Self::Query {
                                query: ChunkListQueryMode::Range(..),
                                ..
                            }
                        ),
                        "Range",
                    )
                    .on_hover_text("Display chunks relevant to the provided range query")
                    .clicked()
                {
                    *self = Self::Query {
                        timeline: current_timeline,
                        query: ChunkListQueryMode::Range(ResolvedTimeRange::EVERYTHING),
                        entity_path: current_entity.clone(),
                        component_name: current_component,
                    };
                }
            });

            let Self::Query {
                timeline: query_timeline,
                component_name: query_component,
                entity_path: query_entity,
                query,
            } = self
            else {
                // No query, we're done here
                return;
            };

            ui.horizontal(|ui| {
                ui.label("timeline:");
                egui::ComboBox::new("timeline", "")
                    .selected_text(current_timeline.name().as_str())
                    .show_ui(ui, |ui| {
                        for timeline in all_timelines {
                            if ui.button(timeline.name().as_str()).clicked() {
                                *query_timeline = timeline;
                            }
                        }
                    });

                ui.label("entity:");
                egui::ComboBox::new("entity_path", "")
                    .selected_text(current_entity.to_string())
                    .show_ui(ui, |ui| {
                        for entpath in all_entities {
                            if ui.button(entpath.to_string()).clicked() {
                                *query_entity = entpath.clone();
                            }
                        }
                    });

                ui.label("component:");
                //TODO(ab): this should be a text edit with auto-complete (like view origin)
                egui::ComboBox::new("component_name", "")
                    .selected_text(current_component.short_name())
                    .height(500.0)
                    .show_ui(ui, |ui| {
                        for component_name in all_components {
                            if ui.button(component_name.short_name()).clicked() {
                                *query_component = component_name;
                            }
                        }
                    });
            });

            let time_drag_value = if let Some(time_range) = chunk_store.time_range(query_timeline) {
                TimeDragValue::from_time_range(RangeInclusive::new(
                    time_range.min().as_i64(),
                    time_range.max().as_i64(),
                ))
            } else {
                TimeDragValue::from_time_range(0..=0)
            };
            let time_typ = query_timeline.typ();

            match query {
                ChunkListQueryMode::LatestAt(time) => {
                    ui.label("at:");
                    match time_typ {
                        TimeType::Time => {
                            time_drag_value.temporal_drag_value_ui(ui, time, true, None, time_zone);
                        }
                        TimeType::Sequence => {
                            time_drag_value.sequence_drag_value_ui(ui, time, true, None);
                        }
                    };
                }
                ChunkListQueryMode::Range(range) => {
                    let (mut min, mut max) = (range.min(), range.max());
                    ui.label("from:");
                    match time_typ {
                        TimeType::Time => {
                            time_drag_value
                                .temporal_drag_value_ui(ui, &mut min, true, None, time_zone);
                            ui.label("to:");
                            time_drag_value.temporal_drag_value_ui(
                                ui,
                                &mut max,
                                true,
                                Some(min),
                                time_zone,
                            );
                        }
                        TimeType::Sequence => {
                            time_drag_value.sequence_drag_value_ui(ui, &mut min, true, None);
                            ui.label("to:");
                            time_drag_value.sequence_drag_value_ui(ui, &mut max, true, Some(min));
                        }
                    };
                    range.set_min(min);
                    range.set_max(max);
                }
            }
        });

        Some(())
    }
}