╭ Undo.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const ui = @import("../../ui.zig"); 4 const evt = @import("../../event.zig"); 5 const inp = @import("../input.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const hash = xit.hash; 9 const wgt = xit.xitui.widget; 10 const layout = xit.xitui.layout; 11 const Key = xit.xitui.input.Key; 12 const Grid = xit.xitui.grid.Grid; 13 const Focus = xit.xitui.focus.Focus; 14 const Self = @This(); 15 16 pub const page_size = 20; 17 18 // replaces the description for a transaction that wrote events 19 pub const EventsLink = struct { 20 label: []const u8, 21 link: []const u8, 22 }; 23 24 pub const Item = struct { 25 index: u64, 26 action: []const u8, 27 description: []const u8, 28 timestamp: []const u8, 29 link: []const u8, 30 form: []const u8, 31 events: ?EventsLink = null, 32 }; 33 34 identity: []const u8, 35 count: u64 = 0, 36 items: []const Item = &.{}, 37 next: ?u64 = null, 38 failure: ?[]const u8 = null, 39 can_undo: bool = false, 40 // the clear-history confirmation stands in for the list 41 clear: bool = false, 42 43 pub fn init(comptime opts: rp.RepoOpts(.xit), arena: *std.heap.ArenaAllocator, repo: *rp.Repo(.xit, opts), identity: []const u8, selected: ?u64) !Self { 44 const aa = arena.allocator(); 45 const DB = rp.Repo(.xit, opts).DB; 46 const history = try DB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 47 const count = try history.count(); 48 if (selected) |index| if (index >= count) return error.NotFound; 49 var items: std.ArrayList(Item) = .empty; 50 var remaining = if (selected) |index| index + 1 else count; 51 var buffer: [opts.max_read_size]u8 = undefined; 52 while (remaining > 0 and items.items.len < page_size) { 53 remaining -= 1; 54 const cursor = try history.getCursor(remaining) orelse return error.TransactionNotFound; 55 const moment = try DB.HashMap(.read_only).init(cursor); 56 const record = try xit.undo.read(opts, moment, &buffer); 57 var description: std.Io.Writer.Allocating = .init(aa); 58 const detail = if (record) |value| try eventDetail(opts, arena, identity, value, moment, remaining) else null; 59 if (detail) |value| { 60 try description.writer.writeAll(value.description); 61 } else if (record) |value| { 62 try xit.undo.format(opts, &repo.core.db, arena.child_allocator, value, &description.writer); 63 } else { 64 try description.writer.writeAll("no undo record"); 65 } 66 const route = ui.RoutablePage.repoUndoRoute(identity, remaining) orelse return error.RouteTooLong; 67 const url = try route.toUrl(arena); 68 try items.append(aa, .{ 69 .index = remaining, 70 .action = if (detail) |value| value.action else if (record) |value| try aa.dupe(u8, value.action) else "unknown", 71 .description = description.written(), 72 .events = if (detail) |value| value.events else null, 73 .timestamp = if (record) |value| try formatTimestamp(aa, value.timestamp) else "timestamp unavailable", 74 .link = try std.fmt.allocPrint(aa, "ai:{s}", .{url}), 75 .form = try std.fmt.allocPrint(aa, "form:{s}/undo", .{url}), 76 }); 77 } 78 return .{ .identity = try aa.dupe(u8, identity), .count = count, .items = items.items, .next = if (remaining > 0) remaining - 1 else null }; 79 } 80 81 const Detail = struct { 82 action: []const u8, 83 description: []const u8, 84 events: ?EventsLink = null, 85 }; 86 87 // every haxy event transaction shares one action, and the events it wrote are 88 // named by the moment's index rather than by the record 89 fn eventDetail( 90 comptime opts: rp.RepoOpts(.xit), 91 arena: *std.heap.ArenaAllocator, 92 identity: []const u8, 93 record: xit.undo.UndoRecord, 94 moment: rp.Repo(.xit, opts).DB.HashMap(.read_only), 95 history_index: u64, 96 ) !?Detail { 97 if (std.mem.eql(u8, record.action, evt.merge_undo_action)) return .{ .action = "merge events", .description = "merged the events ref from a remote" }; 98 if (!std.mem.eql(u8, record.action, evt.undo_action)) return null; 99 100 const count = try momentEventCount(opts, moment) orelse return .{ .action = "events", .description = "updated the event database" }; 101 // a transaction that consumed only merge commits indexed no events of its own 102 if (count == 0) return .{ .action = "events", .description = "merged the event history" }; 103 104 // the page reads the transaction's own moment, so the url names the 105 // transaction rather than the state it produced 106 const aa = arena.allocator(); 107 const route = ui.RoutablePage.repoEventsRoute(identity, .active, null, "", history_index) orelse return error.RouteTooLong; 108 return .{ 109 .action = try std.fmt.allocPrint(aa, "events ({d})", .{count}), 110 .description = "updated the event database", 111 .events = .{ 112 .label = try std.fmt.allocPrint(aa, "view events ({d})", .{count}), 113 .link = try std.fmt.allocPrint(aa, "a:{s}", .{try route.toUrl(arena)}), 114 }, 115 }; 116 } 117 118 // how many events the transaction's moment wrote: zero when it named a moment 119 // but indexed nothing, null when its state cannot be read at all 120 fn momentEventCount(comptime opts: rp.RepoOpts(.xit), moment: rp.Repo(.xit, opts).DB.HashMap(.read_only)) !?u64 { 121 const DB = rp.Repo(.xit, opts).DB; 122 const haxy_moment = evt.currentMomentFromRepoMoment(opts.hash, moment) catch return null; 123 const index_cursor = try haxy_moment.getCursor(hash.hashInt(opts.hash, evt.moment_index_key)) orelse return null; 124 const ids = try evt.momentEventIds(DB, opts.hash, haxy_moment, try index_cursor.readUint()) orelse return 0; 125 return try ids.count(); 126 } 127 128 pub fn formatTimestamp(aa: std.mem.Allocator, timestamp: i64) ![]const u8 { 129 if (timestamp < 0) return aa.dupe(u8, "timestamp unavailable"); 130 const seconds = std.time.epoch.EpochSeconds{ .secs = @intCast(timestamp) }; 131 const year = seconds.getEpochDay().calculateYearDay(); 132 const month = year.calculateMonthDay(); 133 const day = seconds.getDaySeconds(); 134 const months = [_][]const u8{ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 135 return std.fmt.allocPrint(aa, "{s} {d}, {d}, {d:0>2}:{d:0>2}:{d:0>2} UTC", .{ months[month.month.numeric() - 1], month.day_index + 1, year.year, day.getHoursIntoDay(), day.getMinutesIntoHour(), day.getSecondsIntoMinute() }); 136 } 137 138 pub fn execute(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource, index: u64) !void { 139 if (source.repo_kind != .xit) return error.NotFound; 140 var any_repo = try rp.AnyRepo(.xit, .{}).open(io, allocator, source.localInitOpts()); 141 defer any_repo.deinit(io, allocator); 142 switch (any_repo) { 143 inline else => |*repo| try repo.undo(io, index), 144 } 145 } 146 147 // discard every earlier transaction by compacting the database. the patch 148 // revisions events point at are unreachable from the refs, so they are named 149 // as extra roots to survive the collection. 150 pub fn clearHistory(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !void { 151 if (source.repo_kind != .xit) return error.NotFound; 152 var any_repo = try rp.AnyRepo(.xit, .{}).open(io, allocator, source.localInitOpts()); 153 defer any_repo.deinit(io, allocator); 154 switch (any_repo) { 155 inline else => |*repo| { 156 const opts = repo.self_repo_opts; 157 // a repo that has consumed no events has no revisions to protect 158 const roots = if (evt.currentMoment(opts, repo)) |moment| 159 try evt.PatchRev.gcRoots(rp.Repo(.xit, opts).DB, opts.hash, allocator, moment) 160 else |_| 161 &.{}; 162 defer allocator.free(roots); 163 _ = try repo.garbageCollect(io, allocator, .{ .extra_roots = roots }); 164 }, 165 } 166 } 167 168 // keep action failures on the current tab without rebuilding a stale route. 169 pub fn handleRequest(allocator: std.mem.Allocator, session: *ui.Session, target: ui.RoutablePage.RepoUndoRoute) void { 170 perform(allocator, session, target) catch |err| { 171 session.data.undo_failure = @errorName(err); 172 return; 173 }; 174 session.data.undo_failure = null; 175 } 176 177 // both terminal hosts resolve and authorize the request afresh before opening 178 // the repository; the web handler uses its matching http authorization path. 179 pub fn perform(allocator: std.mem.Allocator, session: *ui.Session, target: ui.RoutablePage.RepoUndoRoute) !void { 180 const identity = target.name.slice(); 181 const index = if (target.clear) null else target.index orelse return error.InvalidHistoryIndex; 182 const source = try authorizedSource(session, identity) orelse return error.Forbidden; 183 const io = session.io orelse return error.NotFound; 184 if (index) |value| try execute(io, allocator, source, value) else try clearHistory(io, allocator, source); 185 try session.navigate(ui.RoutablePage.repoUndoRoute(identity, null) orelse return error.RouteTooLong); 186 } 187 188 // the repo an owner may act on, or null when they may not 189 fn authorizedSource(session: *ui.Session, identity: []const u8) !?ui.RepoSource { 190 if (try session.authorize(identity, .owner) == null) return null; 191 if (session.local) |local| return local; 192 const admin_repo = session.admin_repo orelse return error.NotFound; 193 const moment = try evt.currentMoment(evt.admin_repo_opts, admin_repo); 194 const pair = ui.RoutablePage.RepoIdentity.parse(identity) orelse return error.NotFound; 195 const found = try evt.Repo.readByOwnerAndName(evt.AdminDB, evt.admin_repo_opts.hash, moment, session.page_arena, pair.owner, pair.name) orelse return error.NotFound; 196 const hex = std.fmt.bytesToHex(found.event_id, .lower); 197 return .{ .path = try std.fs.path.join(session.page_arena.allocator(), &.{ session.repos_dir orelse return error.NotFound, &hex }), .repo_kind = .xit }; 198 } 199 200 pub const View = struct { 201 box: wgt.Box(ui.Widget), 202 data: *const Self, 203 session: *ui.Session, 204 detailed_index: ?usize = null, 205 failure_id: ?usize = null, 206 shown_failure: ?[]const u8 = null, 207 208 const list_max_width = 20; 209 const detail_min_width = 40; 210 const header_index = 0; 211 const content_index = 1; 212 213 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 214 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 215 errdefer outer.deinit(allocator); 216 217 // the sub header carries the tab's only action 218 { 219 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 220 errdefer header.deinit(allocator); 221 const route = ui.RoutablePage.repoUndoClearRoute(data.identity) orelse return error.RouteTooLong; 222 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}); 223 try addText(allocator, &header, "clear undo history", link, .single); 224 header.getFocus().child_id = header.children.keys()[0]; 225 try outer.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 226 } 227 228 if (data.clear) { 229 var center = try initClearForm(allocator, data, session); 230 errdefer center.deinit(allocator); 231 try outer.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 232 outer.getFocus().child_id = outer.children.keys()[content_index]; 233 return .{ .box = outer, .data = data, .session = session }; 234 } 235 236 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 237 errdefer box.deinit(allocator); 238 { 239 var scroll = blk: { 240 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 241 errdefer rows.deinit(allocator); 242 for (data.items) |item| try addText(allocator, &rows, item.action, item.link, .hidden); 243 if (data.next) |next| { 244 const route = ui.RoutablePage.repoUndoRoute(data.identity, next) orelse return error.RouteTooLong; 245 try addText(allocator, &rows, "next →", try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}), .hidden); 246 } 247 if (data.items.len == 0) try addText(allocator, &rows, "no undo history", null, .hidden); 248 rows.getFocus().child_id = rows.children.keys()[0]; 249 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 250 }; 251 errdefer scroll.deinit(allocator); 252 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 253 } 254 { 255 var scroll = blk: { 256 var details = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 257 errdefer details.deinit(allocator); 258 if (data.items.len > 0) { 259 try addText(allocator, &details, "", null, .single); 260 try addText(allocator, &details, "", null, .single); 261 details.children.values()[1].widget.text_box.options.label = " timestamp "; 262 try addText(allocator, &details, "", null, .single); 263 details.children.values()[2].widget.text_box.options.label = " description "; 264 details.getFocus().child_id = details.children.keys()[0]; 265 } 266 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = details }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 267 }; 268 errdefer scroll.deinit(allocator); 269 scroll.getFocus().mode = .mouse; 270 try box.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 271 } 272 box.getFocus().child_id = box.children.keys()[0]; 273 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 274 outer.getFocus().child_id = outer.children.keys()[content_index]; 275 return .{ .box = outer, .data = data, .session = session }; 276 } 277 278 // the same shape the thread views use to confirm a removal 279 fn initClearForm(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !ui.widget.Center { 280 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .rounded_corners = true, .direction = .vert }); 281 errdefer box.deinit(allocator); 282 const route = ui.RoutablePage.repoUndoClearRoute(data.identity) orelse return error.RouteTooLong; 283 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "form:{s}", .{try route.toUrl(session.page_arena)}) }; 284 285 var prompt = try wgt.Text.init(allocator, "are you sure?"); 286 errdefer prompt.deinit(allocator); 287 try box.children.put(allocator, prompt.getFocus().id, .{ .widget = .{ .text = prompt }, .rect = null, .min_size = null }); 288 289 var button = try wgt.TextBox.init(allocator, "clear undo history", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 290 errdefer button.deinit(allocator); 291 button.getFocus().mode = .all; 292 button.getFocus().kind = if (data.can_undo) .{ .custom = "submit" } else .text_box; 293 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 294 box.getFocus().child_id = button.getFocus().id; 295 296 return ui.widget.Center.init(allocator, .{ .box = box }); 297 } 298 299 fn addText(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: ?[]const u8, border: wgt.BorderStyle) !void { 300 var row = try wgt.TextBox.init(allocator, text, .{ .border_style = border, .rounded_corners = true, .wrap_kind = .word }); 301 errdefer row.deinit(allocator); 302 row.getFocus().mode = .all; 303 if (link) |value| row.getFocus().kind = .{ .custom = value }; 304 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 305 } 306 307 // enter counts only on the focused button; a click counts anywhere on it 308 fn activated(root_focus: *Focus, button_id: usize, key: Key) bool { 309 return switch (key) { 310 .enter => root_focus.grandchild_id == button_id, 311 .mouse => |mouse| inp.leftClickOn(root_focus, button_id, mouse), 312 else => false, 313 }; 314 } 315 316 // the terminal hosts run the action after rendering; the web renderer 317 // posts the form instead, and wasm has no repository to act on 318 fn requestUndo(self: *View, route: ui.RoutablePage) void { 319 if (builtin.target.cpu.arch == .wasm32) return; 320 if (!self.session.is_terminal or self.session.host_request != null) return; 321 self.session.host_request = .{ .undo = route.repo_undo }; 322 } 323 324 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 325 return &self.box.children.values()[header_index].widget.box; 326 } 327 fn headerActive(self: *View) bool { 328 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 329 } 330 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 331 return &self.box.children.values()[content_index].widget.box; 332 } 333 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 334 return &self.contentBox().children.values()[0].widget.scroll; 335 } 336 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 337 return &self.contentBox().children.values()[1].widget.scroll; 338 } 339 fn selected(self: *View) ?usize { 340 const rows = &self.listScroll().child.box; 341 const index = rows.children.getIndex(rows.getFocus().child_id orelse return null) orelse return null; 342 return if (index < self.data.items.len) index else null; 343 } 344 fn detailActive(self: *View) bool { 345 return self.contentBox().getFocus().child_id == self.detailScroll().getFocus().id; 346 } 347 348 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 349 const index = self.selected() orelse return; 350 const failure = self.session.data.undo_failure orelse self.data.failure; 351 if (self.detailed_index == index and std.meta.eql(self.shown_failure, failure)) return; 352 const details = &self.detailScroll().child.box; 353 if (self.failure_id) |id| { 354 if (details.children.getPtr(id)) |child| child.widget.deinit(allocator); 355 _ = details.children.orderedRemove(id); 356 self.failure_id = null; 357 } 358 const item = self.data.items[index]; 359 const button = &details.children.values()[0].widget.text_box; 360 const description = &details.children.values()[2].widget.text_box; 361 try button.setContent(allocator, if (item.index == 0) "initial state cannot be undone" else if (item.index == self.data.count - 1) "undo this" else "undo this and all above it"); 362 // a transaction that wrote events links to them instead of describing itself 363 if (item.events) |events| { 364 try description.setContent(allocator, events.label); 365 description.options.label = " events "; 366 description.getFocus().kind = .{ .custom = events.link }; 367 } else { 368 try description.setContent(allocator, item.description); 369 description.options.label = " description "; 370 description.getFocus().kind = .text_box; 371 } 372 const enabled = self.data.can_undo and item.index > 0; 373 button.getFocus().kind = if (enabled) .{ .custom = "submit" } else .text_box; 374 try details.children.values()[1].widget.text_box.setContent(allocator, item.timestamp); 375 if (failure) |message| { 376 try addText(allocator, details, try std.fmt.allocPrint(self.session.page_arena.allocator(), "error: {s}", .{message}), null, .single); 377 self.failure_id = details.children.keys()[details.children.count() - 1]; 378 } 379 self.shown_failure = failure; 380 details.getFocus().kind = if (enabled) .{ .custom = item.form } else .container; 381 details.getFocus().child_id = details.children.keys()[0]; 382 self.detailScroll().x = 0; 383 self.detailScroll().y = 0; 384 self.detailScroll().getFocus().version +%= 1; 385 self.detailed_index = index; 386 } 387 388 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 389 self.clearGrid(); 390 if (self.data.clear) return try self.box.build(allocator, constraint, root_focus); 391 try self.refreshDetail(allocator); 392 const rows = &self.listScroll().child.box; 393 if (self.data.items.len == 0) { 394 const failure = self.session.data.undo_failure orelse self.data.failure; 395 if (!std.meta.eql(self.shown_failure, failure)) { 396 const message = if (failure) |name| try std.fmt.allocPrint(self.session.page_arena.allocator(), "error: {s}", .{name}) else "no undo history"; 397 try rows.children.values()[0].widget.text_box.setContent(allocator, message); 398 self.shown_failure = failure; 399 } 400 } 401 for (rows.children.keys(), rows.children.values()) |id, *child| child.widget.text_box.options.border_style = if (rows.getFocus().child_id == id) .single else .hidden; 402 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 403 self.contentBox().children.values()[0].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 404 const width = if (constraint.max_size.width) |value| if (both_fit) value - list_max_width else value else detail_min_width; 405 self.contentBox().children.values()[1].min_size = .{ .width = width, .height = null }; 406 for (self.detailScroll().child.box.children.values()) |*child| child.max_size = .{ .width = width -| 2, .height = null }; 407 try self.box.build(allocator, constraint, root_focus); 408 } 409 410 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 411 // the sub header sits above whichever content the route selects. 412 // scrolling crosses the boundary the same way the arrow keys do 413 const direction = inp.vertDirection(key); 414 if (self.headerActive()) { 415 if (direction == .down) root_focus.setFocus(self.box.children.values()[content_index].widget.getFocus().id); 416 return; 417 } 418 if (self.data.clear) { 419 if (direction == .up) root_focus.setFocus(self.headerBox().getFocus().id) else try self.clearInput(key, root_focus); 420 return; 421 } 422 if (direction == .up and self.contentAtTop()) { 423 root_focus.setFocus(self.headerBox().getFocus().id); 424 return; 425 } 426 try self.refreshDetail(allocator); 427 if (self.detailActive()) { 428 const details = &self.detailScroll().child.box; 429 if (self.selected()) |index| { 430 if (activated(root_focus, details.children.keys()[0], key) and self.data.can_undo and self.data.items[index].index > 0) { 431 self.requestUndo(ui.RoutablePage.repoUndoRoute(self.data.identity, self.data.items[index].index) orelse return error.RouteTooLong); 432 return; 433 } 434 } 435 if (key == .arrow_left) root_focus.setFocus(self.listScroll().getFocus().id) else if (inp.rowDelta(key, @intCast(details.children.count()))) |delta| ui.widget.moveRowFocus(details, self.detailScroll(), root_focus, delta); 436 } else if (inp.rowDelta(key, @intCast(self.listScroll().child.box.children.count()))) |delta| { 437 ui.widget.moveRowFocus(&self.listScroll().child.box, self.listScroll(), root_focus, delta); 438 } else switch (key) { 439 .arrow_right, .enter => if (self.selected() != null) { 440 root_focus.setFocus(self.detailScroll().getFocus().id); 441 }, 442 else => {}, 443 } 444 } 445 446 // the confirm button submits the form the web renderer posts 447 fn clearInput(self: *View, key: Key, root_focus: *Focus) !void { 448 const center = &self.box.children.values()[content_index].widget.center; 449 const button_id = center.child.box.getFocus().child_id orelse return; 450 if (!activated(root_focus, button_id, key) or !self.data.can_undo) return; 451 self.requestUndo(ui.RoutablePage.repoUndoClearRoute(self.data.identity) orelse return error.RouteTooLong); 452 } 453 454 fn contentAtTop(self: *View) bool { 455 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 456 const rows = &scroll.child.box; 457 const id = rows.getFocus().child_id orelse return true; 458 return rows.children.getIndex(id) == 0 and scroll.y == 0; 459 } 460 461 // moving up from the sub header returns to the page's tabs 462 pub fn atTop(self: *View) bool { 463 return self.headerActive(); 464 } 465 466 // arriving from the page's tabs lands on the sub header, not the content 467 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 468 root_focus.setFocus(self.headerBox().getFocus().id); 469 return true; 470 } 471 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 472 self.box.deinit(allocator); 473 } 474 pub fn clearGrid(self: *View) void { 475 self.box.clearGrid(); 476 } 477 pub fn getGrid(self: View) ?Grid { 478 return self.box.getGrid(); 479 } 480 pub fn getFocus(self: *View) *Focus { 481 return self.box.getFocus(); 482 } 483 }; 484