╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const ui = @import("../ui.zig"); 3 const xit = @import("xit"); 4 const xitui = xit.xitui; 5 const wgt = xitui.widget; 6 const layout = xitui.layout; 7 const Key = xitui.input.Key; 8 const Grid = xitui.grid.Grid; 9 const Focus = xitui.focus.Focus; 10 const inp = @import("input.zig"); 11 12 pub const thread = @import("widget/thread.zig"); 13 14 pub const Widget = union(enum) { 15 text: wgt.Text, 16 box: wgt.Box(Widget), 17 text_box: wgt.TextBox, 18 text_input: wgt.TextInput, 19 scroll: wgt.Scroll(Widget), 20 stack: wgt.Stack(Widget), 21 flow_box: FlowBox, 22 flow_box_scroll: FlowBox.Scroll, 23 word_flow: WordFlow, 24 rich_text: RichText, 25 spacer: Spacer, 26 center: Center, 27 section_label: SectionLabel, 28 submit_button: SubmitButton, 29 background: AnsiBackground, 30 home: ui.Home.View, 31 user: ui.User.View, 32 repo: ui.Repo.View, 33 fork: ui.Fork.View, 34 quit: ui.Quit.View, 35 unauthorized: ui.Unauthorized.View, 36 title: ui.Title.View, 37 sub_title: ui.SubTitle.View, 38 home_header: ui.Home.Header.View, 39 user_header: ui.User.Header.View, 40 user_repos: ui.User.ReposView, 41 repo_header: ui.Repo.Header.View, 42 fork_header: ui.Fork.Header.View, 43 search_box: SearchBox, 44 radio: Radio, 45 search_header: SearchHeader, 46 repo_issues_header: ui.Repo.Issues.Header, 47 repo_patches_header: ui.Repo.Patches.Header, 48 repo_discussions_header: ui.Repo.Discussions.Header, 49 repo_files: ui.Repo.Files.View, 50 markdown: ui.Repo.Markdown.View, 51 markdown_heading: ui.Repo.Markdown.Heading, 52 markdown_gutter: ui.Repo.Markdown.Gutter, 53 repo_commits: ui.Repo.Commits.View, 54 diff_view: ui.Repo.Diff.View, 55 repo_refs: ui.Repo.Refs.View, 56 repo_issues: ui.Repo.Issues.View, 57 repo_patches: ui.Repo.Patches.View, 58 repo_discussions: ui.Repo.Discussions.View, 59 repo_issue_detail: ui.Repo.Issues.Detail, 60 repo_patch_detail: ui.Repo.Patches.Detail, 61 repo_discussion_detail: ui.Repo.Discussions.Detail, 62 repo_events_header: ui.Repo.Events.Header.View, 63 repo_events: ui.Repo.Events.View, 64 repo_undo: ui.Repo.Undo.View, 65 repo_comment: ui.Repo.Comment.Item, 66 copyable_text: CopyableText, 67 patch_source: ui.Repo.Patches.Source, 68 home_about: ui.Home.About.View, 69 home_users: ui.Home.Users.View, 70 auth_tab: ui.Home.Header.AuthTab.View, 71 new_repo: ui.NewRepo.View, 72 home_auth: ui.Home.Auth.View, 73 auth_login: ui.Home.Auth.Login.View, 74 auth_logout: ui.Home.Auth.Logout.View, 75 footer: Footer, 76 77 pub fn deinit(self: *Widget, allocator: std.mem.Allocator) void { 78 switch (self.*) { 79 inline else => |*case| case.deinit(allocator), 80 } 81 } 82 83 pub fn build(self: *Widget, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) anyerror!void { 84 switch (self.*) { 85 inline else => |*case| try case.build(allocator, constraint, root_focus), 86 } 87 } 88 89 pub fn input(self: *Widget, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) anyerror!void { 90 switch (self.*) { 91 inline else => |*case| try case.input(allocator, key, root_focus), 92 } 93 } 94 95 pub fn clearGrid(self: *Widget) void { 96 switch (self.*) { 97 inline else => |*case| case.clearGrid(), 98 } 99 } 100 101 pub fn getGrid(self: Widget) ?Grid { 102 switch (self) { 103 inline else => |*case| return case.getGrid(), 104 } 105 } 106 107 pub fn getFocus(self: *Widget) *Focus { 108 switch (self.*) { 109 inline else => |*case| return case.getFocus(), 110 } 111 } 112 113 // whether moving up should return to the page header 114 pub fn atTop(self: *Widget, root_focus: *Focus) bool { 115 return switch (self.*) { 116 inline .home_about, .home_users, .user_repos, .flow_box_scroll, .new_repo, .auth_login, .auth_logout, .quit, .repo_files, .repo_commits, .diff_view, .repo_refs, .repo_issues, .repo_patches, .repo_discussions, .repo_events, .repo_undo => |*view| view.atTop(), 117 inline .home_auth, .repo_patch_detail => |*view| view.atTop(root_focus), 118 else => false, 119 }; 120 } 121 }; 122 123 pub const back_button_width = 3; 124 const back_button_height = 3; 125 const back_kind = "back"; 126 127 // draw a text box as the selected one of its group: bordered and inverted, 128 // while the rest stay borderless 129 pub fn markSelected(text_box: *wgt.TextBox, selected: bool) void { 130 text_box.options.border_style = if (selected) .single else .hidden; 131 text_box.options.invert = selected; 132 } 133 134 pub fn addBackButton(allocator: std.mem.Allocator, box: *wgt.Box(Widget), session: *ui.Session) !void { 135 if (!session.is_terminal) return; 136 var back = try wgt.TextBox.init(allocator, "←", .{ .border_style = .hidden, .wrap_kind = .none }); 137 errdefer back.deinit(allocator); 138 back.getFocus().kind = .{ .custom = back_kind }; 139 back.getFocus().mode = .mouse; 140 try box.children.put(allocator, back.getFocus().id, .{ 141 .widget = .{ .text_box = back }, 142 .rect = null, 143 .min_size = .{ .width = back_button_width, .height = back_button_height }, 144 .max_size = .{ .width = back_button_width, .height = back_button_height }, 145 .hidden = session.back != .available, 146 }); 147 } 148 149 pub fn setBackButtonVisible(box: *wgt.Box(Widget), visible: bool) void { 150 for (box.children.values()) |*child| { 151 if (isBackButton(child.widget.getFocus())) { 152 child.hidden = !visible; 153 continue; 154 } 155 switch (child.widget) { 156 .box => |*nested| setBackButtonVisible(nested, visible), 157 else => {}, 158 } 159 } 160 } 161 162 pub fn isBackButton(focus: *Focus) bool { 163 return switch (focus.kind) { 164 .custom => |kind| std.mem.eql(u8, kind, back_kind), 165 else => false, 166 }; 167 } 168 169 // move the focused row in `box` by `delta`, clamped to the ends, scrolling 170 // `scroll` to keep it visible. 171 pub fn moveRowFocus(box: *wgt.Box(Widget), scroll: *wgt.Scroll(Widget), root_focus: *Focus, delta: isize) void { 172 const keys = box.children.keys(); 173 if (keys.len == 0) return; 174 const cur_id = box.getFocus().child_id orelse return; 175 const cur: isize = @intCast(box.children.getIndex(cur_id) orelse return); 176 const last: isize = @intCast(keys.len - 1); 177 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 178 if (next == @as(usize, @intCast(cur))) return; 179 root_focus.setFocus(keys[next]); 180 if (box.children.values()[next].rect) |rect| scroll.scrollToRect(rect); 181 } 182 183 // focus the box beside the selected one in `row`, false when there is none that way. 184 pub fn moveInRow(row: *wgt.Box(Widget), root_focus: *Focus, right: bool) bool { 185 const selected = row.getFocus().child_id orelse return false; 186 const index = row.children.getIndex(selected) orelse return false; 187 const target = if (right) index + 1 else index -| 1; 188 if (target == index or target >= row.children.count()) return false; 189 root_focus.setFocus(row.children.keys()[target]); 190 return true; 191 } 192 193 pub const FlowBox = struct { 194 focus: *Focus, 195 grid: ?Grid, 196 text_boxes: std.ArrayList(wgt.TextBox), 197 // backs each link item's `.custom` focus kind (e.g. "a:/user/foo"). reset 198 // wholesale on each setItems so there's no per-string ownership to track. 199 arena: std.heap.ArenaAllocator, 200 // column count from the last build — FlowBox.Scroll.input uses it so arrow 201 // up/down can step by a row's worth of items. 202 last_cols: usize, 203 options: Options, 204 205 const border_rows: usize = 2; 206 207 pub const Options = struct { 208 cell_width: usize = 40, 209 cell_height: usize = 3, 210 }; 211 212 pub fn init(allocator: std.mem.Allocator, options: Options) !FlowBox { 213 return .{ 214 .focus = try Focus.create(allocator, .container), 215 .grid = null, 216 .text_boxes = .empty, 217 .arena = std.heap.ArenaAllocator.init(allocator), 218 .last_cols = 1, 219 .options = options, 220 }; 221 } 222 223 pub fn deinit(self: *FlowBox, allocator: std.mem.Allocator) void { 224 self.focus.destroy(allocator); 225 if (self.grid) |*grid| { 226 grid.deinit(); 227 self.grid = null; 228 } 229 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 230 self.text_boxes.deinit(allocator); 231 self.arena.deinit(); 232 } 233 234 // one flow item: its display text plus an optional link and bottom label. a 235 // non-empty `link` sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, 236 // which the web renderer turns into an anchor. 237 pub const Item = struct { text: []const u8, link: []const u8 = "", bottom_label: []const u8 = "" }; 238 239 // the text box copies the item text; links and bottom labels are copied into 240 // the arena, so the caller's slices needn't outlive this call. 241 pub fn setItems(self: *FlowBox, allocator: std.mem.Allocator, items: []const Item) !void { 242 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 243 self.text_boxes.clearAndFree(allocator); 244 245 // the old text boxes and their borrowed links are gone now, so the link 246 // arena can be reclaimed. 247 _ = self.arena.reset(.retain_capacity); 248 const aa = self.arena.allocator(); 249 250 self.focus.clear(); 251 self.focus.child_id = null; 252 253 for (items) |item| { 254 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .word, .bottom_label = try aa.dupe(u8, item.bottom_label) }); 255 errdefer text_box.deinit(allocator); 256 text_box.getFocus().mode = .all; 257 258 if (item.link.len > 0) { 259 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 260 } 261 262 try self.text_boxes.append(allocator, text_box); 263 } 264 265 if (self.text_boxes.items.len > 0) { 266 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 267 } 268 } 269 270 pub fn build(self: *FlowBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 271 self.clearGrid(); 272 self.focus.clear(); 273 274 const cell_width = self.options.cell_width; 275 const max_width = constraint.max_size.width orelse cell_width; 276 const cols = if (cell_width == 0) 1 else @max(1, max_width / cell_width); 277 self.last_cols = cols; 278 279 const count = self.text_boxes.items.len; 280 if (count == 0) return; 281 const slot_height = self.options.cell_height + border_rows; 282 if (slot_height == 0) return; 283 284 // build at the slot size so every text box fits a single grid cell 285 for (self.text_boxes.items) |*tb| { 286 tb.options.border_style = if (root_focus.grandchild_id == tb.getFocus().id) .double else .hidden; 287 try tb.build(allocator, .{ 288 .min_size = .{ .width = cell_width, .height = null }, 289 .max_size = .{ .width = cell_width, .height = slot_height }, 290 }, root_focus); 291 } 292 293 const rows = (count + cols - 1) / cols; 294 const content_height = rows * slot_height; 295 const total_width = cols * cell_width; 296 if (total_width == 0 or content_height == 0) return; 297 298 var grid = try Grid.init(allocator, .{ .width = total_width, .height = content_height }); 299 errdefer grid.deinit(); 300 301 for (self.text_boxes.items, 0..) |*tb, i| { 302 const tb_grid = tb.getGrid() orelse continue; 303 const col = i % cols; 304 const row = i / cols; 305 const cell_x = col * cell_width; 306 const cell_y = row * slot_height; 307 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, cell_x, cell_y); 308 try grid.drawGrid(tb_grid, cell_x, cell_y); 309 } 310 311 self.grid = grid; 312 } 313 314 pub fn input(self: *FlowBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 315 _ = self; 316 _ = allocator; 317 _ = key; 318 _ = root_focus; 319 } 320 321 pub fn clearGrid(self: *FlowBox) void { 322 if (self.grid) |*grid| { 323 grid.deinit(); 324 self.grid = null; 325 } 326 for (self.text_boxes.items) |*tb| tb.clearGrid(); 327 } 328 329 pub fn getGrid(self: FlowBox) ?Grid { 330 return self.grid; 331 } 332 333 pub fn getFocus(self: *FlowBox) *Focus { 334 return self.focus; 335 } 336 337 pub fn cellRect(self: FlowBox, index: usize) ?layout.IRect { 338 if (self.last_cols == 0 or index >= self.text_boxes.items.len) return null; 339 const slot_height = self.options.cell_height + border_rows; 340 const col = index % self.last_cols; 341 const row = index / self.last_cols; 342 return .{ 343 .x = @intCast(col * self.options.cell_width), 344 .y = @intCast(row * slot_height), 345 .size = .{ .width = self.options.cell_width, .height = slot_height }, 346 }; 347 } 348 349 pub fn indexOfFocusId(self: FlowBox, focus_id: usize) ?usize { 350 for (self.text_boxes.items, 0..) |tb, i| { 351 if (tb.focus.id == focus_id) return i; 352 } 353 return null; 354 } 355 356 pub const Scroll = struct { 357 scroll: wgt.Scroll(Widget), 358 359 pub fn init(allocator: std.mem.Allocator, options: FlowBox.Options, web_native: bool) !Scroll { 360 var layout_inner = try FlowBox.init(allocator, options); 361 errdefer layout_inner.deinit(allocator); 362 var scroll = try wgt.Scroll(Widget).init(allocator, .{ .flow_box = layout_inner }, .{ .direction = .vert, .web_native = web_native }); 363 errdefer scroll.deinit(allocator); 364 return .{ .scroll = scroll }; 365 } 366 367 pub fn deinit(self: *Scroll, allocator: std.mem.Allocator) void { 368 self.scroll.deinit(allocator); 369 } 370 371 pub fn setItems(self: *Scroll, allocator: std.mem.Allocator, items: []const Item) !void { 372 self.scroll.x = 0; 373 self.scroll.y = 0; 374 try self.inner().setItems(allocator, items); 375 } 376 377 pub fn build(self: *Scroll, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 378 try self.scroll.build(allocator, constraint, root_focus); 379 } 380 381 pub fn input(self: *Scroll, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 382 _ = allocator; 383 const in = self.inner(); 384 const child_id = in.focus.child_id orelse return; 385 const current_index = in.indexOfFocusId(child_id) orelse return; 386 const count = in.text_boxes.items.len; 387 if (count == 0) return; 388 const cols = in.last_cols; 389 const slot_height = in.options.cell_height + FlowBox.border_rows; 390 391 var index = current_index; 392 switch (key) { 393 .arrow_up => index -|= cols, 394 .arrow_down => if (index + cols < count) { 395 index += cols; 396 }, 397 .arrow_left => index -|= 1, 398 .arrow_right => if (index + 1 < count) { 399 index += 1; 400 }, 401 .home => index = 0, 402 .end => index = count - 1, 403 .page_up => { 404 if (self.scroll.grid) |grid| if (slot_height > 0) { 405 const rows_per_page = grid.size.height / slot_height; 406 index -|= rows_per_page * cols; 407 }; 408 }, 409 .page_down => { 410 if (self.scroll.grid) |grid| if (slot_height > 0) { 411 const rows_per_page = grid.size.height / slot_height; 412 index = @min(index + rows_per_page * cols, count - 1); 413 }; 414 }, 415 // scroll wheel moves the focused cell by a full row so the 416 // viewport (via scrollToRect) follows in row-sized steps, 417 // matching how a scroll wheel feels in a grid view. 418 .mouse => |mouse| switch (mouse.action) { 419 .scroll => |dir| switch (dir) { 420 .up => index -|= cols, 421 .down => if (index + cols < count) { 422 index += cols; 423 }, 424 }, 425 else => {}, 426 }, 427 else => {}, 428 } 429 430 if (index != current_index) { 431 root_focus.setFocus(in.text_boxes.items[index].getFocus().id); 432 if (in.cellRect(index)) |rect| { 433 self.scroll.scrollToRect(rect); 434 } 435 } 436 } 437 438 pub fn clearGrid(self: *Scroll) void { 439 self.scroll.clearGrid(); 440 } 441 442 pub fn getGrid(self: Scroll) ?Grid { 443 return self.scroll.getGrid(); 444 } 445 446 pub fn getFocus(self: *Scroll) *Focus { 447 return self.scroll.getFocus(); 448 } 449 450 pub fn isEmpty(self: Scroll) bool { 451 return self.scroll.child.flow_box.text_boxes.items.len == 0; 452 } 453 454 pub fn atTop(self: Scroll) bool { 455 const in = self.scroll.child.flow_box; 456 const child_id = in.focus.child_id orelse return false; 457 return in.indexOfFocusId(child_id) == 0; 458 } 459 460 fn inner(self: *Scroll) *FlowBox { 461 return &self.scroll.child.flow_box; 462 } 463 }; 464 }; 465 466 // a left-to-right flow of variable-width focusable items that wraps at the 467 // available width, like words in a paragraph. unlike FlowBox there is no cell 468 // grid: each item is as wide as its text. selection movement is driven by the 469 // owning view via indexOfFocusId/verticalNeighbor. 470 pub const WordFlow = struct { 471 focus: *Focus, 472 grid: ?Grid, 473 text_boxes: std.ArrayList(wgt.TextBox), 474 // last build's item rects (content space), for vertical navigation and 475 // scroll-into-view. 476 rects: std.ArrayList(layout.IRect), 477 // backs each link item's `.custom` focus kind. 478 arena: std.heap.ArenaAllocator, 479 480 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 481 482 pub fn init(allocator: std.mem.Allocator) !WordFlow { 483 return .{ 484 .focus = try Focus.create(allocator, .container), 485 .grid = null, 486 .text_boxes = .empty, 487 .rects = .empty, 488 .arena = std.heap.ArenaAllocator.init(allocator), 489 }; 490 } 491 492 pub fn deinit(self: *WordFlow, allocator: std.mem.Allocator) void { 493 self.focus.destroy(allocator); 494 if (self.grid) |*grid| { 495 grid.deinit(); 496 self.grid = null; 497 } 498 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 499 self.text_boxes.deinit(allocator); 500 self.rects.deinit(allocator); 501 self.arena.deinit(); 502 } 503 504 // the text box copies the item text; links are copied into the arena, so the 505 // caller's slices needn't outlive this call. 506 pub fn setItems(self: *WordFlow, allocator: std.mem.Allocator, items: []const Item) !void { 507 for (self.text_boxes.items) |*tb| tb.deinit(allocator); 508 self.text_boxes.clearAndFree(allocator); 509 510 _ = self.arena.reset(.retain_capacity); 511 const aa = self.arena.allocator(); 512 513 self.focus.clear(); 514 self.focus.child_id = null; 515 516 for (items) |item| { 517 var text_box = try wgt.TextBox.init(allocator, item.text, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 518 errdefer text_box.deinit(allocator); 519 text_box.getFocus().mode = .all; 520 521 if (item.link.len > 0) { 522 text_box.getFocus().kind = .{ .custom = try aa.dupe(u8, item.link) }; 523 } 524 525 try self.text_boxes.append(allocator, text_box); 526 } 527 528 if (self.text_boxes.items.len > 0) { 529 self.focus.child_id = self.text_boxes.items[0].getFocus().id; 530 } 531 } 532 533 pub fn build(self: *WordFlow, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 534 self.clearGrid(); 535 self.focus.clear(); 536 537 if (self.text_boxes.items.len == 0) return; 538 const max_width = constraint.max_size.width orelse 80; 539 if (max_width == 0) return; 540 541 // lay the items out left to right, wrapping to a new row when the next 542 // one wouldn't fit. 543 self.rects.clearRetainingCapacity(); 544 var x: usize = 0; 545 var y: usize = 0; 546 var row_height: usize = 0; 547 var total_width: usize = 0; 548 for (self.text_boxes.items) |*tb| { 549 try tb.build(allocator, .{ 550 .min_size = .{ .width = null, .height = null }, 551 .max_size = .{ .width = max_width, .height = null }, 552 }, root_focus); 553 const tb_grid_maybe = tb.getGrid(); 554 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 555 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 556 if (x > 0 and x + w > max_width) { 557 x = 0; 558 y += row_height; 559 row_height = 0; 560 } 561 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 562 if (h > row_height) row_height = h; 563 x += w; 564 if (x > total_width) total_width = x; 565 } 566 567 const total_height = y + row_height; 568 if (total_width == 0 or total_height == 0) return; 569 570 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 571 errdefer grid.deinit(); 572 573 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 574 const tb_grid = tb.getGrid() orelse continue; 575 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 576 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 577 } 578 579 self.grid = grid; 580 } 581 582 pub fn input(self: *WordFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 583 _ = self; 584 _ = allocator; 585 _ = key; 586 _ = root_focus; 587 } 588 589 pub fn clearGrid(self: *WordFlow) void { 590 if (self.grid) |*grid| { 591 grid.deinit(); 592 self.grid = null; 593 } 594 for (self.text_boxes.items) |*tb| tb.clearGrid(); 595 } 596 597 pub fn getGrid(self: WordFlow) ?Grid { 598 return self.grid; 599 } 600 601 pub fn getFocus(self: *WordFlow) *Focus { 602 return self.focus; 603 } 604 605 pub fn indexOfFocusId(self: WordFlow, focus_id: usize) ?usize { 606 for (self.text_boxes.items, 0..) |tb, i| { 607 if (tb.focus.id == focus_id) return i; 608 } 609 return null; 610 } 611 612 // the first item of the row below (when `downward`) or above, or null at 613 // the flow's edge. items are laid out in order, so y is nondecreasing. 614 pub fn rowStep(self: WordFlow, index: usize, downward: bool) ?usize { 615 const rects = self.rects.items; 616 if (index >= rects.len) return null; 617 const cur_y = rects[index].y; 618 if (downward) { 619 for (rects[index + 1 ..], index + 1..) |r, i| { 620 if (r.y > cur_y) return i; 621 } 622 return null; 623 } 624 // walk the earlier rows; the last one visited is the row above. 625 var prev_first: ?usize = null; 626 var i: usize = 0; 627 while (i < rects.len and rects[i].y < cur_y) { 628 prev_first = i; 629 const y = rects[i].y; 630 while (i < rects.len and rects[i].y == y) i += 1; 631 } 632 return prev_first; 633 } 634 }; 635 636 // a word-wrapped paragraph of styled runs. each line a link touches is a 637 // focusable piece, and a focused link draws all its pieces inverted. 638 pub const RichText = struct { 639 focus: *Focus, 640 grid: ?Grid, 641 // backs the decoded content and the link targets 642 arena: std.heap.ArenaAllocator, 643 runes: []const u21, 644 // each rune's span 645 span_of: []const usize, 646 styles: []const Grid.Style, 647 // each span's link index, null outside a link 648 span_links: []const ?usize, 649 links: []const []const u8, 650 // each link's piece focuses in line order, kept across builds so a link 651 // keeps its ids when the text rewraps 652 link_focuses: []std.ArrayList(*Focus), 653 lines: std.ArrayList(wgt.Line), 654 // scratch for each build's link pieces 655 pieces: std.ArrayList(Piece), 656 657 pub const Span = struct { 658 text: []const u8, 659 style: Grid.Style = .{}, 660 // the focus kind a click or enter follows; empty for plain text 661 link: []const u8 = "", 662 }; 663 664 const Piece = struct { 665 link: usize, 666 rect: layout.URect, 667 }; 668 669 pub fn init(allocator: std.mem.Allocator, spans: []const Span) !RichText { 670 var arena = std.heap.ArenaAllocator.init(allocator); 671 errdefer arena.deinit(); 672 const aa = arena.allocator(); 673 674 var runes: std.ArrayList(u21) = .empty; 675 var span_of: std.ArrayList(usize) = .empty; 676 const styles = try aa.alloc(Grid.Style, spans.len); 677 const span_links = try aa.alloc(?usize, spans.len); 678 var links: std.ArrayList([]const u8) = .empty; 679 for (spans, 0..) |span, i| { 680 styles[i] = span.style; 681 span_links[i] = null; 682 if (span.link.len > 0) { 683 // adjacent spans with the same target are one link 684 const same = i > 0 and spans[i - 1].link.len > 0 and std.mem.eql(u8, spans[i - 1].link, span.link); 685 if (!same) try links.append(aa, try aa.dupe(u8, span.link)); 686 span_links[i] = links.items.len - 1; 687 } 688 var utf8 = (try std.unicode.Utf8View.init(span.text)).iterator(); 689 while (utf8.nextCodepoint()) |codepoint| { 690 try runes.append(aa, codepoint); 691 try span_of.append(aa, i); 692 } 693 } 694 695 const link_focuses = try aa.alloc(std.ArrayList(*Focus), links.items.len); 696 @memset(link_focuses, .empty); 697 698 return .{ 699 .focus = try Focus.create(allocator, .container), 700 .grid = null, 701 .arena = arena, 702 .runes = runes.items, 703 .span_of = span_of.items, 704 .styles = styles, 705 .span_links = span_links, 706 .links = links.items, 707 .link_focuses = link_focuses, 708 .lines = .empty, 709 .pieces = .empty, 710 }; 711 } 712 713 pub fn deinit(self: *RichText, allocator: std.mem.Allocator) void { 714 self.focus.destroy(allocator); 715 self.clearGrid(); 716 for (self.link_focuses) |*focuses| { 717 for (focuses.items) |focus| focus.destroy(allocator); 718 focuses.deinit(allocator); 719 } 720 self.pieces.deinit(allocator); 721 self.lines.deinit(allocator); 722 self.arena.deinit(); 723 } 724 725 // the url a terminal can open for a link: raw links to absolute urls 726 fn terminalLink(link: []const u8) ?[]const u8 { 727 if (!std.mem.startsWith(u8, link, ui.raw_link_prefix)) return null; 728 const url = link[ui.raw_link_prefix.len..]; 729 return if (std.mem.startsWith(u8, url, "/")) null else url; 730 } 731 732 // the link one of whose pieces has focus id `id` 733 fn linkOf(self: *const RichText, id: ?usize) ?usize { 734 for (self.link_focuses, 0..) |focuses, l| { 735 for (focuses.items) |focus| if (focus.id == id) return l; 736 } 737 return null; 738 } 739 740 pub fn build(self: *RichText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 741 self.clearGrid(); 742 self.focus.clear(); 743 const max_width = constraint.max_size.width; 744 if (max_width == 0 or constraint.max_size.height == 0) return; 745 const focused_link = self.linkOf(root_focus.grandchild_id); 746 747 try wgt.wrapLines(allocator, &self.lines, self.runes, .word, max_width); 748 // trailing spaces are dropped so a wrapped link doesn't highlight them 749 for (self.lines.items) |*line| { 750 while (line.end > line.start and self.runes[line.end - 1] == ' ') { 751 line.end -= 1; 752 line.width -= 1; 753 } 754 } 755 const visible = @min(self.lines.items.len, constraint.max_size.height orelse self.lines.items.len); 756 var width: usize = 1; 757 for (self.lines.items[0..visible]) |line| width = @max(width, line.width); 758 if (max_width) |w| width = @min(width, w); 759 760 var grid = try Grid.init(allocator, .{ .width = width, .height = visible }); 761 errdefer grid.deinit(); 762 self.pieces.clearRetainingCapacity(); 763 for (self.lines.items[0..visible], 0..) |line, y| { 764 var x: usize = 0; 765 for (self.runes[line.start..line.end], line.start..) |rune, i| { 766 const rune_width = xitui.width.cellWidth(rune); 767 if (x + rune_width > width) break; 768 const link = self.span_links[self.span_of[i]]; 769 const cell = try grid.cell(x, y); 770 cell.style = self.styles[self.span_of[i]]; 771 if (link) |l| { 772 cell.link = terminalLink(self.links[l]); 773 if (l == focused_link) cell.style.inverted = true; 774 // extend this line's piece of the link, or start one 775 const items = self.pieces.items; 776 if (items.len > 0 and items[items.len - 1].link == l and items[items.len - 1].rect.y == y) { 777 items[items.len - 1].rect.size.width += rune_width; 778 } else { 779 try self.pieces.append(allocator, .{ .link = l, .rect = .{ .x = x, .y = y, .size = .{ .width = rune_width, .height = 1 } } }); 780 } 781 } 782 try grid.setRune(x, y, rune); 783 x += rune_width; 784 } 785 } 786 787 // a link's pieces are consecutive, since its runes are 788 var piece_index: usize = 0; 789 for (self.pieces.items, 0..) |piece, i| { 790 piece_index = if (i > 0 and self.pieces.items[i - 1].link == piece.link) piece_index + 1 else 0; 791 const focuses = &self.link_focuses[piece.link]; 792 if (piece_index == focuses.items.len) { 793 const focus = try Focus.create(allocator, .{ .custom = self.links[piece.link] }); 794 errdefer focus.destroy(allocator); 795 focus.mode = .all; 796 try focuses.append(allocator, focus); 797 } 798 try self.focus.addChild(allocator, focuses.items[piece_index], piece.rect.size, piece.rect.x, piece.rect.y); 799 } 800 801 // focus rests on a link's first piece, which stays while the link shows 802 if (focused_link) |l| if (self.link_focuses[l].items.len > 0) { 803 const first = self.link_focuses[l].items[0].id; 804 if (self.focus.children.contains(first)) { 805 root_focus.grandchild_id = first; 806 self.focus.child_id = first; 807 } 808 }; 809 810 self.grid = grid; 811 } 812 813 pub fn input(self: *RichText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 814 _ = self; 815 _ = allocator; 816 _ = key; 817 _ = root_focus; 818 } 819 820 pub fn clearGrid(self: *RichText) void { 821 if (self.grid) |*grid| { 822 grid.deinit(); 823 self.grid = null; 824 } 825 } 826 827 pub fn getGrid(self: RichText) ?Grid { 828 return self.grid; 829 } 830 831 pub fn getFocus(self: *RichText) *Focus { 832 return self.focus; 833 } 834 }; 835 836 // an invisible widget that fills the horizontal space granted by its parent. 837 // used inside Box(horiz) with a min_size so the box reserves space for the 838 // children that follow, pushing them to the right. 839 pub const Spacer = struct { 840 focus: *Focus, 841 grid: ?Grid, 842 843 pub fn init(allocator: std.mem.Allocator) !Spacer { 844 return .{ 845 .focus = try Focus.create(allocator, .container), 846 .grid = null, 847 }; 848 } 849 850 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 851 self.focus.destroy(allocator); 852 if (self.grid) |*grid| { 853 grid.deinit(); 854 self.grid = null; 855 } 856 } 857 858 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 859 _ = root_focus; 860 self.clearGrid(); 861 const width = constraint.max_size.width orelse return; 862 if (width == 0) return; 863 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 864 } 865 866 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 867 _ = self; 868 _ = allocator; 869 _ = key; 870 _ = root_focus; 871 } 872 873 pub fn clearGrid(self: *Spacer) void { 874 if (self.grid) |*grid| { 875 grid.deinit(); 876 self.grid = null; 877 } 878 } 879 880 pub fn getGrid(self: Spacer) ?Grid { 881 return self.grid; 882 } 883 884 pub fn getFocus(self: *Spacer) *Focus { 885 return self.focus; 886 } 887 }; 888 889 // a sub-header's search box: a labeled text input whose enter the owning view 890 // turns into a navigation. shared by the files, commits and thread tabs. 891 pub const SearchBox = struct { 892 text_input: wgt.TextInput, 893 session: *ui.Session, 894 895 // the room the box asks its row for 896 pub const min_size: layout.MaybeSize = .{ .width = 20, .height = 3 }; 897 898 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, label: []const u8, name: []const u8, text_value: ?[]const u8) !SearchBox { 899 var text_input = try wgt.TextInput.init(allocator, .{ .label = label, .name = name, .round_corners = true, .visible_width = 18, .render_content = session.is_terminal }); 900 errdefer text_input.deinit(allocator); 901 text_input.getFocus().mode = .all; 902 if (text_value) |value| try text_input.setContent(allocator, value); 903 return .{ .text_input = text_input, .session = session }; 904 } 905 906 pub fn deinit(self: *SearchBox, allocator: std.mem.Allocator) void { 907 self.text_input.deinit(allocator); 908 } 909 910 pub fn build(self: *SearchBox, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 911 self.text_input.options.bottom_label = if (root_focus.grandchild_id == self.getFocus().id) " press enter " else ""; 912 try self.text_input.build(allocator, constraint, root_focus); 913 if (!self.session.is_terminal) { 914 try self.session.text_inputs.put(self.session.arena.allocator(), self.getFocus().id, &self.text_input); 915 } 916 } 917 918 pub fn input(self: *SearchBox, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 919 try self.text_input.input(allocator, key, root_focus); 920 } 921 922 // the typed text; the caller owns it 923 pub fn text(self: *SearchBox, allocator: std.mem.Allocator) ![]u8 { 924 return self.text_input.text(allocator); 925 } 926 927 // whether the cursor sits past the last character, where right-arrow leaves 928 // the box rather than moving within it 929 pub fn atEnd(self: *SearchBox) bool { 930 return self.text_input.cursor == self.text_input.content.items.len; 931 } 932 933 pub fn clearGrid(self: *SearchBox) void { 934 self.text_input.clearGrid(); 935 } 936 937 pub fn getGrid(self: SearchBox) ?Grid { 938 return self.text_input.getGrid(); 939 } 940 941 pub fn getFocus(self: *SearchBox) *Focus { 942 return self.text_input.getFocus(); 943 } 944 }; 945 946 // a row of bordered options posted as one form field. the selection is the 947 // focused option, so a click selects on the web too. 948 pub const Radio = struct { 949 box: wgt.Box(Widget), 950 name: []const u8, 951 values: []const []const u8, 952 953 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8) !Radio { 954 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 955 errdefer box.deinit(allocator); 956 for (values) |value| { 957 var option = try wgt.TextBox.init(allocator, value, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 958 errdefer option.deinit(allocator); 959 option.getFocus().mode = .all; 960 option.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 961 if (std.mem.eql(u8, value, initial)) box.getFocus().child_id = option.getFocus().id; 962 try box.children.put(allocator, option.getFocus().id, .{ .widget = .{ .text_box = option }, .rect = null, .min_size = .{ .width = try xitui.width.displayWidth(value) + 2, .height = 3 } }); 963 } 964 return .{ .box = box, .name = name, .values = values }; 965 } 966 967 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 968 self.box.deinit(allocator); 969 } 970 971 // the focused option's value 972 pub fn selected(self: *Radio) []const u8 { 973 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse unreachable) orelse unreachable; 974 return self.values[index]; 975 } 976 977 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 978 self.clearGrid(); 979 const selected_id = self.box.getFocus().child_id; 980 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 981 child.widget.text_box.options.border_style = if (id == selected_id) .single else .hidden; 982 } 983 try self.box.build(allocator, constraint, root_focus); 984 } 985 986 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 987 _ = allocator; 988 const ids = self.box.children.keys(); 989 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse return) orelse return; 990 switch (key) { 991 .arrow_left => if (index > 0) root_focus.setFocus(ids[index - 1]), 992 .arrow_right => if (index + 1 < ids.len) root_focus.setFocus(ids[index + 1]), 993 else => {}, 994 } 995 } 996 997 pub fn clearGrid(self: *Radio) void { 998 self.box.clearGrid(); 999 } 1000 1001 pub fn getGrid(self: Radio) ?Grid { 1002 return self.box.getGrid(); 1003 } 1004 1005 pub fn getFocus(self: *Radio) *Focus { 1006 return self.box.getFocus(); 1007 } 1008 }; 1009 1010 // a tab's sub-header: an optional search box on the left, then a spacer, then 1011 // the clone url on the right. the owning view turns a submitted query into a 1012 // route, so route building stays with the tab that knows its own url. 1013 pub const SearchHeader = struct { 1014 box: wgt.Box(Widget), 1015 session: *ui.Session, 1016 1017 pub fn init( 1018 allocator: std.mem.Allocator, 1019 session: *ui.Session, 1020 location: ui.RoutablePage.RepoLocation, 1021 label: []const u8, 1022 name: []const u8, 1023 text: ?[]const u8, 1024 show_input: bool, 1025 ) !SearchHeader { 1026 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1027 errdefer box.deinit(allocator); 1028 1029 // the search box only appears where an index can serve it. 1030 if (show_input) { 1031 var search_box = try SearchBox.init(allocator, session, label, name, text); 1032 errdefer search_box.deinit(allocator); 1033 box.getFocus().child_id = search_box.getFocus().id; 1034 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1035 } 1036 1037 // the spacer pushes the clone url to the right edge. 1038 { 1039 var spacer = try Spacer.init(allocator); 1040 errdefer spacer.deinit(allocator); 1041 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1042 } 1043 1044 if (session.data.host_kind == .server) { 1045 if (try CopyableText.initClone(allocator, session, location)) |value| { 1046 var clone_url = value; 1047 errdefer clone_url.deinit(allocator); 1048 const min_width = clone_url.minWidth(); 1049 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1050 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 } }); 1051 } 1052 } 1053 1054 return .{ .box = box, .session = session }; 1055 } 1056 1057 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1058 self.box.deinit(allocator); 1059 } 1060 1061 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1062 self.clearGrid(); 1063 try self.box.build(allocator, constraint, root_focus); 1064 } 1065 1066 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1067 const current = self.box.getFocus().child_id orelse return; 1068 if (self.searchBox()) |search_box| { 1069 if (current == search_box.getFocus().id) { 1070 // tab, and right-arrow past the last character, step to the 1071 // clone url beside the box 1072 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1073 if (self.cloneUrl()) |clone_url| { 1074 root_focus.setFocus(clone_url.getFocus().id); 1075 return; 1076 } 1077 } 1078 return search_box.input(allocator, key, root_focus); 1079 } 1080 } 1081 const clone_url = self.cloneUrl() orelse return; 1082 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1083 if (self.searchBox()) |search_box| { 1084 root_focus.setFocus(search_box.getFocus().id); 1085 return; 1086 } 1087 } 1088 try clone_url.input(allocator, key, root_focus); 1089 } 1090 1091 // the typed query when enter should submit it: the box exists and holds 1092 // focus. the caller owns the returned text. 1093 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1094 const search_box = self.searchBox() orelse return null; 1095 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1096 return try search_box.text(allocator); 1097 } 1098 1099 // enter the header, preferring the search box. 1100 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1101 if (self.searchBox()) |search_box| { 1102 root_focus.setFocus(search_box.getFocus().id); 1103 return true; 1104 } 1105 const clone_url = self.cloneUrl() orelse return false; 1106 root_focus.setFocus(clone_url.getFocus().id); 1107 return true; 1108 } 1109 1110 pub fn hasFocusable(self: *SearchHeader) bool { 1111 return self.cloneUrl() != null or self.searchBox() != null; 1112 } 1113 1114 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1115 for (self.box.children.values()) |*child| switch (child.widget) { 1116 .copyable_text => |*copyable_text| return copyable_text, 1117 else => {}, 1118 }; 1119 return null; 1120 } 1121 1122 fn searchBox(self: *SearchHeader) ?*SearchBox { 1123 for (self.box.children.values()) |*child| switch (child.widget) { 1124 .search_box => |*search_box| return search_box, 1125 else => {}, 1126 }; 1127 return null; 1128 } 1129 1130 pub fn clearGrid(self: *SearchHeader) void { 1131 self.box.clearGrid(); 1132 } 1133 1134 pub fn getGrid(self: SearchHeader) ?Grid { 1135 return self.box.getGrid(); 1136 } 1137 1138 pub fn getFocus(self: *SearchHeader) *Focus { 1139 return self.box.getFocus(); 1140 } 1141 }; 1142 1143 // a one-row, non-focusable status bar showing the current page's url 1144 pub const Footer = struct { 1145 focus: *Focus, 1146 grid: ?Grid, 1147 session: *ui.Session, 1148 arena: std.heap.ArenaAllocator, 1149 url: []const u8 = "", 1150 url_width: usize = 0, 1151 1152 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1153 return .{ 1154 .focus = try Focus.create(allocator, .container), 1155 .grid = null, 1156 .session = session, 1157 .arena = std.heap.ArenaAllocator.init(allocator), 1158 }; 1159 } 1160 1161 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1162 self.focus.destroy(allocator); 1163 if (self.grid) |*grid| grid.deinit(); 1164 self.arena.deinit(); 1165 } 1166 1167 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1168 _ = root_focus; 1169 self.clearGrid(); 1170 const width = constraint.max_size.width orelse return; 1171 if (width == 0) return; 1172 1173 _ = self.arena.reset(.retain_capacity); 1174 self.url = ""; 1175 self.url_width = 0; 1176 const aa = self.arena.allocator(); 1177 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1178 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1179 self.url = text; 1180 1181 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1182 errdefer grid.deinit(); 1183 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1184 var i: usize = 0; 1185 while (utf8.nextCodepoint()) |ch| { 1186 const rune_width = xitui.width.cellWidth(ch); 1187 if (i + rune_width > width) break; 1188 // a terminal hyperlink to the page 1189 (try grid.cell(i, 0)).link = text; 1190 try grid.setRune(i, 0, ch); 1191 i += rune_width; 1192 } 1193 self.url_width = i; 1194 self.grid = grid; 1195 } 1196 1197 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1198 _ = allocator; 1199 switch (key) { 1200 .mouse => |mouse| { 1201 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1202 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1203 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1204 }, 1205 else => {}, 1206 } 1207 } 1208 1209 pub fn clearGrid(self: *Footer) void { 1210 if (self.grid) |*grid| { 1211 grid.deinit(); 1212 self.grid = null; 1213 } 1214 } 1215 1216 pub fn getGrid(self: Footer) ?Grid { 1217 return self.grid; 1218 } 1219 1220 pub fn getFocus(self: *Footer) *Focus { 1221 return self.focus; 1222 } 1223 }; 1224 1225 // a single-child wrapper that builds the child at its natural size and 1226 // positions its grid in the middle of the area granted by the parent 1227 // a bordered label, indented past the content it precedes 1228 pub const SectionLabel = struct { 1229 box: wgt.Box(Widget), 1230 1231 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1232 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1233 errdefer box.deinit(allocator); 1234 1235 { 1236 var gap = try wgt.Text.init(allocator, " "); 1237 errdefer gap.deinit(allocator); 1238 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1239 } 1240 1241 { 1242 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1243 errdefer tb.deinit(allocator); 1244 tb.getFocus().mode = .all; 1245 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1246 } 1247 1248 box.getFocus().child_id = box.children.keys()[1]; 1249 return .{ .box = box }; 1250 } 1251 1252 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1253 self.box.deinit(allocator); 1254 } 1255 1256 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1257 try self.box.build(allocator, constraint, root_focus); 1258 } 1259 1260 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1261 _ = self; 1262 _ = allocator; 1263 _ = key; 1264 _ = root_focus; 1265 } 1266 1267 pub fn clearGrid(self: *SectionLabel) void { 1268 self.box.clearGrid(); 1269 } 1270 1271 pub fn getGrid(self: SectionLabel) ?Grid { 1272 return self.box.getGrid(); 1273 } 1274 1275 pub fn getFocus(self: *SectionLabel) *Focus { 1276 return self.box.getFocus(); 1277 } 1278 }; 1279 1280 // a submit button, indented past its form's content. the web form POSTs 1281 // the button's form; terminal hosts detect presses through `buttonId`. 1282 pub const SubmitButton = struct { 1283 box: wgt.Box(Widget), 1284 1285 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1286 return initLabeled(allocator, "submit"); 1287 } 1288 1289 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1290 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1291 errdefer box.deinit(allocator); 1292 1293 { 1294 var gap = try wgt.Text.init(allocator, " "); 1295 errdefer gap.deinit(allocator); 1296 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1297 } 1298 1299 { 1300 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1301 errdefer button.deinit(allocator); 1302 button.getFocus().mode = .all; 1303 // the renderer distinguishes plain clickables from buttons that 1304 // should POST to a server route by this kind. 1305 button.getFocus().kind = .{ .custom = "submit" }; 1306 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1307 } 1308 1309 box.getFocus().child_id = box.children.keys()[1]; 1310 return .{ .box = box }; 1311 } 1312 1313 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1314 self.box.deinit(allocator); 1315 } 1316 1317 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1318 const button = &self.box.children.values()[1]; 1319 try button.widget.text_box.setContent(allocator, label); 1320 button.min_size = .{ .width = label.len + 2, .height = null }; 1321 } 1322 1323 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1324 try self.box.build(allocator, constraint, root_focus); 1325 } 1326 1327 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1328 _ = self; 1329 _ = allocator; 1330 _ = key; 1331 _ = root_focus; 1332 } 1333 1334 pub fn clearGrid(self: *SubmitButton) void { 1335 self.box.clearGrid(); 1336 } 1337 1338 pub fn getGrid(self: SubmitButton) ?Grid { 1339 return self.box.getGrid(); 1340 } 1341 1342 pub fn getFocus(self: *SubmitButton) *Focus { 1343 return self.box.getFocus(); 1344 } 1345 1346 // the button's focus id, for click hit-testing 1347 pub fn buttonId(self: *const SubmitButton) usize { 1348 return self.box.children.keys()[1]; 1349 } 1350 }; 1351 1352 pub const Center = struct { 1353 focus: *Focus, 1354 grid: ?Grid, 1355 child: *Widget, 1356 1357 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1358 const child = try allocator.create(Widget); 1359 errdefer allocator.destroy(child); 1360 child.* = child_widget; 1361 return .{ 1362 .focus = try Focus.create(allocator, .container), 1363 .grid = null, 1364 .child = child, 1365 }; 1366 } 1367 1368 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1369 self.focus.destroy(allocator); 1370 if (self.grid) |*grid| { 1371 grid.deinit(); 1372 self.grid = null; 1373 } 1374 self.child.deinit(allocator); 1375 allocator.destroy(self.child); 1376 } 1377 1378 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1379 self.clearGrid(); 1380 self.getFocus().clear(); 1381 1382 // build the child without forcing it to fill min_size; it sizes 1383 // itself to its content within the available max. 1384 try self.child.build(allocator, .{ 1385 .min_size = .{ .width = null, .height = null }, 1386 .max_size = constraint.max_size, 1387 }, root_focus); 1388 1389 const child_grid = self.child.getGrid() orelse return; 1390 1391 // prefer max when given; otherwise grow to max(min, child) so that 1392 // when only a min is set (e.g. the wasm path passes viewport rows 1393 // as min_height with no max), we can still vertically center while 1394 // letting taller content extend past the min. 1395 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; 1396 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; 1397 if (width == 0 or height == 0) return; 1398 1399 const offset_x: usize = (width -| child_grid.size.width) / 2; 1400 const offset_y: usize = (height -| child_grid.size.height) / 2; 1401 1402 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1403 errdefer grid.deinit(); 1404 try grid.drawGrid(child_grid, offset_x, offset_y); 1405 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1406 self.getFocus().child_id = self.child.getFocus().id; 1407 1408 self.grid = grid; 1409 } 1410 1411 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1412 try self.child.input(allocator, key, root_focus); 1413 } 1414 1415 pub fn clearGrid(self: *Center) void { 1416 if (self.grid) |*grid| { 1417 grid.deinit(); 1418 self.grid = null; 1419 } 1420 self.child.clearGrid(); 1421 } 1422 1423 pub fn getGrid(self: Center) ?Grid { 1424 return self.grid; 1425 } 1426 1427 pub fn getFocus(self: *Center) *Focus { 1428 return self.focus; 1429 } 1430 }; 1431 1432 // renders truecolor ANSI art 1433 pub const AnsiArt = struct { 1434 focus: *Focus, 1435 grid: ?Grid, 1436 content: []const u8, 1437 1438 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1439 return .{ 1440 .focus = try Focus.create(allocator, .container), 1441 .grid = null, 1442 .content = content, 1443 }; 1444 } 1445 1446 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1447 self.focus.destroy(allocator); 1448 if (self.grid) |*grid| { 1449 grid.deinit(); 1450 self.grid = null; 1451 } 1452 } 1453 1454 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1455 _ = root_focus; 1456 self.clearGrid(); 1457 1458 // parse into rows of cells, then copy into a rectangular grid 1459 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1460 defer { 1461 for (rows.items) |*row| row.deinit(allocator); 1462 rows.deinit(allocator); 1463 } 1464 var row: std.ArrayList(Grid.Cell) = .empty; 1465 errdefer row.deinit(allocator); 1466 1467 var style: Grid.Style = .{}; 1468 var width: usize = 0; 1469 const content = self.content; 1470 var i: usize = 0; 1471 while (i < content.len) { 1472 const byte = content[i]; 1473 if (byte == '\n') { 1474 width = @max(width, row.items.len); 1475 try rows.append(allocator, row); 1476 row = .empty; 1477 i += 1; 1478 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1479 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1480 var j = i + 2; 1481 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1482 if (j >= content.len) { 1483 i = content.len; // malformed trailing escape; stop 1484 } else { 1485 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1486 i = j + 1; 1487 } 1488 } else { 1489 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1490 const end = @min(content.len, i + len); 1491 const rune = content[i..end]; 1492 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1493 // a foreground color does not make a space visible. only an 1494 // effective background makes it opaque; foreground state often 1495 // remains active through the padding at the end of a row. 1496 const effective_background = if (style.inverted) style.fg else style.bg; 1497 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1498 try row.append(allocator, .{ 1499 .rune = if (transparent) null else codepoint, 1500 .style = style, 1501 }); 1502 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1503 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1504 } 1505 i = end; 1506 } 1507 } 1508 // a trailing row with content but no closing newline 1509 if (row.items.len > 0) { 1510 width = @max(width, row.items.len); 1511 try rows.append(allocator, row); 1512 } else { 1513 row.deinit(allocator); 1514 } 1515 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1516 1517 const height = rows.items.len; 1518 if (width == 0 or height == 0) return; 1519 1520 const clamped_w = @min(width, constraint.max_size.width orelse width); 1521 const clamped_h = @min(height, constraint.max_size.height orelse height); 1522 1523 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1524 errdefer grid.deinit(); 1525 for (rows.items[0..clamped_h], 0..) |r, y| { 1526 const n = @min(r.items.len, clamped_w); 1527 for (r.items[0..n], 0..) |cell, x| { 1528 if (cell.continuation) continue; 1529 (try grid.cell(x, y)).style = cell.style; 1530 try grid.setRune(x, y, cell.rune); 1531 } 1532 } 1533 self.grid = grid; 1534 } 1535 1536 pub fn clearGrid(self: *AnsiArt) void { 1537 if (self.grid) |*grid| { 1538 grid.deinit(); 1539 self.grid = null; 1540 } 1541 } 1542 1543 pub fn getGrid(self: AnsiArt) ?Grid { 1544 return self.grid; 1545 } 1546 1547 pub fn getFocus(self: *AnsiArt) *Focus { 1548 return self.focus; 1549 } 1550 1551 fn applySgr(style: *Grid.Style, params: []const u8) void { 1552 var nums: [16]u32 = undefined; 1553 var n: usize = 0; 1554 var it = std.mem.splitScalar(u8, params, ';'); 1555 while (it.next()) |tok| { 1556 if (n >= nums.len) break; 1557 // an empty parameter (e.g. bare "\x1b[m") means 0 1558 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1559 n += 1; 1560 } 1561 if (n == 0) { 1562 style.* = .{}; 1563 return; 1564 } 1565 var i: usize = 0; 1566 while (i < n) : (i += 1) { 1567 switch (nums[i]) { 1568 0 => style.* = .{}, 1569 7 => style.inverted = true, 1570 27 => style.inverted = false, 1571 39 => style.fg = null, 1572 49 => style.bg = null, 1573 38, 48 => { 1574 // truecolor form: 38;2;r;g;b — anything else is ignored 1575 if (i + 4 < n and nums[i + 1] == 2) { 1576 const c = Grid.Color{ .rgb = .{ 1577 .r = @truncate(nums[i + 2]), 1578 .g = @truncate(nums[i + 3]), 1579 .b = @truncate(nums[i + 4]), 1580 } }; 1581 if (nums[i] == 38) style.fg = c else style.bg = c; 1582 i += 4; 1583 } 1584 }, 1585 else => {}, 1586 } 1587 } 1588 } 1589 }; 1590 1591 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1592 pub const AnsiBackground = struct { 1593 grid: ?Grid, 1594 child: *Widget, 1595 art: AnsiArt, 1596 session: *ui.Session, 1597 1598 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1599 var cw = child_widget; 1600 const child = allocator.create(Widget) catch |e| { 1601 cw.deinit(allocator); 1602 return e; 1603 }; 1604 child.* = cw; 1605 errdefer { 1606 child.deinit(allocator); 1607 allocator.destroy(child); 1608 } 1609 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1610 } 1611 1612 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1613 if (self.grid) |*grid| grid.deinit(); 1614 self.art.deinit(allocator); 1615 self.child.deinit(allocator); 1616 allocator.destroy(self.child); 1617 } 1618 1619 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1620 self.clearGrid(); 1621 1622 // make the wrapped page fill the whole available area 1623 try self.child.build(allocator, .{ 1624 .min_size = .{ 1625 .width = constraint.max_size.width orelse constraint.min_size.width, 1626 .height = constraint.max_size.height orelse constraint.min_size.height, 1627 }, 1628 .max_size = constraint.max_size, 1629 }, root_focus); 1630 1631 if (!self.session.data.enable_ansi) return; 1632 const foreground = self.child.getGrid() orelse return; 1633 self.art.content = self.session.data.ansi_art; 1634 try self.buildArt(allocator, foreground.size, root_focus); 1635 if (!self.session.is_terminal) return; 1636 const art_grid = self.art.getGrid() orelse return; 1637 self.grid = try artBehind(allocator, foreground, art_grid); 1638 } 1639 1640 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1641 try self.child.input(allocator, key, root_focus); 1642 } 1643 1644 pub fn clearGrid(self: *AnsiBackground) void { 1645 if (self.grid) |*grid| { 1646 grid.deinit(); 1647 self.grid = null; 1648 } 1649 self.child.clearGrid(); 1650 self.art.clearGrid(); 1651 } 1652 1653 pub fn getGrid(self: AnsiBackground) ?Grid { 1654 return self.grid orelse self.child.getGrid(); 1655 } 1656 1657 pub fn getFocus(self: *AnsiBackground) *Focus { 1658 return self.child.getFocus(); 1659 } 1660 1661 // a foreground cell counts as blank if it's empty or a bare space without styling 1662 fn cellIsBlank(cell: Grid.Cell) bool { 1663 if (cell.continuation) return false; 1664 if (cell.rune) |rune| { 1665 return rune == ' ' and 1666 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1667 } 1668 return true; 1669 } 1670 1671 // the art is dimmed to this fraction of its brightness so foreground text 1672 // stays legible over it 1673 const art_brightness = 35; // percent 1674 1675 // the art parser only emits truecolor, so a palette color never occurs 1676 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1677 const c = color orelse return null; 1678 return switch (c) { 1679 .rgb => |v| v, 1680 else => null, 1681 }; 1682 } 1683 1684 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1685 const v = artRgb(color) orelse return color; 1686 return .{ .rgb = .{ 1687 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1688 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1689 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1690 } }; 1691 } 1692 1693 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1694 try self.art.build(allocator, .{ 1695 .min_size = .{ .width = null, .height = null }, 1696 .max_size = .{ .width = size.width, .height = size.height }, 1697 }, root_focus); 1698 if (self.art.grid) |*grid| { 1699 for (grid.cells) |*cell| { 1700 cell.style.fg = dimColor(cell.style.fg); 1701 cell.style.bg = dimColor(cell.style.bg); 1702 } 1703 } 1704 } 1705 1706 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1707 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1708 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1709 // snap such channels clear of that range so the backdrop doesn't blink or 1710 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1711 // actually render truecolor. 1712 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1713 const v = artRgb(color) orelse return color; 1714 const snap = struct { 1715 fn f(c: u8) u8 { 1716 return if (c >= 1 and c <= 9) 10 else c; 1717 } 1718 }.f; 1719 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1720 } 1721 1722 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1723 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1724 dst.style.bg = background_maybe; 1725 // change unstyled text to contrast with the art behind it 1726 if (dst.style.fg == null) { 1727 if (artRgb(background_maybe)) |v| { 1728 // use near-black and near-white, which is easier on the eyes 1729 // while retaining high contrast 1730 const luminance = (@as(u32, v.r) * 299 + 1731 @as(u32, v.g) * 587 + 1732 @as(u32, v.b) * 114) / 1000; 1733 dst.style.fg = if (luminance >= 128) 1734 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1735 else 1736 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1737 } 1738 } 1739 } 1740 1741 // composites ANSI art behind a foreground grid 1742 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1743 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1744 errdefer out.deinit(); 1745 1746 const anchor_x = foreground.size.width -| art_grid.size.width; 1747 for (0..art_grid.size.height) |y| { 1748 for (0..art_grid.size.width) |x| { 1749 const src = (try art_grid.cell(x, y)).*; 1750 if (src.rune == null) continue; 1751 const dst = try out.cell(anchor_x + x, y); 1752 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1753 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1754 // blank cells take the art; occupied cells take its background 1755 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1756 dst.style = src.style; 1757 dst.style.fg = sgrSafe(dst.style.fg); 1758 dst.style.bg = sgrSafe(dst.style.bg); 1759 try out.setRune(anchor_x + x, y, src.rune); 1760 } else { 1761 applyArtBackground(dst, src); 1762 if (next_dst) |next| applyArtBackground(next, src); 1763 } 1764 } 1765 } 1766 return out; 1767 } 1768 }; 1769 1770 pub const CopyableText = struct { 1771 pub const Choice = struct { 1772 selector: []const u8 = "", 1773 text: []const u8, 1774 copyable_text: ?[]const u8 = null, 1775 label: []const u8 = "", 1776 bottom_label: []const u8 = "", 1777 }; 1778 1779 box: wgt.Box(Widget), 1780 session: *ui.Session, 1781 choices: []Choice, 1782 selected: usize = 0, 1783 1784 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1785 if (choices.len == 0) return error.MissingCopyableText; 1786 1787 const owned_choices = try allocator.dupe(Choice, choices); 1788 errdefer allocator.free(owned_choices); 1789 1790 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1791 errdefer box.deinit(allocator); 1792 1793 if (choices.len > 1) for (choices) |choice| { 1794 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1795 errdefer selector_box.deinit(allocator); 1796 selector_box.getFocus().mode = .all; 1797 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 } }); 1798 }; 1799 1800 var text_input = try wgt.TextInput.init(allocator, .{ 1801 .border_style = .single, 1802 .round_corners = true, 1803 .label = choices[0].label, 1804 .bottom_label = choices[0].bottom_label, 1805 .read_only = true, 1806 .render_content = session.is_terminal, 1807 .visible_width = null, 1808 }); 1809 errdefer text_input.deinit(allocator); 1810 text_input.getFocus().mode = .all; 1811 try text_input.setContent(allocator, choices[0].text); 1812 box.getFocus().child_id = text_input.getFocus().id; 1813 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1814 1815 return .{ .box = box, .session = session, .choices = owned_choices }; 1816 } 1817 1818 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1819 const aa = session.page_arena.allocator(); 1820 var choices: [2]Choice = undefined; 1821 var count: usize = 0; 1822 switch (location) { 1823 .repo => |identity| { 1824 if (session.data.git_http_port) |port| { 1825 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1826 choices[count] = .{ 1827 .selector = "http", 1828 .text = url, 1829 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1830 .label = " clone ", 1831 }; 1832 count += 1; 1833 } 1834 if (session.data.git_ssh_port) |port| { 1835 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1836 choices[count] = .{ 1837 .selector = "ssh", 1838 .text = url, 1839 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1840 .label = " clone ", 1841 }; 1842 count += 1; 1843 } 1844 }, 1845 .fork => |f| if (session.data.git_ssh_port) |port| { 1846 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1847 choices[count] = .{ 1848 .text = url, 1849 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1850 .label = " clone ", 1851 }; 1852 count += 1; 1853 }, 1854 } 1855 if (count == 0) return null; 1856 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1857 } 1858 1859 pub fn minWidth(self: *const CopyableText) usize { 1860 var width = maxWidth(self.choices) + 2; 1861 if (self.choices.len > 1) { 1862 for (self.choices) |choice| width += choice.selector.len + 2; 1863 } 1864 return width; 1865 } 1866 1867 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1868 self.box.deinit(allocator); 1869 allocator.free(self.choices); 1870 } 1871 1872 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1873 self.clearGrid(); 1874 1875 if (self.choices.len > 1) { 1876 if (root_focus.grandchild_id) |id| { 1877 for (0..self.choices.len) |i| { 1878 if (id != self.selector(i).getFocus().id) continue; 1879 if (i != self.selected) try self.select(allocator, i); 1880 root_focus.setFocus(self.textInput().getFocus().id); 1881 break; 1882 } 1883 } 1884 1885 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1886 for (0..self.choices.len) |i| { 1887 self.selector(i).options.border_style = if (i == self.selected) 1888 (if (focused) .double else .single) 1889 else 1890 .hidden; 1891 } 1892 } 1893 self.textInput().options.border_style = .single; 1894 self.textInput().cursor = 0; 1895 self.textInput().scroll_offset = 0; 1896 try self.box.build(allocator, constraint, root_focus); 1897 1898 if (!self.session.is_terminal) { 1899 const aa = self.session.arena.allocator(); 1900 const id = self.textInput().getFocus().id; 1901 try self.session.text_inputs.put(aa, id, self.textInput()); 1902 } 1903 } 1904 1905 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1906 switch (key) { 1907 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1908 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1909 .enter => if (self.session.is_terminal) self.show(), 1910 .mouse => |mouse| { 1911 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1912 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1913 try self.select(allocator, i); 1914 root_focus.setFocus(self.textInput().getFocus().id); 1915 return; 1916 }; 1917 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1918 }, 1919 else => {}, 1920 } 1921 } 1922 1923 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1924 self.selected = selected; 1925 const choice = self.choices[selected]; 1926 const text_input = self.textInput(); 1927 text_input.options.label = choice.label; 1928 text_input.options.bottom_label = choice.bottom_label; 1929 try text_input.setContent(allocator, choice.text); 1930 } 1931 1932 fn show(self: *CopyableText) void { 1933 const choice = self.choices[self.selected]; 1934 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1935 } 1936 1937 fn textInput(self: *CopyableText) *wgt.TextInput { 1938 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1939 } 1940 1941 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1942 return &self.box.children.values()[index].widget.text_box; 1943 } 1944 1945 fn maxWidth(choices: []const Choice) usize { 1946 var width: usize = 0; 1947 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1948 return width; 1949 } 1950 1951 pub fn clearGrid(self: *CopyableText) void { 1952 self.box.clearGrid(); 1953 } 1954 1955 pub fn getGrid(self: CopyableText) ?Grid { 1956 return self.box.getGrid(); 1957 } 1958 1959 pub fn getFocus(self: *CopyableText) *Focus { 1960 return self.box.getFocus(); 1961 } 1962 }; 1963