╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1228 1229 // on an edit page, register the issue's content as the inputs' 1230 // page-constant initial values, which the web overlay renders 1231 // into them. 1232 if (self.data.view == .edit) { 1233 if (self.data.selectedIssue()) |entry| { 1234 const fields = form.children.values(); 1235 try self.session.input_values.put(inputs_arena, fields[title_field_index].widget.text_input.getFocus().id, entry.issue.event.title); 1236 try self.session.input_values.put(inputs_arena, fields[tags_field_index].widget.text_input.getFocus().id, entry.issue.event.tags); 1237 try self.session.input_values.put(inputs_arena, fields[description_field_index].widget.text_input.getFocus().id, entry.issue.event.description); 1238 } 1239 } 1240 1241 // the resolve form's inputs prefill from the picked side, which 1242 // is url state and so page-constant like the edit values above 1243 if (self.data.view == .resolve) { 1244 for (form.children.values()) |*child| switch (child.widget) { 1245 .text_input => |*ti| if (self.resolvePrefill(ti.options.name)) |value| { 1246 try self.session.input_values.put(inputs_arena, ti.getFocus().id, value); 1247 }, 1248 else => {}, 1249 }; 1250 } 1251 } 1252 1253 try self.box.build(allocator, constraint, root_focus); 1254 } 1255 1256 fn refreshDetail(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1257 const sel = self.selectedIssueIndex(index) orelse return; 1258 if (self.detailed_index[index]) |d| if (d == sel) return; 1259 try self.populateDetail(allocator, index, sel); 1260 self.detailed_index[index] = sel; 1261 } 1262 1263 fn populateDetail(self: *View, allocator: std.mem.Allocator, index: usize, sel: usize) !void { 1264 const entry = self.window(index).issues[sel]; 1265 const inner = self.detailInner(index); 1266 // the /description page replaces its issue's detail with a back link 1267 // and the whole description; other issues keep their normal detail. 1268 const description_page = self.data.description_page and std.mem.eql(u8, entry.id, self.data.selected_id); 1269 1270 for (inner.children.values()) |*child| child.widget.deinit(allocator); 1271 inner.children.clearAndFree(allocator); 1272 inner.getFocus().child_id = null; 1273 self.author_id[index] = null; 1274 1275 // the tool row: the open/close and edit buttons; the description page 1276 // shows none. 1277 { 1278 const row = self.toolRow(index); 1279 for (row.children.values()) |*child| child.widget.deinit(allocator); 1280 row.children.clearAndFree(allocator); 1281 row.getFocus().child_id = null; 1282 row.getFocus().kind = .container; 1283 } 1284 if (!description_page) { 1285 const row = self.toolRow(index); 1286 const pa = self.session.page_arena.allocator(); 1287 1288 { 1289 var spacer = try ui.Spacer.init(allocator); 1290 errdefer spacer.deinit(allocator); 1291 try row.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1292 } 1293 1294 // a conflicted issue's only action is resolving it. the button 1295 // stays visible logged out; the resolve page shows the 1296 // unauthorized view then. 1297 if (entry.conflicted) { 1298 var button = try wgt.TextBox(ui.Widget).init(allocator, "resolve", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1299 errdefer button.deinit(allocator); 1300 button.getFocus().focusable = true; 1301 const route = ui.RoutablePage.repoIssuesResolveRoute(self.data.identity, entry.id, "") orelse return error.RouteTooLong; 1302 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1303 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "resolve".len + 2, .height = null } }); 1304 } else { 1305 // a logged-out session can't flip the status, so it gets no 1306 // open/close button (and no form action on the row) 1307 if (self.session.data.is_local or self.session.data.user_id != null) { 1308 const action: []const u8 = switch (entry.issue.event.status) { 1309 .open => "close", 1310 .closed => "open", 1311 }; 1312 row.getFocus().kind = .{ .custom = if (self.data.identity.len == 0) 1313 try std.fmt.allocPrint(pa, "form:/issues/{s}/{s}", .{ entry.id, action }) 1314 else 1315 try std.fmt.allocPrint(pa, "form:/repo/{s}/issues/{s}/{s}", .{ self.data.identity, entry.id, action }) }; 1316 1317 var button = try wgt.TextBox(ui.Widget).init(allocator, action, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1318 errdefer button.deinit(allocator); 1319 button.getFocus().focusable = true; 1320 // the renderer distinguishes plain clickables from buttons that 1321 // should POST to a server route by this kind. 1322 button.getFocus().kind = .{ .custom = "submit" }; 1323 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = action.len + 2, .height = null } }); 1324 } 1325 1326 // the edit button links to the issue's edit page. 1327 { 1328 var button = try wgt.TextBox(ui.Widget).init(allocator, "edit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1329 errdefer button.deinit(allocator); 1330 button.getFocus().focusable = true; 1331 const route = ui.RoutablePage.repoIssuesEditRoute(self.data.identity, entry.id) orelse return error.RouteTooLong; 1332 button.getFocus().kind = .{ .custom = try std.fmt.allocPrint(pa, "a:{s}", .{try route.toUrl(self.session.page_arena)}) }; 1333 try row.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = "edit".len + 2, .height = null } }); 1334 } 1335 } 1336 1337 // the resolve button on a conflicted issue, else the open/close 1338 // button when present, the edit button otherwise 1339 row.getFocus().child_id = row.children.keys()[button_in_row_index]; 1340 } 1341 1342 if (description_page) { 1343 // the back link, in the title slot so the pane's input handling 1344 // applies to it unchanged. 1345 var tb = try wgt.TextBox(ui.Widget).init(allocator, "← back to issue", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1346 errdefer tb.deinit(allocator); 1347 tb.getFocus().focusable = true; 1348 tb.getFocus().kind = .{ .custom = try issuesLink(self.session.page_arena, self.data.identity, entry.issue.event.status, "", entry.id) }; 1349 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1350 } else { 1351 // the issue's title as a focusable word-wrapped text box. 1352 { 1353 var tb = try wgt.TextBox(ui.Widget).init(allocator, entry.issue.event.title, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " title " }); 1354 errdefer tb.deinit(allocator); 1355 tb.getFocus().focusable = true; 1356 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1357 } 1358 1359 self.author_id[index] = blk: { 1360 var tb = try ui.authorBox(allocator, self.session.page_arena, entry.author); 1361 errdefer tb.deinit(allocator); 1362 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1363 break :blk tb.getFocus().id; 1364 }; 1365 1366 // the issue's tags, each linking to this status's list filtered to 1367 // that tag. 1368 { 1369 var items: std.ArrayList(ui.TagFlow.Item) = .empty; 1370 defer items.deinit(allocator); 1371 var tag_iter = evt.Issue.tagIterator(entry.issue.event.tags); 1372 while (tag_iter.next()) |tag| { 1373 if (tag.len == 0) continue; 1374 try items.append(allocator, .{ .text = tag, .link = try tagLink(self.session.page_arena, self.data.identity, entry.issue.event.status, tag) }); 1375 } 1376 if (items.items.len > 0) { 1377 var tf = try ui.TagFlow.init(allocator); 1378 errdefer tf.deinit(allocator); 1379 try tf.setItems(allocator, items.items); 1380 try inner.children.put(allocator, tf.getFocus().id, .{ .widget = .{ .tag_flow = tf }, .rect = null, .min_size = null }); 1381 } 1382 } 1383 } 1384 1385 // the description as a focusable word-wrapped text box. past the limit 1386 // it's cut at the last whole line and links to the /description page, 1387 // which shows the whole thing. 1388 self.description_id[index] = blk: { 1389 const whole = entry.issue.event.description; 1390 const cut_short = !description_page and whole.len > max_description_size; 1391 const shown = if (cut_short) 1392 std.mem.trimEnd(u8, whole[0 .. std.mem.lastIndexOfScalar(u8, whole[0..max_description_size], '\n') orelse 0], " \t\r\n") 1393 else if (whole.len == 0) 1394 "(no description)" 1395 else 1396 whole; 1397 var tb = try wgt.TextBox(ui.Widget).init(allocator, shown, .{ 1398 .border_style = .single, 1399 .rounded_corners = true, 1400 .wrap_kind = .word, 1401 .label = " description ", 1402 .bottom_label = if (cut_short) " click or press enter to see more " else "", 1403 }); 1404 errdefer tb.deinit(allocator); 1405 tb.getFocus().focusable = true; 1406 if (cut_short) tb.getFocus().kind = .{ .custom = try descriptionLink(self.session.page_arena, self.data.identity, entry.id) }; 1407 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1408 break :blk tb.getFocus().id; 1409 }; 1410 1411 // select the title by default 1412 inner.getFocus().child_id = inner.children.keys()[title_child_index]; 1413 1414 // reset the scroll to the top for the newly-shown issue: directly on the 1415 // terminal (the wasm offset), and via a version bump on the web (so the 1416 // renderer's scroll id changes and JS drops the preserved position). 1417 const sc = self.detailScroll(index); 1418 sc.x = 0; 1419 sc.y = 0; 1420 sc.getFocus().version +%= 1; 1421 } 1422 1423 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1424 if (self.headerActive()) { 1425 // down from the tabs re-enters the stack if the selected view has 1426 // something to focus; other keys move the tabs. 1427 if (inp.vertDirection(key) == .down) { 1428 const enterable = if (self.viewStack().getSelected()) |selected| switch (selected.*) { 1429 .tag_flow => |*tf| tf.text_boxes.items.len > 0, 1430 // nothing focusable in the unauthorized view 1431 .unauthorized => false, 1432 else => true, 1433 } else false; 1434 if (enterable) root_focus.setFocus(self.box.children.keys()[stack_index]); 1435 } else { 1436 try self.header().input(allocator, key, root_focus); 1437 } 1438 return; 1439 } 1440 if (self.tagsViewActive()) { 1441 self.tagsViewInput(key, root_focus); 1442 return; 1443 } 1444 if (self.formViewActive()) { 1445 if (self.data.view == .resolve) { 1446 try self.resolveInput(allocator, key, root_focus); 1447 } else { 1448 try self.formInput(allocator, key, root_focus); 1449 } 1450 return; 1451 } 1452 const i = self.selectedSplitIndex() orelse return; 1453 if (self.detailActive(i)) { 1454 try self.detailInput(allocator, i, key, root_focus); 1455 } else { 1456 try self.listInput(i, key, root_focus); 1457 } 1458 } 1459 1460 // arrow keys move the tag selection; up from the top row crosses to the 1461 // header tabs. 1462 fn tagsViewInput(self: *View, key: Key, root_focus: *Focus) void { 1463 const tf = self.tagsView(); 1464 const cid = tf.focus.child_id orelse return; 1465 const cur = tf.indexOfFocusId(cid) orelse return; 1466 const count = tf.text_boxes.items.len; 1467 switch (key) { 1468 .arrow_left => if (cur > 0) root_focus.setFocus(tf.text_boxes.items[cur - 1].getFocus().id), 1469 .arrow_right => if (cur + 1 < count) root_focus.setFocus(tf.text_boxes.items[cur + 1].getFocus().id), 1470 .arrow_up => if (tf.rowStep(cur, false)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id) else self.focusHeader(root_focus), 1471 .arrow_down => if (tf.rowStep(cur, true)) |i| root_focus.setFocus(tf.text_boxes.items[i].getFocus().id), 1472 .home => root_focus.setFocus(tf.text_boxes.items[0].getFocus().id), 1473 .end => root_focus.setFocus(tf.text_boxes.items[count - 1].getFocus().id), 1474 else => {}, 1475 } 1476 } 1477 1478 fn listInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1479 // up/down (and the scroll wheel) move the selection a row; page up/down 1480 // jump a fixed amount. right/Enter cross into the detail pane. up from 1481 // the top row crosses into the header tabs. 1482 if (inp.rowDelta(key, @intCast(self.listBox(index).children.count()))) |delta| { 1483 const lb = self.listBox(index); 1484 const at_top = if (lb.getFocus().child_id) |cid| lb.children.getIndex(cid) == 0 else true; 1485 if (delta < 0 and at_top) return self.focusHeader(root_focus); 1486 ui.moveRowFocus(lb, self.listScroll(index), root_focus, delta); 1487 return; 1488 } 1489 switch (key) { 1490 .enter, .arrow_right => try self.focusDetail(index, root_focus), 1491 else => {}, 1492 } 1493 } 1494 1495 fn detailInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1496 if (self.toolRowFocused(index)) { 1497 try self.toolRowInput(allocator, index, key, root_focus); 1498 } else if (self.titleFocused(index)) { 1499 try self.titleInput(index, key, root_focus); 1500 } else if (self.authorFocused(index)) { 1501 try self.authorInput(index, key, root_focus); 1502 } else if (self.tagsFocused(index)) { 1503 try self.tagsInput(index, key, root_focus); 1504 } else { 1505 try self.descriptionInput(index, key, root_focus); 1506 } 1507 } 1508 1509 // the tool row: enter or a click on the open/close button flips the 1510 // issue's status; the edit and resolve buttons are a: links the host 1511 // follows. arrows cross between the buttons and to the neighboring 1512 // widgets. 1513 fn toolRowInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 1514 const row = self.toolRow(index); 1515 const cur = if (row.getFocus().child_id) |cid| row.children.getIndex(cid) orelse return else return; 1516 const on_status = cur == button_in_row_index and self.statusButton(index) != null; 1517 switch (key) { 1518 .arrow_left => if (cur > button_in_row_index) root_focus.setFocus(row.children.keys()[cur - 1]) else try self.focusList(index, root_focus), 1519 .arrow_right => if (cur + 1 < row.children.count()) root_focus.setFocus(row.children.keys()[cur + 1]), 1520 .arrow_up => self.focusHeader(root_focus), 1521 .arrow_down => self.focusTitle(index, root_focus), 1522 .enter => if (on_status) try self.toggleIssueStatus(allocator, index), 1523 .mouse => |mouse| switch (mouse.action) { 1524 .scroll => |dir| { 1525 const sc = self.detailScroll(index); 1526 sc.y += if (dir == .up) @as(isize, -1) else 1; 1527 sc.clampToContent(); 1528 }, 1529 else => if (self.statusButton(index)) |button| { 1530 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 1531 }, 1532 }, 1533 else => {}, 1534 } 1535 } 1536 1537 fn titleInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1538 const sc = self.detailScroll(index); 1539 switch (key) { 1540 .arrow_left => try self.focusList(index, root_focus), 1541 .arrow_up => self.focusToolRow(index, root_focus), 1542 .arrow_down => if (self.authorPresent(index)) self.focusAuthor(index, root_focus) else try self.focusDescription(index, root_focus), 1543 .mouse => |mouse| switch (mouse.action) { 1544 .scroll => |dir| { 1545 sc.y += if (dir == .up) @as(isize, -1) else 1; 1546 sc.clampToContent(); 1547 }, 1548 else => {}, 1549 }, 1550 else => {}, 1551 } 1552 } 1553 1554 // the author box's a: link (when it has one) is followed by the host on 1555 // enter / a click; arrows cross to the neighboring widgets. 1556 fn authorInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1557 const sc = self.detailScroll(index); 1558 switch (key) { 1559 .arrow_left => try self.focusList(index, root_focus), 1560 .arrow_up => self.focusTitle(index, root_focus), 1561 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 1562 .mouse => |mouse| switch (mouse.action) { 1563 .scroll => |dir| { 1564 sc.y += if (dir == .up) @as(isize, -1) else 1; 1565 sc.clampToContent(); 1566 }, 1567 else => {}, 1568 }, 1569 else => {}, 1570 } 1571 } 1572 1573 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1574 const sc = self.detailScroll(index); 1575 switch (key) { 1576 .arrow_left => return self.focusList(index, root_focus), 1577 // once the scroll can't move further, cross into the tags (or the 1578 // open/close button when the issue has none). 1579 .arrow_up => { 1580 const before = sc.y; 1581 sc.y -= 1; 1582 sc.clampToContent(); 1583 if (sc.y == before) { 1584 if (self.tagFlow(index) != null) 1585 try self.focusTags(index, root_focus) 1586 else if (self.authorPresent(index)) 1587 self.focusAuthor(index, root_focus) 1588 else 1589 self.focusTitle(index, root_focus); 1590 } 1591 return; 1592 }, 1593 .arrow_down => sc.y += 1, 1594 .page_up => sc.y -= 10, 1595 .page_down => sc.y += 10, 1596 .home => sc.y = 0, 1597 .end => sc.y = std.math.maxInt(isize), 1598 .mouse => |mouse| switch (mouse.action) { 1599 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 1600 else => {}, 1601 }, 1602 else => return, 1603 } 1604 sc.clampToContent(); 1605 } 1606 1607 // up/down (and tab/shift+tab) move between a form's fields; up from the 1608 // title crosses into the header tabs. the multiline description keeps 1609 // enter and any up/down that has a row to move to. 1610 fn formInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1611 const form = self.issueForm() orelse return; 1612 const cid = form.getFocus().child_id orelse return; 1613 const cur = form.children.getIndex(cid) orelse return; 1614 1615 if (cur == description_field_index) { 1616 const child = &form.children.values()[cur]; 1617 if (try multilineKeepsKey(&child.widget.text_input, allocator, key)) 1618 return child.widget.input(allocator, key, root_focus); 1619 } 1620 1621 switch (key) { 1622 .arrow_up, .back_tab => if (cur > 0) 1623 root_focus.setFocus(form.children.keys()[cur - 1]) 1624 else 1625 self.focusHeader(root_focus), 1626 .arrow_down, .tab => if (cur < submit_field_index) { 1627 root_focus.setFocus(form.children.keys()[cur + 1]); 1628 }, 1629 .enter => if (cur == submit_field_index) try self.submitForm(allocator), 1630 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 1631 try self.submitForm(allocator); 1632 }, 1633 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 1634 } 1635 } 1636 1637 // the multiline hunk inputs keep enter and any up/down that has a row to 1638 // move to; enter on a "use this" link is a navigation the host follows 1639 // before this runs. 1640 fn resolveInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1641 const form = self.issueForm() orelse return; 1642 const cid = form.getFocus().child_id orelse return; 1643 const cur = form.children.getIndex(cid) orelse return; 1644 const child = &form.children.values()[cur]; 1645 1646 if (child.widget == .text_input) { 1647 const ti = &child.widget.text_input; 1648 if (ti.options.multiline and try multilineKeepsKey(ti, allocator, key)) 1649 return child.widget.input(allocator, key, root_focus); 1650 } 1651 1652 const on_submit = isSubmitButton(&child.widget); 1653 1654 switch (key) { 1655 .arrow_up, .back_tab => if (resolveStep(form, cur, false)) |i| self.focusResolveChild(form, i, root_focus) else self.focusHeader(root_focus), 1656 .arrow_down, .tab => if (resolveStep(form, cur, true)) |i| self.focusResolveChild(form, i, root_focus), 1657 .arrow_left, .arrow_right => switch (child.widget) { 1658 .box => |*row| { 1659 const row_cid = row.getFocus().child_id orelse return; 1660 const row_index = row.children.getIndex(row_cid) orelse return; 1661 if (key == .arrow_left) { 1662 if (row_index > 0) root_focus.setFocus(row.children.keys()[row_index - 1]); 1663 } else if (row_index + 1 < row.children.count()) { 1664 root_focus.setFocus(row.children.keys()[row_index + 1]); 1665 } 1666 }, 1667 else => try child.widget.input(allocator, key, root_focus), 1668 }, 1669 .enter => if (on_submit) try self.submitResolution(allocator), 1670 .mouse => |mouse| if (on_submit and inp.leftClickOn(root_focus, cid, mouse)) { 1671 try self.submitResolution(allocator); 1672 } else if (mouse.action == .scroll) { 1673 if (self.resolveScroll()) |sc| { 1674 sc.y += if (mouse.action.scroll == .up) @as(isize, -1) else 1; 1675 sc.clampToContent(); 1676 } 1677 }, 1678 else => try child.widget.input(allocator, key, root_focus), 1679 } 1680 } 1681 1682 // a multiline input keeps enter and any up/down that has a row to move to 1683 fn multilineKeepsKey(text_input: *wgt.TextInput(ui.Widget), allocator: std.mem.Allocator, key: Key) !bool { 1684 return switch (key) { 1685 .enter => true, 1686 .arrow_up => !try text_input.cursorOnFirstRow(allocator), 1687 .arrow_down => !try text_input.cursorOnLastRow(allocator), 1688 else => false, 1689 }; 1690 } 1691 1692 // whether the widget is a button that posts its form 1693 fn isSubmitButton(widget: *const ui.Widget) bool { 1694 return switch (widget.*) { 1695 .text_box => |*tb| switch (tb.box.focus.kind) { 1696 .custom => |custom| std.mem.eql(u8, custom, "submit"), 1697 else => false, 1698 }, 1699 else => false, 1700 }; 1701 } 1702 1703 // the neighboring focusable resolve-form child, skipping the spacer and 1704 // the blank gap rows 1705 fn resolveStep(form: *wgt.Box(ui.Widget), cur: usize, down: bool) ?usize { 1706 var i = cur; 1707 while (true) { 1708 if (down) { 1709 i += 1; 1710 if (i >= form.children.count()) return null; 1711 } else { 1712 if (i == 0) return null; 1713 i -= 1; 1714 } 1715 switch (form.children.values()[i].widget) { 1716 .spacer, .text => continue, 1717 else => return i, 1718 } 1719 } 1720 } 1721 1722 // focus the resolve-form child at `i` (a version row focuses its selected 1723 // child) and keep it visible on the terminal 1724 fn focusResolveChild(self: *View, form: *wgt.Box(ui.Widget), i: usize, root_focus: *Focus) void { 1725 const child = &form.children.values()[i]; 1726 switch (child.widget) { 1727 .box => |*row| root_focus.setFocus(row.getFocus().child_id orelse form.children.keys()[i]), 1728 else => root_focus.setFocus(form.children.keys()[i]), 1729 } 1730 if (self.session.is_terminal) { 1731 if (self.resolveScroll()) |sc| { 1732 if (child.rect) |rect| sc.scrollToRect(rect); 1733 } 1734 } 1735 } 1736 1737 // re-emit the conflicted issue's event with the resolved content; any 1738 // event on the issue settles its conflict entry. this is the terminal 1739 // path; the web posts the form to the resolve route. 1740 fn submitResolution(self: *View, allocator: std.mem.Allocator) !void { 1741 if (comptime wasm) return; 1742 const io = self.session.io orelse return; 1743 const src = self.data.repo_source orelse return; 1744 const entry = self.data.selectedIssue() orelse return; 1745 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1746 const form = self.issueForm() orelse return; 1747 1748 // gather the inputs by name; the d<n> hunk inputs appear in chunk order 1749 var title: ?[]u8 = null; 1750 var tags: ?[]u8 = null; 1751 var hunks: std.ArrayList([]const u8) = .empty; 1752 defer { 1753 if (title) |t| allocator.free(t); 1754 if (tags) |t| allocator.free(t); 1755 for (hunks.items) |h| allocator.free(h); 1756 hunks.deinit(allocator); 1757 } 1758 for (form.children.values()) |*child| switch (child.widget) { 1759 .text_input => |*ti| { 1760 const text = try ti.text(allocator); 1761 errdefer allocator.free(text); 1762 if (std.mem.eql(u8, ti.options.name, "title")) { 1763 title = text; 1764 } else if (std.mem.eql(u8, ti.options.name, "tags")) { 1765 tags = text; 1766 } else { 1767 try hunks.append(allocator, text); 1768 } 1769 }, 1770 else => {}, 1771 }; 1772 1773 var id_bytes: [evt.event_id_size]u8 = undefined; 1774 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1775 1776 switch (src.repo_kind) { 1777 inline else => |repo_kind| { 1778 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1779 defer any_repo.deinit(io, allocator); 1780 switch (any_repo) { 1781 inline else => |*repo| evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .resolve = .{ 1782 .title = title, 1783 .tags = tags, 1784 .hunks = hunks.items, 1785 } }, author_email) catch |err| switch (err) { 1786 // leave the form up for correction 1787 error.InvalidFields => return, 1788 else => |e| return e, 1789 }, 1790 } 1791 }, 1792 } 1793 1794 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1795 try self.session.navigate(route); 1796 } 1797 1798 // the terminal submit paths: the new form commits a new-issue event, the 1799 // edit form re-emits the selected issue's event with the form's content. 1800 // the web posts the forms to their routes instead. 1801 fn submitForm(self: *View, allocator: std.mem.Allocator) !void { 1802 if (self.data.view == .edit) { 1803 try self.submitEditedIssue(allocator); 1804 } else { 1805 try self.submitNewIssue(allocator); 1806 } 1807 } 1808 1809 // commit the new issue to the repo's events branch and navigate to it. 1810 // this is the terminal path; the web posts the form to the issue route, 1811 // so the wasm side never runs (or compiles) the repo access below. 1812 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1813 if (comptime wasm) return; 1814 const io = self.session.io orelse return; 1815 const src = self.data.repo_source orelse return; 1816 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1817 1818 const form = self.issueForm() orelse return; 1819 const title_input = &form.children.values()[title_field_index].widget.text_input; 1820 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1821 const description_input = &form.children.values()[description_field_index].widget.text_input; 1822 1823 const title = try title_input.text(allocator); 1824 defer allocator.free(title); 1825 const tags = try tags_input.text(allocator); 1826 defer allocator.free(tags); 1827 const description = try description_input.text(allocator); 1828 defer allocator.free(description); 1829 1830 if (!evt.Issue.fieldsValid(title, tags)) return; 1831 1832 var id_bytes: [evt.event_id_size]u8 = undefined; 1833 io.random(&id_bytes); 1834 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1835 1836 const event = evt.EventWithId{ 1837 .id = event_id_hex, 1838 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1839 .author_email = author_email, 1840 .event = .{ .issue = .{ 1841 .title = title, 1842 .description = description, 1843 .tags = tags, 1844 } }, 1845 }; 1846 1847 switch (src.repo_kind) { 1848 inline else => |repo_kind| { 1849 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1850 defer any_repo.deinit(io, allocator); 1851 switch (any_repo) { 1852 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1853 } 1854 }, 1855 } 1856 1857 // wipe the form so a return visit starts fresh 1858 title_input.clear(allocator); 1859 tags_input.clear(allocator); 1860 description_input.clear(allocator); 1861 1862 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1863 try self.session.navigate(route); 1864 } 1865 1866 // re-emit the selected issue's event with the form's content (status 1867 // preserved), then reload the issue's page. this is the terminal path; 1868 // the web posts the form to the edit route. 1869 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1870 if (comptime wasm) return; 1871 const io = self.session.io orelse return; 1872 const src = self.data.repo_source orelse return; 1873 const entry = self.data.selectedIssue() orelse return; 1874 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1875 1876 const form = self.issueForm() orelse return; 1877 const title_input = &form.children.values()[title_field_index].widget.text_input; 1878 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1879 const description_input = &form.children.values()[description_field_index].widget.text_input; 1880 1881 const title = try title_input.text(allocator); 1882 defer allocator.free(title); 1883 const tags = try tags_input.text(allocator); 1884 defer allocator.free(tags); 1885 const description = try description_input.text(allocator); 1886 defer allocator.free(description); 1887 1888 if (!evt.Issue.fieldsValid(title, tags)) return; 1889 1890 var id_bytes: [evt.event_id_size]u8 = undefined; 1891 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1892 1893 switch (src.repo_kind) { 1894 inline else => |repo_kind| { 1895 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1896 defer any_repo.deinit(io, allocator); 1897 switch (any_repo) { 1898 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1899 .title = title, 1900 .tags = tags, 1901 .description = description, 1902 } }, author_email), 1903 } 1904 }, 1905 } 1906 1907 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1908 try self.session.navigate(route); 1909 } 1910 1911 // flip the shown issue's status by re-emitting its event, then reload the 1912 // page rooted at the issue so the view reflects the change. this is the 1913 // terminal path; the web posts the button's form to the status route. 1914 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1915 if (comptime wasm) return; 1916 const io = self.session.io orelse return; 1917 const src = self.data.repo_source orelse return; 1918 const sel = self.detailed_index[index] orelse return; 1919 const entry = self.window(index).issues[sel]; 1920 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1921 1922 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1923 .open => .closed, 1924 .closed => .open, 1925 }; 1926 1927 var id_bytes: [evt.event_id_size]u8 = undefined; 1928 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1929 1930 switch (src.repo_kind) { 1931 inline else => |repo_kind| { 1932 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1933 defer any_repo.deinit(io, allocator); 1934 switch (any_repo) { 1935 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1936 } 1937 }, 1938 } 1939 1940 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1941 try self.session.navigate(route); 1942 } 1943 1944 // arrow keys move the tag selection; at the flow's edges focus crosses to 1945 // the neighboring widgets. 1946 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1947 const tf = self.tagFlow(index) orelse return; 1948 const cid = tf.focus.child_id orelse return; 1949 const cur = tf.indexOfFocusId(cid) orelse return; 1950 const count = tf.text_boxes.items.len; 1951 const sc = self.detailScroll(index); 1952 switch (key) { 1953 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1954 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1955 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1956 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1957 .home => self.focusTag(index, tf, root_focus, 0), 1958 .end => self.focusTag(index, tf, root_focus, count - 1), 1959 .mouse => |mouse| switch (mouse.action) { 1960 .scroll => |dir| { 1961 sc.y += if (dir == .up) @as(isize, -1) else 1; 1962 sc.clampToContent(); 1963 }, 1964 else => {}, 1965 }, 1966 else => {}, 1967 } 1968 } 1969 1970 const button_in_row_index: usize = 1; 1971 const title_child_index: usize = 0; 1972 const tags_child_index: usize = 2; 1973 1974 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1975 const inner = self.detailInner(index); 1976 if (inner.children.count() <= tags_child_index) return null; 1977 return switch (inner.children.values()[tags_child_index].widget) { 1978 .tag_flow => |*tf| tf, 1979 else => null, 1980 }; 1981 } 1982 1983 fn tagsFocused(self: *View, index: usize) bool { 1984 const inner = self.detailInner(index); 1985 const cid = inner.getFocus().child_id orelse return false; 1986 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1987 } 1988 1989 // the open/close button inside the detail frame's tool row; a logged-out 1990 // session's or conflicted issue's row has none. 1991 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1992 const row = self.toolRow(index); 1993 if (row.children.count() <= button_in_row_index) return null; 1994 const child = &row.children.values()[button_in_row_index].widget; 1995 if (!isSubmitButton(child)) return null; 1996 return &child.text_box; 1997 } 1998 1999 fn toolRowFocused(self: *View, index: usize) bool { 2000 const outer = self.detailOuter(index); 2001 const cid = outer.getFocus().child_id orelse return false; 2002 return outer.children.getIndex(cid) == tool_row_index; 2003 } 2004 2005 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 2006 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 2007 // keep the tag visible on the terminal: its rect offset by the flow's 2008 // position in the pane. 2009 if (self.session.is_terminal and item < tf.rects.items.len) { 2010 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 2011 var rect = tf.rects.items[item]; 2012 rect.x += flow_rect.x; 2013 rect.y += flow_rect.y; 2014 self.detailScroll(index).scrollToRect(rect); 2015 } 2016 } 2017 } 2018 2019 fn titleFocused(self: *View, index: usize) bool { 2020 const inner = self.detailInner(index); 2021 const cid = inner.getFocus().child_id orelse return false; 2022 return inner.children.getIndex(cid) == title_child_index; 2023 } 2024 2025 fn authorPresent(self: *View, index: usize) bool { 2026 return self.author_id[index] != null; 2027 } 2028 2029 fn authorFocused(self: *View, index: usize) bool { 2030 const inner = self.detailInner(index); 2031 const cid = inner.getFocus().child_id orelse return false; 2032 return cid == self.author_id[index]; 2033 } 2034 2035 // the author sits right under the title at the top of the pane, so 2036 // focusing it scrolls there. 2037 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 2038 const id = self.author_id[index] orelse return; 2039 root_focus.setFocus(id); 2040 self.detailScroll(index).y = 0; 2041 } 2042 2043 // the title sits at the top of the pane, so focusing it scrolls there. 2044 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 2045 const inner = self.detailInner(index); 2046 if (inner.children.count() == 0) return; 2047 root_focus.setFocus(inner.children.keys()[title_child_index]); 2048 self.detailScroll(index).y = 0; 2049 } 2050 2051 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 2052 const tf = self.tagFlow(index) orelse return; 2053 if (tf.text_boxes.items.len == 0) return; 2054 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 2055 const item = tf.indexOfFocusId(cid) orelse 0; 2056 self.focusTag(index, tf, root_focus, item); 2057 } 2058 2059 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 2060 if (self.description_id[index]) |id| root_focus.setFocus(id); 2061 } 2062 2063 // return to the tool row's last-focused button (the header when the 2064 // description page shows no row). 2065 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 2066 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 2067 root_focus.setFocus(cid); 2068 } 2069 2070 // enter the detail pane. an empty pane (no issues) can't be entered. 2071 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 2072 if (self.detailInner(index).children.count() == 0) return; 2073 root_focus.setFocus(self.detailOuter(index).getFocus().id); 2074 } 2075 2076 // return to the list. 2077 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 2078 root_focus.setFocus(self.listScroll(index).getFocus().id); 2079 } 2080 2081 // cross to the header tabs above the stack. 2082 fn focusHeader(self: *View, root_focus: *Focus) void { 2083 root_focus.setFocus(self.box.children.keys()[header_index]); 2084 } 2085 2086 pub fn clearGrid(self: *View) void { 2087 self.box.clearGrid(); 2088 } 2089 2090 pub fn getGrid(self: View) ?Grid { 2091 return self.box.getGrid(); 2092 } 2093 2094 pub fn getFocus(self: *View) *Focus { 2095 return self.box.getFocus(); 2096 } 2097 2098 // for the parent's "scroll up at the top jumps to the header" check: at the 2099 // top only while the header tabs hold focus, so up from the split 2100 // crosses into the tabs first. 2101 pub fn getSelectedIndex(self: *View) ?usize { 2102 return if (self.headerActive()) 0 else 1; 2103 } 2104 }; 2105 2106 // the "a:" navigation link for `status`'s issues page filtered to the 2107 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 2108 // a rooted url ignores `status` (it derives its view from the issue's own); 2109 // with an empty `id` this is the bare list link. 2110 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 2111 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 2112 const url = try route.toUrl(page_arena); 2113 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2114 } 2115 2116 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 2117 // href is only followed with js off. 2118 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2119 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 2120 const url = try route.toUrl(page_arena); 2121 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 2122 } 2123 2124 // the "a:" link to the window `status_maybe` names, keeping the tag filter, 2125 // or the conflicts window when null, rooted at issue `id`. 2126 fn windowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, status_maybe: ?evt.Issue.Status, id: []const u8) ![]const u8 { 2127 if (status_maybe) |status| return issuesLink(page_arena, data.identity, status, data.tag, id); 2128 return conflictsLink(page_arena, data.identity, id); 2129 } 2130 2131 // the "a:" link to the conflicts list rooted at issue `start` ("" = the 2132 // first window). 2133 fn conflictsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start: []const u8) ![]const u8 { 2134 const route = ui.RoutablePage.repoIssuesConflictsRoute(identity, start) orelse return error.RouteTooLong; 2135 const url = try route.toUrl(page_arena); 2136 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2137 } 2138 2139 // the "a:" link to the whole-description page of issue `id` within `identity`. 2140 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 2141 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 2142 const url = try route.toUrl(page_arena); 2143 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2144 } 2145 2146 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 2147 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 2148 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 2149 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 2150 const url = try route.toUrl(page_arena); 2151 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2152 } 2153 2154 // tabs switching between the issues page's views. 2155 pub const Header = struct { 2156 box: wgt.Box(ui.Widget), 2157 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 2158 2159 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 2160 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2161 errdefer box.deinit(allocator); 2162 2163 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 2164 errdefer tab_ids.deinit(allocator); 2165 2166 const aa = session.page_arena.allocator(); 2167 2168 // a list tab per status, labeled with its listing's issue count 2169 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 2170 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 2171 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2172 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 2173 try addTab(allocator, &box, &tab_ids, label, link); 2174 } 2175 2176 // tags tab, labeled with the active tag filter 2177 { 2178 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 2179 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 2180 const label = if (data.tag.len == 0) "tags" else blk: { 2181 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 2182 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 2183 }; 2184 try addTab(allocator, &box, &tab_ids, label, tags_link); 2185 } 2186 2187 // new-issue tab; an edit or resolve url shows its tab in this place 2188 { 2189 const route = switch (data.view) { 2190 .edit => ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong, 2191 .resolve => ui.RoutablePage.repoIssuesResolveRoute(data.identity, data.selected_id, data.theirs_picks) orelse return error.RouteTooLong, 2192 else => ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong, 2193 }; 2194 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2195 const label: []const u8 = switch (data.view) { 2196 .edit => "edit", 2197 .resolve => "resolve", 2198 else => "new", 2199 }; 2200 try addTab(allocator, &box, &tab_ids, label, link); 2201 } 2202 2203 // conflicts tab, labeled with the conflict count 2204 if (data.conflicts.count > 0) { 2205 const route = ui.RoutablePage.repoIssuesConflictsRoute(data.identity, "") orelse return error.RouteTooLong; 2206 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 2207 const label = try std.fmt.allocPrint(aa, "conflicts ({d})", .{data.conflicts.count}); 2208 try addTab(allocator, &box, &tab_ids, label, link); 2209 } 2210 2211 var self = Header{ .box = box, .tab_ids = tab_ids }; 2212 // the tab matching the page's view is selected initially. 2213 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 2214 return self; 2215 } 2216 2217 fn addTab(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), tab_ids: *std.AutoArrayHashMapUnmanaged(usize, void), label: []const u8, link: []const u8) !void { 2218 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2219 errdefer text_box.deinit(allocator); 2220 text_box.getFocus().focusable = true; 2221 text_box.getFocus().kind = .{ .custom = link }; 2222 try tab_ids.put(allocator, text_box.getFocus().id, {}); 2223 try box.children.put(allocator, text_box.getFocus().id, .{ 2224 .widget = .{ .text_box = text_box }, 2225 .rect = null, 2226 .min_size = .{ .width = label.len + 2, .height = null }, 2227 }); 2228 } 2229 2230 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 2231 self.box.deinit(allocator); 2232 self.tab_ids.deinit(allocator); 2233 } 2234 2235 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2236 self.clearGrid(); 2237 // only the selected tab shows its border 2238 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 2239 switch (child.widget) { 2240 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 2241 else => {}, 2242 } 2243 } 2244 try self.box.build(allocator, constraint, root_focus); 2245 } 2246 2247 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2248 _ = allocator; 2249 const current_tab = self.currentTabIndex() orelse return; 2250 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 2251 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 2252 } 2253 } 2254 2255 pub fn clearGrid(self: *Header) void { 2256 self.box.clearGrid(); 2257 } 2258 2259 pub fn getGrid(self: Header) ?Grid { 2260 return self.box.getGrid(); 2261 } 2262 2263 pub fn getFocus(self: *Header) *Focus { 2264 return self.box.getFocus(); 2265 } 2266 2267 pub fn getSelectedIndex(self: Header) ?usize { 2268 return self.currentTabIndex(); 2269 } 2270 2271 fn currentTabIndex(self: Header) ?usize { 2272 const child_id = self.box.focus.child_id orelse return null; 2273 return self.tab_ids.getIndex(child_id); 2274 } 2275 }; 2276