╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1267 } 1268 }; 1269 1270 pub const CopyableText = struct { 1271 pub const Choice = struct { 1272 selector: []const u8 = "", 1273 text: []const u8, 1274 copyable_text: ?[]const u8 = null, 1275 label: []const u8 = "", 1276 bottom_label: []const u8 = "", 1277 }; 1278 1279 box: wgt.Box(Widget), 1280 session: *ui.Session, 1281 choices: []Choice, 1282 selected: usize = 0, 1283 1284 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1285 if (choices.len == 0) return error.MissingCopyableText; 1286 1287 const owned_choices = try allocator.dupe(Choice, choices); 1288 errdefer allocator.free(owned_choices); 1289 1290 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1291 errdefer box.deinit(allocator); 1292 1293 if (choices.len > 1) for (choices) |choice| { 1294 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .wrap_kind = .none }); 1295 errdefer selector_box.deinit(allocator); 1296 selector_box.getFocus().mode = .all; 1297 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 } }); 1298 }; 1299 1300 var text_input = try wgt.TextInput.init(allocator, .{ 1301 .border_style = .single, 1302 .label = choices[0].label, 1303 .bottom_label = choices[0].bottom_label, 1304 .read_only = true, 1305 .render_content = session.is_terminal, 1306 .visible_width = null, 1307 }); 1308 errdefer text_input.deinit(allocator); 1309 text_input.getFocus().mode = .all; 1310 try text_input.setContent(allocator, choices[0].text); 1311 box.getFocus().child_id = text_input.getFocus().id; 1312 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1313 1314 return .{ .box = box, .session = session, .choices = owned_choices }; 1315 } 1316 1317 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1318 const aa = session.page_arena.allocator(); 1319 var choices: [2]Choice = undefined; 1320 var count: usize = 0; 1321 switch (location) { 1322 .repo => |identity| { 1323 if (session.data.git_http_port) |port| { 1324 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1325 choices[count] = .{ 1326 .selector = "http", 1327 .text = url, 1328 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1329 .label = " clone ", 1330 }; 1331 count += 1; 1332 } 1333 if (session.data.git_ssh_port) |port| { 1334 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1335 choices[count] = .{ 1336 .selector = "ssh", 1337 .text = url, 1338 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1339 .label = " clone ", 1340 }; 1341 count += 1; 1342 } 1343 }, 1344 .fork => |f| if (session.data.git_ssh_port) |port| { 1345 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1346 choices[count] = .{ 1347 .text = url, 1348 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1349 .label = " clone ", 1350 }; 1351 count += 1; 1352 }, 1353 } 1354 if (count == 0) return null; 1355 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1356 } 1357 1358 pub fn minWidth(self: *const CopyableText) usize { 1359 var width = maxWidth(self.choices) + 2; 1360 if (self.choices.len > 1) { 1361 for (self.choices) |choice| width += choice.selector.len + 2; 1362 } 1363 return width; 1364 } 1365 1366 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1367 self.box.deinit(allocator); 1368 allocator.free(self.choices); 1369 } 1370 1371 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1372 self.clearGrid(); 1373 1374 if (self.choices.len > 1) { 1375 if (root_focus.grandchild_id) |id| { 1376 for (0..self.choices.len) |i| { 1377 if (id != self.selector(i).getFocus().id) continue; 1378 if (i != self.selected) try self.select(allocator, i); 1379 root_focus.setFocus(self.textInput().getFocus().id); 1380 break; 1381 } 1382 } 1383 1384 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1385 for (0..self.choices.len) |i| { 1386 self.selector(i).options.border_style = if (i == self.selected) 1387 (if (focused) .double else .single) 1388 else 1389 .hidden; 1390 } 1391 } 1392 self.textInput().options.border_style = .single; 1393 self.textInput().cursor = 0; 1394 self.textInput().scroll_offset = 0; 1395 try self.box.build(allocator, constraint, root_focus); 1396 1397 if (!self.session.is_terminal) { 1398 const aa = self.session.arena.allocator(); 1399 const id = self.textInput().getFocus().id; 1400 try self.session.text_inputs.put(aa, id, self.textInput()); 1401 } 1402 } 1403 1404 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1405 switch (key) { 1406 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1407 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1408 .enter => if (self.session.is_terminal) self.show(), 1409 .mouse => |mouse| { 1410 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1411 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1412 try self.select(allocator, i); 1413 root_focus.setFocus(self.textInput().getFocus().id); 1414 return; 1415 }; 1416 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1417 }, 1418 else => {}, 1419 } 1420 } 1421 1422 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1423 self.selected = selected; 1424 const choice = self.choices[selected]; 1425 const text_input = self.textInput(); 1426 text_input.options.label = choice.label; 1427 text_input.options.bottom_label = choice.bottom_label; 1428 try text_input.setContent(allocator, choice.text); 1429 } 1430 1431 fn show(self: *CopyableText) void { 1432 const choice = self.choices[self.selected]; 1433 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1434 } 1435 1436 fn textInput(self: *CopyableText) *wgt.TextInput { 1437 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1438 } 1439 1440 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1441 return &self.box.children.values()[index].widget.text_box; 1442 } 1443 1444 fn maxWidth(choices: []const Choice) usize { 1445 var width: usize = 0; 1446 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1447 return width; 1448 } 1449 1450 pub fn clearGrid(self: *CopyableText) void { 1451 self.box.clearGrid(); 1452 } 1453 1454 pub fn getGrid(self: CopyableText) ?Grid { 1455 return self.box.getGrid(); 1456 } 1457 1458 pub fn getFocus(self: *CopyableText) *Focus { 1459 return self.box.getFocus(); 1460 } 1461 }; 1462