╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
272 self.box.clearGrid(); 273 } 274 275 pub fn getGrid(self: Item) ?Grid { 276 return self.box.getGrid(); 277 } 278 279 pub fn getFocus(self: *Item) *Focus { 280 return self.box.getFocus(); 281 } 282 }; 283 284 // append one comment's metadata bar and body. 285 pub fn appendComment(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, entry: CommentWithId) !void { 286 var item = try Item.init(allocator, session, identity, thread_kind, entry); 287 errdefer item.deinit(allocator); 288 try box.children.put(allocator, item.getFocus().id, .{ .widget = .{ .repo_comment = item }, .rect = null, .min_size = null }); 289 } 290 291 pub fn appendCount(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), count: usize, singular: []const u8, plural: []const u8) !void { 292 var text_buf: [64]u8 = undefined; 293 const text = try std.fmt.bufPrint(&text_buf, "{d} {s}", .{ count, if (count == 1) singular else plural }); 294 var count_box = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 295 errdefer count_box.deinit(allocator); 296 try box.children.put(allocator, count_box.getFocus().id, .{ .widget = .{ .text_box = count_box }, .rect = null, .min_size = null }); 297 } 298 299 pub fn appendWindowNav(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, thread_id: []const u8, selected_id: ?[]const u8, window: Window) !void { 300 const has_prev = window.start > 0; 301 const has_more = window.start < window.count and window.count - window.start > page_size; 302 if (!has_prev and !has_more) return; 303 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 304 errdefer row.deinit(allocator); 305 if (has_prev) { 306 const start = window.start -| page_size; 307 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 308 var previous = try linkBox(allocator, session, "← previous", route orelse return error.RouteTooLong); 309 errdefer previous.deinit(allocator); 310 try row.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 311 } 312 if (has_more) { 313 const start = window.start + page_size; 314 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 315 var next = try linkBox(allocator, session, "next →", route orelse return error.RouteTooLong); 316 errdefer next.deinit(allocator); 317 try row.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 318 } 319 row.getFocus().child_id = row.children.keys()[0]; 320 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 321 } 322 323 fn commentsRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, selected_id: ?[]const u8, start: usize) ?ui.RoutablePage { 324 return if (selected_id) |id| 325 ui.RoutablePage.repoThreadCommentRoute(kind, identity, thread_id, id, start) 326 else 327 ui.RoutablePage.repoThreadCommentsRoute(kind, identity, thread_id, start); 328 } 329 330 fn commentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?ui.RoutablePage { 331 return ui.RoutablePage.repoThreadCommentNewRoute(kind, identity, thread_id, parent_id); 332 } 333 334 fn commentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 335 return ui.RoutablePage.repoThreadCommentEditRoute(kind, identity, thread_id, comment_id); 336 } 337 338 fn removeRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 339 return ui.RoutablePage.repoThreadRemoveRoute(kind, identity, thread_id, comment_id); 340 } 341 342 pub fn linkBox(allocator: std.mem.Allocator, session: *ui.Session, text: []const u8, route: ui.RoutablePage) !wgt.TextBox { 343 var box = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 344 errdefer box.deinit(allocator); 345 box.getFocus().mode = .all; 346 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 347 return box; 348 } 349