╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserNotifications.zig ────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
108 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 109 errdefer header.deinit(allocator); 110 const on_tab = std.meta.activeTag(session.data.current_page) == std.meta.activeTag(user_route); 111 const routes = [_]ui.RoutablePage{ user_route.withUserTab(.{}), settings_route, keys_route }; 112 for ([_]ui.RoutablePage.UserView{ .notifications, .settings, .keys }, [_][]const u8{ "notifications", "user settings", "ssh keys" }, routes, 0..) |view, label, route, index| { 113 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 114 errdefer tab.deinit(allocator); 115 tab.getFocus().mode = .all; 116 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, on_tab and data.view == view) }; 117 control_ids[index] = tab.getFocus().id; 118 try header.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 119 } 120 // the spacer pushes the buttons to the right edge 121 { 122 var spacer = try ui.widget.Spacer.init(allocator); 123 errdefer spacer.deinit(allocator); 124 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 125 } 126 { 127 var button = try wgt.TextBox.init(allocator, repos_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 128 errdefer button.deinit(allocator); 129 button.getFocus().mode = .all; 130 button.getFocus().kind = .{ .custom = try ui.userLink(session.page_arena, session.data.user_name orelse unreachable) }; 131 control_ids[repos_index] = button.getFocus().id; 132 try header.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = repos_label.len + 2, .height = 3 } }); 133 } 134 { 135 // the form kind makes the web renderer post the button to the logout route 136 header.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}/logout", .{try settings_route.toUrl(session.page_arena)}) }; 137 var button = try wgt.TextBox.init(allocator, logout_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 138 errdefer button.deinit(allocator); 139 button.getFocus().mode = .all; 140 button.getFocus().kind = .{ .custom = "submit" }; 141 control_ids[logout_index] = button.getFocus().id; 142 try header.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = logout_label.len + 2, .height = 3 } }); 143 } 144 header.getFocus().child_id = control_ids[@backingInt(data.view)]; 145 // a row taller than the button, leaving a blank line beneath it 146 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 147 } 148 149 { 150 var stack = try wgt.Stack(ui.Widget).init(allocator); 151 errdefer stack.deinit(allocator); 152 // an empty first window has no rows, not even paging ones 153 if (data.isEmpty()) { 154 var center = try ui.widget.initEmpty(allocator); 155 errdefer center.deinit(allocator); 156 try stack.children.put(allocator, center.getFocus().id, .{ .center = center }); 157 } else { 158 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 159 errdefer list.deinit(allocator); 160 161 var arena = std.heap.ArenaAllocator.init(allocator); 162 defer arena.deinit(); 163 const aa = arena.allocator(); 164 165 // a leading "previous" row off the first window, one row per 166 // notif, then a trailing "next" row when more remain 167 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 168 if (data.start > 0) { 169 const prev = user_route.withUserTab(.{ .start = data.start -| page_size }); 170 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:{s}", .{try prev.toUrl(&arena)}) }); 171 } 172 for (data.rows) |row| try items.append(aa, .{ 173 .text = row.title, 174 .link = try aa.print("a:{s}", .{row.url}), 175 .bottom_label = try aa.print("{s} {s}", .{ if (row.unread) "☐" else "☑", row.identity }), 176 .bold = row.unread, 177 }); 178 if (data.next_start) |next_start| { 179 const next = user_route.withUserTab(.{ .start = next_start }); 180 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:{s}", .{try next.toUrl(&arena)}) }); 181 } 182 try list.setItems(allocator, items.items); 183 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 184 } 185 { 186 var settings = try ui.UserSettings.View.init(allocator, session, settings_route); 187 errdefer settings.deinit(allocator); 188 try stack.children.put(allocator, settings.getFocus().id, .{ .user_settings = settings }); 189 } 190 { 191 var keys = try ui.UserKeys.View.init(allocator, &data.ssh_keys, session, keys_route); 192 errdefer keys.deinit(allocator); 193 try stack.children.put(allocator, keys.getFocus().id, .{ .user_keys = keys }); 194 } 195 stack.getFocus().child_id = stack.children.keys()[@backingInt(data.view)]; 196 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 197 } 198 box.getFocus().child_id = box.children.keys()[content_index]; 199 return .{ .box = box, .data = data, .session = session, .control_ids = control_ids, .view_index = @backingInt(data.view) }; 200 } 201 202 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 203 self.box.deinit(allocator); 204 } 205 206 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 207 return &self.box.children.values()[header_index].widget.box; 208 } 209 210 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 211 return &self.box.children.values()[content_index].widget.stack; 212 } 213 214 fn headerActive(self: *View) bool { 215 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 216 } 217 218 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 219 self.clearGrid(); 220 const header = self.headerBox(); 221 // focusing a sub tab shows its content, while the logout button leaves it 222 if (header.getFocus().child_id) |child_id| if (std.mem.indexOfScalar(usize, self.control_ids[0..tab_count], child_id)) |index| { 223 self.view_index = index; 224 }; 225 for (self.control_ids[0..tab_count], 0..) |id, index| { 226 const tab = header.children.getPtr(id) orelse unreachable; 227 ui.widget.markSelected(&tab.widget.text_box, index == self.view_index); 228 } 229 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[self.view_index]; 230 // clear the incoming min height so the list scrolls within what's left 231 try self.box.build(allocator, .{ 232 .min_size = .{ .width = null, .height = null }, 233 .max_size = constraint.max_size, 234 }, root_focus); 235 } 236 237 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 238 const logout_id = self.control_ids[logout_index]; 239 if (key == .mouse and inp.leftClickOn(root_focus, logout_id, key.mouse)) return logOut(self.session); 240 const direction = inp.vertDirection(key); 241 if (self.headerActive()) { 242 const focused = root_focus.grandchild_id orelse return; 243 if (key == .enter and focused == logout_id) return logOut(self.session); 244 if (direction == .down) { 245 // the empty message has nothing to focus 246 if (self.view_index == @backingInt(ui.RoutablePage.UserView.notifications) and self.data.isEmpty()) return; 247 root_focus.setFocus(self.contentStack().getFocus().id); 248 } else if (std.mem.indexOfScalar(usize, &self.control_ids, focused)) |current| { 249 if (inp.moveTab(key, current, self.control_ids.len)) |next| root_focus.setFocus(self.control_ids[next]); 250 } 251 return; 252 } 253 if (direction == .up) if (self.contentStack().getSelected()) |selected| if (selected.atTop(root_focus)) { 254 root_focus.setFocus(self.control_ids[self.view_index]); 255 return; 256 }; 257 try self.contentStack().input(allocator, key, root_focus); 258 } 259 260 // moving up from the sub header returns to the page's tabs 261 pub fn atTop(self: *View) bool { 262 return self.headerActive(); 263 } 264 265 // arriving from the page's tabs lands on the sub header, not the content 266 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 267 root_focus.setFocus(self.control_ids[self.view_index]); 268 return true; 269 } 270 271 pub fn clearGrid(self: *View) void { 272 self.box.clearGrid(); 273 } 274 275 pub fn getGrid(self: View) ?Grid { 276 return self.box.getGrid(); 277 } 278 279 pub fn getFocus(self: *View) *Focus { 280 return self.box.getFocus(); 281 } 282 }; 283