╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
269 .notify => evt.Notify, 270 .discuss => evt.Discussion, 271 .issue => evt.Issue, 272 .comment => evt.Comment, 273 .attach => evt.Attachment, 274 .assign => evt.Assignment, 275 .patchrev => evt.PatchRev, 276 .patch => evt.Patch, 277 }; 278 break :blk (try evt.readRecordSubset(T, EventMeta, evt.EventDB(hash_kind), hash_kind, haxy_moment, arena, id)) orelse return error.NotFound; 279 }, 280 }; 281 } 282 283 fn readItem( 284 comptime hash_kind: hash.HashKind, 285 arena: *std.heap.ArenaAllocator, 286 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 287 kind_map: evt.EventDB(hash_kind).HashMap(.read_only), 288 admin_moment: ?evt.AdminDB.HashMap(.read_only), 289 identity: []const u8, 290 repo_event: evt.Repo, 291 include_view_url: bool, 292 kind: evt.EventKind, 293 id: *const [evt.event_id_size]u8, 294 ) !?Item { 295 const DB = evt.EventDB(hash_kind); 296 // the parts of a record a row shows 297 const Authored = struct { author_email: ?[]const u8 }; 298 const Owned = struct { event: struct { user_id: [evt.event_id_size * 2]u8 } }; 299 var item: Item = .{ 300 .id = try formatId(arena.allocator(), id), 301 .kind = kind, 302 .author = .unknown, 303 }; 304 switch (kind) { 305 .user => { 306 const record = (try evt.readRecordSubset(evt.User, struct { event: struct { email: []const u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 307 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.event.email); 308 }, 309 .repo => { 310 const record = (try evt.readRecordSubset(evt.Repo, Owned, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 311 if (admin_moment) |moment| { 312 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, &try evt.parseEventId(&record.event.user_id))) |user| 313 item.author = .{ .user_name = user.event.name }; 314 } 315 }, 316 .fork => _ = (try evt.readRecordSubset(evt.Fork, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 317 .grant => _ = (try evt.readRecordSubset(evt.Grant, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 318 .notify => _ = (try evt.readRecordSubset(evt.Notify, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 319 .discuss => { 320 const record = (try evt.readRecordSubset(evt.Discussion, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 321 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 322 if (!include_view_url or repo_event.threadRole(.discuss) == null) return item; 323 const route = ui.RoutablePage.repoThreadCommentsRoute(.discuss, identity, item.id, 0) orelse return error.RouteTooLong; 324 item.view_url = try route.toUrl(arena); 325 }, 326 .issue => { 327 const record = (try evt.readRecordSubset(evt.Issue, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 328 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 329 if (!include_view_url or repo_event.threadRole(.issue) == null) return item; 330 const route = ui.RoutablePage.repoThreadCommentsRoute(.issue, identity, item.id, 0) orelse return error.RouteTooLong; 331 item.view_url = try route.toUrl(arena); 332 }, 333 .comment => { 334 const record = (try evt.readRecordSubset(evt.Comment, struct { author_email: ?[]const u8, event: struct { thread_id: [evt.event_id_size * 2]u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 335 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 336 if (!include_view_url) return item; 337 var thread_id: [evt.event_id_size]u8 = undefined; 338 _ = std.fmt.hexToBytes(&thread_id, &record.event.thread_id) catch return error.InvalidEventId; 339 const thread_kind = (try evt.readEventKind(hash_kind, kind_map, &thread_id)) orelse return item; 340 if (repo_event.threadRole(thread_kind) == null) return item; 341 const route = ui.RoutablePage.repoThreadCommentRoute(thread_kind, identity, &record.event.thread_id, item.id, 0) orelse return item; 342 item.view_url = try route.toUrl(arena); 343 }, 344 .attach => { 345 const record = (try evt.readRecordSubset(evt.Attachment, struct { author_email: ?[]const u8, event: struct { target_id: [evt.event_id_size * 2]u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 346 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 347 if (!include_view_url) return item; 348 const target_id = try evt.parseEventId(&record.event.target_id); 349 const target_kind = (try evt.readEventKind(hash_kind, kind_map, &target_id)) orelse return item; 350 if (repo_event.threadRole(target_kind) == null) return item; 351 const route = ui.RoutablePage.repoThreadCommentsRoute(target_kind, identity, &record.event.target_id, 0) orelse return item; 352 item.view_url = try route.toUrl(arena); 353 }, 354 .assign => { 355 const record = (try evt.readRecordSubset(evt.Assignment, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 356 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 357 }, 358 .patch => { 359 const record = (try evt.readRecordSubset(evt.Patch, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 360 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 361 if (!include_view_url or repo_event.threadRole(.patch) == null) return item; 362 const route = ui.RoutablePage.repoThreadCommentsRoute(.patch, identity, item.id, 0) orelse return error.RouteTooLong; 363 item.view_url = try route.toUrl(arena); 364 }, 365 .patchrev => { 366 const record = (try evt.readRecordSubset(evt.PatchRev, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 367 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 368 }, 369 } 370 return item; 371 } 372 373 pub const View = struct { 374 box: wgt.Box(ui.Widget), 375 data: *const Self, 376 session: *ui.Session, 377 detailed_index: [2]?usize, 378 379 const header_index = 0; 380 const content_index = 1; 381 const list_index = 0; 382 const detail_index = 1; 383 const list_max_width = 20; 384 const detail_min_width = 40; 385 386 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 387 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 388 errdefer outer.deinit(allocator); 389 390 var header_view = try Header.View.init(allocator, data, session); 391 errdefer header_view.deinit(allocator); 392 // a row taller than the header, leaving a blank line beneath it 393 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_events_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 394 395 var content_stack = try wgt.Stack(ui.Widget).init(allocator); 396 errdefer content_stack.deinit(allocator); 397 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 398 var view_content = try initContent(allocator, data, session, view); 399 errdefer view_content.deinit(allocator); 400 try content_stack.children.put(allocator, view_content.getFocus().id, view_content); 401 } 402 content_stack.getFocus().child_id = content_stack.children.keys()[@backingInt(data.view)]; 403 try outer.children.put(allocator, content_stack.getFocus().id, .{ .widget = .{ .stack = content_stack }, .rect = null, .min_size = null }); 404 outer.getFocus().child_id = outer.children.keys()[content_index]; 405 return .{ .box = outer, .data = data, .session = session, .detailed_index = .{ null, null } }; 406 } 407 408 // an empty window shows the empty message in place of the list and detail 409 fn initContent(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, view: ui.RoutablePage.EventsView) !ui.Widget { 410 const event_window = data.window(view); 411 if (event_window.isEmpty()) return .{ .center = try ui.widget.initEmpty(allocator) }; 412 413 var content_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 414 errdefer content_box.deinit(allocator); 415 416 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 417 errdefer list_box.deinit(allocator); 418 for (event_window.events) |event| { 419 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, event.kind, event.id, data.moment) orelse return error.RouteTooLong; 420 const link = try session.page_arena.allocator().print("ai:{s}", .{try route.toUrl(session.page_arena)}); 421 try addRow(allocator, &list_box, @tagName(event.kind), link); 422 } 423 if (event_window.next) |next| { 424 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, next.kind, next.id, data.moment) orelse return error.RouteTooLong; 425 const link = try session.page_arena.allocator().print("a:{s}", .{try route.toUrl(session.page_arena)}); 426 try addRow(allocator, &list_box, "next →", link); 427 } 428 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 429 var list_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 430 errdefer list_scroll.deinit(allocator); 431 try content_box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 432 433 // the detail scroll, below the way back to a hidden list 434 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 435 errdefer frame.deinit(allocator); 436 try ui.widget.addListBack(allocator, &frame, "← back to event list"); 437 { 438 var detail_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 439 errdefer detail_box.deinit(allocator); 440 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 441 errdefer detail_scroll.deinit(allocator); 442 detail_scroll.getFocus().mode = .mouse; 443 frame.getFocus().child_id = detail_scroll.getFocus().id; 444 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 445 } 446 try content_box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 447 448 content_box.getFocus().child_id = content_box.children.keys()[detail_index]; 449 return .{ .box = content_box }; 450 } 451 452 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 453 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 454 errdefer row.deinit(allocator); 455 row.getFocus().mode = .all; 456 row.getFocus().kind = .{ .custom = link }; 457 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 458 } 459 460 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 461 self.box.deinit(allocator); 462 } 463 464 fn header(self: *View) *Header.View { 465 return &self.box.children.values()[header_index].widget.repo_events_header; 466 } 467 468 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 469 return &self.box.children.values()[content_index].widget.stack; 470 } 471 472 fn selectedView(self: *View) ui.RoutablePage.EventsView { 473 const stack = self.contentStack(); 474 const id = stack.getFocus().child_id orelse return .active; 475 return @fromBackingInt(@intCast(stack.children.getIndex(id) orelse 0)); 476 } 477 478 fn eventWindow(self: *View) *const Window { 479 return self.data.window(self.selectedView()); 480 } 481 482 fn content(self: *View) *wgt.Box(ui.Widget) { 483 const stack = self.contentStack(); 484 return &stack.children.values()[@backingInt(self.selectedView())].box; 485 } 486 487 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 488 return &self.content().children.values()[list_index].widget.scroll; 489 } 490 491 fn listBox(self: *View) *wgt.Box(ui.Widget) { 492 return &self.listScroll().child.box; 493 } 494 495 fn detailFrame(self: *View) *wgt.Box(ui.Widget) { 496 return &self.content().children.values()[detail_index].widget.box; 497 } 498 499 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 500 return &self.detailFrame().children.values()[ui.widget.list_pane_index].widget.scroll; 501 } 502 503 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 504 return &self.detailScroll().child.box; 505 } 506 507 fn detailActive(self: *View) bool { 508 return self.content().getFocus().child_id == self.detailFrame().getFocus().id; 509 } 510 511 fn headerActive(self: *View) bool { 512 return self.box.getFocus().child_id == self.header().getFocus().id; 513 } 514 515 fn selectedEventIndex(self: *View) ?usize { 516 const id = self.listBox().getFocus().child_id orelse return null; 517 const index = self.listBox().children.getIndex(id) orelse return null; 518 return if (index < self.eventWindow().events.len) index else null; 519 } 520 521 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 522 self.clearGrid(); 523 if (self.header().getSelectedIndex()) |index| 524 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 525 if (self.eventWindow().isEmpty()) return self.box.build(allocator, constraint, root_focus); 526 try self.refreshDetail(allocator); 527 528 const list = self.listBox(); 529 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 530 .text_box => |*text_box| ui.widget.markSelected(text_box, list.getFocus().child_id == id), 531 else => {}, 532 }; 533 534 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 535 ui.widget.showListBack(self.detailFrame(), !both_fit); 536 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 537 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 538 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 539 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 540 541 try self.box.build(allocator, constraint, root_focus); 542 } 543 544 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 545 const index = self.selectedEventIndex() orelse return; 546 const detailed_index = &self.detailed_index[@backingInt(self.selectedView())]; 547 if (detailed_index.*) |old| if (old == index) return; 548 const box = self.detailBox(); 549 for (box.children.values()) |*child| child.widget.deinit(allocator); 550 box.children.clearAndFree(allocator); 551 box.getFocus().child_id = null; 552 553 const item = self.eventWindow().events[index]; 554 if (item.view_url) |url| { 555 var link = try wgt.TextBox.init(allocator, "view event", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 556 errdefer link.deinit(allocator); 557 link.getFocus().mode = .all; 558 link.getFocus().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{url}) }; 559 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 560 } 561 { 562 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " kind " } }); 563 errdefer kind.deinit(allocator); 564 kind.getFocus().mode = .all; 565 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 566 } 567 { 568 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 569 errdefer author.deinit(allocator); 570 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 571 } 572 573 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 574 self.detailScroll().x = 0; 575 self.detailScroll().y = 0; 576 self.detailScroll().getFocus().version +%= 1; 577 ui.widget.setListBackLink(self.detailFrame(), self.listBox().children.values()[index].widget.text_box.getFocus().kind.custom); 578 detailed_index.* = index; 579 } 580 581 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 582 // scrolling crosses the sub header boundary the same way arrows do 583 const direction = inp.vertDirection(key); 584 if (self.headerActive()) { 585 if (direction == .down) { 586 // the empty message has nothing to focus 587 if (!self.eventWindow().isEmpty()) root_focus.setFocus(self.content().getFocus().id); 588 } else try self.header().input(allocator, key, root_focus); 589 return; 590 } 591 // the way back to the list, by enter or a click 592 if (self.detailActive()) if (ui.widget.listBackId(self.detailFrame())) |id| { 593 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 594 if (root_focus.grandchild_id == id) return switch (direction) { 595 .up => self.header().focusCurrent(root_focus), 596 .down => root_focus.setFocus(self.detailScroll().getFocus().id), 597 .none => if (key == .arrow_left) self.focusList(root_focus), 598 }; 599 if (direction == .up and self.contentAtTop()) return root_focus.setFocus(id); 600 }; 601 if (direction == .up and self.contentAtTop()) { 602 self.header().focusCurrent(root_focus); 603 return; 604 } 605 if (self.detailActive()) { 606 if (key == .arrow_left) { 607 self.focusList(root_focus); 608 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 609 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 610 } 611 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 612 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 613 } else switch (key) { 614 .arrow_right => if (self.detailBox().children.count() > 0) self.focusDetail(root_focus), 615 .enter => if (self.selectedEventIndex() != null) self.focusDetail(root_focus), 616 // when the window is too narrow to lay out the detail pane beside 617 // the list, a click on a row opens it like enter. the row was just 618 // selected, so the detail is swapped to it ahead of the build. 619 .mouse => |mouse| if (self.selectedEventIndex() != null and self.content().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 620 try self.refreshDetail(allocator); 621 self.focusDetail(root_focus); 622 }, 623 else => {}, 624 } 625 } 626 627 // enter the detail pane, landing on the way back to the list when it shows 628 fn focusDetail(self: *View, root_focus: *Focus) void { 629 if (ui.widget.listBackId(self.detailFrame()) != null) return ui.widget.focusListBack(self.detailFrame(), root_focus); 630 root_focus.setFocus(self.detailScroll().getFocus().id); 631 } 632 633 fn focusList(self: *View, root_focus: *Focus) void { 634 root_focus.setFocus(self.listScroll().getFocus().id); 635 } 636 637 fn contentAtTop(self: *View) bool { 638 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 639 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 640 const id = box.getFocus().child_id orelse return true; 641 return box.children.getIndex(id) == 0 and scroll.y == 0; 642 } 643 644 pub fn atTop(self: *View) bool { 645 return self.headerActive(); 646 } 647 648 // arriving from the page's tabs lands on the sub header, not the content 649 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 650 self.header().focusCurrent(root_focus); 651 return true; 652 } 653 654 pub fn clearGrid(self: *View) void { 655 self.box.clearGrid(); 656 } 657 658 pub fn getGrid(self: View) ?Grid { 659 return self.box.getGrid(); 660 } 661 662 pub fn getFocus(self: *View) *Focus { 663 return self.box.getFocus(); 664 } 665 }; 666 667 pub const Header = struct { 668 sync_status: ?[]const u8, 669 670 const nothing = "nothing to sync"; 671 const unsynced = "sync needed!"; 672 673 const Progress = struct { 674 message: *std.Io.Writer.Allocating, 675 676 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 677 switch (event) { 678 .text => |value| { 679 self.message.clearRetainingCapacity(); 680 try self.message.writer.writeAll(value); 681 }, 682 else => {}, 683 } 684 } 685 }; 686 687 pub fn local( 688 comptime repo_kind: rp.RepoKind, 689 comptime repo_opts: rp.RepoOpts(repo_kind), 690 aa: std.mem.Allocator, 691 repo: *rp.Repo(repo_kind, repo_opts), 692 io: std.Io, 693 failure: ?[]const u8, 694 ) !Header { 695 if (failure != null) return Header.empty(aa, failure); 696 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 697 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 698 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 699 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 700 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 701 } 702 703 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 704 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 705 } 706 707 pub fn remote() Header { 708 return .{ .sync_status = null }; 709 } 710 711 pub const View = struct { 712 box: wgt.Box(ui.Widget), 713 session: *ui.Session, 714 tab_ids: [2]usize, 715 // the tab stays selected while the sync button has focus 716 selected_tab: usize, 717 button_id: ?usize, 718 719 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 720 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 721 errdefer box.deinit(allocator); 722 var tab_ids: [2]usize = undefined; 723 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 724 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 725 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 726 const count_label = try session.page_arena.allocator().print("({d})", .{data.window(view).count}); 727 var tab = try wgt.TextBox.init(allocator, view_label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none, .bottom_label = .{ .text = count_label } }); 728 errdefer tab.deinit(allocator); 729 tab.getFocus().mode = .all; 730 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 731 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 732 tab_ids[i] = tab.getFocus().id; 733 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = @max(view_label.len, count_label.len) + 2, .height = null } }); 734 } 735 736 // pushes the sync button and status to the right 737 { 738 var spacer = try ui.widget.Spacer.init(allocator); 739 errdefer spacer.deinit(allocator); 740 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 741 } 742 743 var button_id: ?usize = null; 744 if (data.header.sync_status != null) { 745 box.getFocus().kind = .{ .custom = "form:/sync" }; 746 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 747 errdefer button.deinit(allocator); 748 button.getFocus().mode = .all; 749 button.getFocus().kind = .{ .custom = "submit" }; 750 button_id = button.getFocus().id; 751 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 752 } 753 754 if (data.header.sync_status) |status_text| { 755 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 756 errdefer status.deinit(allocator); 757 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 758 } 759 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 760 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 761 } 762 763 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 764 self.box.deinit(allocator); 765 } 766 767 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 768 self.clearGrid(); 769 if (self.getSelectedIndex()) |index| self.selected_tab = index; 770 for (self.tab_ids, 0..) |id, index| { 771 const child = self.box.children.getPtr(id) orelse continue; 772 const tab = switch (child.widget) { 773 .text_box => |*text_box| text_box, 774 else => continue, 775 }; 776 ui.widget.markSelected(tab, index == self.selected_tab); 777 } 778 try self.box.build(allocator, constraint, root_focus); 779 } 780 781 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 782 const selected = self.box.getFocus().child_id orelse return; 783 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 784 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 785 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 786 if (inp.moveTab(key, current, count)) |next| { 787 root_focus.setFocus(stops[next] orelse unreachable); 788 return; 789 } 790 switch (key) { 791 .enter => if (self.syncFocused()) try self.requestSync(allocator), 792 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 793 else => {}, 794 } 795 } 796 797 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 798 if (!self.session.is_terminal) return; 799 if (self.session.host_request != null) return; 800 const button_id = self.button_id orelse return; 801 const child = self.box.children.getPtr(button_id) orelse return; 802 const button = switch (child.widget) { 803 .text_box => |*button| button, 804 else => return, 805 }; 806 try button.setContent(allocator, "syncing..."); 807 self.session.host_request = .sync_events; 808 } 809 810 fn syncFocused(self: *Header.View) bool { 811 const button_id = self.button_id orelse return false; 812 return self.box.getFocus().child_id == button_id; 813 } 814 815 pub fn getSelectedIndex(self: *Header.View) ?usize { 816 const id = self.box.getFocus().child_id orelse return null; 817 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 818 } 819 820 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 821 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 822 } 823 824 pub fn clearGrid(self: *Header.View) void { 825 self.box.clearGrid(); 826 } 827 828 pub fn getGrid(self: Header.View) ?Grid { 829 return self.box.getGrid(); 830 } 831 832 pub fn getFocus(self: *Header.View) *Focus { 833 return self.box.getFocus(); 834 } 835 }; 836 }; 837 838 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 839 if (comptime builtin.target.cpu.arch == .wasm32) return; 840 const io = session.io orelse return; 841 const source = session.local orelse return; 842 defer session.refresh_requested = true; 843 const failure = sync(io, allocator, source) catch |err| { 844 session.data.sync_failure = @errorName(err); 845 return; 846 }; 847 if (failure) |message| { 848 defer allocator.free(message); 849 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 850 return; 851 } 852 session.data.sync_failure = null; 853 switch (session.data.current_page) { 854 .repo_events => |route| { 855 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 856 session.data.current_page = first; 857 }, 858 else => {}, 859 } 860 } 861 862 fn remoteName( 863 comptime repo_kind: rp.RepoKind, 864 comptime repo_opts: rp.RepoOpts(repo_kind), 865 repo: *rp.Repo(repo_kind, repo_opts), 866 io: std.Io, 867 allocator: std.mem.Allocator, 868 ) !?[]u8 { 869 var remotes = try repo.listRemotes(io, allocator); 870 defer remotes.deinit(); 871 const names = remotes.sections.keys(); 872 if (names.len == 0) return null; 873 for (names) |name| { 874 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 875 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 876 } 877 return try allocator.dupe(u8, names[0]); 878 } 879 880 fn isLoopbackSshUrl(url: []const u8) bool { 881 const uri = std.Uri.parse(url) catch return false; 882 const host = switch (uri.host orelse return false) { 883 .raw => |value| value, 884 .percent_encoded => |value| value, 885 }; 886 return std.mem.eql(u8, host, "localhost") or 887 std.mem.eql(u8, host, "127.0.0.1") or 888 std.mem.eql(u8, host, "::1") or 889 std.mem.eql(u8, host, "[::1]"); 890 } 891 892 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 893 var message: std.Io.Writer.Allocating = .init(allocator); 894 defer message.deinit(); 895 896 switch (source.repo_kind) { 897 inline else => |repo_kind| { 898 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 899 defer any_repo.deinit(io, allocator); 900 switch (any_repo) { 901 inline else => |*repo| { 902 const repo_opts = repo.self_repo_opts; 903 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 904 defer allocator.free(remote_name); 905 906 var config = try repo.listConfig(io, allocator); 907 defer config.deinit(); 908 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 909 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 910 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 911 defer allocator.free(remote_section); 912 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 913 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 914 const push_url = remote_config.get("pushurl") orelse fetch_url; 915 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 916 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 917 defer allocator.free(fetch_ssh_command); 918 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 919 defer allocator.free(push_ssh_command); 920 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 921 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 922 923 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 924 defer allocator.free(fetch_refspec); 925 var fetch_opts = fetch_transport_opts; 926 fetch_opts.refspecs = &.{fetch_refspec}; 927 message.clearRetainingCapacity(); 928 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 929 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 930 return err; 931 }; 932 933 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 934 const local_oid = try repo.readRef(io, evt.events_ref); 935 const remote_oid = try repo.readRef(io, remote_ref); 936 if (local_oid == null and remote_oid == null) { 937 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 938 return null; 939 } 940 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 941 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 942 943 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 944 defer allocator.free(push_refspec); 945 message.clearRetainingCapacity(); 946 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 947 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 948 return err; 949 }; 950 message.clearRetainingCapacity(); 951 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 952 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 953 return err; 954 }; 955 }, 956 } 957 }, 958 } 959 return null; 960 } 961