|
| 1 | +use egui::{Id, RichText, Stroke, StrokeKind, Tooltip, Ui, WidgetText}; |
| 2 | + |
| 3 | +use re_format::format_uint; |
| 4 | +use re_ui::UiExt as _; |
| 5 | +use re_ui::list_item::{LabelContent, PropertyContent, list_item_scope}; |
| 6 | +use re_ui::syntax_highlighting::SyntaxHighlightedBuilder; |
| 7 | + |
| 8 | +use crate::datatype_ui::DataTypeUi; |
| 9 | +use crate::show_index::ShowIndex; |
| 10 | + |
| 11 | +enum NodeLabel { |
| 12 | + /// The index to *display*. May be different from the actual index of the value. |
| 13 | + /// |
| 14 | + /// E.g. in a [`arrow::array::ListArray`], this is the index in the child list. The index passed to |
| 15 | + /// [`ArrowNode::show`] is the index in the parent array. |
| 16 | + /// |
| 17 | + /// Also see [`crate::show_index::list_ui`] for a more thorough explanation. |
| 18 | + Index(usize), |
| 19 | + Name(String), |
| 20 | + Custom(WidgetText), |
| 21 | +} |
| 22 | + |
| 23 | +/// Display an item of an Arrow array in a list item with some label. |
| 24 | +pub struct ArrowNode<'a> { |
| 25 | + label: NodeLabel, |
| 26 | + values: &'a dyn ShowIndex, |
| 27 | +} |
| 28 | + |
| 29 | +impl<'a> ArrowNode<'a> { |
| 30 | + /// Create a new [`ArrowNode`] with a custom label |
| 31 | + pub fn custom(name: impl Into<WidgetText>, values: &'a dyn ShowIndex) -> Self { |
| 32 | + Self { |
| 33 | + label: NodeLabel::Custom(name.into()), |
| 34 | + values, |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Create a new [`ArrowNode`] from an Arrow field. |
| 39 | + /// |
| 40 | + /// This will set the name to the fields name. |
| 41 | + pub fn field(field: &'a arrow::datatypes::Field, values: &'a dyn ShowIndex) -> Self { |
| 42 | + Self { |
| 43 | + label: NodeLabel::Name(field.name().to_owned()), |
| 44 | + values, |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// The index to *display* (See [`NodeLabel::Index`]). |
| 49 | + pub fn index(idx: usize, values: &'a dyn ShowIndex) -> Self { |
| 50 | + Self { |
| 51 | + label: NodeLabel::Index(idx), |
| 52 | + values, |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + /// Index is the index of the *value* to display. |
| 57 | + /// |
| 58 | + /// Can be different from [`ArrowNode::index`] (the index to display) e.g. in a sliced array. |
| 59 | + /// See also [`NodeLabel::Index`]. |
| 60 | + pub fn show(self, ui: &mut Ui, index: usize) { |
| 61 | + let label = match self.label { |
| 62 | + NodeLabel::Index(idx) => { |
| 63 | + let mut builder = SyntaxHighlightedBuilder::new(ui.style()); |
| 64 | + builder.code_index(&format_uint(idx)); |
| 65 | + builder.into_widget_text() |
| 66 | + } |
| 67 | + NodeLabel::Name(name) => { |
| 68 | + let mut builder = SyntaxHighlightedBuilder::new(ui.style()); |
| 69 | + builder.code_identifier(&name); |
| 70 | + builder.into_widget_text() |
| 71 | + } |
| 72 | + NodeLabel::Custom(name) => name, |
| 73 | + }; |
| 74 | + |
| 75 | + let mut value = SyntaxHighlightedBuilder::new(ui.style()); |
| 76 | + let result = self.values.write(index, &mut value); |
| 77 | + let value = match result { |
| 78 | + Ok(()) => value.into_widget_text(), |
| 79 | + Err(err) => RichText::new(format!("Error: {err}")) |
| 80 | + .color(ui.tokens().error_fg_color) |
| 81 | + .into(), |
| 82 | + }; |
| 83 | + |
| 84 | + let nested = self.values.is_item_nested(); |
| 85 | + let data_type = self.values.array().data_type(); |
| 86 | + let data_type_ui = DataTypeUi::new(data_type); |
| 87 | + |
| 88 | + let item = ui.list_item(); |
| 89 | + // We *don't* use index for the ID, since it might change across timesteps, |
| 90 | + // while referring the same logical data. |
| 91 | + let id = ui.id().with(label.text()); |
| 92 | + let content = PropertyContent::new(label) |
| 93 | + .value_fn(|ui, visuals| { |
| 94 | + ui.horizontal(|ui| { |
| 95 | + egui::Sides::new().shrink_left().show( |
| 96 | + ui, |
| 97 | + |ui| { |
| 98 | + if visuals.is_collapsible() && visuals.openness() != 0.0 { |
| 99 | + if visuals.openness() == 1.0 { |
| 100 | + return; |
| 101 | + } |
| 102 | + ui.set_opacity(1.0 - visuals.openness()); |
| 103 | + } |
| 104 | + ui.label(value); |
| 105 | + }, |
| 106 | + |ui| { |
| 107 | + let tooltip_open = |
| 108 | + Tooltip::was_tooltip_open_last_frame(ui.ctx(), ui.next_auto_id()); |
| 109 | + // Keep showing the data type when the tooltip is open, so the |
| 110 | + // user can interact with it. |
| 111 | + if visuals.hovered || tooltip_open { |
| 112 | + // TODO(lucas): We should show the nullability here too |
| 113 | + let response = |
| 114 | + ui.small(RichText::new(&data_type_ui.type_name).strong()); |
| 115 | + ui.painter().rect_stroke( |
| 116 | + response.rect.expand(2.0), |
| 117 | + 4.0, |
| 118 | + Stroke::new(1.0, visuals.text_color()), |
| 119 | + StrokeKind::Middle, |
| 120 | + ); |
| 121 | + |
| 122 | + if let Some(content) = data_type_ui.content { |
| 123 | + response.on_hover_ui(|ui| { |
| 124 | + list_item_scope( |
| 125 | + ui, |
| 126 | + Id::new("arrow data type hover"), |
| 127 | + |ui| { |
| 128 | + ui.list_item().show_hierarchical_with_children( |
| 129 | + ui, |
| 130 | + Id::new("arrow data type hover item"), |
| 131 | + true, |
| 132 | + LabelContent::new(data_type_ui.type_name), |
| 133 | + content, |
| 134 | + ); |
| 135 | + }, |
| 136 | + ); |
| 137 | + }); |
| 138 | + } |
| 139 | + } |
| 140 | + }, |
| 141 | + ); |
| 142 | + }); |
| 143 | + }) |
| 144 | + .show_only_when_collapsed(false); |
| 145 | + |
| 146 | + if nested { |
| 147 | + item.show_hierarchical_with_children(ui, id, false, content, |ui| { |
| 148 | + list_item_scope(ui, id.with("child_scope"), |ui| { |
| 149 | + self.values.show(index, ui); |
| 150 | + }); |
| 151 | + }); |
| 152 | + } else { |
| 153 | + item.show_hierarchical(ui, content); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments