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
//! A simple widget for selecting between, naming and creating new graphs.
use super::head_row::{HeadRowType, head_row};
use std::collections::{HashMap, HashSet};
/// The glyph for the filter-options button (swap if it doesn't render).
const FILTER_GLYPH: &str = "⛭";
/// A widget for selecting between, naming, and creating new graphs.
pub struct GraphSelect<'a> {
id: egui::Id,
registry: &'a dyn crate::Registry,
heads: &'a [gantz_ca::Head],
focused_head: Option<usize>,
base_names: &'a gantz_ca::registry::Names,
/// Demo associations (graph name -> demo name), for the row context menu.
demos: Option<&'a HashMap<String, String>>,
}
#[derive(Clone)]
struct GraphSelectState {
name_filter: String,
/// Whether base (non-demo) graphs are shown.
show_base: bool,
/// Whether demo graphs are shown (including base demos).
show_demo: bool,
}
impl Default for GraphSelectState {
fn default() -> Self {
Self {
name_filter: String::new(),
show_base: false,
show_demo: true,
}
}
}
/// Methods required on the provided graph registry.
pub trait GraphRegistry {
/// All selectable commit addresses.
fn commits(&self) -> Vec<(&gantz_ca::CommitAddr, &gantz_ca::Commit)>;
/// An iterator yielding all name -> CA pairs.
fn names(&self) -> &gantz_ca::registry::Names;
}
/// Commands emitted from the `GraphSelect` widget.
#[derive(Debug, Default)]
pub struct GraphSelectResponse {
/// Indicates the new graph button was clicked.
pub new_graph: bool,
/// Indicates the import button was clicked.
pub import: bool,
/// Indicates the export-all button was clicked.
pub export_all: bool,
/// Click while the focused head is named: replace the focused head with this one.
pub replaced: Option<gantz_ca::Head>,
/// Open this head as a new tab, or focus it if already open.
///
/// Emitted on ctrl+click of a head that is not open, or on a plain click
/// while the focused head is an unnamed commit (so that clicking another
/// head can't silently lose an unnamed graph).
pub opened: Option<gantz_ca::Head>,
/// Ctrl+click on a head that is already open: close this head.
pub closed: Option<gantz_ca::Head>,
/// The name mapping was removed.
pub name_removed: Option<String>,
}
impl GraphSelectResponse {
/// Combine two responses, preferring `Some` values from `other`.
pub fn union(self, other: Self) -> Self {
Self {
new_graph: self.new_graph || other.new_graph,
import: self.import || other.import,
export_all: self.export_all || other.export_all,
replaced: other.replaced.or(self.replaced),
opened: other.opened.or(self.opened),
closed: other.closed.or(self.closed),
name_removed: other.name_removed.or(self.name_removed),
}
}
}
impl std::ops::BitOr for GraphSelectResponse {
type Output = Self;
fn bitor(self, rhs: Self) -> Self::Output {
self.union(rhs)
}
}
impl std::ops::BitOrAssign for GraphSelectResponse {
fn bitor_assign(&mut self, rhs: Self) {
*self = std::mem::take(self).union(rhs);
}
}
impl<'a> GraphSelect<'a> {
pub fn new(
registry: &'a dyn crate::Registry,
heads: &'a [gantz_ca::Head],
base_names: &'a gantz_ca::registry::Names,
) -> Self {
let id = egui::Id::new("gantz-graph-select");
Self {
registry,
heads,
id,
focused_head: None,
base_names,
demos: None,
}
}
pub fn with_id(mut self, id: egui::Id) -> Self {
self.id = id;
self
}
/// Provide demo associations so a graph's row context menu can offer to
/// open its associated demo.
pub fn demos(mut self, demos: Option<&'a HashMap<String, String>>) -> Self {
self.demos = demos;
self
}
/// Set the index of the focused head to show a focus indicator.
pub fn focused_head(mut self, focused_head: usize) -> Self {
self.focused_head = Some(focused_head);
self
}
pub fn show(&mut self, ui: &mut egui::Ui) -> GraphSelectResponse {
// Load any state specific to this widget (e.g. working text strings).
let state_id = self.id.with("state");
let mut state = ui
.memory_mut(|mem| mem.data.get_temp::<GraphSelectState>(state_id))
.unwrap_or_default();
let mut response = GraphSelectResponse::default();
// A name filter text field, with a filter-options button on the right
// that opens a menu of `base`/`demo` visibility checkboxes.
ui.horizontal(|ui| {
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let h = ui.spacing().interact_size.y;
let btn = ui
.add_sized([h, h], egui::Button::new(FILTER_GLYPH))
.on_hover_text("filter options");
egui::Popup::menu(&btn)
.close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
.show(|ui| {
ui.checkbox(&mut state.show_base, "base").on_hover_text(
"show base nodes, the pre-composed nodes provided with gantz",
);
ui.checkbox(&mut state.show_demo, "demo")
.on_hover_text("show demos");
});
// The name filter fills the remaining width.
egui::TextEdit::singleline(&mut state.name_filter)
.desired_width(ui.available_width())
.hint_text("🔎 Name Filter")
.show(ui);
});
});
let names = self.registry.names();
let demos = self.demos;
// Captured by `show_named` to surface each named graph's description and
// input/output docs on hover.
let registry = self.registry;
// List all the graphs, named then unnamed.
egui::ScrollArea::vertical()
// Limit the scroll height to allow for the `+` button below.
.max_height(
ui.available_height() - ui.spacing().interact_size.y - ui.spacing().item_spacing.y,
)
.show(ui, |ui| {
// Partition names into groups:
// 1. User-named, non-demo
// 2. Base-named, non-demo
// 3. All demos (alphabetical, regardless of user/base)
let is_base = |name: &str| self.base_names.contains_key(name);
let is_demo = |name: &str| name.starts_with("demo-");
// Nested graphs (`parent:child`) are hidden from the root list;
// they are reached by navigating into their parent.
let is_nested = |name: &str| name.contains(crate::node::NESTED_SEP);
let matches_filter = |name: &str| {
state.name_filter.is_empty()
|| state
.name_filter
.split_whitespace()
.all(|s| name.contains(s))
};
let mut visited = HashSet::new();
// Helper: show a named graph row, its right-click menu, and
// handle clicks.
let show_named =
|ui: &mut egui::Ui,
name: &str,
ca: &gantz_ca::CommitAddr,
row_type: HeadRowType<'_>,
heads: &[gantz_ca::Head],
focused_head: Option<usize>,
response: &mut GraphSelectResponse| {
let head = gantz_ca::Head::Branch(name.to_string());
let mut res = head_row(heads, &head, row_type, ca, focused_head, ui);
// Show the graph's description + input/output docs on hover.
res.row = res.row.on_hover_ui(|ui| {
// Re-assert wrap width every frame (see `socket_hover`).
let max_width = ui.spacing().tooltip_width;
ui.set_max_width(max_width);
crate::node_info_ui(®istry.command_info(name), ui);
});
// Deletable iff the row offers an `×` (named, non-base).
let deletable = res.delete.is_some();
// The associated demo to offer, if any.
let demo = demos.and_then(|d| d.get(name)).cloned();
res.row.context_menu(|ui| {
if ui.button("open").clicked() {
response.replaced = Some(head.clone());
ui.close();
}
if ui.button("open tab").clicked() {
response.opened = Some(head.clone());
ui.close();
}
if let Some(demo_name) = &demo {
if ui
.button("demo")
.on_hover_text("open the associated demo in a new tab")
.clicked()
{
response.opened =
Some(gantz_ca::Head::Branch(demo_name.clone()));
ui.close();
}
}
if deletable && ui.button("delete").clicked() {
response.name_removed = Some(name.to_string());
ui.close();
}
});
if res.row.clicked() {
click_head(ui, heads, focused_head, head, response);
} else if let Some(delete) = res.delete {
if delete.clicked() {
response.name_removed = Some(name.to_string());
}
}
};
// 1. User-named, non-demo.
for (name, ca) in names
.iter()
.filter(|(n, _)| !is_base(n) && !is_demo(n) && !is_nested(n))
{
if !matches_filter(name) {
continue;
}
visited.insert(*ca);
show_named(
ui,
name,
ca,
HeadRowType::Named(name),
self.heads,
self.focused_head,
&mut response,
);
}
// 2. Base-named, non-demo (hidden when the `base` filter is off).
for (name, ca) in names
.iter()
.filter(|(n, _)| state.show_base && is_base(n) && !is_demo(n) && !is_nested(n))
{
if !matches_filter(name) {
continue;
}
visited.insert(*ca);
show_named(
ui,
name,
ca,
HeadRowType::Base(name),
self.heads,
self.focused_head,
&mut response,
);
}
// 3. All demos, alphabetical, regardless of user/base (hidden
// when the `demo` filter is off; shown even if also a base).
for (name, ca) in names
.iter()
.filter(|(n, _)| state.show_demo && is_demo(n) && !is_nested(n))
{
if !matches_filter(name) {
continue;
}
visited.insert(*ca);
let row_type = if is_base(name) {
HeadRowType::Base(name)
} else {
HeadRowType::Named(name)
};
show_named(
ui,
name,
ca,
row_type,
self.heads,
self.focused_head,
&mut response,
);
}
// Collect commit addresses for open heads (excluding named ones already shown).
let open_head_cas: HashSet<_> = self
.heads
.iter()
.filter_map(|head| match head {
gantz_ca::Head::Branch(_) => None, // Already shown in named section
gantz_ca::Head::Commit(ca) => Some(*ca),
})
.collect();
// Show only unnamed commits that are currently open as heads.
for (ca, commit) in self
.registry
.commits()
.into_iter()
.filter(|(ca, _)| !visited.contains(ca) && open_head_cas.contains(ca))
{
if !state.name_filter.is_empty() {
let ca_str = format!("{ca}");
if !state.name_filter.split(" ").all(|s| ca_str.contains(s)) {
continue;
}
}
// Use the timestamp as a row name.
let head = gantz_ca::Head::Commit(*ca);
let row_type = HeadRowType::Unnamed(&commit.timestamp);
let res = head_row(self.heads, &head, row_type, ca, self.focused_head, ui);
if res.row.clicked() {
click_head(ui, self.heads, self.focused_head, head, &mut response);
}
}
});
ui.horizontal(|ui| {
// Place import and export buttons on the right.
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
if ui
.button("\u{2B07}")
.on_hover_text("export all named graphs")
.clicked()
{
response.export_all = true;
}
if ui
.button("\u{2B06}")
.on_hover_text("import graph(s)")
.clicked()
{
response.import = true;
}
// Fill remaining space with the "+" button.
ui.with_layout(egui::Layout::left_to_right(egui::Align::Center), |ui| {
if ui
.add(egui::Button::new("+").min_size(ui.available_size()))
.on_hover_text("add graph (Ctrl+N)")
.clicked()
{
response.new_graph = true;
}
});
});
});
// Store the modified state back in memory
ui.memory_mut(|mem| mem.data.insert_temp(state_id, state));
response
}
}
/// Update `response` for a click on the row for `head`.
///
/// Ctrl+click toggles the head: closes it if open, otherwise opens it as a new
/// tab. A plain click replaces the focused head, unless the focused head is an
/// unnamed commit, in which case the clicked head is opened as a new tab
/// instead (or focused if already open) so that the unnamed graph isn't lost.
pub(crate) fn click_head(
ui: &egui::Ui,
heads: &[gantz_ca::Head],
focused_head: Option<usize>,
head: gantz_ca::Head,
response: &mut GraphSelectResponse,
) {
let ctrl = ui.input(|i| i.modifiers.ctrl);
if ctrl {
if heads.contains(&head) {
response.closed = Some(head);
} else {
response.opened = Some(head);
}
return;
}
let focused_is_named = focused_head
.and_then(|ix| heads.get(ix))
.is_some_and(|head| matches!(head, gantz_ca::Head::Branch(_)));
if focused_is_named {
response.replaced = Some(head);
} else {
response.opened = Some(head);
}
}