╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Comment.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
155 var bytes: [evt.event_id_size]u8 = undefined; 156 _ = std.fmt.hexToBytes(&bytes, id) catch return null; 157 if (!std.mem.eql(u8, id, &std.fmt.bytesToHex(bytes, .lower))) return null; 158 return bytes; 159 } 160 161 pub const Item = struct { 162 box: wgt.Box(ui.Widget), 163 164 const metadata_index: usize = 0; 165 const body_index: usize = 1; 166 const gap_index: usize = 2; 167 168 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, can_reply: bool, entry: CommentWithId) !Item { 169 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 170 errdefer box.deinit(allocator); 171 172 { 173 var bar = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 174 errdefer bar.deinit(allocator); 175 176 // the author sits on the left and the buttons on the right 177 var author = try ui.authorBox(allocator, session.page_arena, entry.author); 178 errdefer author.deinit(allocator); 179 try bar.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null, .flex = .shrink }); 180 181 var spacer = try ui.widget.Spacer.init(allocator); 182 errdefer spacer.deinit(allocator); 183 try bar.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 184 185 if (can_reply) { 186 var reply = try linkBox(allocator, session, "new reply", commentNewRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 187 errdefer reply.deinit(allocator); 188 try bar.children.put(allocator, reply.getFocus().id, .{ .widget = .{ .text_box = reply }, .rect = null, .min_size = .{ .width = "new reply".len + 2, .height = null } }); 189 } 190 191 if (entry.can_modify and !entry.comment.removed) { 192 var edit = try linkBox(allocator, session, "edit comment", commentEditRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 193 errdefer edit.deinit(allocator); 194 try bar.children.put(allocator, edit.getFocus().id, .{ .widget = .{ .text_box = edit }, .rect = null, .min_size = .{ .width = "edit comment".len + 2, .height = null } }); 195 } 196 197 if (!std.mem.eql(u8, &entry.comment.event.parent_id, &entry.comment.event.thread_id)) { 198 const parent_text = if (entry.parent_author) |parent_author| switch (parent_author) { 199 .unknown => "", 200 .email, .user_name => |text| text, 201 } else ""; 202 var parent = try wgt.TextBox.init(allocator, parent_text, .{ 203 .border_style = .single, 204 .round_corners = true, 205 .wrap_kind = .none, 206 .label = " replying to ", 207 }); 208 errdefer parent.deinit(allocator); 209 parent.getFocus().mode = .all; 210 const route = commentsRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.comment.event.parent_id, 0) orelse return error.RouteTooLong; 211 parent.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 212 try bar.children.put(allocator, parent.getFocus().id, .{ .widget = .{ .text_box = parent }, .rect = null, .min_size = .{ .width = @max(parent_text.len, " replying to ".len) + 2, .height = null } }); 213 } 214 215 var permalink = try linkBox(allocator, session, "permalink", commentsRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id, 0) orelse return error.RouteTooLong); 216 errdefer permalink.deinit(allocator); 217 try bar.children.put(allocator, permalink.getFocus().id, .{ .widget = .{ .text_box = permalink }, .rect = null, .min_size = .{ .width = "permalink".len + 2, .height = null } }); 218 219 if (entry.can_modify and !entry.comment.removed) { 220 var remove = try linkBox(allocator, session, "✕", removeRoute(thread_kind, identity, &entry.comment.event.thread_id, &entry.id) orelse return error.RouteTooLong); 221 errdefer remove.deinit(allocator); 222 try bar.children.put(allocator, remove.getFocus().id, .{ .widget = .{ .text_box = remove }, .rect = null, .min_size = .{ .width = 3, .height = null } }); 223 } 224 225 bar.getFocus().child_id = bar.children.keys()[0]; 226 try box.children.put(allocator, bar.getFocus().id, .{ .widget = .{ .box = bar }, .rect = null, .min_size = null }); 227 } 228 229 const body_text = if (entry.comment.removed) "(removed)" else entry.comment.event.body; 230 var body_box = try wgt.TextBox.init(allocator, body_text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .word }); 231 errdefer body_box.deinit(allocator); 232 body_box.getFocus().mode = .all; 233 try box.children.put(allocator, body_box.getFocus().id, .{ .widget = .{ .text_box = body_box }, .rect = null, .min_size = null }); 234 235 var gap = try wgt.Text.init(allocator, ""); 236 errdefer gap.deinit(allocator); 237 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 238 239 box.getFocus().child_id = box.children.keys()[metadata_index]; 240 241 return .{ .box = box }; 242 } 243 244 fn metadata(self: *Item) *wgt.Box(ui.Widget) { 245 return &self.box.children.values()[metadata_index].widget.box; 246 } 247 248 fn body(self: *Item) *wgt.TextBox { 249 return &self.box.children.values()[body_index].widget.text_box; 250 } 251 252 pub fn bodyFocused(self: *Item) bool { 253 return self.box.getFocus().child_id == self.body().getFocus().id; 254 } 255 256 pub fn focusMetadata(self: *Item, root_focus: *Focus) void { 257 const bar = self.metadata(); 258 root_focus.setFocus(bar.getFocus().child_id orelse bar.children.keys()[0]); 259 } 260 261 pub fn focusBody(self: *Item, root_focus: *Focus) void { 262 root_focus.setFocus(self.body().getFocus().id); 263 } 264 265 pub fn moveHorizontal(self: *Item, root_focus: *Focus, right: bool) bool { 266 if (self.bodyFocused()) return false; 267 const bar = self.metadata(); 268 const selected = bar.getFocus().child_id orelse return false; 269 var target = bar.children.getIndex(selected) orelse return false; 270 // step past the spacer between the author and the buttons 271 while (true) { 272 if (right) target += 1 else target = std.math.sub(usize, target, 1) catch return false; 273 if (target >= bar.children.count()) return false; 274 if (bar.children.values()[target].widget != .spacer) break; 275 } 276 root_focus.setFocus(bar.children.keys()[target]); 277 return true; 278 } 279 280 pub fn rowRect(self: *Item, body_row: bool) ?layout.IRect { 281 if (!body_row) return self.box.children.values()[metadata_index].rect; 282 var rect = self.box.children.values()[body_index].rect orelse return null; 283 if (self.box.children.values()[gap_index].rect) |gap| rect.size.height += gap.size.height; 284 return rect; 285 } 286 287 pub fn deinit(self: *Item, allocator: std.mem.Allocator) void { 288 self.box.deinit(allocator); 289 } 290 291 pub fn build(self: *Item, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 292 try self.box.build(allocator, constraint, root_focus); 293 } 294 295 pub fn input(self: *Item, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 296 try self.box.input(allocator, key, root_focus); 297 } 298 299 pub fn clearGrid(self: *Item) void { 300 self.box.clearGrid(); 301 } 302 303 pub fn getGrid(self: Item) ?Grid { 304 return self.box.getGrid(); 305 } 306 307 pub fn getFocus(self: *Item) *Focus { 308 return self.box.getFocus(); 309 } 310 }; 311 312 // append one comment's metadata bar and body. 313 pub fn appendComment(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), session: *ui.Session, identity: []const u8, thread_kind: evt.EventKind, can_reply: bool, entry: CommentWithId) !void { 314 var item = try Item.init(allocator, session, identity, thread_kind, can_reply, entry); 315 errdefer item.deinit(allocator); 316 try box.children.put(allocator, item.getFocus().id, .{ .widget = .{ .repo_comment = item }, .rect = null, .min_size = null }); 317 } 318 319 pub fn appendCount(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), count: usize, singular: []const u8, plural: []const u8) !void { 320 var text_buf: [64]u8 = undefined; 321 const text = try std.fmt.bufPrint(&text_buf, "{d} {s}", .{ count, if (count == 1) singular else plural }); 322 var count_box = try wgt.TextBox.init(allocator, text, .{ .border_style = .hidden, .wrap_kind = .none }); 323 errdefer count_box.deinit(allocator); 324 try box.children.put(allocator, count_box.getFocus().id, .{ .widget = .{ .text_box = count_box }, .rect = null, .min_size = null }); 325 } 326 327 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 { 328 const has_prev = window.start > 0; 329 const has_more = window.start < window.count and window.count - window.start > page_size; 330 if (!has_prev and !has_more) return; 331 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 332 errdefer row.deinit(allocator); 333 if (has_prev) { 334 const start = window.start -| page_size; 335 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 336 var previous = try linkBox(allocator, session, "← previous", route orelse return error.RouteTooLong); 337 errdefer previous.deinit(allocator); 338 try row.children.put(allocator, previous.getFocus().id, .{ .widget = .{ .text_box = previous }, .rect = null, .min_size = null }); 339 } 340 if (has_more) { 341 const start = window.start + page_size; 342 const route = commentsRoute(thread_kind, identity, thread_id, selected_id, start); 343 var next = try linkBox(allocator, session, "next →", route orelse return error.RouteTooLong); 344 errdefer next.deinit(allocator); 345 try row.children.put(allocator, next.getFocus().id, .{ .widget = .{ .text_box = next }, .rect = null, .min_size = null }); 346 } 347 row.getFocus().child_id = row.children.keys()[0]; 348 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 349 } 350 351 fn commentsRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, selected_id: ?[]const u8, start: usize) ?ui.RoutablePage { 352 return if (selected_id) |id| 353 ui.RoutablePage.repoThreadCommentRoute(kind, identity, thread_id, id, start) 354 else 355 ui.RoutablePage.repoThreadCommentsRoute(kind, identity, thread_id, start); 356 } 357 358 fn commentNewRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, parent_id: []const u8) ?ui.RoutablePage { 359 return ui.RoutablePage.repoThreadCommentNewRoute(kind, identity, thread_id, parent_id); 360 } 361 362 fn commentEditRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 363 return ui.RoutablePage.repoThreadCommentEditRoute(kind, identity, thread_id, comment_id); 364 } 365 366 fn removeRoute(kind: evt.EventKind, identity: []const u8, thread_id: []const u8, comment_id: []const u8) ?ui.RoutablePage { 367 return ui.RoutablePage.repoThreadRemoveRoute(kind, identity, thread_id, comment_id); 368 } 369 370 pub fn linkBox(allocator: std.mem.Allocator, session: *ui.Session, text: []const u8, route: ui.RoutablePage) !wgt.TextBox { 371 var box = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 372 errdefer box.deinit(allocator); 373 box.getFocus().mode = .all; 374 box.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}) }; 375 return box; 376 } 377