╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1226 session: *ui.Session, 1227 choices: []Choice, 1228 selected: usize = 0, 1229 1230 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1231 if (choices.len == 0) return error.MissingCopyableText; 1232 1233 const owned_choices = try allocator.dupe(Choice, choices); 1234 errdefer allocator.free(owned_choices); 1235 1236 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1237 errdefer box.deinit(allocator); 1238 1239 if (choices.len > 1) for (choices) |choice| { 1240 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1241 errdefer selector_box.deinit(allocator); 1242 selector_box.getFocus().focusable = true; 1243 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 } }); 1244 }; 1245 1246 var text_input = try wgt.TextInput.init(allocator, .{ 1247 .border_style = .single, 1248 .label = choices[0].label, 1249 .bottom_label = choices[0].bottom_label, 1250 .read_only = true, 1251 .render_content = session.is_terminal, 1252 .visible_width = null, 1253 }); 1254 errdefer text_input.deinit(allocator); 1255 text_input.getFocus().focusable = true; 1256 try text_input.setContent(allocator, choices[0].text); 1257 box.getFocus().child_id = text_input.getFocus().id; 1258 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1259 1260 return .{ .box = box, .session = session, .choices = owned_choices }; 1261 } 1262 1263 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1264 const aa = session.page_arena.allocator(); 1265 var choices: [2]Choice = undefined; 1266 var count: usize = 0; 1267 switch (location) { 1268 .repo => |identity| { 1269 if (session.data.git_http_port) |port| { 1270 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1271 choices[count] = .{ 1272 .selector = "http", 1273 .text = url, 1274 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1275 .label = " clone ", 1276 }; 1277 count += 1; 1278 } 1279 if (session.data.git_ssh_port) |port| { 1280 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1281 choices[count] = .{ 1282 .selector = "ssh", 1283 .text = url, 1284 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1285 .label = " clone ", 1286 }; 1287 count += 1; 1288 } 1289 }, 1290 .fork => |f| if (f.target_branch.len != 0) if (session.data.git_ssh_port) |port| { 1291 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}/patch:{s}/branch:{s}", .{ port, f.identity, f.id, try ui.urlEncodeRef(aa, f.target_branch) }); 1292 choices[count] = .{ 1293 .text = url, 1294 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1295 .label = " clone ", 1296 }; 1297 count += 1; 1298 }, 1299 } 1300 if (count == 0) return null; 1301 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1302 } 1303 1304 pub fn minWidth(self: *const CopyableText) usize { 1305 var width = maxWidth(self.choices) + 2; 1306 if (self.choices.len > 1) { 1307 for (self.choices) |choice| width += choice.selector.len + 2; 1308 } 1309 return width; 1310 } 1311 1312 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1313 self.box.deinit(allocator); 1314 allocator.free(self.choices); 1315 } 1316 1317 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1318 self.clearGrid(); 1319 1320 if (self.choices.len > 1) { 1321 if (root_focus.grandchild_id) |id| { 1322 for (0..self.choices.len) |i| { 1323 if (id != self.selector(i).getFocus().id) continue; 1324 if (i != self.selected) try self.select(allocator, i); 1325 root_focus.setFocus(self.textInput().getFocus().id); 1326 break; 1327 } 1328 } 1329 1330 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1331 for (0..self.choices.len) |i| { 1332 self.selector(i).options.border_style = if (i == self.selected) 1333 (if (focused) .double else .single) 1334 else 1335 .hidden; 1336 } 1337 } 1338 self.textInput().options.border_style = .single; 1339 self.textInput().cursor = 0; 1340 self.textInput().scroll_offset = 0; 1341 try self.box.build(allocator, constraint, root_focus); 1342 1343 if (!self.session.is_terminal) { 1344 const aa = self.session.arena.allocator(); 1345 const id = self.textInput().getFocus().id; 1346 try self.session.text_inputs.put(aa, id, self.textInput()); 1347 } 1348 } 1349 1350 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1351 switch (key) { 1352 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1353 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1354 .enter => if (self.session.is_terminal) self.show(), 1355 .mouse => |mouse| { 1356 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1357 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1358 try self.select(allocator, i); 1359 root_focus.setFocus(self.textInput().getFocus().id); 1360 return; 1361 }; 1362 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1363 }, 1364 else => {}, 1365 } 1366 } 1367 1368 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1369 self.selected = selected; 1370 const choice = self.choices[selected]; 1371 const text_input = self.textInput(); 1372 text_input.options.label = choice.label; 1373 text_input.options.bottom_label = choice.bottom_label; 1374 try text_input.setContent(allocator, choice.text); 1375 } 1376 1377 fn show(self: *CopyableText) void { 1378 const choice = self.choices[self.selected]; 1379 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1380 } 1381 1382 fn textInput(self: *CopyableText) *wgt.TextInput { 1383 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1384 } 1385 1386 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1387 return &self.box.children.values()[index].widget.text_box; 1388 } 1389 1390 fn maxWidth(choices: []const Choice) usize { 1391 var width: usize = 0; 1392 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1393 return width; 1394 } 1395 1396 pub fn clearGrid(self: *CopyableText) void { 1397 self.box.clearGrid(); 1398 } 1399 1400 pub fn getGrid(self: CopyableText) ?Grid { 1401 return self.box.getGrid(); 1402 } 1403 1404 pub fn getFocus(self: *CopyableText) *Focus { 1405 return self.box.getFocus(); 1406 } 1407 }; 1408