gantz_egui 0.6.1

UI traits and widgets that make up the GUI for gantz, an environment for creative systems.
Documentation
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
//! A simple widget for selecting between, naming and creating new graphs.

use super::head_row::{HeadRowType, head_row};
use gantz_ca::Name;
use std::collections::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 crate::Env<'a>,
    heads: &'a [gantz_ca::Head],
    focused_head: Option<usize>,
    base_names: &'a crate::reg::Names,
    /// Collaborative-session display state, when a collab layer is wired:
    /// enables the join button and the per-row session dots.
    collab: Option<&'a crate::collab::CollabUiState>,
    /// A host-provided clipboard reader, for the join popup's right-click
    /// paste (egui alone cannot read the clipboard).
    clipboard: Option<&'a dyn Fn() -> Option<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,
        }
    }
}

/// 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<Name>,
    /// A session invite ticket was submitted via the join popup.
    pub join_ticket: 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),
            join_ticket: other.join_ticket.or(self.join_ticket),
        }
    }
}

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 crate::Env<'a>,
        heads: &'a [gantz_ca::Head],
        base_names: &'a crate::reg::Names,
    ) -> Self {
        let id = egui::Id::new("gantz-graph-select");
        Self {
            registry,
            heads,
            id,
            focused_head: None,
            base_names,
            collab: None,
            clipboard: None,
        }
    }

    pub fn with_id(mut self, id: egui::Id) -> Self {
        self.id = id;
        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
    }

    /// Provide the collaborative-session display state. Without this call
    /// (no networking layer wired), the join button and per-row session
    /// dots are hidden.
    pub fn collab(mut self, collab: Option<&'a crate::collab::CollabUiState>) -> Self {
        self.collab = collab;
        self
    }

    /// Provide a clipboard reader for the join popup's right-click paste.
    /// Without it the paste menu item is hidden (Ctrl+V keeps working
    /// through egui's own event path).
    pub fn clipboard(mut self, clipboard: Option<&'a dyn Fn() -> Option<String>>) -> Self {
        self.clipboard = clipboard;
        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 = crate::reg::names(self.registry.registry);
        // 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: &Name| self.base_names.contains_key(name);
                let is_demo = self::is_demo;
                // Nested graphs (`parent:child`) are hidden from the root list;
                // they are reached by navigating into their parent.
                let is_nested = |name: &Name| name.is_nested();
                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();
                let collab = self.collab;

                // Helper: show a named graph row, its right-click menu, and
                // handle clicks.
                let show_named =
                    |ui: &mut egui::Ui,
                     name: &Name,
                     ca: &gantz_ca::CommitAddr,
                     base: bool,
                     heads: &[gantz_ca::Head],
                     focused_head: Option<usize>,
                     response: &mut GraphSelectResponse| {
                        let name_str = name.to_string();
                        let row_type = if base {
                            HeadRowType::Base(&name_str)
                        } else {
                            HeadRowType::Named(&name_str)
                        };
                        let head = gantz_ca::Head::Branch(name.clone());
                        // A live session's dot beside the name (mirroring the
                        // graph tabs) - sessions outlive their tabs, so this
                        // is where a closed head's session stays visible.
                        let status = collab
                            .and_then(|c| c.sessions.get(name))
                            .map(|d| (d.conn.color(), d.hover_text().into()));
                        let mut res =
                            head_row(heads, &head, row_type, ca, focused_head, status, 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(&registry.command_info(&name_str), 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 = registry.demo_graph(&name_str);
                        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.parse().expect("infallible"),
                                    ));
                                    ui.close();
                                }
                            }
                            if deletable && ui.button("delete").clicked() {
                                response.name_removed = Some(name.clone());
                                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.clone());
                            }
                        }
                    };

                // 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.to_string()) {
                        continue;
                    }
                    visited.insert(*ca);
                    show_named(
                        ui,
                        name,
                        ca,
                        false,
                        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.to_string()) {
                        continue;
                    }
                    visited.insert(*ca);
                    show_named(
                        ui,
                        name,
                        ca,
                        true,
                        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.to_string()) {
                        continue;
                    }
                    visited.insert(*ca);
                    show_named(
                        ui,
                        name,
                        ca,
                        is_base(name),
                        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 commits_by_recency(self.registry.registry)
                    .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, None, 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;
                }
                // Join a session from an invite ticket (only when a collab
                // layer is wired).
                if self.collab.is_some() {
                    let btn = ui
                        .button("\u{1F310} join")
                        .on_hover_text("join a session from an invite ticket");
                    let ticket_id = self.id.with("join_ticket");
                    let clipboard = self.clipboard;
                    egui::Popup::menu(&btn)
                        .close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside)
                        .show(|ui| {
                            let mut ticket = ui
                                .data(|d| d.get_temp::<String>(ticket_id))
                                .unwrap_or_default();
                            ui.horizontal(|ui| {
                                let edit = ui.add(
                                    egui::TextEdit::singleline(&mut ticket)
                                        .hint_text("paste an invite ticket"),
                                );
                                // Right-click paste: pasting is how this field
                                // is nearly always filled. egui alone cannot
                                // read the clipboard, so the affordance needs
                                // a host-provided reader (Ctrl+V works through
                                // egui's event path regardless).
                                if let Some(read) = clipboard {
                                    edit.context_menu(|ui| {
                                        if ui.button("paste").clicked() {
                                            if let Some(text) = read() {
                                                ticket = text.trim().to_string();
                                            }
                                            ui.close();
                                        }
                                    });
                                }
                                let ready = !ticket.trim().is_empty();
                                if ui
                                    .add_enabled(ready, egui::Button::new("connect"))
                                    .clicked()
                                {
                                    response.join_ticket = Some(ticket.trim().to_string());
                                    ticket.clear();
                                    ui.close();
                                }
                            });
                            ui.data_mut(|d| d.insert_temp(ticket_id, ticket));
                        });
                }
                // 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
    }
}

/// All commits in the registry, sorted newest to oldest.
///
/// The head-listing widgets (graph select, history view) share this ordering
/// for their unnamed-commit rows.
pub fn commits_by_recency(
    reg: &gantz_ca::Registry,
) -> Vec<(&gantz_ca::CommitAddr, &gantz_ca::Commit)> {
    let mut commits: Vec<_> = reg.commits().iter().collect();
    commits.sort_by(|(_, a), (_, b)| b.timestamp.cmp(&a.timestamp));
    commits
}

/// Whether the name follows the `demo-*` naming convention for demo graphs.
pub(crate) fn is_demo(name: &Name) -> bool {
    name.segments()
        .first()
        .is_some_and(|s| s.starts_with("demo-"))
}

/// 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);
    }
}