╭ UserNotifications.zig ────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../event.zig"); 3 const notify = @import("../notify.zig"); 4 const ui = @import("../ui.zig"); 5 const inp = @import("./input.zig"); 6 const xit = @import("xit"); 7 const xitui = xit.xitui; 8 const wgt = xitui.widget; 9 const layout = xitui.layout; 10 const Key = xitui.input.Key; 11 const Grid = xitui.grid.Grid; 12 const Focus = xitui.focus.Focus; 13 14 pub const page_size = 20; // how many notifs one window shows 15 16 // a notif as the list shows it 17 pub const Row = struct { 18 title: []const u8, 19 // the thread's url 20 url: []const u8, 21 // the thread's repo, as "owner:name" 22 identity: []const u8, 23 unread: bool, 24 }; 25 26 // the logged-in user's notifications window, empty when logged out 27 rows: []const Row, 28 start: usize, // the window start this page was built with, mirrored into the url 29 next_start: ?usize, // the `start` for the "next" row, or null when this is the last window 30 // the sub tab the route names 31 view: ui.RoutablePage.UserView, 32 33 const Self = @This(); 34 35 // also refreshes the session's unread count 36 pub fn init(arena: *std.heap.ArenaAllocator, session: *ui.Session, tab: ui.RoutablePage.UserTab) !Self { 37 var result: Self = .{ .rows = &.{}, .start = tab.start, .next_start = null, .view = tab.view }; 38 // zero until read, so a logout leaves no stale count 39 session.data.unread_notifs = 0; 40 // local mode has no users 41 if (session.data.host_kind != .server) return result; 42 const user_id = session.userId() orelse return result; 43 const io = session.io orelse return error.NoMoment; 44 const users_dir = session.users_dir orelse return error.NoMoment; 45 const gpa = arena.child_allocator; 46 const aa = arena.allocator(); 47 48 var user_repo = (try evt.openUserRepo(io, gpa, users_dir, &user_id)) orelse return result; 49 defer user_repo.deinit(io, gpa); 50 const moment = (try notify.latestMoment(&user_repo)) orelse return result; 51 session.data.unread_notifs = try notify.unreadCount(moment); 52 53 const items = try notify.readRecent(moment, arena, tab.start, page_size + 1); 54 if (items.len > page_size) result.next_start = tab.start + page_size; 55 const rows = try aa.alloc(Row, @min(items.len, page_size)); 56 for (rows, items[0..rows.len]) |*row, item| { 57 const identity = try aa.print("{s}:{s}", .{ item.notif.owner_name, item.notif.repo_name }); 58 const route = ui.RoutablePage.repoThreadCommentsRoute(item.notif.thread_kind, identity, &std.fmt.bytesToHex(item.notif.thread_id, .lower), 0) orelse return error.RouteTooLong; 59 row.* = .{ .title = item.notif.title, .url = try route.toUrl(arena), .identity = identity, .unread = item.unread }; 60 } 61 result.rows = rows; 62 return result; 63 } 64 65 // leave the user tab for the page it belongs to, where the web's logout redirect also lands 66 pub fn logOut(session: *ui.Session) !void { 67 session.logOut(); 68 try session.navigate(session.data.current_page.pageRoot()); 69 } 70 71 const logout_label = "logout"; 72 73 // the notifications and user settings sub tabs over their content, with the logout button across from them 74 pub const View = struct { 75 box: wgt.Box(ui.Widget), 76 session: *ui.Session, 77 // the sub tabs' ids then the logout button's, in header order 78 control_ids: [3]usize, 79 // the sub tab whose content shows 80 view_index: usize, 81 82 const header_index = 0; 83 const content_index = 1; 84 const logout_index = 2; 85 86 // `user_route` is the page's user tab route 87 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, user_route: ui.RoutablePage) !View { 88 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 89 errdefer box.deinit(allocator); 90 91 var control_ids: [3]usize = undefined; 92 const settings_route = user_route.withUserTab(.{ .view = .settings }); 93 { 94 var header = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 95 errdefer header.deinit(allocator); 96 const on_tab = std.meta.activeTag(session.data.current_page) == std.meta.activeTag(user_route); 97 const routes = [_]ui.RoutablePage{ user_route.withUserTab(.{}), settings_route }; 98 for ([_]ui.RoutablePage.UserView{ .notifications, .settings }, [_][]const u8{ "notifications", "user settings" }, routes, 0..) |view, label, route, index| { 99 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 100 errdefer tab.deinit(allocator); 101 tab.getFocus().mode = .all; 102 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, on_tab and data.view == view) }; 103 control_ids[index] = tab.getFocus().id; 104 try header.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 105 } 106 // the spacer pushes the logout button to the right edge 107 { 108 var spacer = try ui.widget.Spacer.init(allocator); 109 errdefer spacer.deinit(allocator); 110 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 111 } 112 { 113 // the form kind makes the web renderer post the button to the logout route 114 header.getFocus().kind = .{ .custom = try session.page_arena.allocator().print("form:{s}/logout", .{try settings_route.toUrl(session.page_arena)}) }; 115 var button = try wgt.TextBox.init(allocator, logout_label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 116 errdefer button.deinit(allocator); 117 button.getFocus().mode = .all; 118 button.getFocus().kind = .{ .custom = "submit" }; 119 control_ids[logout_index] = button.getFocus().id; 120 try header.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = logout_label.len + 2, .height = 3 } }); 121 } 122 header.getFocus().child_id = control_ids[@backingInt(data.view)]; 123 // a row taller than the button, leaving a blank line beneath it 124 try box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .box = header }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 125 } 126 127 { 128 var stack = try wgt.Stack(ui.Widget).init(allocator); 129 errdefer stack.deinit(allocator); 130 { 131 var list = try ui.widget.FlowBox.Scroll.init(allocator, .{}, !session.is_terminal); 132 errdefer list.deinit(allocator); 133 134 var arena = std.heap.ArenaAllocator.init(allocator); 135 defer arena.deinit(); 136 const aa = arena.allocator(); 137 138 // a leading "previous" row off the first window, one row per 139 // notif, then a trailing "next" row when more remain 140 var items: std.ArrayList(ui.widget.FlowBox.Item) = .empty; 141 if (data.start > 0) { 142 const prev = user_route.withUserTab(.{ .start = data.start -| page_size }); 143 try items.append(aa, .{ .text = "← previous", .link = try aa.print("a:{s}", .{try prev.toUrl(&arena)}) }); 144 } 145 for (data.rows) |row| try items.append(aa, .{ 146 .text = row.title, 147 .link = try aa.print("a:{s}", .{row.url}), 148 .bottom_label = try aa.print("{s} {s}", .{ if (row.unread) "☐" else "☑", row.identity }), 149 .bold = row.unread, 150 }); 151 if (data.next_start) |next_start| { 152 const next = user_route.withUserTab(.{ .start = next_start }); 153 try items.append(aa, .{ .text = "next →", .link = try aa.print("a:{s}", .{try next.toUrl(&arena)}) }); 154 } 155 try list.setItems(allocator, items.items); 156 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 157 } 158 { 159 var settings = try ui.UserSettings.View.init(allocator, session, settings_route); 160 errdefer settings.deinit(allocator); 161 try stack.children.put(allocator, settings.getFocus().id, .{ .user_settings = settings }); 162 } 163 stack.getFocus().child_id = stack.children.keys()[@backingInt(data.view)]; 164 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 165 } 166 box.getFocus().child_id = box.children.keys()[content_index]; 167 return .{ .box = box, .session = session, .control_ids = control_ids, .view_index = @backingInt(data.view) }; 168 } 169 170 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 171 self.box.deinit(allocator); 172 } 173 174 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 175 return &self.box.children.values()[header_index].widget.box; 176 } 177 178 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 179 return &self.box.children.values()[content_index].widget.stack; 180 } 181 182 fn headerActive(self: *View) bool { 183 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 184 } 185 186 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 187 self.clearGrid(); 188 const header = self.headerBox(); 189 // focusing a sub tab shows its content, while the logout button leaves it 190 if (header.getFocus().child_id) |child_id| if (std.mem.indexOfScalar(usize, self.control_ids[0..logout_index], child_id)) |index| { 191 self.view_index = index; 192 }; 193 for (self.control_ids[0..logout_index], 0..) |id, index| { 194 const tab = header.children.getPtr(id) orelse unreachable; 195 ui.widget.markSelected(&tab.widget.text_box, index == self.view_index); 196 } 197 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[self.view_index]; 198 // clear the incoming min height so the list scrolls within what's left 199 try self.box.build(allocator, .{ 200 .min_size = .{ .width = null, .height = null }, 201 .max_size = constraint.max_size, 202 }, root_focus); 203 } 204 205 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 206 const logout_id = self.control_ids[logout_index]; 207 if (key == .mouse and inp.leftClickOn(root_focus, logout_id, key.mouse)) return logOut(self.session); 208 const direction = inp.vertDirection(key); 209 if (self.headerActive()) { 210 const focused = root_focus.grandchild_id orelse return; 211 if (key == .enter and focused == logout_id) return logOut(self.session); 212 if (direction == .down) { 213 // an empty list has nothing to focus 214 if (self.contentStack().getSelected()) |selected| if (selected.* == .flow_box_scroll and selected.flow_box_scroll.isEmpty()) return; 215 root_focus.setFocus(self.contentStack().getFocus().id); 216 } else if (std.mem.indexOfScalar(usize, &self.control_ids, focused)) |current| { 217 if (inp.moveTab(key, current, self.control_ids.len)) |next| root_focus.setFocus(self.control_ids[next]); 218 } 219 return; 220 } 221 if (direction == .up) if (self.contentStack().getSelected()) |selected| if (selected.atTop(root_focus)) { 222 root_focus.setFocus(self.control_ids[self.view_index]); 223 return; 224 }; 225 try self.contentStack().input(allocator, key, root_focus); 226 } 227 228 // moving up from the sub header returns to the page's tabs 229 pub fn atTop(self: *View) bool { 230 return self.headerActive(); 231 } 232 233 // arriving from the page's tabs lands on the sub header, not the content 234 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 235 root_focus.setFocus(self.control_ids[self.view_index]); 236 return true; 237 } 238 239 pub fn clearGrid(self: *View) void { 240 self.box.clearGrid(); 241 } 242 243 pub fn getGrid(self: View) ?Grid { 244 return self.box.getGrid(); 245 } 246 247 pub fn getFocus(self: *View) *Focus { 248 return self.box.getFocus(); 249 } 250 }; 251