╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
883 }; 884 885 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 886 const page_widget: Widget = switch (page.*) { 887 .home => |*p| .{ .home = try .init(allocator, p, session) }, 888 .user => |*p| .{ .user = try .init(allocator, p, session) }, 889 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 890 }; 891 892 const demon_art = @embedFile("embed/demon.ans"); 893 894 // on the TUI/SSH, the page sits above a one-row footer showing the url 895 var root = if (session.is_terminal) blk: { 896 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 897 errdefer box.deinit(allocator); 898 const bg_id = bg_blk: { 899 var background = try AnsiBackground.init(allocator, page_widget, demon_art, session); 900 errdefer background.deinit(allocator); 901 const id = background.getFocus().id; 902 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 903 break :bg_blk id; 904 }; 905 { 906 var footer = try Footer.init(allocator, session); 907 errdefer footer.deinit(allocator); 908 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 909 } 910 box.getFocus().child_id = bg_id; 911 break :blk Widget{ .box = box }; 912 } else Widget{ .background = try AnsiBackground.init(allocator, page_widget, demon_art, session) }; 913 errdefer root.deinit(allocator); 914 915 // input-owning views build their TextInputs in init — so reset the 916 // focus-id -> *TextInput map here 917 session.text_inputs.clearRetainingCapacity(); 918 919 try root.build(allocator, .{ 920 .min_size = .{ .width = null, .height = 40 }, 921 .max_size = .{ .width = 80, .height = null }, 922 }, root.getFocus()); 923 if (root.getFocus().child_id) |child_id| { 924 try root.getFocus().setFocus(child_id); 925 } 926 927 return root; 928 } 929 930 pub const Widget = union(enum) { 931 text: wgt.Text(Widget), 932 box: wgt.Box(Widget), 933 text_box: wgt.TextBox(Widget), 934 text_input: wgt.TextInput(Widget), 935 scroll: wgt.Scroll(Widget), 936 stack: wgt.Stack(Widget), 937 flow_box: FlowBox, 938 flow_box_scroll: FlowBox.Scroll, 939 spacer: Spacer, 940 center: Center, 941 ansi_art: AnsiArt, 942 background: AnsiBackground, 943 home: Home.View, 944 user: User.View, 945 repo: Repo.View, 946 quit: Quit.View, 947 title: Title.View, 948 sub_title: SubTitle.View, 949 home_header: Home.Header.View, 950 user_header: User.Header.View, 951 repo_header: Repo.Header.View, 952 repo_files: Repo.Files.View, 953 repo_commits: Repo.Commits.View, 954 repo_refs: Repo.Refs.View, 955 home_users: Home.Users.View, 956 home_repos: Home.Repos.View, 957 auth_tab: Home.Header.AuthTab.View, 958 home_settings: Home.Settings.View, 959 home_auth: Home.Auth.View, 960 footer: Footer, 961 962 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 963 switch (self.*) { 964 inline else => |*case| case.deinit(allocator), 965 } 966 } 967 968 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 969 switch (self.*) { 970 inline else => |*case| try case.build(allocator, constraint, root_focus), 971 } 972 } 973 974 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) anyerror!void { 975 switch (self.*) { 976 inline else => |*case| try case.input(allocator, key, root_focus), 977 } 978 } 979 980 pub fn clearGrid(self: *Widget) void { 981 switch (self.*) { 982 inline else => |*case| case.clearGrid(), 983 } 984 } 985 986 pub fn getGrid(self: Widget) ?Grid { 987 switch (self) { 988 inline else => |*case| return case.getGrid(), 989 } 990 } 991 992 pub fn getFocus(self: *Widget) *Focus { 993 switch (self.*) { 994 inline else => |*case| return case.getFocus(), 995 } 996 } 997 }; 998 999 pub const FlowBox = struct { 1000 focus: *Focus, 1001 grid: ?Grid, 1002 text_boxes: std.ArrayList(wgt.TextBox(Widget)), 1003 // backs the per-item strings the text boxes borrow: each box's `content` 1004 // and, for link items, its `.custom` focus kind (e.g. "a:/user/foo"). reset 1005 // wholesale on each setItems so there's no per-string ownership to track. 1006 arena: std.heap.ArenaAllocator, 1007 // column count from the last build — FlowBox.Scroll.input uses it so arrow 1008 // up/down can step by a row's worth of items. 1009 last_cols: usize, 1010 options: Options, 1011 1012 const border_rows: usize = 2; 1013 1014 pub const Options = struct { 1015 cell_width: usize = 40, 1016 cell_height: usize = 3, 1017 }; 1018 1019 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 1020 return .{ 1021 .focus = try Focus.create(allocator, .container), 1022 .grid = null, 1023 .text_boxes = .empty, 1024 .arena = std.heap.ArenaAllocator.init(allocator), 1025 .last_cols = 1, 1026 .options = options, 1027 }; 1028 } 1029 1030 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 1031 self.focus.destroy(allocator); 1032 if (self.grid) |*grid| { 1033 grid.deinit(); 1034 self.grid = null; 1035 } 1036 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1037 self.text_boxes.deinit(allocator); 1038 self.arena.deinit(); 1039 } 1040 1041 // one flow item: its display text plus an optional link. a non-empty `link` 1042 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 1043 // the web renderer turns into an anchor. 1044 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 1045 1046 // both the item text and the link are copied into the arena, so the caller's 1047 // slices needn't outlive this call. 1048 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 1049 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 1050 self.text_boxes.clearAndFree(allocator); 1051 1052 // the old text boxes (and their borrowed content/links) are gone now, so 1053 // the arena that backed those strings can be reclaimed. 1054 _ = self.arena.reset(.retain_capacity); 1055 const aa = self.arena.allocator(); 1056 1057 self.focus.clear(); 1058 self.focus.child_id = null; 1059 1060 for (items) |item| { 1061 const line = try aa.dupe(u8, item.text); 1062 1063 var text_box = try wgt.TextBox(Widget).init(allocator, line, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 1064 errdefer text_box.deinit(allocator); 1065 text_box.getFocus().focusable = true; 1066 1067 if (item.link.len > 0) { 1068 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 1069 } 1070 1071 try self.text_boxes.append(allocator, text_box); 1072 } 1073 1074 if (self.text_boxes.items.len > 0) { 1075 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 1076 } 1077 } 1078 1079 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1080 self.clearGrid(); 1081 self.focus.clear(); 1082 1083 const cell_width = self.options.cell_width; 1084 const max_width = constraint.max_size.width orelse cell_width; 1085 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 1086 self.last_cols = cols; 1087 1088 const count = self.text_boxes.items.len; 1089 if (count == 0) return; 1090 const slot_height = self.options.cell_height + border_rows; 1091 if (slot_height == 0) return; 1092 1093 // build at the slot size so every text box fits a single grid cell 1094 for (self.text_boxes.items) |*tb| { 1095 tb.options.border_style = if (self.focus.child_id == tb.getFocus().id) .single else .hidden; 1096 try tb.build(allocator, .{ 1097 .min_size = .{ .width = cell_width, .height = null }, 1098 .max_size = .{ .width = cell_width, .height = slot_height }, 1099 }, root_focus); 1100 } 1101 1102 const rows = (count + cols - 1) / cols; 1103 const content_height = rows * slot_height; 1104 const total_width = cols * cell_width; 1105 if (total_width == 0 or content_height == 0) return; 1106 1107 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 1108 errdefer grid.deinit(); 1109 1110 for (self.text_boxes.items, 0..) |*tb, i| { 1111 const tb_grid = tb.getGrid() orelse continue; 1112 const col = i % cols; 1113 const row = i / cols; 1114 const cell_x = col * cell_width; 1115 const cell_y = row * slot_height; 1116 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 1117 try grid.drawGrid(tb_grid, cell_x, cell_y); 1118 } 1119 1120 self.grid = grid; 1121 } 1122 1123 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1124 _ = self; 1125 _ = allocator; 1126 _ = key; 1127 _ = root_focus; 1128 } 1129 1130 pub fn clearGrid(self: *FlowBox) void { 1131 if (self.grid) |*grid| { 1132 grid.deinit(); 1133 self.grid = null; 1134 } 1135 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1136 } 1137 1138 pub fn getGrid(self: FlowBox) ?Grid { 1139 return self.grid; 1140 } 1141 1142 pub fn getFocus(self: *FlowBox) *Focus { 1143 return self.focus; 1144 } 1145 1146 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 1147 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 1148 const slot_height = self.options.cell_height + border_rows; 1149 const col = index % self.last_cols; 1150 const row = index / self.last_cols; 1151 return .{ 1152 .x = @intCast(col * self.options.cell_width), 1153 .y = @intCast(row * slot_height), 1154 .size = .{ .width = self.options.cell_width, .height = slot_height }, 1155 }; 1156 } 1157 1158 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 1159 for (self.text_boxes.items, 0..) |tb, i| { 1160 if (tb.box.focus.id == focus_id) return i; 1161 } 1162 return null; 1163 } 1164 1165 pub const Scroll = struct { 1166 scroll: wgt.Scroll(Widget), 1167 1168 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 1169 var layout_inner = try FlowBox.init(allocator, options); 1170 errdefer layout_inner.deinit(allocator); 1171 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 1172 errdefer scroll.deinit(allocator); 1173 return .{ .scroll = scroll }; 1174 } 1175 1176 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 1177 self.scroll.deinit(allocator); 1178 } 1179 1180 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 1181 self.scroll.x = 0; 1182 self.scroll.y = 0; 1183 try self.inner().setItems(allocator, items); 1184 } 1185 1186 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1187 try self.scroll.build(allocator, constraint, root_focus); 1188 } 1189 1190 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1191 _ = allocator; 1192 const in = self.inner(); 1193 const child_id = in.focus.child_id orelse return; 1194 const current_index = in.indexOfFocusId(child_id) orelse return; 1195 const count = in.text_boxes.items.len; 1196 if (count == 0) return; 1197 const cols = in.last_cols; 1198 const slot_height = in.options.cell_height + FlowBox.border_rows; 1199 1200 var index = current_index; 1201 switch (key) { 1202 .arrow_up => index -|= cols, 1203 .arrow_down => if (index + cols < count) { 1204 index += cols; 1205 }, 1206 .arrow_left => index -|= 1, 1207 .arrow_right => if (index + 1 < count) { 1208 index += 1; 1209 }, 1210 .home => index = 0, 1211 .end => index = count - 1, 1212 .page_up => { 1213 if (self.scroll.grid) |grid| if (slot_height > 0) { 1214 const rows_per_page = grid.size.height / slot_height; 1215 index -|= rows_per_page * cols; 1216 }; 1217 }, 1218 .page_down => { 1219 if (self.scroll.grid) |grid| if (slot_height > 0) { 1220 const rows_per_page = grid.size.height / slot_height; 1221 index = @min(index + rows_per_page * cols, count - 1); 1222 }; 1223 }, 1224 // scroll wheel moves the focused cell by a full row so the 1225 // viewport (via scrollToRect) follows in row-sized steps, 1226 // matching how a scroll wheel feels in a grid view. 1227 .mouse => |mouse| switch (mouse.action) { 1228 .scroll => |dir| switch (dir) { 1229 .up => index -|= cols, 1230 .down => if (index + cols < count) { 1231 index += cols; 1232 }, 1233 }, 1234 else => {}, 1235 }, 1236 else => {}, 1237 } 1238 1239 if (index != current_index) { 1240 try root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 1241 if (in.cellRect(index)) |rect| { 1242 self.scroll.scrollToRect(rect); 1243 } 1244 } 1245 } 1246 1247 pub fn clearGrid(self: *Scroll) void { 1248 self.scroll.clearGrid(); 1249 } 1250 1251 pub fn getGrid(self: Scroll) ?Grid { 1252 return self.scroll.getGrid(); 1253 } 1254 1255 pub fn getFocus(self: *Scroll) *Focus { 1256 return self.scroll.getFocus(); 1257 } 1258 1259 pub fn getSelectedIndex(self: Scroll) ?usize { 1260 const in = self.scroll.child.flow_box; 1261 const child_id = in.focus.child_id orelse return null; 1262 return in.indexOfFocusId(child_id); 1263 } 1264 1265 fn inner(self: *Scroll) *FlowBox { 1266 return &self.scroll.child.flow_box; 1267 } 1268 }; 1269 }; 1270 1271 // an invisible widget that fills the horizontal space granted by its parent. 1272 // used inside Box(horiz) with a min_size so the box reserves space for the 1273 // children that follow, pushing them to the right. 1274 pub const Spacer = struct { 1275 focus: *Focus, 1276 grid: ?Grid, 1277 1278 pub fn init(allocator: std.mem.Allocator) !Spacer { 1279 return .{ 1280 .focus = try Focus.create(allocator, .container), 1281 .grid = null, 1282 }; 1283 } 1284 1285 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1286 self.focus.destroy(allocator); 1287 if (self.grid) |*grid| { 1288 grid.deinit(); 1289 self.grid = null; 1290 } 1291 } 1292 1293 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1294 _ = root_focus; 1295 self.clearGrid(); 1296 const width = constraint.max_size.width orelse return; 1297 if (width == 0) return; 1298 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1299 } 1300 1301 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1302 _ = self; 1303 _ = allocator; 1304 _ = key; 1305 _ = root_focus; 1306 } 1307 1308 pub fn clearGrid(self: *Spacer) void { 1309 if (self.grid) |*grid| { 1310 grid.deinit(); 1311 self.grid = null; 1312 } 1313 } 1314 1315 pub fn getGrid(self: Spacer) ?Grid { 1316 return self.grid; 1317 } 1318 1319 pub fn getFocus(self: *Spacer) *Focus { 1320 return self.focus; 1321 } 1322 }; 1323 1324 // a one-row, non-focusable status bar showing the current page's url 1325 pub const Footer = struct { 1326 focus: *Focus, 1327 grid: ?Grid, 1328 session: *Session, 1329 arena: std.heap.ArenaAllocator, 1330 1331 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1332 return .{ 1333 .focus = try Focus.create(allocator, .container), 1334 .grid = null, 1335 .session = session, 1336 .arena = std.heap.ArenaAllocator.init(allocator), 1337 }; 1338 } 1339 1340 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1341 self.focus.destroy(allocator); 1342 if (self.grid) |*grid| grid.deinit(); 1343 self.arena.deinit(); 1344 } 1345 1346 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1347 _ = root_focus; 1348 self.clearGrid(); 1349 const width = constraint.max_size.width orelse return; 1350 if (width == 0) return; 1351 1352 _ = self.arena.reset(.retain_capacity); 1353 const aa = self.arena.allocator(); 1354 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1355 const text = if (self.session.web_port) |port| 1356 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1357 else 1358 path; 1359 1360 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1361 errdefer grid.deinit(); 1362 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1363 var i: usize = 0; 1364 while (utf8.nextCodepointSlice()) |ch| { 1365 if (i == width) break; 1366 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1367 i += 1; 1368 } 1369 self.grid = grid; 1370 } 1371 1372 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1373 _ = self; 1374 _ = allocator; 1375 _ = key; 1376 _ = root_focus; 1377 } 1378 1379 pub fn clearGrid(self: *Footer) void { 1380 if (self.grid) |*grid| { 1381 grid.deinit(); 1382 self.grid = null; 1383 } 1384 } 1385 1386 pub fn getGrid(self: Footer) ?Grid { 1387 return self.grid; 1388 } 1389 1390 pub fn getFocus(self: *Footer) *Focus { 1391 return self.focus; 1392 } 1393 }; 1394 1395 // a single-child wrapper that builds the child at its natural size and 1396 // positions its grid in the middle of the area granted by the parent 1397 pub const Center = struct { 1398 focus: *Focus, 1399 grid: ?Grid, 1400 child: *Widget, 1401 direction: Direction, 1402 1403 pub const Direction = enum { both, horiz, vert }; 1404 1405 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, direction: Direction) !Center { 1406 const child = try allocator.create(Widget); 1407 errdefer allocator.destroy(child); 1408 child.* = child_widget; 1409 return .{ 1410 .focus = try Focus.create(allocator, .container), 1411 .grid = null, 1412 .child = child, 1413 .direction = direction, 1414 }; 1415 } 1416 1417 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1418 self.focus.destroy(allocator); 1419 if (self.grid) |*grid| { 1420 grid.deinit(); 1421 self.grid = null; 1422 } 1423 self.child.deinit(allocator); 1424 allocator.destroy(self.child); 1425 } 1426 1427 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1428 self.clearGrid(); 1429 self.getFocus().clear(); 1430 1431 // build the child without forcing it to fill min_size; it sizes 1432 // itself to its content within the available max. 1433 try self.child.build(allocator, .{ 1434 .min_size = .{ .width = null, .height = null }, 1435 .max_size = constraint.max_size, 1436 }, root_focus); 1437 1438 const child_grid = self.child.getGrid() orelse return; 1439 1440 // prefer max when given; otherwise grow to max(min, child) so that 1441 // when only a min is set (e.g. the wasm path passes viewport rows 1442 // as min_height with no max), we can still vertically center while 1443 // letting taller content extend past the min. 1444 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; 1445 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; 1446 if (width == 0 or height == 0) return; 1447 1448 const offset_x: usize = if (self.direction == .horiz or self.direction == .both) 1449 (width -| child_grid.size.width) / 2 1450 else 1451 0; 1452 const offset_y: usize = if (self.direction == .vert or self.direction == .both) 1453 (height -| child_grid.size.height) / 2 1454 else 1455 0; 1456 1457 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1458 errdefer grid.deinit(); 1459 try grid.drawGrid(child_grid, offset_x, offset_y); 1460 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1461 self.getFocus().child_id = self.child.getFocus().id; 1462 1463 self.grid = grid; 1464 } 1465 1466 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1467 try self.child.input(allocator, key, root_focus); 1468 } 1469 1470 pub fn clearGrid(self: *Center) void { 1471 if (self.grid) |*grid| { 1472 grid.deinit(); 1473 self.grid = null; 1474 } 1475 self.child.clearGrid(); 1476 } 1477 1478 pub fn getGrid(self: Center) ?Grid { 1479 return self.grid; 1480 } 1481 1482 pub fn getFocus(self: *Center) *Focus { 1483 return self.focus; 1484 } 1485 }; 1486 1487 // renders truecolor ANSI art 1488 pub const AnsiArt = struct { 1489 focus: *Focus, 1490 grid: ?Grid, 1491 content: []const u8, 1492 1493 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1494 return .{ 1495 .focus = try Focus.create(allocator, .container), 1496 .grid = null, 1497 .content = content, 1498 }; 1499 } 1500 1501 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1502 self.focus.destroy(allocator); 1503 if (self.grid) |*grid| { 1504 grid.deinit(); 1505 self.grid = null; 1506 } 1507 } 1508 1509 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1510 _ = root_focus; 1511 self.clearGrid(); 1512 1513 // parse into rows of cells, then copy into a rectangular grid 1514 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1515 defer { 1516 for (rows.items) |*row| row.deinit(allocator); 1517 rows.deinit(allocator); 1518 } 1519 var row: std.ArrayList(Grid.Cell) = .empty; 1520 errdefer row.deinit(allocator); 1521 1522 var style: Grid.Style = .{}; 1523 var width: usize = 0; 1524 const content = self.content; 1525 var i: usize = 0; 1526 while (i < content.len) { 1527 const byte = content[i]; 1528 if (byte == '\n') { 1529 width = @max(width, row.items.len); 1530 try rows.append(allocator, row); 1531 row = .empty; 1532 i += 1; 1533 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1534 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1535 var j = i + 2; 1536 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1537 if (j >= content.len) { 1538 i = content.len; // malformed trailing escape; stop 1539 } else { 1540 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1541 i = j + 1; 1542 } 1543 } else { 1544 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1545 const end = @min(content.len, i + len); 1546 const rune = content[i..end]; 1547 const transparent = end - i == 1 and rune[0] == ' ' and 1548 style.fg == null and style.bg == null and !style.inverted; 1549 try row.append(allocator, .{ 1550 .rune = if (transparent) null else rune, 1551 .style = style, 1552 }); 1553 i = end; 1554 } 1555 } 1556 // a trailing row with content but no closing newline 1557 if (row.items.len > 0) { 1558 width = @max(width, row.items.len); 1559 try rows.append(allocator, row); 1560 } else { 1561 row.deinit(allocator); 1562 } 1563 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1564 1565 const height = rows.items.len; 1566 if (width == 0 or height == 0) return; 1567 1568 const clamped_w = @min(width, constraint.max_size.width orelse width); 1569 const clamped_h = @min(height, constraint.max_size.height orelse height); 1570 1571 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1572 errdefer grid.deinit(); 1573 for (rows.items[0..clamped_h], 0..) |r, y| { 1574 const n = @min(r.items.len, clamped_w); 1575 for (r.items[0..n], 0..) |cell, x| { 1576 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1577 } 1578 } 1579 self.grid = grid; 1580 } 1581 1582 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1583 _ = self; 1584 _ = allocator; 1585 _ = key; 1586 _ = root_focus; 1587 } 1588 1589 pub fn clearGrid(self: *AnsiArt) void { 1590 if (self.grid) |*grid| { 1591 grid.deinit(); 1592 self.grid = null; 1593 } 1594 } 1595 1596 pub fn getGrid(self: AnsiArt) ?Grid { 1597 return self.grid; 1598 } 1599 1600 pub fn getFocus(self: *AnsiArt) *Focus { 1601 return self.focus; 1602 } 1603 1604 fn applySgr(style: *Grid.Style, params: []const u8) void { 1605 var nums: [16]u32 = undefined; 1606 var n: usize = 0; 1607 var it = std.mem.splitScalar(u8, params, ';'); 1608 while (it.next()) |tok| { 1609 if (n >= nums.len) break; 1610 // an empty parameter (e.g. bare "\x1b[m") means 0 1611 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1612 n += 1; 1613 } 1614 if (n == 0) { 1615 style.* = .{}; 1616 return; 1617 } 1618 var i: usize = 0; 1619 while (i < n) : (i += 1) { 1620 switch (nums[i]) { 1621 0 => style.* = .{}, 1622 7 => style.inverted = true, 1623 27 => style.inverted = false, 1624 39 => style.fg = null, 1625 49 => style.bg = null, 1626 38, 48 => { 1627 // truecolor form: 38;2;r;g;b — anything else is ignored 1628 if (i + 4 < n and nums[i + 1] == 2) { 1629 const c = Grid.Color{ 1630 .r = @truncate(nums[i + 2]), 1631 .g = @truncate(nums[i + 3]), 1632 .b = @truncate(nums[i + 4]), 1633 }; 1634 if (nums[i] == 38) style.fg = c else style.bg = c; 1635 i += 4; 1636 } 1637 }, 1638 else => {}, 1639 } 1640 } 1641 } 1642 }; 1643 1644 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1645 pub const AnsiBackground = struct { 1646 grid: ?Grid, 1647 child: *Widget, 1648 art: AnsiArt, 1649 session: *Session, 1650 1651 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1652 var cw = child_widget; 1653 const child = allocator.create(Widget) catch |e| { 1654 cw.deinit(allocator); 1655 return e; 1656 }; 1657 child.* = cw; 1658 errdefer { 1659 child.deinit(allocator); 1660 allocator.destroy(child); 1661 } 1662 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1663 } 1664 1665 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1666 if (self.grid) |*grid| grid.deinit(); 1667 self.art.deinit(allocator); 1668 self.child.deinit(allocator); 1669 allocator.destroy(self.child); 1670 } 1671 1672 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1673 self.clearGrid(); 1674 1675 // make the wrapped page fill the whole available area 1676 try self.child.build(allocator, .{ 1677 .min_size = .{ 1678 .width = constraint.max_size.width orelse constraint.min_size.width, 1679 .height = constraint.max_size.height orelse constraint.min_size.height, 1680 }, 1681 .max_size = constraint.max_size, 1682 }, root_focus); 1683 1684 if (self.session.data.enable_ansi) { 1685 if (self.child.getGrid()) |fg| { 1686 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1687 } 1688 } 1689 } 1690 1691 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1692 try self.child.input(allocator, key, root_focus); 1693 } 1694 1695 pub fn clearGrid(self: *AnsiBackground) void { 1696 if (self.grid) |*grid| { 1697 grid.deinit(); 1698 self.grid = null; 1699 } 1700 self.child.clearGrid(); 1701 self.art.clearGrid(); 1702 } 1703 1704 pub fn getGrid(self: AnsiBackground) ?Grid { 1705 return self.grid orelse self.child.getGrid(); 1706 } 1707 1708 pub fn getFocus(self: *AnsiBackground) *Focus { 1709 return self.child.getFocus(); 1710 } 1711 1712 // which top corner the art hugs. 1713 const ArtAnchor = enum { top_left, top_right }; 1714 1715 // a foreground cell counts as blank if it's empty or a bare space without styling 1716 fn cellIsBlank(cell: Grid.Cell) bool { 1717 if (cell.rune) |rune| { 1718 return std.mem.eql(u8, rune, " ") and 1719 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1720 } 1721 return true; 1722 } 1723 1724 // the art is dimmed to this fraction of its brightness so foreground text 1725 // stays legible over it 1726 const art_brightness = 45; // percent 1727 1728 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1729 const c = color orelse return null; 1730 return .{ 1731 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1732 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1733 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1734 }; 1735 } 1736 1737 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1738 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1739 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1740 // snap such channels clear of that range so the backdrop doesn't blink or 1741 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1742 // actually render truecolor. 1743 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1744 const c = color orelse return null; 1745 const snap = struct { 1746 fn f(v: u8) u8 { 1747 return if (v >= 1 and v <= 9) 10 else v; 1748 } 1749 }.f; 1750 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1751 } 1752 1753 // composites ANSI art behind a foreground grid 1754 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1755 art.clearGrid(); 1756 try art.build(allocator, .{ 1757 .min_size = .{ .width = null, .height = null }, 1758 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1759 }, root_focus); 1760 1761 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1762 errdefer out.deinit(); 1763 1764 if (art.getGrid()) |art_grid| { 1765 const anchor_x = switch (anchor) { 1766 .top_left => 0, 1767 .top_right => foreground.size.width -| art_grid.size.width, 1768 }; 1769 for (0..art_grid.size.height) |y| { 1770 for (0..art_grid.size.width) |x| { 1771 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1772 if (src.rune == null) continue; 1773 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1774 const dst = &out.cells.items[idx]; 1775 // composite the dimmed art behind every cell so it shows 1776 // through the whole UI: a blank cell becomes the art, while a 1777 // cell with a glyph keeps the glyph but takes the dimmed art as 1778 // its background (replacing whatever background it had, which 1779 // would otherwise obscure the art). 1780 if (cellIsBlank(dst.*)) { 1781 dst.* = src; 1782 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1783 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1784 } else { 1785 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1786 } 1787 } 1788 } 1789 } 1790 return out; 1791 } 1792 }; 1793