╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Header.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
57 try box.children.put(allocator, title_view.getFocus().id, .{ 58 .widget = .{ .title = title_view }, 59 .rect = null, 60 .min_size = null, 61 .flex = .shrink, 62 }); 63 } 64 65 // spacer 66 { 67 var text = try wgt.Text.init(allocator, " "); 68 errdefer text.deinit(allocator); 69 try box.children.put(allocator, text.getFocus().id, .{ 70 .widget = .{ .text = text }, 71 .rect = null, 72 .min_size = .{ .width = 1, .height = null }, 73 }); 74 } 75 76 // the tab matching the current page is focused initially; matching by 77 // link (rather than position) keeps this robust to tab changes. 78 const current_tag = std.meta.activeTag(session.data.current_page); 79 const repos_link = try ui.inPageTabLink(session, .{ .home_repos = 0 }, current_tag == .home_repos); 80 const users_link = try ui.inPageTabLink(session, .{ .home_users = 0 }, current_tag == .home_users); 81 const settings_link = try ui.inPageTabLink(session, .home_settings, current_tag == .home_settings); 82 const auth_link = try ui.inPageTabLink(session, .home_auth, current_tag == .home_auth); 83 const current_link: []const u8 = switch (current_tag) { 84 .home_users => users_link, 85 .home_settings => settings_link, 86 .home_auth => auth_link, 87 else => repos_link, 88 }; 89 var selected_tab: ?usize = null; 90 91 // repos tab 92 { 93 var text_box = try wgt.TextBox.init(allocator, repos_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 94 errdefer text_box.deinit(allocator); 95 text_box.getFocus().mode = .all; 96 text_box.getFocus().kind = .{ .custom = repos_link }; 97 try tab_ids.put(allocator, text_box.getFocus().id, {}); 98 if (std.mem.eql(u8, repos_link, current_link)) selected_tab = text_box.getFocus().id; 99 try box.children.put(allocator, text_box.getFocus().id, .{ 100 .widget = .{ .text_box = text_box }, 101 .rect = null, 102 .min_size = .{ .width = repos_tab_label.len + 2, .height = null }, 103 }); 104 } 105 106 // users tab 107 { 108 var text_box = try wgt.TextBox.init(allocator, users_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 109 errdefer text_box.deinit(allocator); 110 text_box.getFocus().mode = .all; 111 text_box.getFocus().kind = .{ .custom = users_link }; 112 try tab_ids.put(allocator, text_box.getFocus().id, {}); 113 if (std.mem.eql(u8, users_link, current_link)) selected_tab = text_box.getFocus().id; 114 try box.children.put(allocator, text_box.getFocus().id, .{ 115 .widget = .{ .text_box = text_box }, 116 .rect = null, 117 .min_size = .{ .width = users_tab_label.len + 2, .height = null }, 118 }); 119 } 120 121 // spacer pushes settings + auth to the right 122 { 123 var spacer = try ui.widget.Spacer.init(allocator); 124 errdefer spacer.deinit(allocator); 125 try box.children.put(allocator, spacer.getFocus().id, .{ 126 .widget = .{ .spacer = spacer }, 127 .rect = null, 128 .min_size = null, 129 .flex = .grow, 130 }); 131 } 132 133 // settings tab. settings are account preferences, so it needs a login. 134 if (session.data.user_id != null) { 135 var text_box = try wgt.TextBox.init(allocator, settings_tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 136 errdefer text_box.deinit(allocator); 137 text_box.getFocus().mode = .all; 138 text_box.getFocus().kind = .{ .custom = settings_link }; 139 try tab_ids.put(allocator, text_box.getFocus().id, {}); 140 if (std.mem.eql(u8, settings_link, current_link)) selected_tab = text_box.getFocus().id; 141 try box.children.put(allocator, text_box.getFocus().id, .{ 142 .widget = .{ .text_box = text_box }, 143 .rect = null, 144 .min_size = .{ .width = settings_tab_label.len + 2, .height = null }, 145 }); 146 } 147 148 // auth tab (login / logout) 149 { 150 var auth_tab = try AuthTab.View.init(allocator, session); 151 errdefer auth_tab.deinit(allocator); 152 auth_tab.text_box.getFocus().kind = .{ .custom = auth_link }; 153 try tab_ids.put(allocator, auth_tab.getFocus().id, {}); 154 if (std.mem.eql(u8, auth_link, current_link)) selected_tab = auth_tab.getFocus().id; 155 try box.children.put(allocator, auth_tab.getFocus().id, .{ 156 .widget = .{ .auth_tab = auth_tab }, 157 .rect = null, 158 .min_size = .{ .width = auth_tab.minWidth(), .height = null }, 159 }); 160 } 161 162 // quit tab 163 if (session.is_terminal) { 164 var text_box = try wgt.TextBox.init(allocator, ui.Quit.tab_label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 165 errdefer text_box.deinit(allocator); 166 text_box.getFocus().mode = .all; 167 text_box.getFocus().kind = .{ .custom = ui.Quit.tab_kind }; 168 try tab_ids.put(allocator, text_box.getFocus().id, {}); 169 try box.children.put(allocator, text_box.getFocus().id, .{ 170 .widget = .{ .text_box = text_box }, 171 .rect = null, 172 // the label is a single column; +2 for the border 173 .min_size = .{ .width = 1 + 2, .height = null }, 174 }); 175 } 176 177 var self = View{ 178 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .horiz, .show_bar = false, .web_native = !session.is_terminal }), 179 .tab_ids = tab_ids, 180 .session = session, 181 }; 182 self.getFocus().child_id = selected_tab orelse self.tab_ids.keys()[0]; 183 return self; 184 } 185 186 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 187 self.scroll.deinit(allocator); 188 self.tab_ids.deinit(allocator); 189 } 190 191 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 192 self.clearGrid(); 193 const box = &self.scroll.child.box; 194 ui.widget.setBackButtonVisible(box, self.session.back == .available); 195 for (box.children.keys(), box.children.values()) |id, *child| { 196 const tb: ?*wgt.TextBox = switch (child.widget) { 197 .text_box => |*x| x, 198 .auth_tab => |*at| blk: { 199 // the label tracks login state per frame, so the width must too 200 child.min_size = .{ .width = at.minWidth(), .height = null }; 201 break :blk &at.text_box; 202 }, 203 else => null, 204 }; 205 if (tb) |t| { 206 t.options.border_style = if (self.getFocus().child_id == id) .single else .hidden; 207 } 208 } 209 var scroll_constraint = constraint; 210 scroll_constraint.min_size.width = constraint.max_size.width orelse constraint.min_size.width; 211 try self.scroll.build(allocator, scroll_constraint, root_focus); 212 } 213 214 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 215 _ = allocator; 216 const current_tab = self.currentTabIndex() orelse return; 217 if (inp.moveTab(key, current_tab, self.tab_ids.count())) |new_tab| { 218 const tab_id = self.tab_ids.keys()[new_tab]; 219 root_focus.setFocus(tab_id); 220 const tab = self.scroll.child.box.children.get(tab_id) orelse return; 221 if (tab.rect) |rect| self.scroll.scrollToRect(rect); 222 } 223 } 224 225 pub fn clearGrid(self: *View) void { 226 self.scroll.clearGrid(); 227 } 228 229 pub fn getGrid(self: View) ?Grid { 230 return self.scroll.getGrid(); 231 } 232 233 pub fn getFocus(self: *View) *Focus { 234 return self.scroll.getFocus(); 235 } 236 237 pub fn getSelectedIndex(self: View) ?usize { 238 return self.currentTabIndex(); 239 } 240 241 fn currentTabIndex(self: View) ?usize { 242 const child_id = self.scroll.child.box.focus.child_id orelse return null; 243 return self.tab_ids.getIndex(child_id); 244 } 245 }; 246