╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ UserNotifications.zig ────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
166 try list.setItems(allocator, items.items); 167 try stack.children.put(allocator, list.getFocus().id, .{ .flow_box_scroll = list }); 168 } 169 { 170 var settings = try ui.UserSettings.View.init(allocator, session, settings_route); 171 errdefer settings.deinit(allocator); 172 try stack.children.put(allocator, settings.getFocus().id, .{ .user_settings = settings }); 173 } 174 stack.getFocus().child_id = stack.children.keys()[@backingInt(data.view)]; 175 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 176 } 177 box.getFocus().child_id = box.children.keys()[content_index]; 178 return .{ .box = box, .data = data, .session = session, .control_ids = control_ids, .view_index = @backingInt(data.view) }; 179 } 180 181 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 182 self.box.deinit(allocator); 183 } 184 185 fn headerBox(self: *View) *wgt.Box(ui.Widget) { 186 return &self.box.children.values()[header_index].widget.box; 187 } 188 189 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 190 return &self.box.children.values()[content_index].widget.stack; 191 } 192 193 fn headerActive(self: *View) bool { 194 return self.box.getFocus().child_id == self.headerBox().getFocus().id; 195 } 196 197 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 198 self.clearGrid(); 199 const header = self.headerBox(); 200 // focusing a sub tab shows its content, while the logout button leaves it 201 if (header.getFocus().child_id) |child_id| if (std.mem.indexOfScalar(usize, self.control_ids[0..logout_index], child_id)) |index| { 202 self.view_index = index; 203 }; 204 for (self.control_ids[0..logout_index], 0..) |id, index| { 205 const tab = header.children.getPtr(id) orelse unreachable; 206 ui.widget.markSelected(&tab.widget.text_box, index == self.view_index); 207 } 208 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[self.view_index]; 209 // clear the incoming min height so the list scrolls within what's left 210 try self.box.build(allocator, .{ 211 .min_size = .{ .width = null, .height = null }, 212 .max_size = constraint.max_size, 213 }, root_focus); 214 } 215 216 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 217 const logout_id = self.control_ids[logout_index]; 218 if (key == .mouse and inp.leftClickOn(root_focus, logout_id, key.mouse)) return logOut(self.session); 219 const direction = inp.vertDirection(key); 220 if (self.headerActive()) { 221 const focused = root_focus.grandchild_id orelse return; 222 if (key == .enter and focused == logout_id) return logOut(self.session); 223 if (direction == .down) { 224 // the empty message has nothing to focus 225 if (self.view_index == @backingInt(ui.RoutablePage.UserView.notifications) and self.data.isEmpty()) return; 226 root_focus.setFocus(self.contentStack().getFocus().id); 227 } else if (std.mem.indexOfScalar(usize, &self.control_ids, focused)) |current| { 228 if (inp.moveTab(key, current, self.control_ids.len)) |next| root_focus.setFocus(self.control_ids[next]); 229 } 230 return; 231 } 232 if (direction == .up) if (self.contentStack().getSelected()) |selected| if (selected.atTop(root_focus)) { 233 root_focus.setFocus(self.control_ids[self.view_index]); 234 return; 235 }; 236 try self.contentStack().input(allocator, key, root_focus); 237 } 238 239 // moving up from the sub header returns to the page's tabs 240 pub fn atTop(self: *View) bool { 241 return self.headerActive(); 242 } 243 244 // arriving from the page's tabs lands on the sub header, not the content 245 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 246 root_focus.setFocus(self.control_ids[self.view_index]); 247 return true; 248 } 249 250 pub fn clearGrid(self: *View) void { 251 self.box.clearGrid(); 252 } 253 254 pub fn getGrid(self: View) ?Grid { 255 return self.box.getGrid(); 256 } 257 258 pub fn getFocus(self: *View) *Focus { 259 return self.box.getFocus(); 260 } 261 }; 262