╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ About.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
19 const Self = @This(); 20 21 pub fn init(arena: *std.heap.ArenaAllocator, session: *ui.Session) !Self { 22 const admin_repo = session.admin_repo orelse return error.NotFound; 23 const io = session.io orelse return error.NotFound; 24 const text = admin_repo.core.work_dir.readFileAlloc(io, evt.admin_about_path, arena.allocator(), .unlimited) catch |err| switch (err) { 25 error.FileNotFound => "", 26 else => |e| return e, 27 }; 28 const line_end = std.mem.indexOfScalar(u8, text, '\n') orelse text.len; 29 const line = std.mem.trimEnd(u8, text[0..line_end], "\r"); 30 if (line.len < 2 or line[0] != '#' or (line[1] != ' ' and line[1] != '\t')) return .{ .title = null, .text = text }; 31 const title = std.mem.trim(u8, line[2..], " \t"); 32 if (title.len == 0) return .{ .title = null, .text = text }; 33 return .{ .title = title, .text = std.mem.trimStart(u8, text[line_end..], "\r\n") }; 34 } 35 36 pub const View = struct { 37 // the scroll wrapped in a border that turns double while the page has focus 38 frame: wgt.Box(ui.Widget), 39 session: *ui.Session, 40 41 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 42 const doc = try md.parseText(session.page_arena.allocator(), data.text); 43 44 var scroll = blk: { 45 var markdown = try Markdown.View.init(allocator, doc, null, evt.admin_about_path, session.page_arena); 46 errdefer markdown.deinit(allocator); 47 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .markdown = markdown }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 48 }; 49 errdefer scroll.deinit(allocator); 50 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = .single, .round_corners = true, .direction = .vert }); 51 errdefer frame.deinit(allocator); 52 frame.getFocus().child_id = scroll.getFocus().id; 53 try frame.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null, .flex = .grow }); 54 return .{ .frame = frame, .session = session }; 55 } 56 57 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 58 self.frame.deinit(allocator); 59 } 60 61 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 62 const focused = if (root_focus.grandchild_id) |id| self.markdownView().owns(id) else false; 63 self.frame.options.border = if (focused) .double else .single; 64 try self.frame.build(allocator, constraint, root_focus); 65 } 66 67 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 68 // the web scrolls natively and leaves tab to the browser 69 if (!self.session.is_terminal) return; 70 const sc = self.contentScroll(); 71 switch (key) { 72 .arrow_up => sc.y -= 1, 73 .arrow_down => sc.y += 1, 74 .page_up => sc.y -= 10, 75 .page_down => sc.y += 10, 76 .home => sc.y = 0, 77 .end => sc.y = std.math.maxInt(isize), 78 .scroll_up => sc.y -= 5, 79 .scroll_down => sc.y += 5, 80 .tab, .back_tab => try self.markdownView().stepLink(allocator, root_focus, sc, 0, 0, key == .tab), 81 else => {}, 82 } 83 sc.clampToContent(); 84 } 85 86 pub fn clearGrid(self: *View) void { 87 self.frame.clearGrid(); 88 } 89 90 pub fn getGrid(self: View) ?Grid { 91 return self.frame.getGrid(); 92 } 93 94 pub fn getFocus(self: *View) *Focus { 95 return self.frame.getFocus(); 96 } 97 98 pub fn atTop(self: View) bool { 99 return self.frame.children.values()[0].widget.scroll.y <= 0; 100 } 101 102 fn contentScroll(self: *View) *wgt.Scroll(ui.Widget) { 103 return &self.frame.children.values()[0].widget.scroll; 104 } 105 106 fn markdownView(self: *View) *Markdown.View { 107 return &self.contentScroll().child.markdown; 108 } 109 }; 110