╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1018 // used inside Box(horiz) with a min_size so the box reserves space for the 1019 // children that follow, pushing them to the right. 1020 pub const Spacer = struct { 1021 focus: *Focus, 1022 grid: ?Grid, 1023 1024 pub fn init(allocator: std.mem.Allocator) !Spacer { 1025 return .{ 1026 .focus = try Focus.create(allocator, .container), 1027 .grid = null, 1028 }; 1029 } 1030 1031 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1032 self.focus.destroy(allocator); 1033 if (self.grid) |*grid| { 1034 grid.deinit(); 1035 self.grid = null; 1036 } 1037 } 1038 1039 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1040 _ = root_focus; 1041 self.clearGrid(); 1042 const width = constraint.max_size.width orelse return; 1043 if (width == 0) return; 1044 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1045 } 1046 1047 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1048 _ = self; 1049 _ = allocator; 1050 _ = key; 1051 _ = root_focus; 1052 } 1053 1054 pub fn clearGrid(self: *Spacer) void { 1055 if (self.grid) |*grid| { 1056 grid.deinit(); 1057 self.grid = null; 1058 } 1059 } 1060 1061 pub fn getGrid(self: Spacer) ?Grid { 1062 return self.grid; 1063 } 1064 1065 pub fn getFocus(self: *Spacer) *Focus { 1066 return self.focus; 1067 } 1068 }; 1069 1070 // a sub-header's search box: a labeled text input whose enter the owning view 1071 // turns into a navigation. shared by the files, commits and thread tabs. 1072 pub const SearchBox = struct { 1073 text_input: wgt.TextInput, 1074 session: *ui.Session, 1075 1076 // the text width, narrowed toward the minimum when the row is tight 1077 const min_width = 18; 1078 const max_width = 30; 1079 1080 // the room the box asks its row for 1081 pub const min_size: layout.MaybeSize = .{ .width = min_width + 2, .height = 3 }; 1082 1083 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 1084 var text_input = try wgt.TextInput.init(allocator, .{ .top_label = .{ .text = label }, .name = name, .round_corners = true, .visible_width = max_width, .render_content = session.is_terminal }); 1085 errdefer text_input.deinit(allocator); 1086 text_input.getFocus().mode = .all; 1087 if (text_value) |value| try text_input.setContent(allocator, value); 1088 return .{ .text_input = text_input, .session = session }; 1089 } 1090 1091 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 1092 self.text_input.deinit(allocator); 1093 } 1094 1095 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1096 // the row leaves room for its other children's minimums, so this is what can be spared 1097 const room = (constraint.max_size.width orelse max_width + 2) -| 2; 1098 self.text_input.options.visible_width = std.math.clamp(room, min_width, max_width); 1099 self.text_input.options.bottom_label.text = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 1100 try self.text_input.build(allocator, constraint, root_focus); 1101 if (!self.session.is_terminal) { 1102 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 1103 } 1104 } 1105 1106 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1107 try self.text_input.input(allocator, key, root_focus); 1108 } 1109 1110 // the typed text; the caller owns it 1111 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 1112 return self.text_input.text(allocator); 1113 } 1114 1115 // whether the cursor sits past the last character, where right-arrow leaves 1116 // the box rather than moving within it 1117 pub fn atEnd(self: *SearchBox) bool { 1118 return self.text_input.cursor == self.text_input.content.items.len; 1119 } 1120 1121 pub fn clearGrid(self: *SearchBox) void { 1122 self.text_input.clearGrid(); 1123 } 1124 1125 pub fn getGrid(self: SearchBox) ?Grid { 1126 return self.text_input.getGrid(); 1127 } 1128 1129 pub fn getFocus(self: *SearchBox) *Focus { 1130 return self.text_input.getFocus(); 1131 } 1132 }; 1133 1134 // a wrapping row of bordered options posted as one form field. the selection 1135 // is the focused option, so a click selects on the web too. 1136 pub const Radio = struct { 1137 // holds the options' flow, framed under the label when there is one 1138 box: wgt.Box(Widget), 1139 name: []const u8, 1140 values: []const []const u8, 1141 1142 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8, label: ?[]const u8) !Radio { 1143 var box = try wgt.Box(Widget).init(allocator, .{ 1144 .border = if (label != null) .single else null, 1145 .round_corners = true, 1146 .direction = .vert, 1147 .top_label = .{ .text = label orelse "" }, 1148 }); 1149 errdefer box.deinit(allocator); 1150 1151 var flow = try WordFlow.init(allocator); 1152 errdefer flow.deinit(allocator); 1153 const pa = session.page_arena.allocator(); 1154 const items = try pa.alloc(WordFlow.Item, values.len); 1155 for (items, values) |*item, value| item.* = .{ .text = value, .link = try pa.print(ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 1156 try flow.setItems(allocator, items); 1157 for (flow.text_boxes.items, values) |*option, value| { 1158 if (std.mem.eql(u8, value, initial)) flow.focus.child_id = option.getFocus().id; 1159 } 1160 1161 box.getFocus().child_id = flow.getFocus().id; 1162 try box.children.put(allocator, flow.getFocus().id, .{ .widget = .{ .word_flow = flow }, .rect = null, .min_size = null }); 1163 return .{ .box = box, .name = name, .values = values }; 1164 } 1165 1166 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 1167 self.box.deinit(allocator); 1168 } 1169 1170 fn optionFlow(self: *Radio) *WordFlow { 1171 return &self.box.children.values()[0].widget.word_flow; 1172 } 1173 1174 // the focused option's value 1175 pub fn selected(self: *Radio) []const u8 { 1176 const options = self.optionFlow(); 1177 return self.values[options.indexOfFocusId(options.focus.child_id orelse unreachable) orelse unreachable]; 1178 } 1179 1180 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1181 self.clearGrid(); 1182 const options = self.optionFlow(); 1183 const selected_id = options.focus.child_id; 1184 for (options.text_boxes.items) |*option| { 1185 option.options.border = if (option.getFocus().id == selected_id) .single else .hidden; 1186 } 1187 try self.box.build(allocator, constraint, root_focus); 1188 } 1189 1190 // left and right step through the options in order, however they wrap 1191 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1192 _ = allocator; 1193 const options = self.optionFlow(); 1194 const index = options.indexOfFocusId(options.focus.child_id orelse return) orelse return; 1195 const option_boxes = options.text_boxes.items; 1196 switch (key) { 1197 .arrow_left => if (index > 0) root_focus.setFocus(option_boxes[index - 1].getFocus().id), 1198 .arrow_right => if (index + 1 < option_boxes.len) root_focus.setFocus(option_boxes[index + 1].getFocus().id), 1199 else => {}, 1200 } 1201 } 1202 1203 pub fn clearGrid(self: *Radio) void { 1204 self.box.clearGrid(); 1205 } 1206 1207 pub fn getGrid(self: Radio) ?Grid { 1208 return self.box.getGrid(); 1209 } 1210 1211 pub fn getFocus(self: *Radio) *Focus { 1212 return self.box.getFocus(); 1213 } 1214 }; 1215 1216 // a tab's sub-header: an optional search box on the left, then a spacer, then 1217 // the clone url on the right. the owning view turns a submitted query into a 1218 // route, so route building stays with the tab that knows its own url. 1219 pub const SearchHeader = struct { 1220 box: wgt.Box(Widget), 1221 session: *ui.Session, 1222 1223 pub fn init( 1224 allocator: std.mem.Allocator, 1225 session: *ui.Session, 1226 handle: ui.RepoHandle, 1227 label: []const u8, 1228 name: []const u8, 1229 text: ?[]const u8, 1230 show_input: bool, 1231 ) !SearchHeader { 1232 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1233 errdefer box.deinit(allocator); 1234 1235 // the search box only appears where an index can serve it. 1236 if (show_input) { 1237 var search_box = try SearchBox.init(allocator, session, label, name, text); 1238 errdefer search_box.deinit(allocator); 1239 box.getFocus().child_id = search_box.getFocus().id; 1240 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1241 } 1242 1243 // the spacer pushes the clone url to the right edge. 1244 { 1245 var spacer = try Spacer.init(allocator); 1246 errdefer spacer.deinit(allocator); 1247 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1248 } 1249 1250 if (session.data.host_kind == .server) { 1251 if (try CopyableText.initClone(allocator, session, handle)) |value| { 1252 var clone_url = value; 1253 errdefer clone_url.deinit(allocator); 1254 const min_width = clone_url.minWidth(); 1255 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1256 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1257 } 1258 } 1259 1260 return .{ .box = box, .session = session }; 1261 } 1262 1263 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1264 self.box.deinit(allocator); 1265 } 1266 1267 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1268 self.clearGrid(); 1269 try self.box.build(allocator, constraint, root_focus); 1270 } 1271 1272 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1273 const current = self.box.getFocus().child_id orelse return; 1274 if (self.searchBox()) |search_box| { 1275 if (current == search_box.getFocus().id) { 1276 // tab, and right-arrow past the last character, step to the 1277 // clone url beside the box 1278 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1279 if (self.cloneUrl()) |clone_url| { 1280 root_focus.setFocus(clone_url.getFocus().id); 1281 return; 1282 } 1283 } 1284 return search_box.input(allocator, key, root_focus); 1285 } 1286 } 1287 const clone_url = self.cloneUrl() orelse return; 1288 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1289 if (self.searchBox()) |search_box| { 1290 root_focus.setFocus(search_box.getFocus().id); 1291 return; 1292 } 1293 } 1294 try clone_url.input(allocator, key, root_focus); 1295 } 1296 1297 // the typed query when enter should submit it: the box exists and holds 1298 // focus. the caller owns the returned text. 1299 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1300 const search_box = self.searchBox() orelse return null; 1301 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1302 return try search_box.text(allocator); 1303 } 1304 1305 // enter the header, preferring the search box. 1306 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1307 if (self.searchBox()) |search_box| { 1308 root_focus.setFocus(search_box.getFocus().id); 1309 return true; 1310 } 1311 const clone_url = self.cloneUrl() orelse return false; 1312 root_focus.setFocus(clone_url.getFocus().id); 1313 return true; 1314 } 1315 1316 pub fn hasFocusable(self: *SearchHeader) bool { 1317 return self.cloneUrl() != null or self.searchBox() != null; 1318 } 1319 1320 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1321 for (self.box.children.values()) |*child| switch (child.widget) { 1322 .copyable_text => |*copyable_text| return copyable_text, 1323 else => {}, 1324 }; 1325 return null; 1326 } 1327 1328 fn searchBox(self: *SearchHeader) ?*SearchBox { 1329 for (self.box.children.values()) |*child| switch (child.widget) { 1330 .search_box => |*search_box| return search_box, 1331 else => {}, 1332 }; 1333 return null; 1334 } 1335 1336 pub fn clearGrid(self: *SearchHeader) void { 1337 self.box.clearGrid(); 1338 } 1339 1340 pub fn getGrid(self: SearchHeader) ?Grid { 1341 return self.box.getGrid(); 1342 } 1343 1344 pub fn getFocus(self: *SearchHeader) *Focus { 1345 return self.box.getFocus(); 1346 } 1347 }; 1348 1349 // a one-row, non-focusable status bar showing the current page's url 1350 pub const Footer = struct { 1351 focus: *Focus, 1352 grid: ?Grid, 1353 session: *ui.Session, 1354 arena: std.heap.ArenaAllocator, 1355 url: []const u8 = "", 1356 url_width: usize = 0, 1357 1358 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1359 return .{ 1360 .focus = try Focus.create(allocator, .container), 1361 .grid = null, 1362 .session = session, 1363 .arena = std.heap.ArenaAllocator.init(allocator), 1364 }; 1365 } 1366 1367 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1368 self.focus.destroy(allocator); 1369 if (self.grid) |*grid| grid.deinit(); 1370 self.arena.deinit(); 1371 } 1372 1373 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1374 _ = root_focus; 1375 self.clearGrid(); 1376 const width = constraint.max_size.width orelse return; 1377 if (width == 0) return; 1378 1379 _ = self.arena.reset(.retain_capacity); 1380 self.url = ""; 1381 self.url_width = 0; 1382 const aa = self.arena.allocator(); 1383 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1384 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1385 self.url = text; 1386 1387 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1388 errdefer grid.deinit(); 1389 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1390 var i: usize = 0; 1391 while (utf8.nextCodepoint()) |ch| { 1392 const rune_width = xitui.width.cellWidth(ch); 1393 if (i + rune_width > width) break; 1394 // a terminal hyperlink to the page 1395 (try grid.cell(i, 0)).link = text; 1396 try grid.setRune(i, 0, ch); 1397 i += rune_width; 1398 } 1399 self.url_width = i; 1400 self.grid = grid; 1401 } 1402 1403 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1404 _ = allocator; 1405 switch (key) { 1406 .mouse => |mouse| { 1407 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1408 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1409 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1410 }, 1411 else => {}, 1412 } 1413 } 1414 1415 pub fn clearGrid(self: *Footer) void { 1416 if (self.grid) |*grid| { 1417 grid.deinit(); 1418 self.grid = null; 1419 } 1420 } 1421 1422 pub fn getGrid(self: Footer) ?Grid { 1423 return self.grid; 1424 } 1425 1426 pub fn getFocus(self: *Footer) *Focus { 1427 return self.focus; 1428 } 1429 }; 1430 1431 // a single-child wrapper that builds the child at its natural size and 1432 // positions its grid in the middle of the area granted by the parent 1433 // a bordered label, indented past the content it precedes 1434 pub const SectionLabel = struct { 1435 box: wgt.Box(Widget), 1436 1437 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1438 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1439 errdefer box.deinit(allocator); 1440 1441 { 1442 var gap = try wgt.Text.init(allocator, " "); 1443 errdefer gap.deinit(allocator); 1444 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1445 } 1446 1447 { 1448 var tb = try wgt.TextBox.init(allocator, content, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1449 errdefer tb.deinit(allocator); 1450 tb.getFocus().mode = .all; 1451 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1452 } 1453 1454 box.getFocus().child_id = box.children.keys()[1]; 1455 return .{ .box = box }; 1456 } 1457 1458 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1459 self.box.deinit(allocator); 1460 } 1461 1462 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1463 try self.box.build(allocator, constraint, root_focus); 1464 } 1465 1466 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1467 _ = self; 1468 _ = allocator; 1469 _ = key; 1470 _ = root_focus; 1471 } 1472 1473 pub fn clearGrid(self: *SectionLabel) void { 1474 self.box.clearGrid(); 1475 } 1476 1477 pub fn getGrid(self: SectionLabel) ?Grid { 1478 return self.box.getGrid(); 1479 } 1480 1481 pub fn getFocus(self: *SectionLabel) *Focus { 1482 return self.box.getFocus(); 1483 } 1484 }; 1485 1486 // a submit button, indented past its form's content. the web form POSTs 1487 // the button's form; terminal hosts detect presses through `buttonId`. 1488 pub const SubmitButton = struct { 1489 box: wgt.Box(Widget), 1490 1491 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1492 return initLabeled(allocator, "submit"); 1493 } 1494 1495 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1496 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1497 errdefer box.deinit(allocator); 1498 1499 { 1500 var gap = try wgt.Text.init(allocator, " "); 1501 errdefer gap.deinit(allocator); 1502 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1503 } 1504 1505 { 1506 var button = try wgt.TextBox.init(allocator, label, .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 1507 errdefer button.deinit(allocator); 1508 button.getFocus().mode = .all; 1509 // the renderer distinguishes plain clickables from buttons that 1510 // should POST to a server route by this kind. 1511 button.getFocus().kind = .{ .custom = "submit" }; 1512 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1513 } 1514 1515 box.getFocus().child_id = box.children.keys()[1]; 1516 return .{ .box = box }; 1517 } 1518 1519 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1520 self.box.deinit(allocator); 1521 } 1522 1523 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1524 const button = &self.box.children.values()[1]; 1525 try button.widget.text_box.setContent(allocator, label); 1526 button.min_size = .{ .width = label.len + 2, .height = null }; 1527 } 1528 1529 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1530 try self.box.build(allocator, constraint, root_focus); 1531 } 1532 1533 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1534 _ = self; 1535 _ = allocator; 1536 _ = key; 1537 _ = root_focus; 1538 } 1539 1540 pub fn clearGrid(self: *SubmitButton) void { 1541 self.box.clearGrid(); 1542 } 1543 1544 pub fn getGrid(self: SubmitButton) ?Grid { 1545 return self.box.getGrid(); 1546 } 1547 1548 pub fn getFocus(self: *SubmitButton) *Focus { 1549 return self.box.getFocus(); 1550 } 1551 1552 // the button's focus id, for click hit-testing 1553 pub fn buttonId(self: *const SubmitButton) usize { 1554 return self.box.children.keys()[1]; 1555 } 1556 }; 1557 1558 // keep `rect`, in a centered widget's space, in view within its scroll 1559 pub fn scrollToCenteredRect(scroll: *wgt.Scroll(Widget), rect: layout.IRect) void { 1560 const center = &scroll.child.center; 1561 const center_grid = center.getGrid() orelse return; 1562 const child_grid = center.child.getGrid() orelse return; 1563 const offset_y: isize = @intCast((center_grid.size.height -| child_grid.size.height) / 2); 1564 scroll.scrollToRect(.{ .x = rect.x, .y = rect.y + offset_y, .size = rect.size }); 1565 } 1566 1567 pub const Center = struct { 1568 focus: *Focus, 1569 grid: ?Grid, 1570 child: *Widget, 1571 1572 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1573 const child = try allocator.create(Widget); 1574 errdefer allocator.destroy(child); 1575 child.* = child_widget; 1576 return .{ 1577 .focus = try Focus.create(allocator, .container), 1578 .grid = null, 1579 .child = child, 1580 }; 1581 } 1582 1583 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1584 self.focus.destroy(allocator); 1585 if (self.grid) |*grid| { 1586 grid.deinit(); 1587 self.grid = null; 1588 } 1589 self.child.deinit(allocator); 1590 allocator.destroy(self.child); 1591 } 1592 1593 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1594 self.clearGrid(); 1595 self.getFocus().clear(); 1596 1597 // build the child without forcing it to fill min_size; it sizes 1598 // itself to its content within the available max. 1599 try self.child.build(allocator, .{ 1600 .min_size = .{ .width = null, .height = null }, 1601 .max_size = constraint.max_size, 1602 }, root_focus); 1603 1604 const child_grid = self.child.getGrid() orelse return; 1605 1606 // prefer max when given; otherwise grow to max(min, child) so that 1607 // when only a min is set (e.g. the wasm path passes viewport rows 1608 // as min_height with no max), we can still vertically center while 1609 // letting taller content extend past the min. 1610 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; 1611 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; 1612 if (width == 0 or height == 0) return; 1613 1614 const offset_x: usize = (width -| child_grid.size.width) / 2; 1615 const offset_y: usize = (height -| child_grid.size.height) / 2; 1616 1617 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1618 errdefer grid.deinit(); 1619 try grid.drawGrid(child_grid, offset_x, offset_y); 1620 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1621 self.getFocus().child_id = self.child.getFocus().id; 1622 1623 self.grid = grid; 1624 } 1625 1626 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1627 try self.child.input(allocator, key, root_focus); 1628 } 1629 1630 pub fn clearGrid(self: *Center) void { 1631 if (self.grid) |*grid| { 1632 grid.deinit(); 1633 self.grid = null; 1634 } 1635 self.child.clearGrid(); 1636 } 1637 1638 pub fn getGrid(self: Center) ?Grid { 1639 return self.grid; 1640 } 1641 1642 pub fn getFocus(self: *Center) *Focus { 1643 return self.focus; 1644 } 1645 }; 1646 1647 // renders truecolor ANSI art 1648 pub const AnsiArt = struct { 1649 focus: *Focus, 1650 grid: ?Grid, 1651 content: []const u8, 1652 1653 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1654 return .{ 1655 .focus = try Focus.create(allocator, .container), 1656 .grid = null, 1657 .content = content, 1658 }; 1659 } 1660 1661 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1662 self.focus.destroy(allocator); 1663 if (self.grid) |*grid| { 1664 grid.deinit(); 1665 self.grid = null; 1666 } 1667 } 1668 1669 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1670 _ = root_focus; 1671 self.clearGrid(); 1672 1673 // parse into rows of cells, then copy into a rectangular grid 1674 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1675 defer { 1676 for (rows.items) |*row| row.deinit(allocator); 1677 rows.deinit(allocator); 1678 } 1679 var row: std.ArrayList(Grid.Cell) = .empty; 1680 errdefer row.deinit(allocator); 1681 1682 var style: Grid.Style = .{}; 1683 var width: usize = 0; 1684 const content = self.content; 1685 var i: usize = 0; 1686 while (i < content.len) { 1687 const byte = content[i]; 1688 if (byte == '\n') { 1689 width = @max(width, row.items.len); 1690 try rows.append(allocator, row); 1691 row = .empty; 1692 i += 1; 1693 } else if (byte == '\r') { 1694 // git may checkout the ansi art with crlf line endings on windows 1695 i += 1; 1696 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1697 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1698 var j = i + 2; 1699 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1700 if (j >= content.len) { 1701 i = content.len; // malformed trailing escape; stop 1702 } else { 1703 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1704 i = j + 1; 1705 } 1706 } else { 1707 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1708 const end = @min(content.len, i + len); 1709 const rune = content[i..end]; 1710 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1711 // a foreground color does not make a space visible. only an 1712 // effective background makes it opaque; foreground state often 1713 // remains active through the padding at the end of a row. 1714 const effective_background = if (style.inverted) style.fg else style.bg; 1715 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1716 try row.append(allocator, .{ 1717 .rune = if (transparent) null else codepoint, 1718 .style = style, 1719 }); 1720 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1721 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1722 } 1723 i = end; 1724 } 1725 } 1726 // a trailing row with content but no closing newline 1727 if (row.items.len > 0) { 1728 width = @max(width, row.items.len); 1729 try rows.append(allocator, row); 1730 } else { 1731 row.deinit(allocator); 1732 } 1733 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1734 1735 const height = rows.items.len; 1736 if (width == 0 or height == 0) return; 1737 1738 const clamped_w = @min(width, constraint.max_size.width orelse width); 1739 const clamped_h = @min(height, constraint.max_size.height orelse height); 1740 1741 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1742 errdefer grid.deinit(); 1743 for (rows.items[0..clamped_h], 0..) |r, y| { 1744 const n = @min(r.items.len, clamped_w); 1745 for (r.items[0..n], 0..) |cell, x| { 1746 if (cell.continuation) continue; 1747 (try grid.cell(x, y)).style = cell.style; 1748 try grid.setRune(x, y, cell.rune); 1749 } 1750 } 1751 self.grid = grid; 1752 } 1753 1754 pub fn clearGrid(self: *AnsiArt) void { 1755 if (self.grid) |*grid| { 1756 grid.deinit(); 1757 self.grid = null; 1758 } 1759 } 1760 1761 pub fn getGrid(self: AnsiArt) ?Grid { 1762 return self.grid; 1763 } 1764 1765 pub fn getFocus(self: *AnsiArt) *Focus { 1766 return self.focus; 1767 } 1768 1769 fn applySgr(style: *Grid.Style, params: []const u8) void { 1770 var nums: [16]u32 = undefined; 1771 var n: usize = 0; 1772 var it = std.mem.splitScalar(u8, params, ';'); 1773 while (it.next()) |tok| { 1774 if (n >= nums.len) break; 1775 // an empty parameter (e.g. bare "\x1b[m") means 0 1776 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1777 n += 1; 1778 } 1779 if (n == 0) { 1780 style.* = .{}; 1781 return; 1782 } 1783 var i: usize = 0; 1784 while (i < n) : (i += 1) { 1785 switch (nums[i]) { 1786 0 => style.* = .{}, 1787 7 => style.inverted = true, 1788 27 => style.inverted = false, 1789 39 => style.fg = null, 1790 49 => style.bg = null, 1791 38, 48 => { 1792 // truecolor form: 38;2;r;g;b — anything else is ignored 1793 if (i + 4 < n and nums[i + 1] == 2) { 1794 const c = Grid.Color{ .rgb = .{ 1795 .r = @truncate(nums[i + 2]), 1796 .g = @truncate(nums[i + 3]), 1797 .b = @truncate(nums[i + 4]), 1798 } }; 1799 if (nums[i] == 38) style.fg = c else style.bg = c; 1800 i += 4; 1801 } 1802 }, 1803 else => {}, 1804 } 1805 } 1806 } 1807 }; 1808 1809 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1810 pub const AnsiBackground = struct { 1811 grid: ?Grid, 1812 child: *Widget, 1813 art: AnsiArt, 1814 session: *ui.Session, 1815 1816 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1817 var cw = child_widget; 1818 const child = allocator.create(Widget) catch |e| { 1819 cw.deinit(allocator); 1820 return e; 1821 }; 1822 child.* = cw; 1823 errdefer { 1824 child.deinit(allocator); 1825 allocator.destroy(child); 1826 } 1827 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1828 } 1829 1830 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1831 if (self.grid) |*grid| grid.deinit(); 1832 self.art.deinit(allocator); 1833 self.child.deinit(allocator); 1834 allocator.destroy(self.child); 1835 } 1836 1837 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1838 self.clearGrid(); 1839 1840 // make the wrapped page fill the whole available area 1841 try self.child.build(allocator, .{ 1842 .min_size = .{ 1843 .width = constraint.max_size.width orelse constraint.min_size.width, 1844 .height = constraint.max_size.height orelse constraint.min_size.height, 1845 }, 1846 .max_size = constraint.max_size, 1847 }, root_focus); 1848 1849 const foreground = self.child.getGrid() orelse return; 1850 if (!self.session.color) return; 1851 // terminals show the art only once they report a background to fade it toward 1852 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1853 self.art.content = self.session.data.ansi_art; 1854 try self.art.build(allocator, .{ 1855 .min_size = .{ .width = null, .height = null }, 1856 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1857 }, root_focus); 1858 // the web fades the art with css opacity instead 1859 const art_backdrop = backdrop orelse return; 1860 if (self.art.grid) |*art_grid| { 1861 for (art_grid.cells) |*cell| { 1862 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1863 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1864 } 1865 self.grid = try artBehind(allocator, foreground, art_grid.*); 1866 } 1867 } 1868 1869 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1870 try self.child.input(allocator, key, root_focus); 1871 } 1872 1873 pub fn clearGrid(self: *AnsiBackground) void { 1874 if (self.grid) |*grid| { 1875 grid.deinit(); 1876 self.grid = null; 1877 } 1878 self.child.clearGrid(); 1879 self.art.clearGrid(); 1880 } 1881 1882 pub fn getGrid(self: AnsiBackground) ?Grid { 1883 return self.grid orelse self.child.getGrid(); 1884 } 1885 1886 pub fn getFocus(self: *AnsiBackground) *Focus { 1887 return self.child.getFocus(); 1888 } 1889 1890 // a foreground cell counts as blank if it's empty or a bare space without styling 1891 fn cellIsBlank(cell: Grid.Cell) bool { 1892 if (cell.continuation) return false; 1893 if (cell.rune) |rune| { 1894 return rune == ' ' and 1895 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1896 } 1897 return true; 1898 } 1899 1900 // the art is faded toward the background at this opacity so foreground 1901 // text stays legible over it 1902 pub const art_opacity = 25; // percent 1903 1904 // the art parser only emits truecolor, so a palette color never occurs 1905 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1906 const c = color orelse return null; 1907 return switch (c) { 1908 .rgb => |v| v, 1909 else => null, 1910 }; 1911 } 1912 1913 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1914 const v = artRgb(color) orelse return color; 1915 const blend = struct { 1916 fn f(c: u8, b: u8) u8 { 1917 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1918 } 1919 }.f; 1920 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1921 } 1922 1923 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1924 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1925 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1926 // snap such channels clear of that range so the backdrop doesn't blink or 1927 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1928 // actually render truecolor. 1929 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1930 const v = artRgb(color) orelse return color; 1931 const snap = struct { 1932 fn f(c: u8) u8 { 1933 return if (c >= 1 and c <= 9) 10 else c; 1934 } 1935 }.f; 1936 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1937 } 1938 1939 // composites ANSI art behind a foreground grid 1940 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1941 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1942 errdefer out.deinit(); 1943 1944 const anchor_x = foreground.size.width -| art_grid.size.width; 1945 for (0..art_grid.size.height) |y| { 1946 for (0..art_grid.size.width) |x| { 1947 const src = (try art_grid.cell(x, y)).*; 1948 if (src.rune == null) continue; 1949 const dst = try out.cell(anchor_x + x, y); 1950 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1951 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1952 // blank cells take the art; occupied cells take its background 1953 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1954 dst.style = src.style; 1955 dst.style.fg = sgrSafe(dst.style.fg); 1956 dst.style.bg = sgrSafe(dst.style.bg); 1957 try out.setRune(anchor_x + x, y, src.rune); 1958 } else { 1959 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1960 dst.style.bg = art_bg; 1961 if (next_dst) |next| next.style.bg = art_bg; 1962 } 1963 } 1964 } 1965 return out; 1966 } 1967 }; 1968 1969 pub const CopyableText = struct { 1970 pub const Choice = struct { 1971 selector: []const u8 = "", 1972 text: []const u8, 1973 copyable_text: ?[]const u8 = null, 1974 label: []const u8 = "", 1975 bottom_label: []const u8 = "", 1976 }; 1977 1978 box: wgt.Box(Widget), 1979 session: *ui.Session, 1980 choices: []Choice, 1981 selected: usize = 0, 1982 1983 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1984 if (choices.len == 0) return error.MissingCopyableText; 1985 1986 const owned_choices = try allocator.dupe(Choice, choices); 1987 errdefer allocator.free(owned_choices); 1988 1989 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1990 errdefer box.deinit(allocator); 1991 1992 if (choices.len > 1) for (choices) |choice| { 1993 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1994 errdefer selector_box.deinit(allocator); 1995 selector_box.getFocus().mode = .all; 1996 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1997 }; 1998 1999 var text_input = try wgt.TextInput.init(allocator, .{ 2000 .border = .single, 2001 .round_corners = true, 2002 .top_label = .{ .text = choices[initial].label }, 2003 .bottom_label = .{ .text = choices[initial].bottom_label }, 2004 .read_only = true, 2005 .render_content = session.is_terminal, 2006 .visible_width = null, 2007 }); 2008 errdefer text_input.deinit(allocator); 2009 text_input.getFocus().mode = .all; 2010 try text_input.setContent(allocator, choices[initial].text); 2011 box.getFocus().child_id = text_input.getFocus().id; 2012 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 2013 2014 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 2015 } 2016 2017 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 2018 const aa = session.page_arena.allocator(); 2019 var choices: [2]Choice = undefined; 2020 var count: usize = 0; 2021 var ssh_index: ?usize = null; 2022 switch (handle.location) { 2023 .repo => |identity| { 2024 if (session.data.git_http_port) |port| { 2025 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 2026 choices[count] = .{ 2027 .selector = "http", 2028 .text = url, 2029 .copyable_text = try aa.print("git clone {s}", .{url}), 2030 .label = " clone ", 2031 }; 2032 count += 1; 2033 } 2034 if (session.data.git_ssh_port) |port| { 2035 ssh_index = count; 2036 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 2037 choices[count] = .{ 2038 .selector = "ssh", 2039 .text = url, 2040 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2041 .label = " clone ", 2042 }; 2043 count += 1; 2044 } 2045 }, 2046 .fork => |f| if (session.data.git_ssh_port) |port| { 2047 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 2048 choices[count] = .{ 2049 .text = url, 2050 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 2051 .label = " clone ", 2052 }; 2053 count += 1; 2054 }, 2055 } 2056 if (count == 0) return null; 2057 // a viewer who can push starts on the ssh url 2058 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 2059 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 2060 } 2061 2062 pub fn minWidth(self: *const CopyableText) usize { 2063 var width = maxWidth(self.choices) + 2; 2064 if (self.choices.len > 1) { 2065 for (self.choices) |choice| width += choice.selector.len + 2; 2066 } 2067 return width; 2068 } 2069 2070 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 2071 self.box.deinit(allocator); 2072 allocator.free(self.choices); 2073 } 2074 2075 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2076 self.clearGrid(); 2077 2078 if (self.choices.len > 1) { 2079 if (root_focus.grandchild_id) |id| { 2080 for (0..self.choices.len) |i| { 2081 if (id != self.selector(i).getFocus().id) continue; 2082 if (i != self.selected) try self.select(allocator, i); 2083 root_focus.setFocus(self.textInput().getFocus().id); 2084 break; 2085 } 2086 } 2087 2088 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2089 for (0..self.choices.len) |i| { 2090 self.selector(i).options.border = if (i == self.selected) 2091 (if (focused) .double else .single) 2092 else 2093 .hidden; 2094 } 2095 } 2096 self.textInput().options.border = .single; 2097 self.textInput().cursor = 0; 2098 self.textInput().scroll_offset = 0; 2099 try self.box.build(allocator, constraint, root_focus); 2100 2101 if (!self.session.is_terminal) { 2102 const aa = self.session.arena.allocator(); 2103 const id = self.textInput().getFocus().id; 2104 try self.session.text_inputs.put(aa, id, self.textInput()); 2105 } 2106 } 2107 2108 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2109 switch (key) { 2110 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2111 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2112 .enter => if (self.session.is_terminal) self.show(), 2113 .mouse => |mouse| { 2114 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2115 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2116 try self.select(allocator, i); 2117 root_focus.setFocus(self.textInput().getFocus().id); 2118 return; 2119 }; 2120 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2121 }, 2122 else => {}, 2123 } 2124 } 2125 2126 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2127 self.selected = selected; 2128 const choice = self.choices[selected]; 2129 const text_input = self.textInput(); 2130 text_input.options.top_label.text = choice.label; 2131 text_input.options.bottom_label.text = choice.bottom_label; 2132 try text_input.setContent(allocator, choice.text); 2133 } 2134 2135 fn show(self: *CopyableText) void { 2136 const choice = self.choices[self.selected]; 2137 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2138 } 2139 2140 fn textInput(self: *CopyableText) *wgt.TextInput { 2141 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2142 } 2143 2144 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2145 return &self.box.children.values()[index].widget.text_box; 2146 } 2147 2148 fn maxWidth(choices: []const Choice) usize { 2149 var width: usize = 0; 2150 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2151 return width; 2152 } 2153 2154 pub fn clearGrid(self: *CopyableText) void { 2155 self.box.clearGrid(); 2156 } 2157 2158 pub fn getGrid(self: CopyableText) ?Grid { 2159 return self.box.getGrid(); 2160 } 2161 2162 pub fn getFocus(self: *CopyableText) *Focus { 2163 return self.box.getFocus(); 2164 } 2165 }; 2166