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
use re_mutex::Mutex;
use super::Item;
use crate::command_sender::{SelectionSource, SetSelection};
use crate::{DataResultInteractionAddress, ItemCollection, ItemContext};
/// Selection highlight, sorted from weakest to strongest.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum SelectionHighlight {
/// No selection highlight at all.
#[default]
None,
/// A closely related object is selected, should apply similar highlight to selection.
/// (e.g. data in a different view)
SiblingSelection,
/// Should apply selection highlight (i.e. the exact selection is highlighted).
Selection,
}
/// Hover highlight, sorted from weakest to strongest.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum HoverHighlight {
/// No hover highlight.
#[default]
None,
/// Apply hover highlight, does *not* exclude a selection highlight.
Hovered,
}
/// Combination of selection & hover highlight which can occur independently.
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
pub struct InteractionHighlight {
pub selection: SelectionHighlight,
pub hover: HoverHighlight,
}
impl InteractionHighlight {
/// Picks the stronger selection & hover highlight from two highlight descriptions.
#[inline]
pub fn max(&self, other: Self) -> Self {
Self {
selection: self.selection.max(other.selection),
hover: self.hover.max(other.hover),
}
}
/// Returns true if either selection or hover is active.
pub fn any(&self) -> bool {
self.selection != SelectionHighlight::None || self.hover != HoverHighlight::None
}
}
/// Selection and hover state.
///
/// Both hover and selection are double buffered:
/// Changes from one frame are only visible in the next frame.
#[derive(Default)]
pub struct ApplicationSelectionState {
/// The selected items. Write to this with [`crate::SystemCommand::set_selection`].
selection: ItemCollection,
/// Has selection changed since the previous frame?
///
/// Some if the selection was changed this frame.
selection_changed: Option<SelectionSource>,
/// What objects are hovered? Read from this.
hovered_previous_frame: ItemCollection,
/// What objects are hovered? Write to this.
hovered_this_frame: Mutex<ItemCollection>,
}
pub enum SelectionChange<'a> {
NoChange,
SelectionChanged(&'a ItemCollection),
}
impl ApplicationSelectionState {
/// Called at the start of each frame.
///
/// `resolve_item` decides the fate of each currently selected item: returning `None` drops it,
/// while returning `Some(item)` keeps it — possibly replacing it with a different item (e.g.
/// downgrading a no-longer-valid data result to a plain entity selection).
pub fn on_frame_start(
&mut self,
resolve_item: impl Fn(&Item) -> Option<Item>,
fallback_selection: Option<Item>,
) -> SelectionChange<'_> {
// Use a different name so we don't get a collision in puffin.
re_tracing::profile_scope!("SelectionState::on_frame_start");
// Purge or repair invalid items.
let resolved = ItemCollection::from_items_and_context(
self.selection
.iter()
.filter_map(|(item, ctx)| resolve_item(item).map(|item| (item, ctx.clone()))),
);
if resolved != self.selection {
self.selection_changed = Some(SelectionSource::Other);
self.selection = resolved;
}
// Set to fallback if empty.
if self.selection.is_empty()
&& let Some(fallback_selection) = fallback_selection
{
re_log::trace!("Current selection invalid in this context; switching to fallback");
self.selection = ItemCollection::from(fallback_selection);
}
// Hovering needs to be refreshed every frame: If it wasn't hovered last frame, it's no longer hovered!
self.hovered_previous_frame = std::mem::take(self.hovered_this_frame.get_mut());
if self.selection_changed.is_some() {
SelectionChange::SelectionChanged(&self.selection)
} else {
SelectionChange::NoChange
}
}
pub fn on_frame_end(&mut self) {
self.selection_changed = None;
}
/// Sets several objects to be selected, updating history as needed.
///
/// Clears the selected item context if none was specified.
pub fn set_selection(&mut self, items: impl Into<SetSelection>) {
let SetSelection { selection, source } = items.into();
if selection != self.selection {
self.selection_changed = Some(source);
self.selection = selection;
}
}
/// Returns the current selection.
pub fn selected_items(&self) -> &ItemCollection {
&self.selection
}
/// Returns the currently hovered objects.
pub fn hovered_items(&self) -> &ItemCollection {
&self.hovered_previous_frame
}
/// Set the hovered objects. Will be in [`Self::hovered_items`] on the next frame.
pub fn set_hovered(&self, hovered: impl Into<ItemCollection>) {
*self.hovered_this_frame.lock() = hovered.into();
}
pub fn selection_item_contexts(&self) -> impl Iterator<Item = &ItemContext> {
self.selection.iter_item_context()
}
pub fn hovered_item_context(&self) -> Option<&ItemContext> {
self.hovered_previous_frame.iter_item_context().next()
}
/// Returns Some if the selection changed this frame.
pub fn selection_changed(&self) -> Option<SelectionSource> {
self.selection_changed
}
pub fn highlight_for_ui_element(&self, test: &Item) -> HoverHighlight {
let hovered = self
.hovered_previous_frame
.iter_items()
.any(|current| match current {
Item::AppId(_)
| Item::TableId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::View(_)
| Item::Container(_)
| Item::RedapEntry { .. }
| Item::RedapServer(_) => current == test,
Item::ComponentPath(component_path) => match test {
Item::AppId(_)
| Item::TableId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::View(_)
| Item::Container(_)
| Item::RedapEntry { .. }
| Item::RedapServer(_) => false,
Item::ComponentPath(test_component_path) => {
test_component_path == component_path
}
Item::InstancePath(test_instance_path) => {
!test_instance_path.instance.is_specific()
&& test_instance_path.entity_path == component_path.entity_path
}
Item::DataResult(test_data_result) => {
test_data_result.instance_path.entity_path == component_path.entity_path
}
},
Item::InstancePath(current_instance_path) => match test {
Item::AppId(_)
| Item::TableId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::ComponentPath(_)
| Item::View(_)
| Item::Container(_)
| Item::RedapEntry { .. }
| Item::RedapServer(_) => false,
Item::InstancePath(instance_path)
| Item::DataResult(DataResultInteractionAddress { instance_path, .. }) => {
current_instance_path.entity_path == instance_path.entity_path
&& either_none_or_same(
current_instance_path.instance.specific_index().as_ref(),
instance_path.instance.specific_index().as_ref(),
)
}
},
Item::DataResult(current_data_result) => match test {
Item::AppId(_)
| Item::TableId(_)
| Item::DataSource(_)
| Item::StoreId(_)
| Item::ComponentPath(_)
| Item::View(_)
| Item::Container(_)
| Item::RedapEntry { .. }
| Item::RedapServer(_) => false,
Item::InstancePath(instance_path)
| Item::DataResult(DataResultInteractionAddress { instance_path, .. }) => {
current_data_result.instance_path.entity_path == instance_path.entity_path
&& either_none_or_same(
current_data_result
.instance_path
.instance
.specific_index()
.as_ref(),
instance_path.instance.specific_index().as_ref(),
)
}
},
});
if hovered {
HoverHighlight::Hovered
} else {
HoverHighlight::None
}
}
}
fn either_none_or_same<T: PartialEq>(a: Option<&T>, b: Option<&T>) -> bool {
a.is_none() || b.is_none() || a == b
}