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