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