╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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