╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1169 } 1170 1171 // commit the new issue to the repo's events branch and navigate to it. 1172 // this is the terminal path; the web posts the form to the issue route, 1173 // so the wasm side never runs (or compiles) the repo access below. 1174 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 1175 if (comptime wasm) return; 1176 const io = self.session.io orelse return; 1177 const src = self.data.repo_source orelse return; 1178 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1179 1180 const form = self.issueForm(); 1181 const title_input = &form.children.values()[title_field_index].widget.text_input; 1182 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1183 const description_input = &form.children.values()[description_field_index].widget.text_input; 1184 1185 const title = try title_input.text(allocator); 1186 defer allocator.free(title); 1187 const tags = try tags_input.text(allocator); 1188 defer allocator.free(tags); 1189 const description = try description_input.text(allocator); 1190 defer allocator.free(description); 1191 1192 if (!evt.Issue.fieldsValid(title, tags)) return; 1193 1194 var id_bytes: [evt.event_id_size]u8 = undefined; 1195 io.random(&id_bytes); 1196 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1197 1198 const event = evt.EventWithId{ 1199 .id = event_id_hex, 1200 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1201 .author_email = author_email, 1202 .event = .{ .issue = .{ 1203 .title = title, 1204 .description = description, 1205 .tags = tags, 1206 } }, 1207 }; 1208 1209 switch (src.repo_kind) { 1210 inline else => |repo_kind| { 1211 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1212 defer any_repo.deinit(io, allocator); 1213 switch (any_repo) { 1214 inline else => |*repo| try evt.consume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1215 } 1216 }, 1217 } 1218 1219 // wipe the form so a return visit starts fresh 1220 title_input.clear(allocator); 1221 tags_input.clear(allocator); 1222 description_input.clear(allocator); 1223 1224 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1225 try self.session.navigate(route); 1226 } 1227 1228 // re-emit the selected issue's event with the form's content (status 1229 // preserved), then reload the issue's page. this is the terminal path; 1230 // the web posts the form to the edit route. 1231 fn submitEditedIssue(self: *View, allocator: std.mem.Allocator) !void { 1232 if (comptime wasm) return; 1233 const io = self.session.io orelse return; 1234 const src = self.data.repo_source orelse return; 1235 const entry = self.data.selectedIssue() orelse return; 1236 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1237 1238 const form = self.issueForm(); 1239 const title_input = &form.children.values()[title_field_index].widget.text_input; 1240 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 1241 const description_input = &form.children.values()[description_field_index].widget.text_input; 1242 1243 const title = try title_input.text(allocator); 1244 defer allocator.free(title); 1245 const tags = try tags_input.text(allocator); 1246 defer allocator.free(tags); 1247 const description = try description_input.text(allocator); 1248 defer allocator.free(description); 1249 1250 if (!evt.Issue.fieldsValid(title, tags)) return; 1251 1252 var id_bytes: [evt.event_id_size]u8 = undefined; 1253 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1254 1255 switch (src.repo_kind) { 1256 inline else => |repo_kind| { 1257 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1258 defer any_repo.deinit(io, allocator); 1259 switch (any_repo) { 1260 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .fields = .{ 1261 .title = title, 1262 .tags = tags, 1263 .description = description, 1264 } }, author_email), 1265 } 1266 }, 1267 } 1268 1269 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, entry.issue.event.status, "", entry.id) orelse return; 1270 try self.session.navigate(route); 1271 } 1272 1273 // flip the shown issue's status by re-emitting its event, then reload the 1274 // page rooted at the issue so the view reflects the change. this is the 1275 // terminal path; the web posts the button's form to the status route. 1276 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1277 if (comptime wasm) return; 1278 const io = self.session.io orelse return; 1279 const src = self.data.repo_source orelse return; 1280 const sel = self.detailed_index[index] orelse return; 1281 const entry = self.window(index).issues[sel]; 1282 const author_email = (try self.session.eventAuthorEmail()) orelse return; 1283 1284 const status: evt.Issue.Status = switch (entry.issue.event.status) { 1285 .open => .closed, 1286 .closed => .open, 1287 }; 1288 1289 var id_bytes: [evt.event_id_size]u8 = undefined; 1290 _ = try std.fmt.hexToBytes(&id_bytes, entry.id[0 .. evt.event_id_size * 2]); 1291 1292 switch (src.repo_kind) { 1293 inline else => |repo_kind| { 1294 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1295 defer any_repo.deinit(io, allocator); 1296 switch (any_repo) { 1297 inline else => |*repo| try evt.Issue.update(repo_kind, repo.self_repo_opts, io, allocator, repo, &id_bytes, .{ .status = status }, author_email), 1298 } 1299 }, 1300 } 1301 1302 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, status, "", entry.id) orelse return; 1303 try self.session.navigate(route); 1304 } 1305 1306 // arrow keys move the tag selection; at the flow's edges focus crosses to 1307 // the neighboring widgets. 1308 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1309 const tf = self.tagFlow(index) orelse return; 1310 const cid = tf.focus.child_id orelse return; 1311 const cur = tf.indexOfFocusId(cid) orelse return; 1312 const count = tf.text_boxes.items.len; 1313 const sc = self.detailScroll(index); 1314 switch (key) { 1315 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1316 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1317 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusAuthor(index, root_focus), 1318 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1319 .home => self.focusTag(index, tf, root_focus, 0), 1320 .end => self.focusTag(index, tf, root_focus, count - 1), 1321 .mouse => |mouse| switch (mouse.action) { 1322 .scroll => |dir| { 1323 sc.y += if (dir == .up) @as(isize, -1) else 1; 1324 sc.clampToContent(); 1325 }, 1326 else => {}, 1327 }, 1328 else => {}, 1329 } 1330 } 1331 1332 const button_in_row_index: usize = 1; 1333 const edit_in_row_index: usize = 2; 1334 const title_child_index: usize = 0; 1335 const tags_child_index: usize = 2; 1336 1337 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1338 const inner = self.detailInner(index); 1339 if (inner.children.count() <= tags_child_index) return null; 1340 return switch (inner.children.values()[tags_child_index].widget) { 1341 .tag_flow => |*tf| tf, 1342 else => null, 1343 }; 1344 } 1345 1346 fn tagsFocused(self: *View, index: usize) bool { 1347 const inner = self.detailInner(index); 1348 const cid = inner.getFocus().child_id orelse return false; 1349 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1350 } 1351 1352 // the open/close button inside the detail frame's tool row. 1353 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1354 const row = self.toolRow(index); 1355 if (row.children.count() == 0) return null; 1356 return &row.children.values()[button_in_row_index].widget.text_box; 1357 } 1358 1359 fn toolRowFocused(self: *View, index: usize) bool { 1360 const outer = self.detailOuter(index); 1361 const cid = outer.getFocus().child_id orelse return false; 1362 return outer.children.getIndex(cid) == tool_row_index; 1363 } 1364 1365 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1366 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1367 // keep the tag visible on the terminal: its rect offset by the flow's 1368 // position in the pane. 1369 if (self.session.is_terminal and item < tf.rects.items.len) { 1370 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1371 var rect = tf.rects.items[item]; 1372 rect.x += flow_rect.x; 1373 rect.y += flow_rect.y; 1374 self.detailScroll(index).scrollToRect(rect); 1375 } 1376 } 1377 } 1378 1379 fn titleFocused(self: *View, index: usize) bool { 1380 const inner = self.detailInner(index); 1381 const cid = inner.getFocus().child_id orelse return false; 1382 return inner.children.getIndex(cid) == title_child_index; 1383 } 1384 1385 fn authorPresent(self: *View, index: usize) bool { 1386 return self.author_id[index] != null; 1387 } 1388 1389 fn authorFocused(self: *View, index: usize) bool { 1390 const inner = self.detailInner(index); 1391 const cid = inner.getFocus().child_id orelse return false; 1392 return cid == self.author_id[index]; 1393 } 1394 1395 // the author sits right under the title at the top of the pane, so 1396 // focusing it scrolls there. 1397 fn focusAuthor(self: *View, index: usize, root_focus: *Focus) void { 1398 const id = self.author_id[index] orelse return; 1399 root_focus.setFocus(id); 1400 self.detailScroll(index).y = 0; 1401 } 1402 1403 // the title sits at the top of the pane, so focusing it scrolls there. 1404 fn focusTitle(self: *View, index: usize, root_focus: *Focus) void { 1405 const inner = self.detailInner(index); 1406 if (inner.children.count() == 0) return; 1407 root_focus.setFocus(inner.children.keys()[title_child_index]); 1408 self.detailScroll(index).y = 0; 1409 } 1410 1411 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1412 const tf = self.tagFlow(index) orelse return; 1413 if (tf.text_boxes.items.len == 0) return; 1414 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1415 const item = tf.indexOfFocusId(cid) orelse 0; 1416 self.focusTag(index, tf, root_focus, item); 1417 } 1418 1419 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1420 if (self.description_id[index]) |id| root_focus.setFocus(id); 1421 } 1422 1423 // return to the tool row's last-focused button (the header when the 1424 // description page shows no row). 1425 fn focusToolRow(self: *View, index: usize, root_focus: *Focus) void { 1426 const cid = self.toolRow(index).getFocus().child_id orelse return self.focusHeader(root_focus); 1427 root_focus.setFocus(cid); 1428 } 1429 1430 // enter the detail pane. an empty pane (no issues) can't be entered. 1431 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1432 if (self.detailInner(index).children.count() == 0) return; 1433 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1434 } 1435 1436 // return to the list. 1437 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1438 root_focus.setFocus(self.listScroll(index).getFocus().id); 1439 } 1440 1441 // cross to the header tabs above the stack. 1442 fn focusHeader(self: *View, root_focus: *Focus) void { 1443 root_focus.setFocus(self.box.children.keys()[header_index]); 1444 } 1445 1446 pub fn clearGrid(self: *View) void { 1447 self.box.clearGrid(); 1448 } 1449 1450 pub fn getGrid(self: View) ?Grid { 1451 return self.box.getGrid(); 1452 } 1453 1454 pub fn getFocus(self: *View) *Focus { 1455 return self.box.getFocus(); 1456 } 1457 1458 // for the parent's "scroll up at the top jumps to the header" check: at the 1459 // top only while the header tabs hold focus, so up from the split 1460 // crosses into the tabs first. 1461 pub fn getSelectedIndex(self: *View) ?usize { 1462 return if (self.headerActive()) 0 else 1; 1463 } 1464 }; 1465 1466 // the "a:" navigation link for `status`'s issues page filtered to the 1467 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1468 // a rooted url ignores `status` (it derives its view from the issue's own); 1469 // with an empty `id` this is the bare list link. 1470 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1471 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1472 const url = try route.toUrl(page_arena); 1473 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1474 } 1475 1476 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1477 // href is only followed with js off. 1478 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1479 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1480 const url = try route.toUrl(page_arena); 1481 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1482 } 1483 1484 // the "a:" link to the whole-description page of issue `id` within `identity`. 1485 fn descriptionLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1486 const route = ui.RoutablePage.repoIssuesDescriptionRoute(identity, id) orelse return error.RouteTooLong; 1487 const url = try route.toUrl(page_arena); 1488 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1489 } 1490 1491 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1492 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1493 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1494 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1495 const url = try route.toUrl(page_arena); 1496 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1497 } 1498 1499 // tabs switching between the issues page's views. 1500 pub const Header = struct { 1501 box: wgt.Box(ui.Widget), 1502 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1503 1504 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1505 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1506 errdefer box.deinit(allocator); 1507 1508 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1509 errdefer tab_ids.deinit(allocator); 1510 1511 const aa = session.page_arena.allocator(); 1512 1513 // a list tab per status, labeled with its listing's issue count 1514 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1515 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1516 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1517 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1518 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1519 errdefer text_box.deinit(allocator); 1520 text_box.getFocus().focusable = true; 1521 text_box.getFocus().kind = .{ .custom = link }; 1522 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1523 try box.children.put(allocator, text_box.getFocus().id, .{ 1524 .widget = .{ .text_box = text_box }, 1525 .rect = null, 1526 .min_size = .{ .width = label.len + 2, .height = null }, 1527 }); 1528 } 1529 1530 // tags tab, labeled with the active tag filter 1531 { 1532 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1533 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1534 const label = if (data.tag.len == 0) "tags" else blk: { 1535 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1536 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1537 }; 1538 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1539 errdefer text_box.deinit(allocator); 1540 text_box.getFocus().focusable = true; 1541 text_box.getFocus().kind = .{ .custom = tags_link }; 1542 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1543 try box.children.put(allocator, text_box.getFocus().id, .{ 1544 .widget = .{ .text_box = text_box }, 1545 .rect = null, 1546 .min_size = .{ .width = label.len + 2, .height = null }, 1547 }); 1548 } 1549 1550 // new-issue tab; an edit url shows the edit tab in its place 1551 { 1552 const route = if (data.view == .edit) 1553 ui.RoutablePage.repoIssuesEditRoute(data.identity, data.selected_id) orelse return error.RouteTooLong 1554 else 1555 ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1556 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1557 const label: []const u8 = if (data.view == .edit) "edit" else "new"; 1558 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1559 errdefer text_box.deinit(allocator); 1560 text_box.getFocus().focusable = true; 1561 text_box.getFocus().kind = .{ .custom = link }; 1562 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1563 try box.children.put(allocator, text_box.getFocus().id, .{ 1564 .widget = .{ .text_box = text_box }, 1565 .rect = null, 1566 .min_size = .{ .width = label.len + 2, .height = null }, 1567 }); 1568 } 1569 1570 var self = Header{ .box = box, .tab_ids = tab_ids }; 1571 // the tab matching the page's view is selected initially. 1572 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1573 return self; 1574 } 1575 1576 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1577 self.box.deinit(allocator); 1578 self.tab_ids.deinit(allocator); 1579 } 1580 1581 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1582 self.clearGrid(); 1583 // only the selected tab shows its border 1584 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1585 switch (child.widget) { 1586 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1587 else => {}, 1588 } 1589 } 1590 try self.box.build(allocator, constraint, root_focus); 1591 } 1592 1593 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1594 _ = allocator; 1595 const current_tab = self.currentTabIndex() orelse return; 1596 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1597 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1598 } 1599 } 1600 1601 pub fn clearGrid(self: *Header) void { 1602 self.box.clearGrid(); 1603 } 1604 1605 pub fn getGrid(self: Header) ?Grid { 1606 return self.box.getGrid(); 1607 } 1608 1609 pub fn getFocus(self: *Header) *Focus { 1610 return self.box.getFocus(); 1611 } 1612 1613 pub fn getSelectedIndex(self: Header) ?usize { 1614 return self.currentTabIndex(); 1615 } 1616 1617 fn currentTabIndex(self: Header) ?usize { 1618 const child_id = self.box.focus.child_id orelse return null; 1619 return self.tab_ids.getIndex(child_id); 1620 } 1621 }; 1622