╭ 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 home_settings: ui.Home.Settings.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, .home_settings, .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. a non-empty `link` 235 // sets the item's focus kind such as `.{ .custom = "a:/user/foo" }`, which 236 // the web renderer turns into an anchor. 237 pub const Item = struct { text: []const u8, link: []const u8 = "" }; 238 239 // the text box copies the item text; links are copied into the arena, so the 240 // 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 }); 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 values: []const []const u8, 951 952 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, name: []const u8, values: []const []const u8, initial: []const u8) !Radio { 953 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 954 errdefer box.deinit(allocator); 955 for (values) |value| { 956 var option = try wgt.TextBox.init(allocator, value, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 957 errdefer option.deinit(allocator); 958 option.getFocus().mode = .all; 959 option.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), ui.radio_prefix ++ "{s}={s}", .{ name, value }) }; 960 if (std.mem.eql(u8, value, initial)) box.getFocus().child_id = option.getFocus().id; 961 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 } }); 962 } 963 return .{ .box = box, .values = values }; 964 } 965 966 pub fn deinit(self: *Radio, allocator: std.mem.Allocator) void { 967 self.box.deinit(allocator); 968 } 969 970 // the focused option's value 971 pub fn selected(self: *Radio) []const u8 { 972 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse unreachable) orelse unreachable; 973 return self.values[index]; 974 } 975 976 pub fn build(self: *Radio, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 977 self.clearGrid(); 978 const selected_id = self.box.getFocus().child_id; 979 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 980 child.widget.text_box.options.border_style = if (id == selected_id) .single else .hidden; 981 } 982 try self.box.build(allocator, constraint, root_focus); 983 } 984 985 pub fn input(self: *Radio, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 986 _ = allocator; 987 const ids = self.box.children.keys(); 988 const index = self.box.children.getIndex(self.box.getFocus().child_id orelse return) orelse return; 989 switch (key) { 990 .arrow_left => if (index > 0) root_focus.setFocus(ids[index - 1]), 991 .arrow_right => if (index + 1 < ids.len) root_focus.setFocus(ids[index + 1]), 992 else => {}, 993 } 994 } 995 996 pub fn clearGrid(self: *Radio) void { 997 self.box.clearGrid(); 998 } 999 1000 pub fn getGrid(self: Radio) ?Grid { 1001 return self.box.getGrid(); 1002 } 1003 1004 pub fn getFocus(self: *Radio) *Focus { 1005 return self.box.getFocus(); 1006 } 1007 }; 1008 1009 // a tab's sub-header: an optional search box on the left, then a spacer, then 1010 // the clone url on the right. the owning view turns a submitted query into a 1011 // route, so route building stays with the tab that knows its own url. 1012 pub const SearchHeader = struct { 1013 box: wgt.Box(Widget), 1014 session: *ui.Session, 1015 1016 pub fn init( 1017 allocator: std.mem.Allocator, 1018 session: *ui.Session, 1019 location: ui.RoutablePage.RepoLocation, 1020 label: []const u8, 1021 name: []const u8, 1022 text: ?[]const u8, 1023 show_input: bool, 1024 ) !SearchHeader { 1025 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1026 errdefer box.deinit(allocator); 1027 1028 // the search box only appears where an index can serve it. 1029 if (show_input) { 1030 var search_box = try SearchBox.init(allocator, session, label, name, text); 1031 errdefer search_box.deinit(allocator); 1032 box.getFocus().child_id = search_box.getFocus().id; 1033 try box.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = SearchBox.min_size }); 1034 } 1035 1036 // the spacer pushes the clone url to the right edge. 1037 { 1038 var spacer = try Spacer.init(allocator); 1039 errdefer spacer.deinit(allocator); 1040 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1041 } 1042 1043 if (session.data.host_kind == .server) { 1044 if (try CopyableText.initClone(allocator, session, location)) |value| { 1045 var clone_url = value; 1046 errdefer clone_url.deinit(allocator); 1047 const min_width = clone_url.minWidth(); 1048 if (box.getFocus().child_id == null) box.getFocus().child_id = clone_url.getFocus().id; 1049 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 } }); 1050 } 1051 } 1052 1053 return .{ .box = box, .session = session }; 1054 } 1055 1056 pub fn deinit(self: *SearchHeader, allocator: std.mem.Allocator) void { 1057 self.box.deinit(allocator); 1058 } 1059 1060 pub fn build(self: *SearchHeader, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1061 self.clearGrid(); 1062 try self.box.build(allocator, constraint, root_focus); 1063 } 1064 1065 pub fn input(self: *SearchHeader, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1066 const current = self.box.getFocus().child_id orelse return; 1067 if (self.searchBox()) |search_box| { 1068 if (current == search_box.getFocus().id) { 1069 // tab, and right-arrow past the last character, step to the 1070 // clone url beside the box 1071 if (key == .tab or (key == .arrow_right and search_box.atEnd())) { 1072 if (self.cloneUrl()) |clone_url| { 1073 root_focus.setFocus(clone_url.getFocus().id); 1074 return; 1075 } 1076 } 1077 return search_box.input(allocator, key, root_focus); 1078 } 1079 } 1080 const clone_url = self.cloneUrl() orelse return; 1081 if (key == .back_tab or (key == .arrow_left and clone_url.selected == 0)) { 1082 if (self.searchBox()) |search_box| { 1083 root_focus.setFocus(search_box.getFocus().id); 1084 return; 1085 } 1086 } 1087 try clone_url.input(allocator, key, root_focus); 1088 } 1089 1090 // the typed query when enter should submit it: the box exists and holds 1091 // focus. the caller owns the returned text. 1092 pub fn submittedText(self: *SearchHeader, allocator: std.mem.Allocator) !?[]u8 { 1093 const search_box = self.searchBox() orelse return null; 1094 if (self.box.getFocus().child_id != search_box.getFocus().id) return null; 1095 return try search_box.text(allocator); 1096 } 1097 1098 // enter the header, preferring the search box. 1099 pub fn focusHeader(self: *SearchHeader, root_focus: *Focus) bool { 1100 if (self.searchBox()) |search_box| { 1101 root_focus.setFocus(search_box.getFocus().id); 1102 return true; 1103 } 1104 const clone_url = self.cloneUrl() orelse return false; 1105 root_focus.setFocus(clone_url.getFocus().id); 1106 return true; 1107 } 1108 1109 pub fn hasFocusable(self: *SearchHeader) bool { 1110 return self.cloneUrl() != null or self.searchBox() != null; 1111 } 1112 1113 fn cloneUrl(self: *SearchHeader) ?*CopyableText { 1114 for (self.box.children.values()) |*child| switch (child.widget) { 1115 .copyable_text => |*copyable_text| return copyable_text, 1116 else => {}, 1117 }; 1118 return null; 1119 } 1120 1121 fn searchBox(self: *SearchHeader) ?*SearchBox { 1122 for (self.box.children.values()) |*child| switch (child.widget) { 1123 .search_box => |*search_box| return search_box, 1124 else => {}, 1125 }; 1126 return null; 1127 } 1128 1129 pub fn clearGrid(self: *SearchHeader) void { 1130 self.box.clearGrid(); 1131 } 1132 1133 pub fn getGrid(self: SearchHeader) ?Grid { 1134 return self.box.getGrid(); 1135 } 1136 1137 pub fn getFocus(self: *SearchHeader) *Focus { 1138 return self.box.getFocus(); 1139 } 1140 }; 1141 1142 // a one-row, non-focusable status bar showing the current page's url 1143 pub const Footer = struct { 1144 focus: *Focus, 1145 grid: ?Grid, 1146 session: *ui.Session, 1147 arena: std.heap.ArenaAllocator, 1148 url: []const u8 = "", 1149 url_width: usize = 0, 1150 1151 pub fn init(allocator: std.mem.Allocator, session: *ui.Session) !Footer { 1152 return .{ 1153 .focus = try Focus.create(allocator, .container), 1154 .grid = null, 1155 .session = session, 1156 .arena = std.heap.ArenaAllocator.init(allocator), 1157 }; 1158 } 1159 1160 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1161 self.focus.destroy(allocator); 1162 if (self.grid) |*grid| grid.deinit(); 1163 self.arena.deinit(); 1164 } 1165 1166 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1167 _ = root_focus; 1168 self.clearGrid(); 1169 const width = constraint.max_size.width orelse return; 1170 if (width == 0) return; 1171 1172 _ = self.arena.reset(.retain_capacity); 1173 self.url = ""; 1174 self.url_width = 0; 1175 const aa = self.arena.allocator(); 1176 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1177 const text = ui.terminalWebUrl(aa, self.session, path) catch return; 1178 self.url = text; 1179 1180 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1181 errdefer grid.deinit(); 1182 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1183 var i: usize = 0; 1184 while (utf8.nextCodepoint()) |ch| { 1185 const rune_width = xitui.width.cellWidth(ch); 1186 if (i + rune_width > width) break; 1187 // a terminal hyperlink to the page 1188 (try grid.cell(i, 0)).link = text; 1189 try grid.setRune(i, 0, ch); 1190 i += rune_width; 1191 } 1192 self.url_width = i; 1193 self.grid = grid; 1194 } 1195 1196 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1197 _ = allocator; 1198 switch (key) { 1199 .mouse => |mouse| { 1200 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 1201 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 1202 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 1203 }, 1204 else => {}, 1205 } 1206 } 1207 1208 pub fn clearGrid(self: *Footer) void { 1209 if (self.grid) |*grid| { 1210 grid.deinit(); 1211 self.grid = null; 1212 } 1213 } 1214 1215 pub fn getGrid(self: Footer) ?Grid { 1216 return self.grid; 1217 } 1218 1219 pub fn getFocus(self: *Footer) *Focus { 1220 return self.focus; 1221 } 1222 }; 1223 1224 // a single-child wrapper that builds the child at its natural size and 1225 // positions its grid in the middle of the area granted by the parent 1226 // a bordered label, indented past the content it precedes 1227 pub const SectionLabel = struct { 1228 box: wgt.Box(Widget), 1229 1230 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 1231 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1232 errdefer box.deinit(allocator); 1233 1234 { 1235 var gap = try wgt.Text.init(allocator, " "); 1236 errdefer gap.deinit(allocator); 1237 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1238 } 1239 1240 { 1241 var tb = try wgt.TextBox.init(allocator, content, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1242 errdefer tb.deinit(allocator); 1243 tb.getFocus().mode = .all; 1244 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 1245 } 1246 1247 box.getFocus().child_id = box.children.keys()[1]; 1248 return .{ .box = box }; 1249 } 1250 1251 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 1252 self.box.deinit(allocator); 1253 } 1254 1255 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1256 try self.box.build(allocator, constraint, root_focus); 1257 } 1258 1259 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1260 _ = self; 1261 _ = allocator; 1262 _ = key; 1263 _ = root_focus; 1264 } 1265 1266 pub fn clearGrid(self: *SectionLabel) void { 1267 self.box.clearGrid(); 1268 } 1269 1270 pub fn getGrid(self: SectionLabel) ?Grid { 1271 return self.box.getGrid(); 1272 } 1273 1274 pub fn getFocus(self: *SectionLabel) *Focus { 1275 return self.box.getFocus(); 1276 } 1277 }; 1278 1279 // a submit button, indented past its form's content. the web form POSTs 1280 // the button's form; terminal hosts detect presses through `buttonId`. 1281 pub const SubmitButton = struct { 1282 box: wgt.Box(Widget), 1283 1284 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 1285 return initLabeled(allocator, "submit"); 1286 } 1287 1288 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 1289 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1290 errdefer box.deinit(allocator); 1291 1292 { 1293 var gap = try wgt.Text.init(allocator, " "); 1294 errdefer gap.deinit(allocator); 1295 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 1296 } 1297 1298 { 1299 var button = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 1300 errdefer button.deinit(allocator); 1301 button.getFocus().mode = .all; 1302 // the renderer distinguishes plain clickables from buttons that 1303 // should POST to a server route by this kind. 1304 button.getFocus().kind = .{ .custom = "submit" }; 1305 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 1306 } 1307 1308 box.getFocus().child_id = box.children.keys()[1]; 1309 return .{ .box = box }; 1310 } 1311 1312 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 1313 self.box.deinit(allocator); 1314 } 1315 1316 pub fn setLabel(self: *SubmitButton, allocator: std.mem.Allocator, label: []const u8) !void { 1317 const button = &self.box.children.values()[1]; 1318 try button.widget.text_box.setContent(allocator, label); 1319 button.min_size = .{ .width = label.len + 2, .height = null }; 1320 } 1321 1322 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1323 try self.box.build(allocator, constraint, root_focus); 1324 } 1325 1326 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1327 _ = self; 1328 _ = allocator; 1329 _ = key; 1330 _ = root_focus; 1331 } 1332 1333 pub fn clearGrid(self: *SubmitButton) void { 1334 self.box.clearGrid(); 1335 } 1336 1337 pub fn getGrid(self: SubmitButton) ?Grid { 1338 return self.box.getGrid(); 1339 } 1340 1341 pub fn getFocus(self: *SubmitButton) *Focus { 1342 return self.box.getFocus(); 1343 } 1344 1345 // the button's focus id, for click hit-testing 1346 pub fn buttonId(self: *const SubmitButton) usize { 1347 return self.box.children.keys()[1]; 1348 } 1349 }; 1350 1351 pub const Center = struct { 1352 focus: *Focus, 1353 grid: ?Grid, 1354 child: *Widget, 1355 1356 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1357 const child = try allocator.create(Widget); 1358 errdefer allocator.destroy(child); 1359 child.* = child_widget; 1360 return .{ 1361 .focus = try Focus.create(allocator, .container), 1362 .grid = null, 1363 .child = child, 1364 }; 1365 } 1366 1367 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1368 self.focus.destroy(allocator); 1369 if (self.grid) |*grid| { 1370 grid.deinit(); 1371 self.grid = null; 1372 } 1373 self.child.deinit(allocator); 1374 allocator.destroy(self.child); 1375 } 1376 1377 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1378 self.clearGrid(); 1379 self.getFocus().clear(); 1380 1381 // build the child without forcing it to fill min_size; it sizes 1382 // itself to its content within the available max. 1383 try self.child.build(allocator, .{ 1384 .min_size = .{ .width = null, .height = null }, 1385 .max_size = constraint.max_size, 1386 }, root_focus); 1387 1388 const child_grid = self.child.getGrid() orelse return; 1389 1390 // prefer max when given; otherwise grow to max(min, child) so that 1391 // when only a min is set (e.g. the wasm path passes viewport rows 1392 // as min_height with no max), we can still vertically center while 1393 // letting taller content extend past the min. 1394 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; 1395 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; 1396 if (width == 0 or height == 0) return; 1397 1398 const offset_x: usize = (width -| child_grid.size.width) / 2; 1399 const offset_y: usize = (height -| child_grid.size.height) / 2; 1400 1401 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1402 errdefer grid.deinit(); 1403 try grid.drawGrid(child_grid, offset_x, offset_y); 1404 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1405 self.getFocus().child_id = self.child.getFocus().id; 1406 1407 self.grid = grid; 1408 } 1409 1410 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1411 try self.child.input(allocator, key, root_focus); 1412 } 1413 1414 pub fn clearGrid(self: *Center) void { 1415 if (self.grid) |*grid| { 1416 grid.deinit(); 1417 self.grid = null; 1418 } 1419 self.child.clearGrid(); 1420 } 1421 1422 pub fn getGrid(self: Center) ?Grid { 1423 return self.grid; 1424 } 1425 1426 pub fn getFocus(self: *Center) *Focus { 1427 return self.focus; 1428 } 1429 }; 1430 1431 // renders truecolor ANSI art 1432 pub const AnsiArt = struct { 1433 focus: *Focus, 1434 grid: ?Grid, 1435 content: []const u8, 1436 1437 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1438 return .{ 1439 .focus = try Focus.create(allocator, .container), 1440 .grid = null, 1441 .content = content, 1442 }; 1443 } 1444 1445 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1446 self.focus.destroy(allocator); 1447 if (self.grid) |*grid| { 1448 grid.deinit(); 1449 self.grid = null; 1450 } 1451 } 1452 1453 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1454 _ = root_focus; 1455 self.clearGrid(); 1456 1457 // parse into rows of cells, then copy into a rectangular grid 1458 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1459 defer { 1460 for (rows.items) |*row| row.deinit(allocator); 1461 rows.deinit(allocator); 1462 } 1463 var row: std.ArrayList(Grid.Cell) = .empty; 1464 errdefer row.deinit(allocator); 1465 1466 var style: Grid.Style = .{}; 1467 var width: usize = 0; 1468 const content = self.content; 1469 var i: usize = 0; 1470 while (i < content.len) { 1471 const byte = content[i]; 1472 if (byte == '\n') { 1473 width = @max(width, row.items.len); 1474 try rows.append(allocator, row); 1475 row = .empty; 1476 i += 1; 1477 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1478 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1479 var j = i + 2; 1480 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1481 if (j >= content.len) { 1482 i = content.len; // malformed trailing escape; stop 1483 } else { 1484 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1485 i = j + 1; 1486 } 1487 } else { 1488 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1489 const end = @min(content.len, i + len); 1490 const rune = content[i..end]; 1491 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1492 // a foreground color does not make a space visible. only an 1493 // effective background makes it opaque; foreground state often 1494 // remains active through the padding at the end of a row. 1495 const effective_background = if (style.inverted) style.fg else style.bg; 1496 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1497 try row.append(allocator, .{ 1498 .rune = if (transparent) null else codepoint, 1499 .style = style, 1500 }); 1501 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1502 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1503 } 1504 i = end; 1505 } 1506 } 1507 // a trailing row with content but no closing newline 1508 if (row.items.len > 0) { 1509 width = @max(width, row.items.len); 1510 try rows.append(allocator, row); 1511 } else { 1512 row.deinit(allocator); 1513 } 1514 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1515 1516 const height = rows.items.len; 1517 if (width == 0 or height == 0) return; 1518 1519 const clamped_w = @min(width, constraint.max_size.width orelse width); 1520 const clamped_h = @min(height, constraint.max_size.height orelse height); 1521 1522 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1523 errdefer grid.deinit(); 1524 for (rows.items[0..clamped_h], 0..) |r, y| { 1525 const n = @min(r.items.len, clamped_w); 1526 for (r.items[0..n], 0..) |cell, x| { 1527 if (cell.continuation) continue; 1528 (try grid.cell(x, y)).style = cell.style; 1529 try grid.setRune(x, y, cell.rune); 1530 } 1531 } 1532 self.grid = grid; 1533 } 1534 1535 pub fn clearGrid(self: *AnsiArt) void { 1536 if (self.grid) |*grid| { 1537 grid.deinit(); 1538 self.grid = null; 1539 } 1540 } 1541 1542 pub fn getGrid(self: AnsiArt) ?Grid { 1543 return self.grid; 1544 } 1545 1546 pub fn getFocus(self: *AnsiArt) *Focus { 1547 return self.focus; 1548 } 1549 1550 fn applySgr(style: *Grid.Style, params: []const u8) void { 1551 var nums: [16]u32 = undefined; 1552 var n: usize = 0; 1553 var it = std.mem.splitScalar(u8, params, ';'); 1554 while (it.next()) |tok| { 1555 if (n >= nums.len) break; 1556 // an empty parameter (e.g. bare "\x1b[m") means 0 1557 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1558 n += 1; 1559 } 1560 if (n == 0) { 1561 style.* = .{}; 1562 return; 1563 } 1564 var i: usize = 0; 1565 while (i < n) : (i += 1) { 1566 switch (nums[i]) { 1567 0 => style.* = .{}, 1568 7 => style.inverted = true, 1569 27 => style.inverted = false, 1570 39 => style.fg = null, 1571 49 => style.bg = null, 1572 38, 48 => { 1573 // truecolor form: 38;2;r;g;b — anything else is ignored 1574 if (i + 4 < n and nums[i + 1] == 2) { 1575 const c = Grid.Color{ .rgb = .{ 1576 .r = @truncate(nums[i + 2]), 1577 .g = @truncate(nums[i + 3]), 1578 .b = @truncate(nums[i + 4]), 1579 } }; 1580 if (nums[i] == 38) style.fg = c else style.bg = c; 1581 i += 4; 1582 } 1583 }, 1584 else => {}, 1585 } 1586 } 1587 } 1588 }; 1589 1590 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1591 pub const AnsiBackground = struct { 1592 grid: ?Grid, 1593 child: *Widget, 1594 art: AnsiArt, 1595 session: *ui.Session, 1596 1597 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1598 var cw = child_widget; 1599 const child = allocator.create(Widget) catch |e| { 1600 cw.deinit(allocator); 1601 return e; 1602 }; 1603 child.* = cw; 1604 errdefer { 1605 child.deinit(allocator); 1606 allocator.destroy(child); 1607 } 1608 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1609 } 1610 1611 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1612 if (self.grid) |*grid| grid.deinit(); 1613 self.art.deinit(allocator); 1614 self.child.deinit(allocator); 1615 allocator.destroy(self.child); 1616 } 1617 1618 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1619 self.clearGrid(); 1620 1621 // make the wrapped page fill the whole available area 1622 try self.child.build(allocator, .{ 1623 .min_size = .{ 1624 .width = constraint.max_size.width orelse constraint.min_size.width, 1625 .height = constraint.max_size.height orelse constraint.min_size.height, 1626 }, 1627 .max_size = constraint.max_size, 1628 }, root_focus); 1629 1630 if (!self.session.data.enable_ansi) return; 1631 const foreground = self.child.getGrid() orelse return; 1632 self.art.content = self.session.data.ansi_art; 1633 try self.buildArt(allocator, foreground.size, root_focus); 1634 if (!self.session.is_terminal) return; 1635 const art_grid = self.art.getGrid() orelse return; 1636 self.grid = try artBehind(allocator, foreground, art_grid); 1637 } 1638 1639 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1640 try self.child.input(allocator, key, root_focus); 1641 } 1642 1643 pub fn clearGrid(self: *AnsiBackground) void { 1644 if (self.grid) |*grid| { 1645 grid.deinit(); 1646 self.grid = null; 1647 } 1648 self.child.clearGrid(); 1649 self.art.clearGrid(); 1650 } 1651 1652 pub fn getGrid(self: AnsiBackground) ?Grid { 1653 return self.grid orelse self.child.getGrid(); 1654 } 1655 1656 pub fn getFocus(self: *AnsiBackground) *Focus { 1657 return self.child.getFocus(); 1658 } 1659 1660 // a foreground cell counts as blank if it's empty or a bare space without styling 1661 fn cellIsBlank(cell: Grid.Cell) bool { 1662 if (cell.continuation) return false; 1663 if (cell.rune) |rune| { 1664 return rune == ' ' and 1665 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1666 } 1667 return true; 1668 } 1669 1670 // the art is dimmed to this fraction of its brightness so foreground text 1671 // stays legible over it 1672 const art_brightness = 35; // percent 1673 1674 // the art parser only emits truecolor, so a palette color never occurs 1675 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1676 const c = color orelse return null; 1677 return switch (c) { 1678 .rgb => |v| v, 1679 else => null, 1680 }; 1681 } 1682 1683 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1684 const v = artRgb(color) orelse return color; 1685 return .{ .rgb = .{ 1686 .r = @intCast(@as(u16, v.r) * art_brightness / 100), 1687 .g = @intCast(@as(u16, v.g) * art_brightness / 100), 1688 .b = @intCast(@as(u16, v.b) * art_brightness / 100), 1689 } }; 1690 } 1691 1692 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 1693 try self.art.build(allocator, .{ 1694 .min_size = .{ .width = null, .height = null }, 1695 .max_size = .{ .width = size.width, .height = size.height }, 1696 }, root_focus); 1697 if (self.art.grid) |*grid| { 1698 for (grid.cells) |*cell| { 1699 cell.style.fg = dimColor(cell.style.fg); 1700 cell.style.bg = dimColor(cell.style.bg); 1701 } 1702 } 1703 } 1704 1705 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1706 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1707 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1708 // snap such channels clear of that range so the backdrop doesn't blink or 1709 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1710 // actually render truecolor. 1711 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1712 const v = artRgb(color) orelse return color; 1713 const snap = struct { 1714 fn f(c: u8) u8 { 1715 return if (c >= 1 and c <= 9) 10 else c; 1716 } 1717 }.f; 1718 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1719 } 1720 1721 fn applyArtBackground(dst: *Grid.Cell, src: Grid.Cell) void { 1722 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 1723 dst.style.bg = background_maybe; 1724 // change unstyled text to contrast with the art behind it 1725 if (dst.style.fg == null) { 1726 if (artRgb(background_maybe)) |v| { 1727 // use near-black and near-white, which is easier on the eyes 1728 // while retaining high contrast 1729 const luminance = (@as(u32, v.r) * 299 + 1730 @as(u32, v.g) * 587 + 1731 @as(u32, v.b) * 114) / 1000; 1732 dst.style.fg = if (luminance >= 128) 1733 .{ .rgb = .{ .r = 16, .g = 16, .b = 16 } } 1734 else 1735 .{ .rgb = .{ .r = 240, .g = 240, .b = 240 } }; 1736 } 1737 } 1738 } 1739 1740 // composites ANSI art behind a foreground grid 1741 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1742 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1743 errdefer out.deinit(); 1744 1745 const anchor_x = foreground.size.width -| art_grid.size.width; 1746 for (0..art_grid.size.height) |y| { 1747 for (0..art_grid.size.width) |x| { 1748 const src = (try art_grid.cell(x, y)).*; 1749 if (src.rune == null) continue; 1750 const dst = try out.cell(anchor_x + x, y); 1751 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1752 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1753 // blank cells take the art; occupied cells take its background 1754 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1755 dst.style = src.style; 1756 dst.style.fg = sgrSafe(dst.style.fg); 1757 dst.style.bg = sgrSafe(dst.style.bg); 1758 try out.setRune(anchor_x + x, y, src.rune); 1759 } else { 1760 applyArtBackground(dst, src); 1761 if (next_dst) |next| applyArtBackground(next, src); 1762 } 1763 } 1764 } 1765 return out; 1766 } 1767 }; 1768 1769 pub const CopyableText = struct { 1770 pub const Choice = struct { 1771 selector: []const u8 = "", 1772 text: []const u8, 1773 copyable_text: ?[]const u8 = null, 1774 label: []const u8 = "", 1775 bottom_label: []const u8 = "", 1776 }; 1777 1778 box: wgt.Box(Widget), 1779 session: *ui.Session, 1780 choices: []Choice, 1781 selected: usize = 0, 1782 1783 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice) !CopyableText { 1784 if (choices.len == 0) return error.MissingCopyableText; 1785 1786 const owned_choices = try allocator.dupe(Choice, choices); 1787 errdefer allocator.free(owned_choices); 1788 1789 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1790 errdefer box.deinit(allocator); 1791 1792 if (choices.len > 1) for (choices) |choice| { 1793 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1794 errdefer selector_box.deinit(allocator); 1795 selector_box.getFocus().mode = .all; 1796 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 } }); 1797 }; 1798 1799 var text_input = try wgt.TextInput.init(allocator, .{ 1800 .border_style = .single, 1801 .round_corners = true, 1802 .label = choices[0].label, 1803 .bottom_label = choices[0].bottom_label, 1804 .read_only = true, 1805 .render_content = session.is_terminal, 1806 .visible_width = null, 1807 }); 1808 errdefer text_input.deinit(allocator); 1809 text_input.getFocus().mode = .all; 1810 try text_input.setContent(allocator, choices[0].text); 1811 box.getFocus().child_id = text_input.getFocus().id; 1812 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1813 1814 return .{ .box = box, .session = session, .choices = owned_choices }; 1815 } 1816 1817 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !?CopyableText { 1818 const aa = session.page_arena.allocator(); 1819 var choices: [2]Choice = undefined; 1820 var count: usize = 0; 1821 switch (location) { 1822 .repo => |identity| { 1823 if (session.data.git_http_port) |port| { 1824 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1825 choices[count] = .{ 1826 .selector = "http", 1827 .text = url, 1828 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1829 .label = " clone ", 1830 }; 1831 count += 1; 1832 } 1833 if (session.data.git_ssh_port) |port| { 1834 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1835 choices[count] = .{ 1836 .selector = "ssh", 1837 .text = url, 1838 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1839 .label = " clone ", 1840 }; 1841 count += 1; 1842 } 1843 }, 1844 .fork => |f| if (session.data.git_ssh_port) |port| { 1845 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1846 choices[count] = .{ 1847 .text = url, 1848 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1849 .label = " clone ", 1850 }; 1851 count += 1; 1852 }, 1853 } 1854 if (count == 0) return null; 1855 return @as(?CopyableText, try init(allocator, session, choices[0..count])); 1856 } 1857 1858 pub fn minWidth(self: *const CopyableText) usize { 1859 var width = maxWidth(self.choices) + 2; 1860 if (self.choices.len > 1) { 1861 for (self.choices) |choice| width += choice.selector.len + 2; 1862 } 1863 return width; 1864 } 1865 1866 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1867 self.box.deinit(allocator); 1868 allocator.free(self.choices); 1869 } 1870 1871 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1872 self.clearGrid(); 1873 1874 if (self.choices.len > 1) { 1875 if (root_focus.grandchild_id) |id| { 1876 for (0..self.choices.len) |i| { 1877 if (id != self.selector(i).getFocus().id) continue; 1878 if (i != self.selected) try self.select(allocator, i); 1879 root_focus.setFocus(self.textInput().getFocus().id); 1880 break; 1881 } 1882 } 1883 1884 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1885 for (0..self.choices.len) |i| { 1886 self.selector(i).options.border_style = if (i == self.selected) 1887 (if (focused) .double else .single) 1888 else 1889 .hidden; 1890 } 1891 } 1892 self.textInput().options.border_style = .single; 1893 self.textInput().cursor = 0; 1894 self.textInput().scroll_offset = 0; 1895 try self.box.build(allocator, constraint, root_focus); 1896 1897 if (!self.session.is_terminal) { 1898 const aa = self.session.arena.allocator(); 1899 const id = self.textInput().getFocus().id; 1900 try self.session.text_inputs.put(aa, id, self.textInput()); 1901 } 1902 } 1903 1904 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1905 switch (key) { 1906 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 1907 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 1908 .enter => if (self.session.is_terminal) self.show(), 1909 .mouse => |mouse| { 1910 if (self.choices.len > 1) for (0..self.choices.len) |i| { 1911 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 1912 try self.select(allocator, i); 1913 root_focus.setFocus(self.textInput().getFocus().id); 1914 return; 1915 }; 1916 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 1917 }, 1918 else => {}, 1919 } 1920 } 1921 1922 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 1923 self.selected = selected; 1924 const choice = self.choices[selected]; 1925 const text_input = self.textInput(); 1926 text_input.options.label = choice.label; 1927 text_input.options.bottom_label = choice.bottom_label; 1928 try text_input.setContent(allocator, choice.text); 1929 } 1930 1931 fn show(self: *CopyableText) void { 1932 const choice = self.choices[self.selected]; 1933 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 1934 } 1935 1936 fn textInput(self: *CopyableText) *wgt.TextInput { 1937 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 1938 } 1939 1940 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 1941 return &self.box.children.values()[index].widget.text_box; 1942 } 1943 1944 fn maxWidth(choices: []const Choice) usize { 1945 var width: usize = 0; 1946 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 1947 return width; 1948 } 1949 1950 pub fn clearGrid(self: *CopyableText) void { 1951 self.box.clearGrid(); 1952 } 1953 1954 pub fn getGrid(self: CopyableText) ?Grid { 1955 return self.box.getGrid(); 1956 } 1957 1958 pub fn getFocus(self: *CopyableText) *Focus { 1959 return self.box.getFocus(); 1960 } 1961 }; 1962