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