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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
//! Basic ui elements & interaction for most `re_viewer_context::Item`.
//!
//! TODO(andreas): This is not a `data_ui`, can this go somewhere else, shouldn't be in `re_data_ui`.

use re_entity_db::{EntityTree, InstancePath};
use re_format::format_uint;
use re_log_types::{ApplicationId, ComponentPath, EntityPath, TimeInt, Timeline};
use re_ui::{icons, list_item, SyntaxHighlighting, UiExt as _};
use re_viewer_context::{HoverHighlight, Item, SpaceViewId, UiLayout, ViewerContext};

use super::DataUi;

// TODO(andreas): This is where we want to go, but we need to figure out how get the [`re_viewer_context::SpaceViewClass`] from the `SpaceViewId`.
// Simply pass in optional icons?
//
// Show a button to an [`Item`] with a given text.
// pub fn item_button_to(
//     ctx: &ViewerContext<'_>,
//     ui: &mut egui::Ui,
//     item: &Item,
//     text: impl Into<egui::WidgetText>,
// ) -> egui::Response {
//     match item {
//         Item::ComponentPath(component_path) => {
//             component_path_button_to(ctx, ui, text, component_path)
//         }
//         Item::SpaceView(space_view_id) => {
//             space_view_button_to(ctx, ui, text, *space_view_id, space_view_category)
//         }
//         Item::InstancePath(space_view_id, instance_path) => {
//             instance_path_button_to(ctx, ui, *space_view_id, instance_path, text)
//         }
//     }
// }

/// Show an entity path and make it selectable.
pub fn entity_path_button(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    entity_path: &EntityPath,
) -> egui::Response {
    instance_path_button_to(
        ctx,
        query,
        db,
        ui,
        space_view_id,
        &InstancePath::entity_all(entity_path.clone()),
        entity_path.syntax_highlighted(ui.style()),
    )
}

/// Show the different parts of an entity path and make them selectable.
pub fn entity_path_parts_buttons(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    entity_path: &EntityPath,
) -> egui::Response {
    let with_icon = false; // too much noise with icons in a path

    ui.horizontal(|ui| {
        ui.spacing_mut().item_spacing.x = 2.0;

        // Show one single icon up-front instead:
        let instance_path = InstancePath::entity_all(entity_path.clone());
        ui.add(instance_path_icon(&query.timeline(), db, &instance_path).as_image());

        let mut accumulated = Vec::new();
        for part in entity_path.iter() {
            accumulated.push(part.clone());

            ui.strong("/");
            instance_path_button_to_ex(
                ctx,
                query,
                db,
                ui,
                space_view_id,
                &InstancePath::entity_all(accumulated.clone().into()),
                part.syntax_highlighted(ui.style()),
                with_icon,
            );
        }
    })
    .response
}

/// Show an entity path that is part of the blueprint and make it selectable.
///
/// Like [`entity_path_button_to`] but with the apriori knowledge that this exists in the blueprint.
pub fn blueprint_entity_path_button_to(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    entity_path: &EntityPath,
    text: impl Into<egui::WidgetText>,
) -> egui::Response {
    // If we're targeting an entity in the blueprint store,
    // it doesn't make much sense to specify the space view id since space view ids are
    // embedded in entity paths of the blueprint store.
    // I.e. if there is a view relationship that we would care about, we would know that from the path!
    let space_view_id = None;

    entity_path_button_to(
        ctx,
        ctx.blueprint_query,
        ctx.blueprint_db(),
        ui,
        space_view_id,
        entity_path,
        text,
    )
}

/// Show an entity path and make it selectable.
pub fn entity_path_button_to(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    entity_path: &EntityPath,
    text: impl Into<egui::WidgetText>,
) -> egui::Response {
    instance_path_button_to(
        ctx,
        query,
        db,
        ui,
        space_view_id,
        &InstancePath::entity_all(entity_path.clone()),
        text,
    )
}

/// Show an instance id and make it selectable.
pub fn instance_path_button(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    instance_path: &InstancePath,
) -> egui::Response {
    instance_path_button_to(
        ctx,
        query,
        db,
        ui,
        space_view_id,
        instance_path,
        instance_path.syntax_highlighted(ui.style()),
    )
}

/// Return the instance path icon.
///
/// The choice of icon is based on whether the instance is "empty" as in hasn't any logged component
/// _on the current timeline_.
pub fn instance_path_icon(
    timeline: &re_chunk_store::Timeline,
    db: &re_entity_db::EntityDb,
    instance_path: &InstancePath,
) -> &'static icons::Icon {
    if instance_path.is_all() {
        // It is an entity path
        if db
            .storage_engine()
            .store()
            .all_components_on_timeline(timeline, &instance_path.entity_path)
            .is_some()
        {
            &icons::ENTITY
        } else {
            &icons::ENTITY_EMPTY
        }
    } else {
        // An instance path
        &icons::ENTITY
    }
}

/// The current time query, based on the current time control and an `entity_path`
///
/// If the user is inspecting the blueprint, and the `entity_path` is on the blueprint
/// timeline, then use the blueprint. Otherwise, use the recording.
// TODO(jleibs): Ideally this wouldn't be necessary and we could make the assessment
// directly from the entity_path.
pub fn guess_query_and_db_for_selected_entity<'a>(
    ctx: &'a ViewerContext<'_>,
    entity_path: &EntityPath,
) -> (re_chunk_store::LatestAtQuery, &'a re_entity_db::EntityDb) {
    if ctx.app_options.inspect_blueprint_timeline
        && ctx.store_context.blueprint.is_logged_entity(entity_path)
    {
        (
            ctx.blueprint_cfg.time_ctrl.read().current_query(),
            ctx.store_context.blueprint,
        )
    } else {
        (
            ctx.rec_cfg.time_ctrl.read().current_query(),
            ctx.recording(),
        )
    }
}

pub fn guess_instance_path_icon(
    ctx: &ViewerContext<'_>,
    instance_path: &InstancePath,
) -> &'static icons::Icon {
    let (query, db) = guess_query_and_db_for_selected_entity(ctx, &instance_path.entity_path);
    instance_path_icon(&query.timeline(), db, instance_path)
}

/// Show an instance id and make it selectable.
pub fn instance_path_button_to(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    instance_path: &InstancePath,
    text: impl Into<egui::WidgetText>,
) -> egui::Response {
    instance_path_button_to_ex(ctx, query, db, ui, space_view_id, instance_path, text, true)
}

/// Show an instance id and make it selectable.
#[allow(clippy::too_many_arguments)]
fn instance_path_button_to_ex(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    instance_path: &InstancePath,
    text: impl Into<egui::WidgetText>,
    with_icon: bool,
) -> egui::Response {
    let item = if let Some(space_view_id) = space_view_id {
        Item::DataResult(space_view_id, instance_path.clone())
    } else {
        Item::InstancePath(instance_path.clone())
    };

    let response = if with_icon {
        ui.selectable_label_with_icon(
            instance_path_icon(&query.timeline(), db, instance_path),
            text,
            ctx.selection().contains_item(&item),
            re_ui::LabelStyle::Normal,
        )
    } else {
        ui.selectable_label(ctx.selection().contains_item(&item), text)
    };

    let response = response.on_hover_ui(|ui| {
        let include_subtree = false;
        instance_hover_card_ui(ui, ctx, query, db, instance_path, include_subtree);
    });

    cursor_interact_with_selectable(ctx, response, item)
}

/// Show the different parts of an instance path and make them selectable.
pub fn instance_path_parts_buttons(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    space_view_id: Option<SpaceViewId>,
    instance_path: &InstancePath,
) -> egui::Response {
    let with_icon = false; // too much noise with icons in a path

    ui.horizontal(|ui| {
        ui.spacing_mut().item_spacing.x = 2.0;

        // Show one single icon up-front instead:
        ui.add(instance_path_icon(&query.timeline(), db, instance_path).as_image());

        let mut accumulated = Vec::new();
        for part in instance_path.entity_path.iter() {
            accumulated.push(part.clone());

            ui.strong("/");
            instance_path_button_to_ex(
                ctx,
                query,
                db,
                ui,
                space_view_id,
                &InstancePath::entity_all(accumulated.clone().into()),
                part.syntax_highlighted(ui.style()),
                with_icon,
            );
        }

        if !instance_path.instance.is_all() {
            ui.weak("[");
            instance_path_button_to_ex(
                ctx,
                query,
                db,
                ui,
                space_view_id,
                instance_path,
                instance_path.instance.syntax_highlighted(ui.style()),
                with_icon,
            );
            ui.weak("]");
        }
    })
    .response
}

/// If `include_subtree=true`, stats for the entire entity subtree will be shown.
fn entity_tree_stats_ui(
    ui: &mut egui::Ui,
    timeline: &Timeline,
    db: &re_entity_db::EntityDb,
    tree: &EntityTree,
    include_subtree: bool,
) {
    use re_format::format_bytes;

    let subtree_caveat = if tree.children.is_empty() {
        ""
    } else if include_subtree {
        " (including subtree)"
    } else {
        " (excluding subtree)"
    };

    let engine = db.storage_engine();

    let (static_stats, timeline_stats) = if include_subtree {
        (
            db.subtree_stats_static(&engine, &tree.path),
            db.subtree_stats_on_timeline(&engine, &tree.path, timeline),
        )
    } else {
        (
            engine.store().entity_stats_static(&tree.path),
            engine
                .store()
                .entity_stats_on_timeline(&tree.path, timeline),
        )
    };

    let total_stats = static_stats + timeline_stats;

    if total_stats.num_rows == 0 {
        return;
    } else if timeline_stats.num_rows == 0 {
        ui.label(format!(
            "{} static rows{subtree_caveat}",
            format_uint(total_stats.num_rows)
        ));
    } else if static_stats.num_rows == 0 {
        ui.label(format!(
            "{} rows on timeline '{timeline}'{subtree_caveat}",
            format_uint(total_stats.num_rows),
            timeline = timeline.name()
        ));
    } else {
        ui.label(format!(
            "{} rows = {} static + {} on timeline '{timeline}'{subtree_caveat}",
            format_uint(total_stats.num_rows),
            format_uint(static_stats.num_rows),
            format_uint(timeline_stats.num_rows),
            timeline = timeline.name()
        ));
    }

    let num_temporal_rows = timeline_stats.num_rows;

    let mut data_rate = None;

    if 0 < timeline_stats.total_size_bytes && 1 < num_temporal_rows {
        // Try to estimate data-rate:
        if let Some(time_range) = engine.store().entity_time_range(timeline, &tree.path) {
            let min_time = time_range.min();
            let max_time = time_range.max();
            if min_time < max_time {
                // Let's do our best to avoid fencepost errors.
                // If we log 1 MiB once every second, then after three
                // events we have a span of 2 seconds, and 3 MiB,
                // but the data rate is still 1 MiB/s.
                //
                //          <-----2 sec----->
                // t:       0s      1s      2s
                // data:   1MiB    1MiB    1MiB

                let duration = max_time.as_f64() - min_time.as_f64();

                let mut bytes_per_time = timeline_stats.total_size_bytes as f64 / duration;

                // Fencepost adjustment:
                bytes_per_time *= (num_temporal_rows - 1) as f64 / num_temporal_rows as f64;

                data_rate = Some(match timeline.typ() {
                    re_log_types::TimeType::Time => {
                        let bytes_per_second = 1e9 * bytes_per_time;

                        format!(
                            "{}/s in '{}'",
                            format_bytes(bytes_per_second),
                            timeline.name()
                        )
                    }

                    re_log_types::TimeType::Sequence => {
                        format!("{} / {}", format_bytes(bytes_per_time), timeline.name())
                    }
                });
            }
        }
    }

    if let Some(data_rate) = data_rate {
        ui.label(format!(
            "Using ~{}{subtree_caveat} ≈ {}",
            format_bytes(total_stats.total_size_bytes as f64),
            data_rate
        ));
    } else {
        ui.label(format!(
            "Using ~{}{subtree_caveat}",
            format_bytes(total_stats.total_size_bytes as f64)
        ));
    }
}

/// Show a component path and make it selectable.
pub fn component_path_button(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    component_path: &ComponentPath,
    db: &re_entity_db::EntityDb,
) -> egui::Response {
    component_path_button_to(
        ctx,
        ui,
        component_path.component_name.short_name(),
        component_path,
        db,
    )
}

/// Show a component path and make it selectable.
pub fn component_path_button_to(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    text: impl Into<egui::WidgetText>,
    component_path: &ComponentPath,
    db: &re_entity_db::EntityDb,
) -> egui::Response {
    let item = Item::ComponentPath(component_path.clone());
    let is_static = db.storage_engine().store().entity_has_static_component(
        component_path.entity_path(),
        component_path.component_name(),
    );
    let icon = if is_static {
        &icons::COMPONENT_STATIC
    } else {
        &icons::COMPONENT_TEMPORAL
    };
    let response = ui.selectable_label_with_icon(
        icon,
        text,
        ctx.selection().contains_item(&item),
        re_ui::LabelStyle::Normal,
    );

    let response = response.on_hover_ui(|ui| {
        ui.style_mut().wrap_mode = Some(egui::TextWrapMode::Extend); // Make tooltip as wide as needed

        list_item::list_item_scope(ui, "component_path_tooltip", |ui| {
            ui.list_item().interactive(false).show_flat(
                ui,
                list_item::LabelContent::new(if is_static {
                    "Static component"
                } else {
                    "Temporal component"
                })
                .with_icon(icon),
            );

            component_path
                .component_name
                .data_ui_recording(ctx, ui, UiLayout::Tooltip);
        });
    });

    cursor_interact_with_selectable(ctx, response, item)
}

pub fn data_blueprint_button_to(
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    ui: &mut egui::Ui,
    text: impl Into<egui::WidgetText>,
    space_view_id: SpaceViewId,
    entity_path: &EntityPath,
) -> egui::Response {
    let item = Item::DataResult(space_view_id, InstancePath::entity_all(entity_path.clone()));
    let response = ui
        .selectable_label(ctx.selection().contains_item(&item), text)
        .on_hover_ui(|ui| {
            let include_subtree = false;
            entity_hover_card_ui(ui, ctx, query, db, entity_path, include_subtree);
        });
    cursor_interact_with_selectable(ctx, response, item)
}

pub fn time_button(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    timeline: &Timeline,
    value: TimeInt,
) -> egui::Response {
    let is_selected = ctx
        .rec_cfg
        .time_ctrl
        .read()
        .is_time_selected(timeline, value);

    let response = ui.selectable_label(
        is_selected,
        timeline.typ().format(value, ctx.app_options.time_zone),
    );
    if response.clicked() {
        ctx.rec_cfg
            .time_ctrl
            .write()
            .set_timeline_and_time(*timeline, value);
        ctx.rec_cfg.time_ctrl.write().pause();
    }
    response
}

pub fn timeline_button(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    timeline: &Timeline,
) -> egui::Response {
    timeline_button_to(ctx, ui, timeline.name().to_string(), timeline)
}

pub fn timeline_button_to(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    text: impl Into<egui::WidgetText>,
    timeline: &Timeline,
) -> egui::Response {
    let is_selected = ctx.rec_cfg.time_ctrl.read().timeline() == timeline;

    let response = ui
        .selectable_label(is_selected, text)
        .on_hover_text("Click to switch to this timeline");
    if response.clicked() {
        let mut time_ctrl = ctx.rec_cfg.time_ctrl.write();
        time_ctrl.set_timeline(*timeline);
        time_ctrl.pause();
    }
    response
}

// TODO(andreas): Move elsewhere, this is not directly part of the item_ui.
pub fn cursor_interact_with_selectable(
    ctx: &ViewerContext<'_>,
    response: egui::Response,
    item: Item,
) -> egui::Response {
    let is_item_hovered =
        ctx.selection_state().highlight_for_ui_element(&item) == HoverHighlight::Hovered;

    ctx.select_hovered_on_click(&response, item);
    // TODO(andreas): How to deal with shift click for selecting ranges?

    if is_item_hovered {
        response.highlight()
    } else {
        response
    }
}

/// Displays the "hover card" (i.e. big tooltip) for an instance or an entity.
///
/// The entity hover card is displayed if the provided instance path doesn't refer to a specific
/// instance.
///
/// If `include_subtree=true`, stats for the entire entity subtree will be shown.
pub fn instance_hover_card_ui(
    ui: &mut egui::Ui,
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    instance_path: &InstancePath,
    include_subtree: bool,
) {
    if !ctx.recording().is_known_entity(&instance_path.entity_path) {
        ui.label("Unknown entity.");
        return;
    }

    ui.horizontal(|ui| {
        let subtype_string = if instance_path.instance.is_all() {
            "Entity"
        } else {
            "Entity instance"
        };
        ui.strong(subtype_string);
        ui.label(instance_path.syntax_highlighted(ui.style()));
    });

    // TODO(emilk): give data_ui an alternate "everything on this timeline" query?
    // Then we can move the size view into `data_ui`.

    if instance_path.instance.is_all() {
        if let Some(subtree) = ctx.recording().tree().subtree(&instance_path.entity_path) {
            entity_tree_stats_ui(ui, &query.timeline(), db, subtree, include_subtree);
        }
    } else {
        // TODO(emilk): per-component stats
    }

    instance_path.data_ui(ctx, ui, UiLayout::Tooltip, query, db);
}

/// Displays the "hover card" (i.e. big tooltip) for an entity.
///
/// If `include_subtree=true`, stats for the entire entity subtree will be shown.
pub fn entity_hover_card_ui(
    ui: &mut egui::Ui,
    ctx: &ViewerContext<'_>,
    query: &re_chunk_store::LatestAtQuery,
    db: &re_entity_db::EntityDb,
    entity_path: &EntityPath,
    include_subtree: bool,
) {
    let instance_path = InstancePath::entity_all(entity_path.clone());
    instance_hover_card_ui(ui, ctx, query, db, &instance_path, include_subtree);
}

pub fn app_id_button_ui(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    app_id: &ApplicationId,
) -> egui::Response {
    let item = Item::AppId(app_id.clone());

    let response = ui.selectable_label_with_icon(
        &icons::APPLICATION,
        app_id.to_string(),
        ctx.selection().contains_item(&item),
        re_ui::LabelStyle::Normal,
    );

    let response = response.on_hover_ui(|ui| {
        app_id.data_ui_recording(ctx, ui, re_viewer_context::UiLayout::Tooltip);
    });

    cursor_interact_with_selectable(ctx, response, item)
}

pub fn data_source_button_ui(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    data_source: &re_smart_channel::SmartChannelSource,
) -> egui::Response {
    let item = Item::DataSource(data_source.clone());

    let response = ui.selectable_label_with_icon(
        &icons::DATA_SOURCE,
        data_source.to_string(),
        ctx.selection().contains_item(&item),
        re_ui::LabelStyle::Normal,
    );

    let response = response.on_hover_ui(|ui| {
        data_source.data_ui_recording(ctx, ui, re_viewer_context::UiLayout::Tooltip);
    });

    cursor_interact_with_selectable(ctx, response, item)
}

/// This uses [`list_item::ListItem::show_hierarchical`], meaning it comes with built-in
/// indentation.
pub fn store_id_button_ui(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    store_id: &re_log_types::StoreId,
) {
    if let Some(entity_db) = ctx.store_context.bundle.get(store_id) {
        entity_db_button_ui(ctx, ui, entity_db, true);
    } else {
        ui.label(store_id.to_string());
    }
}

/// Show button for a store (recording or blueprint).
///
/// You can set `include_app_id` to hide the App Id, but usually you want to show it.
///
/// This uses [`list_item::ListItem::show_hierarchical`], meaning it comes with built-in
/// indentation.
pub fn entity_db_button_ui(
    ctx: &ViewerContext<'_>,
    ui: &mut egui::Ui,
    entity_db: &re_entity_db::EntityDb,
    include_app_id: bool,
) {
    use re_types_core::SizeBytes as _;
    use re_viewer_context::{SystemCommand, SystemCommandSender as _};

    let app_id_prefix = if include_app_id {
        entity_db
            .app_id()
            .map_or(String::default(), |app_id| format!("{app_id} - "))
    } else {
        String::default()
    };

    let creation_time = entity_db
        .store_info()
        .and_then(|info| {
            info.started
                .format_time_custom("[hour]:[minute]:[second]", ctx.app_options.time_zone)
        })
        .unwrap_or("<unknown time>".to_owned());

    let size = re_format::format_bytes(entity_db.total_size_bytes() as _);
    let title = format!("{app_id_prefix}{creation_time} - {size}");

    let store_id = entity_db.store_id().clone();
    let item = re_viewer_context::Item::StoreId(store_id.clone());

    let icon = match entity_db.store_kind() {
        re_log_types::StoreKind::Recording => &icons::RECORDING,
        re_log_types::StoreKind::Blueprint => &icons::BLUEPRINT,
    };

    let item_content = list_item::LabelContent::new(title)
        .with_icon_fn(|ui, rect, visuals| {
            // Color icon based on whether this is the active recording or not:
            let color = if ctx.store_context.is_active(&store_id) {
                visuals.fg_stroke.color
            } else {
                ui.visuals().widgets.noninteractive.fg_stroke.color
            };
            icon.as_image().tint(color).paint_at(ui, rect);
        })
        .with_buttons(|ui| {
            // Close-button:
            let resp = ui
                .small_icon_button(&icons::REMOVE)
                .on_hover_text(match store_id.kind {
                    re_log_types::StoreKind::Recording => {
                        "Close this recording (unsaved data will be lost)"
                    }
                    re_log_types::StoreKind::Blueprint => {
                        "Close this blueprint (unsaved data will be lost)"
                    }
                });
            if resp.clicked() {
                ctx.command_sender
                    .send_system(SystemCommand::CloseStore(store_id.clone()));
            }
            resp
        });

    let mut list_item = ui
        .list_item()
        .selected(ctx.selection().contains_item(&item));

    if ctx.hovered().contains_item(&item) {
        list_item = list_item.force_hovered(true);
    }

    let response = list_item::list_item_scope(ui, "entity db button", |ui| {
        list_item
            .show_hierarchical(ui, item_content)
            .on_hover_ui(|ui| {
                entity_db.data_ui(
                    ctx,
                    ui,
                    re_viewer_context::UiLayout::Tooltip,
                    &ctx.current_query(),
                    entity_db,
                );
            })
    });

    if response.hovered() {
        ctx.selection_state().set_hovered(item.clone());
    }

    if response.clicked() {
        // When we click on a recording, we directly activate it. This is safe to do because
        // it's non-destructive and recordings are immutable. Switching back is easy.
        // We don't do the same thing for blueprints as swapping them can be much more disruptive.
        // It is much less obvious how to undo a blueprint switch and what happened to your original
        // blueprint.
        // TODO(jleibs): We should still have an `Activate this Blueprint` button in the selection panel
        // for the blueprint.
        if store_id.kind == re_log_types::StoreKind::Recording {
            ctx.command_sender
                .send_system(SystemCommand::ActivateRecording(store_id.clone()));
        }

        ctx.command_sender
            .send_system(SystemCommand::SetSelection(item));
    }
}