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
//! A simple widget for selecting between, naming and creating new graphs.
use super::head_row::{HeadRowType, head_row};
use std::collections::HashSet;
/// A widget for selecting between, naming, and creating new graphs.
pub struct GraphSelect<'a> {
id: egui::Id,
registry: &'a dyn GraphRegistry,
heads: &'a [gantz_ca::Head],
focused_head: Option<usize>,
base_names: &'a gantz_ca::registry::Names,
}
#[derive(Clone, Default)]
struct GraphSelectState {
name_filter: String,
}
/// 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,
/// Single click: replace the focused head with this one.
pub replaced: Option<gantz_ca::Head>,
/// Ctrl+click on a head that is not open: open this head as a new tab.
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 GraphRegistry,
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,
}
}
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
}
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 text edit for filtering names.
egui::TextEdit::singleline(&mut state.name_filter)
.desired_width(ui.available_width())
.hint_text("🔎 Name Filter")
.show(ui);
let names = self.registry.names();
// 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 user names and base names.
let is_base = |name: &str| self.base_names.contains_key(name);
// Show user-named graphs first.
let mut visited = HashSet::new();
for (name, ca) in names.iter().filter(|(n, _)| !is_base(n)) {
if !state.name_filter.is_empty()
&& !state
.name_filter
.split_whitespace()
.all(|s| name.contains(s))
{
continue;
}
visited.insert(ca);
let head = gantz_ca::Head::Branch(name.to_string());
let res = head_row(
self.heads,
&head,
HeadRowType::Named(name),
ca,
self.focused_head,
ui,
);
if res.row.clicked() {
let ctrl = ui.input(|i| i.modifiers.ctrl);
if ctrl {
if self.heads.contains(&head) {
response.closed = Some(head);
} else {
response.opened = Some(head);
}
} else {
response.replaced = Some(head);
}
} else if let Some(delete) = res.delete {
if delete.clicked() {
response.name_removed = Some(name.to_string());
}
}
}
// Show base-named graphs after user graphs.
for (name, ca) in names.iter().filter(|(n, _)| is_base(n)) {
if !state.name_filter.is_empty()
&& !state
.name_filter
.split_whitespace()
.all(|s| name.contains(s))
{
continue;
}
visited.insert(ca);
let head = gantz_ca::Head::Branch(name.to_string());
let res = head_row(
self.heads,
&head,
HeadRowType::Base(name),
ca,
self.focused_head,
ui,
);
if res.row.clicked() {
let ctrl = ui.input(|i| i.modifiers.ctrl);
if ctrl {
if self.heads.contains(&head) {
response.closed = Some(head);
} else {
response.opened = Some(head);
}
} else {
response.replaced = Some(head);
}
}
}
// 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() {
let ctrl = ui.input(|i| i.modifiers.ctrl);
if ctrl {
if self.heads.contains(&head) {
response.closed = Some(head);
} else {
response.opened = Some(head);
}
} else {
response.replaced = Some(head);
}
}
}
});
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")
.clicked()
{
response.new_graph = true;
}
});
});
});
// Store the modified state back in memory
ui.memory_mut(|mem| mem.data.insert_temp(state_id, state));
response
}
}