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