╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Issues.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
882 } else if (self.tagsFocused(index)) { 883 try self.tagsInput(index, key, root_focus); 884 } else { 885 try self.descriptionInput(index, key, root_focus); 886 } 887 } 888 889 // the open/close button: enter or a click flips the issue's status; 890 // arrows cross to the neighboring widgets. 891 fn statusButtonInput(self: *View, allocator: std.mem.Allocator, index: usize, key: Key, root_focus: *Focus) !void { 892 switch (key) { 893 .arrow_left => try self.focusList(index, root_focus), 894 .arrow_up => self.focusHeader(root_focus), 895 .arrow_down => if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else try self.focusDescription(index, root_focus), 896 .enter => try self.toggleIssueStatus(allocator, index), 897 .mouse => |mouse| switch (mouse.action) { 898 .scroll => |dir| { 899 const sc = self.detailScroll(index); 900 sc.y += if (dir == .up) @as(isize, -1) else 1; 901 sc.clampToContent(); 902 }, 903 else => if (self.statusButton(index)) |button| { 904 if (inp.leftClickOn(root_focus, button.getFocus().id, mouse)) try self.toggleIssueStatus(allocator, index); 905 }, 906 }, 907 else => {}, 908 } 909 } 910 911 fn descriptionInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 912 const sc = self.detailScroll(index); 913 switch (key) { 914 .arrow_left => return self.focusList(index, root_focus), 915 // once the scroll can't move further, cross into the tags (or the 916 // open/close button when the issue has none). 917 .arrow_up => { 918 const before = sc.y; 919 sc.y -= 1; 920 sc.clampToContent(); 921 if (sc.y == before) { 922 if (self.tagFlow(index) != null) try self.focusTags(index, root_focus) else self.focusStatusButton(index, root_focus); 923 } 924 return; 925 }, 926 .arrow_down => sc.y += 1, 927 .page_up => sc.y -= 10, 928 .page_down => sc.y += 10, 929 .home => sc.y = 0, 930 .end => sc.y = std.math.maxInt(isize), 931 .mouse => |mouse| switch (mouse.action) { 932 .scroll => |dir| sc.y += if (dir == .up) @as(isize, -1) else 1, 933 else => {}, 934 }, 935 else => return, 936 } 937 sc.clampToContent(); 938 } 939 940 // up/down (and tab/shift+tab) move between the form's fields; up from the 941 // title crosses into the header tabs. the multiline description keeps 942 // enter and any up/down that has a row to move to. 943 fn newViewInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 944 const form = self.newForm(); 945 const cid = form.getFocus().child_id orelse return; 946 const cur = form.children.getIndex(cid) orelse return; 947 948 if (cur == description_field_index) { 949 const child = &form.children.values()[cur]; 950 const description = &child.widget.text_input; 951 switch (key) { 952 .enter => return child.widget.input(allocator, key, root_focus), 953 .arrow_up => if (!try description.cursorOnFirstRow(allocator)) 954 return child.widget.input(allocator, key, root_focus), 955 .arrow_down => if (!try description.cursorOnLastRow(allocator)) 956 return child.widget.input(allocator, key, root_focus), 957 else => {}, 958 } 959 } 960 961 switch (key) { 962 .arrow_up, .back_tab => if (cur > 0) 963 root_focus.setFocus(form.children.keys()[cur - 1]) 964 else 965 self.focusHeader(root_focus), 966 .arrow_down, .tab => if (cur < submit_field_index) { 967 root_focus.setFocus(form.children.keys()[cur + 1]); 968 }, 969 .enter => if (cur == submit_field_index) try self.submitNewIssue(allocator), 970 .mouse => |mouse| if (cur == submit_field_index and inp.leftClickOn(root_focus, cid, mouse)) { 971 try self.submitNewIssue(allocator); 972 }, 973 else => try form.children.values()[cur].widget.input(allocator, key, root_focus), 974 } 975 } 976 977 // commit the new issue to the repo's events branch and navigate to it. 978 // this is the terminal path; the web posts the form to the issue route, 979 // so the wasm side never runs (or compiles) the repo access below. 980 fn submitNewIssue(self: *View, allocator: std.mem.Allocator) !void { 981 if (comptime wasm) return; 982 const io = self.session.io orelse return; 983 const src = self.data.repo_source orelse return; 984 985 const form = self.newForm(); 986 const title_input = &form.children.values()[title_field_index].widget.text_input; 987 const tags_input = &form.children.values()[tags_field_index].widget.text_input; 988 const description_input = &form.children.values()[description_field_index].widget.text_input; 989 990 const title = try title_input.text(allocator); 991 defer allocator.free(title); 992 const tags = try tags_input.text(allocator); 993 defer allocator.free(tags); 994 const description = try description_input.text(allocator); 995 defer allocator.free(description); 996 997 // a title is required, and a too-long tag would fail consumption 998 // after the event is already committed 999 if (title.len == 0) return; 1000 var tag_iter = evt.Issue.tagIterator(tags); 1001 while (tag_iter.next()) |tag| { 1002 if (tag.len > evt.Issue.tag_max_len) return; 1003 } 1004 1005 var id_bytes: [evt.event_id_size]u8 = undefined; 1006 io.random(&id_bytes); 1007 const event_id_hex = std.fmt.bytesToHex(id_bytes, .lower); 1008 1009 const event = evt.EventWithId{ 1010 .id = event_id_hex, 1011 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1012 .event = .{ .issue = .{ 1013 .title = title, 1014 .description = description, 1015 .tags = tags, 1016 } }, 1017 }; 1018 1019 switch (src.repo_kind) { 1020 inline else => |repo_kind| { 1021 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1022 defer any_repo.deinit(io, allocator); 1023 switch (any_repo) { 1024 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1025 } 1026 }, 1027 } 1028 1029 // wipe the form so a return visit starts fresh 1030 title_input.clear(allocator); 1031 tags_input.clear(allocator); 1032 description_input.clear(allocator); 1033 1034 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, .open, "", &event_id_hex) orelse return; 1035 try self.session.navigate(route); 1036 } 1037 1038 // flip the shown issue's status by re-emitting its event, then reload the 1039 // page rooted at the issue so the view reflects the change. this is the 1040 // terminal path; the web posts the button's form to the status route. 1041 fn toggleIssueStatus(self: *View, allocator: std.mem.Allocator, index: usize) !void { 1042 if (comptime wasm) return; 1043 const io = self.session.io orelse return; 1044 const src = self.data.repo_source orelse return; 1045 const sel = self.detailed_index[index] orelse return; 1046 const entry = self.window(index).issues[sel]; 1047 1048 var updated = entry.issue; 1049 updated.status = switch (entry.issue.status) { 1050 .open => .closed, 1051 .closed => .open, 1052 }; 1053 1054 const event = evt.EventWithId{ 1055 .id = entry.id[0 .. evt.event_id_size * 2].*, 1056 .timestamp = @intCast(std.Io.Timestamp.now(io, .real).toSeconds()), 1057 .event = .{ .issue = updated }, 1058 }; 1059 1060 switch (src.repo_kind) { 1061 inline else => |repo_kind| { 1062 var any_repo = try rp.AnyRepo(repo_kind, .{}).open(io, allocator, .{ .path = src.path }); 1063 defer any_repo.deinit(io, allocator); 1064 switch (any_repo) { 1065 inline else => |*repo| try evt.commitAndConsume(repo_kind, repo.self_repo_opts, io, allocator, repo, evt.events_ref, &.{event}), 1066 } 1067 }, 1068 } 1069 1070 const route = ui.RoutablePage.repoIssuesRoute(self.data.identity, updated.status, "", entry.id) orelse return; 1071 try self.session.navigate(route); 1072 } 1073 1074 // arrow keys move the tag selection; at the flow's edges focus crosses to 1075 // the neighboring widgets. 1076 fn tagsInput(self: *View, index: usize, key: Key, root_focus: *Focus) !void { 1077 const tf = self.tagFlow(index) orelse return; 1078 const cid = tf.focus.child_id orelse return; 1079 const cur = tf.indexOfFocusId(cid) orelse return; 1080 const count = tf.text_boxes.items.len; 1081 const sc = self.detailScroll(index); 1082 switch (key) { 1083 .arrow_left => if (cur > 0) self.focusTag(index, tf, root_focus, cur - 1) else try self.focusList(index, root_focus), 1084 .arrow_right => if (cur + 1 < count) self.focusTag(index, tf, root_focus, cur + 1), 1085 .arrow_up => if (tf.rowStep(cur, false)) |i| self.focusTag(index, tf, root_focus, i) else self.focusStatusButton(index, root_focus), 1086 .arrow_down => if (tf.rowStep(cur, true)) |i| self.focusTag(index, tf, root_focus, i) else try self.focusDescription(index, root_focus), 1087 .home => self.focusTag(index, tf, root_focus, 0), 1088 .end => self.focusTag(index, tf, root_focus, count - 1), 1089 .mouse => |mouse| switch (mouse.action) { 1090 .scroll => |dir| { 1091 sc.y += if (dir == .up) @as(isize, -1) else 1; 1092 sc.clampToContent(); 1093 }, 1094 else => {}, 1095 }, 1096 else => {}, 1097 } 1098 } 1099 1100 const button_in_row_index: usize = 1; 1101 const tags_child_index: usize = 0; 1102 1103 fn tagFlow(self: *View, index: usize) ?*ui.TagFlow { 1104 const inner = self.detailInner(index); 1105 if (inner.children.count() == 0) return null; 1106 return switch (inner.children.values()[tags_child_index].widget) { 1107 .tag_flow => |*tf| tf, 1108 else => null, 1109 }; 1110 } 1111 1112 fn tagsFocused(self: *View, index: usize) bool { 1113 const inner = self.detailInner(index); 1114 const cid = inner.getFocus().child_id orelse return false; 1115 return inner.children.getIndex(cid) == tags_child_index and self.tagFlow(index) != null; 1116 } 1117 1118 // the open/close button inside the detail frame's tool row. 1119 fn statusButton(self: *View, index: usize) ?*wgt.TextBox(ui.Widget) { 1120 const row = self.toolRow(index); 1121 if (row.children.count() == 0) return null; 1122 return &row.children.values()[button_in_row_index].widget.text_box; 1123 } 1124 1125 fn statusButtonFocused(self: *View, index: usize) bool { 1126 const outer = self.detailOuter(index); 1127 const cid = outer.getFocus().child_id orelse return false; 1128 return outer.children.getIndex(cid) == tool_row_index; 1129 } 1130 1131 fn focusTag(self: *View, index: usize, tf: *ui.TagFlow, root_focus: *Focus, item: usize) void { 1132 root_focus.setFocus(tf.text_boxes.items[item].getFocus().id); 1133 // keep the tag visible on the terminal: its rect offset by the flow's 1134 // position in the pane. 1135 if (self.session.is_terminal and item < tf.rects.items.len) { 1136 if (self.detailInner(index).children.values()[tags_child_index].rect) |flow_rect| { 1137 var rect = tf.rects.items[item]; 1138 rect.x += flow_rect.x; 1139 rect.y += flow_rect.y; 1140 self.detailScroll(index).scrollToRect(rect); 1141 } 1142 } 1143 } 1144 1145 fn focusTags(self: *View, index: usize, root_focus: *Focus) !void { 1146 const tf = self.tagFlow(index) orelse return; 1147 if (tf.text_boxes.items.len == 0) return; 1148 const cid = tf.focus.child_id orelse tf.text_boxes.items[0].getFocus().id; 1149 const item = tf.indexOfFocusId(cid) orelse 0; 1150 self.focusTag(index, tf, root_focus, item); 1151 } 1152 1153 fn focusDescription(self: *View, index: usize, root_focus: *Focus) !void { 1154 if (self.description_id[index]) |id| root_focus.setFocus(id); 1155 } 1156 1157 fn focusStatusButton(self: *View, index: usize, root_focus: *Focus) void { 1158 if (self.statusButton(index)) |button| root_focus.setFocus(button.getFocus().id); 1159 } 1160 1161 // enter the detail pane. an empty pane (no issues) can't be entered. 1162 fn focusDetail(self: *View, index: usize, root_focus: *Focus) !void { 1163 if (self.detailInner(index).children.count() == 0) return; 1164 root_focus.setFocus(self.detailOuter(index).getFocus().id); 1165 } 1166 1167 // return to the list. 1168 fn focusList(self: *View, index: usize, root_focus: *Focus) !void { 1169 root_focus.setFocus(self.listScroll(index).getFocus().id); 1170 } 1171 1172 // cross to the header tabs above the stack. 1173 fn focusHeader(self: *View, root_focus: *Focus) void { 1174 root_focus.setFocus(self.box.children.keys()[header_index]); 1175 } 1176 1177 pub fn clearGrid(self: *View) void { 1178 self.box.clearGrid(); 1179 } 1180 1181 pub fn getGrid(self: View) ?Grid { 1182 return self.box.getGrid(); 1183 } 1184 1185 pub fn getFocus(self: *View) *Focus { 1186 return self.box.getFocus(); 1187 } 1188 1189 // for the parent's "scroll up at the top jumps to the header" check: at the 1190 // top only while the header tabs hold focus, so up from the split 1191 // crosses into the tabs first. 1192 pub fn getSelectedIndex(self: *View) ?usize { 1193 return if (self.headerActive()) 0 else 1; 1194 } 1195 }; 1196 1197 // the "a:" navigation link for `status`'s issues page filtered to the 1198 // url-encoded `tag` and rooted at issue `id` within `identity` ("owner/name"). 1199 // a rooted url ignores `status` (it derives its view from the issue's own); 1200 // with an empty `id` this is the bare list link. 1201 fn issuesLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8, id: []const u8) ![]const u8 { 1202 const route = ui.RoutablePage.repoIssuesRoute(identity, status, tag, id) orelse return error.RouteTooLong; 1203 const url = try route.toUrl(page_arena); 1204 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1205 } 1206 1207 // the in-page "ai:" anchor for selecting issue `id` in `identity`'s list; the 1208 // href is only followed with js off. 1209 fn issueRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, id: []const u8) ![]const u8 { 1210 const route = ui.RoutablePage.repoIssuesRoute(identity, .open, "", id) orelse return error.RouteTooLong; 1211 const url = try route.toUrl(page_arena); 1212 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 1213 } 1214 1215 // the "a:" link to `status`'s issues list filtered to `tag` (raw; encoded here). 1216 fn tagLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, status: evt.Issue.Status, tag: []const u8) ![]const u8 { 1217 const encoded = try ui.urlEncodeRef(page_arena.allocator(), tag); 1218 const route = ui.RoutablePage.repoIssuesRoute(identity, status, encoded, "") orelse return error.RouteTooLong; 1219 const url = try route.toUrl(page_arena); 1220 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1221 } 1222 1223 // tabs switching between the issues page's views. 1224 pub const Header = struct { 1225 box: wgt.Box(ui.Widget), 1226 tab_ids: std.AutoArrayHashMapUnmanaged(usize, void), 1227 1228 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, data: *const Self) !Header { 1229 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1230 errdefer box.deinit(allocator); 1231 1232 var tab_ids: std.AutoArrayHashMapUnmanaged(usize, void) = .empty; 1233 errdefer tab_ids.deinit(allocator); 1234 1235 const aa = session.page_arena.allocator(); 1236 1237 // a list tab per status, labeled with its listing's issue count 1238 for ([_]evt.Issue.Status{ .open, .closed }) |status| { 1239 const route = ui.RoutablePage.repoIssuesRoute(data.identity, status, data.tag, "") orelse return error.RouteTooLong; 1240 const link = try std.fmt.allocPrint(aa, "ai:{s}", .{try route.toUrl(session.page_arena)}); 1241 const label = try std.fmt.allocPrint(aa, "{s} ({d})", .{ @tagName(status), data.window(status).count }); 1242 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1243 errdefer text_box.deinit(allocator); 1244 text_box.getFocus().focusable = true; 1245 text_box.getFocus().kind = .{ .custom = link }; 1246 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1247 try box.children.put(allocator, text_box.getFocus().id, .{ 1248 .widget = .{ .text_box = text_box }, 1249 .rect = null, 1250 .min_size = .{ .width = label.len + 2, .height = null }, 1251 }); 1252 } 1253 1254 // tags tab, labeled with the active tag filter 1255 { 1256 const tags_route = ui.RoutablePage.repoIssuesTagsRoute(data.identity, data.tag) orelse return error.RouteTooLong; 1257 const tags_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try tags_route.toUrl(session.page_arena)}); 1258 const label = if (data.tag.len == 0) "tags" else blk: { 1259 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, data.tag)); 1260 break :blk try std.fmt.allocPrint(aa, "tags ({s})", .{decoded}); 1261 }; 1262 var text_box = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1263 errdefer text_box.deinit(allocator); 1264 text_box.getFocus().focusable = true; 1265 text_box.getFocus().kind = .{ .custom = tags_link }; 1266 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1267 try box.children.put(allocator, text_box.getFocus().id, .{ 1268 .widget = .{ .text_box = text_box }, 1269 .rect = null, 1270 .min_size = .{ .width = label.len + 2, .height = null }, 1271 }); 1272 } 1273 1274 // new-issue tab 1275 { 1276 const new_route = ui.RoutablePage.repoIssuesNewRoute(data.identity) orelse return error.RouteTooLong; 1277 const new_link = try std.fmt.allocPrint(aa, "ai:{s}", .{try new_route.toUrl(session.page_arena)}); 1278 var text_box = try wgt.TextBox(ui.Widget).init(allocator, "new", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 1279 errdefer text_box.deinit(allocator); 1280 text_box.getFocus().focusable = true; 1281 text_box.getFocus().kind = .{ .custom = new_link }; 1282 try tab_ids.put(allocator, text_box.getFocus().id, {}); 1283 try box.children.put(allocator, text_box.getFocus().id, .{ 1284 .widget = .{ .text_box = text_box }, 1285 .rect = null, 1286 .min_size = .{ .width = "new".len + 2, .height = null }, 1287 }); 1288 } 1289 1290 var self = Header{ .box = box, .tab_ids = tab_ids }; 1291 // the tab matching the page's view is selected initially. 1292 self.getFocus().child_id = self.tab_ids.keys()[View.viewIndex(data.view)]; 1293 return self; 1294 } 1295 1296 pub fn deinit(self: *Header, allocator: std.mem.Allocator) void { 1297 self.box.deinit(allocator); 1298 self.tab_ids.deinit(allocator); 1299 } 1300 1301 pub fn build(self: *Header, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1302 self.clearGrid(); 1303 // only the selected tab shows its border 1304 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 1305 switch (child.widget) { 1306 .text_box => |*tb| tb.options.border_style = if (self.getFocus().child_id == id) .single else .hidden, 1307 else => {}, 1308 } 1309 } 1310 try self.box.build(allocator, constraint, root_focus); 1311 } 1312 1313 pub fn input(self: *Header, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1314 _ = allocator; 1315 const current_tab = self.currentTabIndex() orelse return; 1316 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 1317 root_focus.setFocus(self.tab_ids.keys()[new_tab]); 1318 } 1319 } 1320 1321 pub fn clearGrid(self: *Header) void { 1322 self.box.clearGrid(); 1323 } 1324 1325 pub fn getGrid(self: Header) ?Grid { 1326 return self.box.getGrid(); 1327 } 1328 1329 pub fn getFocus(self: *Header) *Focus { 1330 return self.box.getFocus(); 1331 } 1332 1333 pub fn getSelectedIndex(self: Header) ?usize { 1334 return self.currentTabIndex(); 1335 } 1336 1337 fn currentTabIndex(self: Header) ?usize { 1338 const child_id = self.box.focus.child_id orelse return null; 1339 return self.tab_ids.getIndex(child_id); 1340 } 1341 }; 1342