╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const xit = @import("xit"); 3 const rp = xit.repo; 4 const hash = xit.hash; 5 const xitui = xit.xitui; 6 const term = xitui.terminal; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const inp = xitui.input; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const evt = @import("./event.zig"); 13 14 pub const canonical_repo_opts: rp.RepoOpts(.xit) = .{}; 15 pub const DB = rp.Repo(.xit, canonical_repo_opts).DB; 16 17 pub const Home = @import("./ui/Home.zig"); 18 pub const Title = @import("./ui/Title.zig"); 19 20 pub const Page = union(enum) { 21 home: Home, 22 }; 23 24 // a top-level "page" the user can navigate to 25 pub const RoutablePage = enum { 26 home_users, 27 home_repos, 28 home_auth, 29 30 pub const default: RoutablePage = .home_users; 31 32 pub fn url(self: RoutablePage) []const u8 { 33 return switch (self) { 34 .home_users => "/users", 35 .home_repos => "/repos", 36 .home_auth => "/auth", 37 }; 38 } 39 40 pub fn fromUrl(path: []const u8) ?RoutablePage { 41 if (std.mem.eql(u8, path, "/")) return default; 42 if (std.mem.eql(u8, path, "/users")) return .home_users; 43 if (std.mem.eql(u8, path, "/repos")) return .home_repos; 44 if (std.mem.eql(u8, path, "/auth")) return .home_auth; 45 return null; 46 } 47 }; 48 49 // per-connection mutable state. each SSH session / web session / local TUI 50 // run gets its own 51 pub const Session = struct { 52 data: Data = .{}, // serializable data sent down to web client 53 arena: *std.heap.ArenaAllocator, 54 haxy_moment: ?DB.HashMap(.read_only) = null, // db cursor (null on the wasm side) 55 56 pub const Data = struct { 57 user_id: ?[]const u8 = null, 58 // a transient outcome to surface from the last /login POST attempt 59 login_failure: ?Home.Auth.Login.Failure = null, 60 current_page: RoutablePage = .default, 61 }; 62 63 pub fn init( 64 comptime repo_opts: rp.RepoOpts(.xit), 65 arena: *std.heap.ArenaAllocator, 66 repo: *rp.Repo(.xit, repo_opts), 67 data: Data, 68 ) !Session { 69 const RepoDB = rp.Repo(.xit, repo_opts).DB; 70 71 const history = try RepoDB.ArrayList(.read_only).init(repo.core.db.rootCursor().readOnly()); 72 73 const moment_cursor = try history.getCursor(-1) orelse return error.NotFound; 74 const moment = try RepoDB.HashMap(.read_only).init(moment_cursor); 75 76 const last_object_id_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy-last-object-id")) orelse return error.NotFound; 77 var last_object_id: [hash.byteLen(repo_opts.hash)]u8 = undefined; 78 _ = try last_object_id_cursor.readBytes(&last_object_id); 79 80 const haxy_cursor = try moment.getCursor(hash.hashInt(repo_opts.hash, "haxy")) orelse return error.NotFound; 81 const haxy = try RepoDB.ArrayList(.read_only).init(haxy_cursor); 82 83 const haxy_moments_cursor = try haxy.getCursor(-1) orelse return error.NotFound; 84 const haxy_moments = try RepoDB.HashMap(.read_only).init(haxy_moments_cursor); 85 86 const haxy_moment_cursor = try haxy_moments.getCursor(hash.bytesToInt(repo_opts.hash, &last_object_id)) orelse return error.NotFound; 87 const haxy_moment = try RepoDB.HashMap(.read_only).init(haxy_moment_cursor); 88 89 return .{ 90 .data = data, 91 .arena = arena, 92 .haxy_moment = haxy_moment, 93 }; 94 } 95 }; 96 97 // what the server hands to the client (and what main_wasm parses on _start). 98 // keeps Page free of any per-request session state. 99 pub const Snapshot = struct { 100 page: Page, 101 session: Session.Data = .{}, 102 }; 103 104 pub fn run(io: std.Io, allocator: std.mem.Allocator, page: *const Page, session: *Session) !void { 105 var root = try initRoot(allocator, page, session); 106 defer root.deinit(allocator); 107 108 var terminal = try term.Terminal.init(io, allocator); 109 defer terminal.deinit(io); 110 111 var last_size = layout.Size{ .width = 0, .height = 0 }; 112 var last_grid = try Grid.init(allocator, last_size); 113 defer last_grid.deinit(); 114 115 while (!terminal.shouldQuit()) { 116 const grid_changed = try terminal.render(&root, &last_grid, &last_size); 117 118 // process any inputs. 119 // 120 // if the grid didn't change, then first do a blocking 121 // read, so the thread will sleep until further input. 122 // after that, all remaining reads are non-blocking so 123 // we can process the rest of the queued inputs. 124 // 125 // if the grid *did* change, then only do non-blocking 126 // reads. we do not want to sleep the thread because 127 // there may be an animation that requires more looping. 128 var blocking = !grid_changed; 129 while (try terminal.readKey(io, blocking)) |key| { 130 try inputKey(allocator, &root, key, &terminal); 131 blocking = false; 132 } 133 134 try root.build(allocator, .{ 135 .min_size = .{ .width = null, .height = null }, 136 .max_size = .{ .width = last_size.width, .height = last_size.height }, 137 }, root.getFocus()); 138 } 139 } 140 141 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: inp.Key, terminal: anytype) !void { 142 switch (key) { 143 .escape => terminal.requestQuit(), 144 .mouse => |mouse| { 145 if (mouse.action == .press and mouse.action.press == .left) { 146 const root_focus = root.getFocus(); 147 var iter = root_focus.children.iterator(); 148 while (iter.next()) |entry| { 149 const child = entry.value_ptr.*; 150 if (!child.focus.focusable) continue; 151 const r = child.rect; 152 if (mouse.x >= r.x and mouse.y >= r.y and 153 mouse.x < r.x + r.size.width and mouse.y < r.y + r.size.height) 154 { 155 try root_focus.setFocus(entry.key_ptr.*); 156 break; 157 } 158 } 159 // forward the press into the widget tree so buttons (and any 160 // future click-aware widgets) can react. widgets that don't 161 // care about presses ignore it. 162 try root.input(allocator, key, root.getFocus()); 163 } else { 164 try root.input(allocator, key, root.getFocus()); 165 } 166 }, 167 else => try root.input(allocator, key, root.getFocus()), 168 } 169 } 170 171 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 172 var root: Widget = switch (page.*) { 173 .home => |*p| .{ .home = try .init(allocator, p, session) }, 174 }; 175 errdefer root.deinit(allocator); 176 177 try root.build(allocator, .{ 178 .min_size = .{ .width = null, .height = null }, 179 .max_size = .{ .width = 80, .height = null }, 180 }, root.getFocus()); 181 if (root.getFocus().child_id) |child_id| { 182 try root.getFocus().setFocus(child_id); 183 } 184 185 return root; 186 } 187 188 pub const Widget = union(enum) { 189 text: wgt.Text(Widget), 190 box: wgt.Box(Widget), 191 text_box: wgt.TextBox(Widget), 192 text_input: wgt.TextInput(Widget), 193 scroll: wgt.Scroll(Widget), 194 stack: wgt.Stack(Widget), 195 flow_box: FlowBox, 196 spacer: Spacer, 197 center: Center, 198 home: Home.View, 199 title: Title.View, 200 home_header: Home.Header.View, 201 home_users: Home.Users.View, 202 home_repos: Home.Repos.View, 203 home_auth_tab: Home.Header.AuthTab.View, 204 home_auth: Home.Auth.View, 205 206 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 207 switch (self.*) { 208 inline else => |*case| case.deinit(allocator), 209 } 210 } 211 212 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 213 switch (self.*) { 214 inline else => |*case| try case.build(allocator, constraint, root_focus), 215 } 216 } 217 218 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 219 switch (self.*) { 220 inline else => |*case| try case.input(allocator, key, root_focus), 221 } 222 } 223 224 pub fn clearGrid(self: *Widget) void { 225 switch (self.*) { 226 inline else => |*case| case.clearGrid(), 227 } 228 } 229 230 pub fn getGrid(self: Widget) ?Grid { 231 switch (self) { 232 inline else => |*case| return case.getGrid(), 233 } 234 } 235 236 pub fn getFocus(self: *Widget) *Focus { 237 switch (self.*) { 238 inline else => |*case| return case.getFocus(), 239 } 240 } 241 }; 242 243 pub const FlowBox = struct { 244 focus: Focus, 245 grid: ?Grid, 246 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 247 lines: std.ArrayList([]const u8), 248 // column count from the last build — FlowBox.Scroll.input uses it so arrow 249 // up/down can step by a row's worth of items. 250 last_cols: usize, 251 options: Options, 252 253 const border_rows: usize = 2; 254 255 pub const Options = struct { 256 cell_width: usize = 40, 257 cell_height: usize = 3, 258 }; 259 260 pub fn init(options: Options) FlowBox { 261 return .{ 262 .focus = Focus.init(.container), 263 .grid = null, 264 .text_boxes = .empty, 265 .lines = .empty, 266 .last_cols = 1, 267 .options = options, 268 }; 269 } 270 271 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 272 self.focus.deinit(allocator); 273 if (self.grid) |*grid| { 274 grid.deinit(); 275 self.grid = null; 276 } 277 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 278 self.text_boxes.deinit(allocator); 279 for (self.lines.items) |line| allocator.free(line); 280 self.lines.deinit(allocator); 281 } 282 283 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const []const u8) !void { 284 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 285 self.text_boxes.clearAndFree(allocator); 286 287 for (self.lines.items) |line| allocator.free(line); 288 self.lines.clearAndFree(allocator); 289 290 self.focus.clear(); 291 self.focus.child_id = null; 292 293 for (items) |item| { 294 const line = try allocator.dupe(u8, item); 295 { 296 errdefer allocator.free(line); 297 try self.lines.append(allocator, line); 298 } 299 300 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 301 errdefer text_box.deinit(allocator); 302 text_box.getFocus().focusable = true; 303 try self.text_boxes.append(allocator, text_box); 304 } 305 306 if (self.text_boxes.items.len > 0) { 307 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 308 } 309 } 310 311 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 312 self.clearGrid(); 313 self.focus.clear(); 314 315 const cell_width = self.options.cell_width; 316 const max_width = constraint.max_size.width orelse cell_width; 317 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 318 self.last_cols = cols; 319 320 const count = self.text_boxes.items.len; 321 if (count == 0) return; 322 const slot_height = self.options.cell_height + border_rows; 323 if (slot_height == 0) return; 324 325 // build at the slot size so every text box fits a single grid cell 326 for (self.text_boxes.items) |*tb| { 327 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 328 try tb.build(allocator, .{ 329 .min_size = .{ .width = cell_width, .height = null }, 330 .max_size = .{ .width = cell_width, .height = slot_height }, 331 }, root_focus); 332 } 333 334 const rows = (count + cols - 1) / cols; 335 const content_height = rows * slot_height; 336 const total_width = cols * cell_width; 337 if (total_width == 0 or content_height == 0) return; 338 339 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 340 errdefer grid.deinit(); 341 342 for (self.text_boxes.items, 0..) |*tb, i| { 343 const tb_grid = tb.getGrid() orelse continue; 344 const col = i % cols; 345 const row = i / cols; 346 const cell_x = col * cell_width; 347 const cell_y = row * slot_height; 348 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 349 try grid.drawGrid(tb_grid, cell_x, cell_y); 350 } 351 352 self.grid = grid; 353 } 354 355 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 356 _ = self; 357 _ = allocator; 358 _ = key; 359 _ = root_focus; 360 } 361 362 pub fn clearGrid(self: *FlowBox) void { 363 if (self.grid) |*grid| { 364 grid.deinit(); 365 self.grid = null; 366 } 367 for (self.text_boxes.items) |*tb| tb.clearGrid(); 368 } 369 370 pub fn getGrid(self: FlowBox) ?Grid { 371 return self.grid; 372 } 373 374 pub fn getFocus(self: *FlowBox) *Focus { 375 return &self.focus; 376 } 377 378 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 379 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 380 const slot_height = self.options.cell_height + border_rows; 381 const col = index % self.last_cols; 382 const row = index / self.last_cols; 383 return .{ 384 .x = @intCast(col * self.options.cell_width), 385 .y = @intCast(row * slot_height), 386 .size = .{ .width = self.options.cell_width, .height = slot_height }, 387 }; 388 } 389 390 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 391 for (self.text_boxes.items, 0..) |tb, i| { 392 if (tb.box.focus.id == focus_id) return i; 393 } 394 return null; 395 } 396 397 pub const Scroll = struct { 398 scroll: wgt.Scroll(Widget), 399 400 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options) !Scroll { 401 var layout_inner = FlowBox.init(options); 402 errdefer layout_inner.deinit(allocator); 403 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .vert); 404 errdefer scroll.deinit(allocator); 405 return .{ .scroll = scroll }; 406 } 407 408 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 409 self.scroll.deinit(allocator); 410 } 411 412 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const []const u8) !void { 413 self.scroll.x = 0; 414 self.scroll.y = 0; 415 try self.inner().setItems(allocator, items); 416 } 417 418 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 419 try self.scroll.build(allocator, constraint, root_focus); 420 } 421 422 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 423 _ = allocator; 424 const in = self.inner(); 425 const child_id = in.focus.child_id orelse return; 426 const current_index = in.indexOfFocusId(child_id) orelse return; 427 const count = in.text_boxes.items.len; 428 if (count == 0) return; 429 const cols = in.last_cols; 430 const slot_height = in.options.cell_height + FlowBox.border_rows; 431 432 var index = current_index; 433 switch (key) { 434 .arrow_up => index -|= cols, 435 .arrow_down => if (index + cols < count) { 436 index += cols; 437 }, 438 .arrow_left => index -|= 1, 439 .arrow_right => if (index + 1 < count) { 440 index += 1; 441 }, 442 .home => index = 0, 443 .end => index = count - 1, 444 .page_up => { 445 if (self.scroll.grid) |grid| if (slot_height > 0) { 446 const rows_per_page = grid.size.height / slot_height; 447 index -|= rows_per_page * cols; 448 }; 449 }, 450 .page_down => { 451 if (self.scroll.grid) |grid| if (slot_height > 0) { 452 const rows_per_page = grid.size.height / slot_height; 453 index = @min(index + rows_per_page * cols, count - 1); 454 }; 455 }, 456 // scroll wheel moves the focused cell by a full row so the 457 // viewport (via scrollToRect) follows in row-sized steps, 458 // matching how a scroll wheel feels in a grid view. 459 .mouse => |mouse| switch (mouse.action) { 460 .scroll => |dir| switch (dir) { 461 .up => index -|= cols, 462 .down => if (index + cols < count) { 463 index += cols; 464 }, 465 }, 466 else => {}, 467 }, 468 else => {}, 469 } 470 471 if (index != current_index) { 472 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 473 if (in.cellRect(index)) |rect| { 474 self.scroll.scrollToRect(rect); 475 } 476 } 477 } 478 479 pub fn clearGrid(self: *Scroll) void { 480 self.scroll.clearGrid(); 481 } 482 483 pub fn getGrid(self: Scroll) ?Grid { 484 return self.scroll.getGrid(); 485 } 486 487 pub fn getFocus(self: *Scroll) *Focus { 488 return self.scroll.getFocus(); 489 } 490 491 pub fn getSelectedIndex(self: Scroll) ?usize { 492 const in = self.scroll.child.flow_box; 493 const child_id = in.focus.child_id orelse return null; 494 return in.indexOfFocusId(child_id); 495 } 496 497 fn inner(self: *Scroll) *FlowBox { 498 return &self.scroll.child.flow_box; 499 } 500 }; 501 }; 502 503 // an invisible widget that fills the horizontal space granted by its parent. 504 // used inside Box(horiz) with a min_size so the box reserves space for the 505 // children that follow, pushing them to the right. 506 pub const Spacer = struct { 507 focus: Focus, 508 grid: ?Grid, 509 510 pub fn init() Spacer { 511 return .{ 512 .focus = Focus.init(.container), 513 .grid = null, 514 }; 515 } 516 517 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 518 self.focus.deinit(allocator); 519 if (self.grid) |*grid| { 520 grid.deinit(); 521 self.grid = null; 522 } 523 } 524 525 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 526 _ = root_focus; 527 self.clearGrid(); 528 const width = constraint.max_size.width orelse return; 529 if (width == 0) return; 530 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 531 } 532 533 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 534 _ = self; 535 _ = allocator; 536 _ = key; 537 _ = root_focus; 538 } 539 540 pub fn clearGrid(self: *Spacer) void { 541 if (self.grid) |*grid| { 542 grid.deinit(); 543 self.grid = null; 544 } 545 } 546 547 pub fn getGrid(self: Spacer) ?Grid { 548 return self.grid; 549 } 550 551 pub fn getFocus(self: *Spacer) *Focus { 552 return &self.focus; 553 } 554 }; 555 556 // a single-child wrapper that builds the child at its natural size and 557 // positions its grid in the middle of the area granted by the parent 558 pub const Center = struct { 559 focus: Focus, 560 grid: ?Grid, 561 child: *Widget, 562 direction: Direction, 563 564 pub const Direction = enum { both, horiz, vert }; 565 566 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 567 const child = try allocator.create(Widget); 568 errdefer allocator.destroy(child); 569 child.* = child_widget; 570 return .{ 571 .focus = Focus.init(.container), 572 .grid = null, 573 .child = child, 574 .direction = direction, 575 }; 576 } 577 578 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 579 self.focus.deinit(allocator); 580 if (self.grid) |*grid| { 581 grid.deinit(); 582 self.grid = null; 583 } 584 self.child.deinit(allocator); 585 allocator.destroy(self.child); 586 } 587 588 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 589 self.clearGrid(); 590 self.getFocus().clear(); 591 592 // build the child without forcing it to fill min_size; it sizes 593 // itself to its content within the available max. 594 try self.child.build(allocator, .{ 595 .min_size = .{ .width = null, .height = null }, 596 .max_size = constraint.max_size, 597 }, root_focus); 598 599 const child_grid = self.child.getGrid() orelse return; 600 601 // prefer max when given; otherwise grow to max(min, child) so that 602 // when only a min is set (e.g. the wasm path passes viewport rows 603 // as min_height with no max), we can still vertically center while 604 // letting taller content extend past the min. 605 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 606 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 607 if (width == 0 or height == 0) return; 608 609 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 610 (width -| child_grid.size.width) / 2 611 else 612 0; 613 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 614 (height -| child_grid.size.height) / 2 615 else 616 0; 617 618 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 619 errdefer grid.deinit(); 620 try grid.drawGrid(child_grid, offset_x, offset_y); 621 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 622 self.getFocus().child_id = self.child.getFocus().id; 623 624 self.grid = grid; 625 } 626 627 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 628 try self.child.input(allocator, key, root_focus); 629 } 630 631 pub fn clearGrid(self: *Center) void { 632 if (self.grid) |*grid| { 633 grid.deinit(); 634 self.grid = null; 635 } 636 self.child.clearGrid(); 637 } 638 639 pub fn getGrid(self: Center) ?Grid { 640 return self.grid; 641 } 642 643 pub fn getFocus(self: *Center) *Focus { 644 return &self.focus; 645 } 646 }; 647