╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Home.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
68 69 { 70 var repos_view = try Repos.View.init(allocator, &data.repos); 71 errdefer repos_view.deinit(allocator); 72 try stack.children.put(allocator, repos_view.getFocus().id, .{ .home_repos = repos_view }); 73 } 74 75 { 76 var auth_view = try Auth.View.init(allocator, &data.auth, session, users_tab_id); 77 errdefer auth_view.deinit(allocator); 78 try stack.children.put(allocator, auth_view.getFocus().id, .{ .home_auth = auth_view }); 79 } 80 81 try box.children.put(allocator, stack.getFocus().id, .{ .widget = .{ .stack = stack }, .rect = null, .min_size = null }); 82 } 83 84 var self = View{ 85 .box = box, 86 .data = data, 87 .session = session, 88 }; 89 self.getFocus().child_id = box.children.keys()[header_index]; 90 return self; 91 } 92 93 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 94 self.box.deinit(allocator); 95 } 96 97 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 98 self.clearGrid(); 99 const header = &self.box.children.values()[header_index].widget.home_header; 100 const stack = &self.box.children.values()[stack_index].widget.stack; 101 102 // each header tab maps 1:1 to a stack child by position 103 if (header.getSelectedIndex()) |index| { 104 stack.getFocus().child_id = stack.children.keys()[index]; 105 self.session.data.current_page = switch (index) { 106 0 => .home_users, 107 1 => .home_repos, 108 2 => .home_auth, 109 else => .home_users, 110 }; 111 } 112 try self.box.build(allocator, constraint, root_focus); 113 } 114 115 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 116 if (self.getFocus().child_id) |child_id| { 117 if (self.box.children.getIndex(child_id)) |current_index| { 118 const child = &self.box.children.values()[current_index].widget; 119 var index = current_index; 120 121 const Direction = enum { up, down, none }; 122 const direction: Direction = switch (key) { 123 .arrow_up => .up, 124 .arrow_down => .down, 125 .mouse => |mouse| if (mouse.action == .scroll) 126 (if (mouse.action.scroll == .up) .up else .down) 127 else 128 .none, 129 else => .none, 130 }; 131 132 switch (direction) { 133 .up => { 134 switch (child.*) { 135 .home_header => { 136 try child.input(allocator, key, root_focus); 137 }, 138 .stack => { 139 if (child.stack.getSelected()) |selected_widget| { 140 const at_top = switch (selected_widget.*) { 141 .home_users => |*v| v.getSelectedIndex() == 0, 142 .home_repos => |*v| v.getSelectedIndex() == 0, 143 .home_auth => |*v| v.getSelectedIndex() == 0, 144 else => false, 145 }; 146 if (at_top) { 147 index = header_index; 148 } else { 149 try child.input(allocator, key, root_focus); 150 } 151 } 152 }, 153 else => {}, 154 } 155 }, 156 .down => { 157 switch (child.*) { 158 .home_header => { 159 index = stack_index; 160 }, 161 .stack => { 162 try child.input(allocator, key, root_focus); 163 }, 164 else => {}, 165 } 166 }, 167 .none => { 168 try child.input(allocator, key, root_focus); 169 }, 170 } 171 172 if (index != current_index) { 173 try root_focus.setFocus(self.box.children.keys()[index]); 174 } 175 } 176 } 177 } 178 179 pub fn clearGrid(self: *View) void { 180 self.box.clearGrid(); 181 } 182 183 pub fn getGrid(self: View) ?Grid { 184 return self.box.getGrid(); 185 } 186 187 pub fn getFocus(self: *View) *Focus { 188 return self.box.getFocus(); 189 } 190 }; 191