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
//! A widget for configuring per-head graph layout settings and renaming.
use super::gantz::OpenHeadState;
use super::head_name_edit::{head_name, head_name_edit};
/// Per-head graph configuration widget.
///
/// Provides a name-editing text field, one-shot `auto-layout`/`center view`
/// buttons, and the per-head layout flow direction. The non-flow layout
/// parameters live globally in `Settings > Global`.
pub struct GraphConfig<'a> {
head: &'a gantz_ca::Head,
head_state: &'a mut OpenHeadState,
names: &'a [(gantz_ca::Name, gantz_ca::CommitAddr)],
is_base: bool,
immutable: bool,
demo_names: &'a [&'a str],
current_demo: Option<&'a str>,
base_sources: &'a [&'a str],
current_base_source: Option<&'a str>,
current_description: Option<&'a str>,
env: Option<(&'a crate::Env<'a>, &'a mut gantz_ca::merge::Resolutions)>,
collab: Option<Option<&'a crate::collab::SessionDisplay>>,
}
/// Response from the [`GraphConfig`] widget.
pub struct GraphConfigResponse {
/// A new branch name was committed via the name editor.
pub new_branch: Option<(gantz_ca::Head, String)>,
/// The "Export" button was clicked.
pub export: bool,
/// Demo graph association changed: `Some(Some(name))` = set, `Some(None)` = clear.
pub demo_changed: Option<Option<String>>,
/// The "Reset" button was clicked for a base graph.
pub reset_base_graph: bool,
/// The graph's description was edited (committed on focus loss). An empty
/// string clears the description.
pub description_changed: Option<String>,
/// A merge candidate was chosen from the merge row.
pub merge: Option<crate::MergeHead>,
/// The graph's base source association changed (see
/// [`base_sources`][GraphConfig::base_sources]).
pub base_source_changed: Option<String>,
/// The "share" button was clicked in the collab row.
pub share: bool,
/// The "stop" (sharing) button was clicked in the collab row.
pub stop_sharing: bool,
}
impl<'a> GraphConfig<'a> {
pub fn new(
head: &'a gantz_ca::Head,
head_state: &'a mut OpenHeadState,
names: &'a [(gantz_ca::Name, gantz_ca::CommitAddr)],
) -> Self {
Self {
head,
head_state,
names,
is_base: false,
immutable: false,
demo_names: &[],
current_demo: None,
base_sources: &[],
current_base_source: None,
current_description: None,
env: None,
collab: None,
}
}
/// Whether this graph is a base node - a pre-composed graph that ships
/// with the binary and is reset to its original form on every launch.
/// Users who want to customize a base node should duplicate it under a
/// new name.
pub fn is_base(mut self, is_base: bool) -> Self {
self.is_base = is_base;
self
}
/// Whether this graph is immutable - layout controls will be disabled.
pub fn immutable(mut self, immutable: bool) -> Self {
self.immutable = immutable;
self
}
/// Available demo graph names for the dropdown.
pub fn demo_names(mut self, demo_names: &'a [&'a str]) -> Self {
self.demo_names = demo_names;
self
}
/// The current demo graph association for this graph, if any.
pub fn current_demo(mut self, current_demo: Option<&'a str>) -> Self {
self.current_demo = current_demo;
self
}
/// The graph's current description, used to seed the description editor.
pub fn current_description(mut self, current_description: Option<&'a str>) -> Self {
self.current_description = current_description;
self
}
/// The available base sources plus this graph's current one, shown as a
/// "source" dropdown selecting which base file the graph belongs to.
/// Without these (the default), the row is hidden - only base-authoring
/// hosts like `update-base` supply them, since an association change is
/// only durable where the per-source write-back runs.
pub fn base_sources(mut self, base_sources: &'a [&'a str], current: Option<&'a str>) -> Self {
self.base_sources = base_sources;
self.current_base_source = current;
self
}
/// Registry access for the merge row's candidates and previews, plus the
/// conflict-resolution strategy its "⛭" menu edits. Without these, the
/// merge row is hidden.
pub fn merge_env(
mut self,
env: &'a crate::Env<'a>,
resolutions: &'a mut gantz_ca::merge::Resolutions,
) -> Self {
self.env = Some((env, resolutions));
self
}
/// The head's collaborative-session state: `None` when the graph is not
/// currently shared. Without this call (no networking layer wired), the
/// collab row is hidden.
pub fn collab(mut self, session: Option<&'a crate::collab::SessionDisplay>) -> Self {
self.collab = Some(session);
self
}
pub fn show(self, ui: &mut egui::Ui) -> GraphConfigResponse {
let is_named = matches!(self.head, gantz_ca::Head::Branch(_));
let is_demo = matches!(
&self.head,
gantz_ca::Head::Branch(name) if super::graph_select::is_demo(name)
);
// Per-head temp state for the name editor.
let edit_id = egui::Id::new("graph_config_name_edit").with(self.head);
let mut name = ui
.memory_mut(|m| m.data.get_temp::<String>(edit_id))
.unwrap_or_else(|| head_name(self.head));
// Per-head temp state for the description editor (named graphs only).
// The edit is committed on focus loss to avoid a commit per keypress.
let desc_id = egui::Id::new("graph_config_desc_edit").with(self.head);
let mut desc = is_named.then(|| {
let current = self.current_description.unwrap_or("");
ui.memory_mut(|m| m.data.get_temp::<String>(desc_id))
.unwrap_or_else(|| current.to_string())
});
// Outputs collected from within the grid.
let mut new_branch = None;
let mut description_changed = None;
let mut demo_changed = None;
let mut base_source_changed = None;
let mut reset_base_graph = false;
let mut export = false;
let mut merge = None;
let mut share = false;
let mut stop_sharing = false;
// Reserve room for the label column so the value column's text fields
// don't expand to fill the entire pane.
let control_w = (ui.available_width() - 64.0).max(64.0);
egui::Grid::new(egui::Id::new("graph_config_grid").with(self.head))
.num_columns(2)
.spacing([8.0, 6.0])
.striped(true)
.show(ui, |ui| {
// name
ui.label("name");
new_branch = ui
.scope(|ui| {
ui.set_max_width(control_w);
head_name_edit(self.head, &mut name, self.names, ui)
})
.inner
.new_branch;
ui.end_row();
// desc.
if let Some(desc) = desc.as_mut() {
ui.label("desc.");
let current = self.current_description.unwrap_or("");
// Multiline + word-wrap; the grid row auto-fits its height
// to the (wrapped) text, growing from a single row.
let resp = ui.add_enabled(
!self.immutable,
egui::TextEdit::multiline(desc)
.hint_text("Description")
.desired_rows(1)
.desired_width(control_w),
);
if resp.lost_focus() && desc.as_str() != current {
description_changed = Some(desc.clone());
}
ui.end_row();
}
// demo (named, non-demo graphs only)
if is_named && !is_demo && !self.demo_names.is_empty() {
ui.label("demo");
ui.add_enabled_ui(!self.immutable, |ui| {
let selected_text = self.current_demo.unwrap_or("none");
egui::ComboBox::from_id_salt("demo_graph_select")
.selected_text(selected_text)
.show_ui(ui, |ui| {
if ui
.selectable_label(self.current_demo.is_none(), "none")
.clicked()
{
demo_changed = Some(None);
}
for &demo_name in self.demo_names {
if ui
.selectable_label(
self.current_demo == Some(demo_name),
demo_name,
)
.clicked()
{
demo_changed = Some(Some(demo_name.to_string()));
}
}
});
});
ui.end_row();
}
// source (named graphs, when a base-authoring host supplies
// the source list): which base file the graph belongs to.
if is_named && !self.base_sources.is_empty() {
ui.label("source");
ui.add_enabled_ui(!self.immutable, |ui| {
let selected_text = self.current_base_source.unwrap_or("none");
egui::ComboBox::from_id_salt("base_source_select")
.selected_text(selected_text)
.show_ui(ui, |ui| {
for &source in self.base_sources {
if ui
.selectable_label(
self.current_base_source == Some(source),
source,
)
.clicked()
&& self.current_base_source != Some(source)
{
base_source_changed = Some(source.to_string());
}
}
});
});
ui.end_row();
}
// reset (base demo graphs only)
if self.is_base && is_demo {
ui.label("reset");
if ui
.button("Reset")
.on_hover_text("reset demo to initial state")
.clicked()
{
reset_base_graph = true;
}
ui.end_row();
}
// base note
if self.is_base {
ui.label("");
ui.label(
egui::RichText::new("\"base\" node, included with gantz")
.italics()
.weak(),
);
ui.end_row();
}
// layout - center-view and auto-layout side by side. Both are
// one-shot: they apply once when clicked (consumed by the graph
// scene next pass), so hand-arranged nodes are never disturbed.
ui.label("layout");
ui.horizontal(|ui| {
if ui
.button("center view")
.on_hover_text("center the view over the graph")
.clicked()
{
self.head_state.scene.pending_center_view = true;
}
ui.add_enabled_ui(!self.immutable, |ui| {
if ui
.button("auto-layout")
.on_hover_text(
"lay out the selection, or the whole graph when nothing is selected",
)
.clicked()
{
self.head_state.scene.pending_auto_layout = true;
}
});
});
ui.end_row();
// flow
ui.label("flow");
ui.add_enabled_ui(!self.immutable, |ui| {
ui.horizontal(|ui| {
ui.radio_value(
&mut self.head_state.layout_flow,
egui::Direction::LeftToRight,
"Right",
);
ui.radio_value(
&mut self.head_state.layout_flow,
egui::Direction::TopDown,
"Down",
);
});
});
ui.end_row();
// merge (named, mutable, non-base graphs only)
if is_named && !self.immutable && !self.is_base {
if let Some((env, resolutions)) = self.env {
ui.label("merge");
ui.horizontal(|ui| {
merge = merge_select(env, self.head, *resolutions, ui);
ui.menu_button("\u{26ED}", |ui| {
resolutions_menu(resolutions, ui);
})
.response
.on_hover_text("conflict resolution strategy");
});
ui.end_row();
}
}
// collab (named, mutable, non-base graphs only)
if is_named && !self.immutable && !self.is_base {
if let Some(session) = self.collab {
ui.label("collab");
ui.horizontal(|ui| match session {
None => {
share = ui
.button("share")
.on_hover_text(
"share this graph as a live session; \
anyone with the invite can join and edit",
)
.clicked();
}
Some(display) => {
let conn = display.conn;
super::status_dot(ui, conn.color())
.on_hover_text(conn.label());
let n = display.peers.len();
let label = format!(
"{n} peer{}",
if n == 1 { "" } else { "s" }
);
let names: Vec<String> = display
.peers
.iter()
.map(|p| match &p.name {
Some(name) => format!("{name} ({})", p.id),
None => p.id.clone(),
})
.collect();
let resp = ui.label(label);
if !names.is_empty() {
resp.on_hover_text(names.join("\n"));
}
if let Some(ticket) = &display.ticket {
if ui
.button("copy invite")
.on_hover_text(
"copy the invite ticket; others join via \
the \u{1F310} join button in the Graphs pane",
)
.clicked()
{
ui.ctx().copy_text(ticket.clone());
}
}
stop_sharing = ui
.button("stop")
.on_hover_text("stop sharing and leave the session")
.clicked();
}
});
ui.end_row();
if let Some(display) = session {
if display.conflicts > 0 {
ui.label("");
ui.label(
egui::RichText::new(format!(
"{} auto-resolved conflict(s)",
display.conflicts
))
.italics()
.weak(),
);
ui.end_row();
}
}
}
}
// export
ui.label("export");
export = ui
.button("export")
.on_hover_text("export this graph and its dependencies to a .gantz file")
.clicked();
ui.end_row();
});
// Persist the per-head editor buffers.
ui.memory_mut(|m| m.data.insert_temp(edit_id, name));
if let Some(desc) = desc {
ui.memory_mut(|m| m.data.insert_temp(desc_id, desc));
}
GraphConfigResponse {
new_branch,
export,
demo_changed,
reset_base_graph,
description_changed,
merge,
base_source_changed,
share,
stop_sharing,
}
}
}
/// The merge row's branch selector: a dropdown of merge candidates, each with
/// a dry-run hover summary. Picking a candidate *is* the action (one-shot,
/// like the layout buttons): a clean candidate merges on click; a conflicted
/// one is disabled, its tooltip listing the conflicts, with a separate opt-in
/// button applying the selected [`Resolutions`]. Hard-blocked candidates
/// (e.g. a reference cycle) can only be inspected.
///
/// Candidates and previews are only computed while the popup is open, and each
/// preview is cached keyed by the two branch tips (content addresses) plus the
/// resolution strategy, so a cached preview can never go stale.
///
/// [`Resolutions`]: gantz_ca::merge::Resolutions
fn merge_select(
env: &crate::Env<'_>,
head: &gantz_ca::Head,
resolutions: gantz_ca::merge::Resolutions,
ui: &mut egui::Ui,
) -> Option<crate::MergeHead> {
let mut merge = None;
egui::ComboBox::from_id_salt("merge_select")
.selected_text("select branch\u{2026}")
.show_ui(ui, |ui| {
let candidates = crate::merge::merge_candidates(env.registry, head);
if candidates.is_empty() {
ui.weak("no mergeable graphs");
return;
}
let ours_tip = match head {
gantz_ca::Head::Branch(name) => env.registry.head(name),
gantz_ca::Head::Commit(ca) => Some(*ca),
};
for candidate in candidates {
// Fetch (or compute and cache) the candidate's dry-run preview.
let preview_id =
egui::Id::new("merge_preview").with((ours_tip, candidate.theirs, resolutions));
let preview = ui
.memory_mut(|m| m.data.get_temp::<crate::merge::MergePreview>(preview_id))
.or_else(|| {
let preview = env.merge_preview(head, &candidate.name, resolutions);
if let Some(preview) = &preview {
ui.memory_mut(|m| m.data.insert_temp(preview_id, preview.clone()));
}
preview
});
let mut summary = preview
.as_ref()
.map(|p| crate::merge::summary_text(&p.summary))
.unwrap_or_default();
if candidate.fast_forward {
summary =
format!("fast-forward: moves this graph to the branch tip\n{summary}");
}
let clean = preview.as_ref().is_none_or(|p| p.is_clean());
if clean {
let mut text = candidate.name.clone();
if candidate.fast_forward {
text.push_str(" (fast-forward)");
}
if ui
.selectable_label(false, text)
.on_hover_text(summary)
.clicked()
{
merge = Some(crate::MergeHead {
source: candidate.name.clone(),
resolutions,
auto_resolve: false,
});
}
continue;
}
// Conflicted or blocked: not directly selectable. Conflicts
// (but not blockers) offer an explicit opt-in that applies the
// selected resolutions.
let preview = preview.expect("`!clean` requires a preview");
let warn = crate::node::named_ref::outdated_color();
ui.horizontal(|ui| {
let text = egui::RichText::new(format!("{} !", candidate.name)).color(warn);
ui.add_enabled(false, egui::Button::selectable(false, text))
.on_disabled_hover_ui(|ui| {
ui.set_max_width(320.0);
ui.label(summary);
for conflict in &preview.conflicts {
ui.colored_label(warn, format!("! {conflict}"));
}
for blocker in &preview.blockers {
ui.colored_label(
crate::node::named_ref::missing_color(),
format!("\u{2715} {blocker}"),
);
}
});
if preview.blockers.is_empty()
&& ui
.small_button("merge anyway")
.on_hover_text(
"merge despite the conflicts, applying the selected \
resolutions (see \u{26ED})",
)
.clicked()
{
merge = Some(crate::MergeHead {
source: candidate.name.clone(),
resolutions,
auto_resolve: true,
});
}
});
}
});
merge
}
/// The "⛭" menu beside the merge dropdown: how conflicts resolve when merging
/// despite them. Edits the persisted, GUI-global strategy in place.
fn resolutions_menu(resolutions: &mut gantz_ca::merge::Resolutions, ui: &mut egui::Ui) {
use gantz_ca::merge::{BothModified, EditOrDelete};
ui.label("when both sides modified a node");
ui.radio_value(
&mut resolutions.both_modified,
BothModified::KeepOurs,
"keep this graph's version",
);
ui.radio_value(
&mut resolutions.both_modified,
BothModified::KeepTheirs,
"keep the branch's version",
);
ui.radio_value(
&mut resolutions.both_modified,
BothModified::KeepNewest,
"keep the most recent edit",
)
.on_hover_text(
"per node: whichever side edited the node last wins \
(ties resolve deterministically)",
);
ui.separator();
ui.label("when a delete meets an edit");
ui.radio_value(
&mut resolutions.delete_modify,
EditOrDelete::KeepEdit,
"keep the edited node",
);
ui.radio_value(
&mut resolutions.delete_modify,
EditOrDelete::KeepDelete,
"keep the delete",
);
}