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