╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1849 errdefer out.deinit(); 1850 1851 const anchor_x = foreground.size.width -| art_grid.size.width; 1852 for (0..art_grid.size.height) |y| { 1853 for (0..art_grid.size.width) |x| { 1854 const src = (try art_grid.cell(x, y)).*; 1855 if (src.rune == null) continue; 1856 const dst = try out.cell(anchor_x + x, y); 1857 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1858 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1859 // blank cells take the art; occupied cells take its background 1860 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1861 dst.style = src.style; 1862 dst.style.fg = sgrSafe(dst.style.fg); 1863 dst.style.bg = sgrSafe(dst.style.bg); 1864 try out.setRune(anchor_x + x, y, src.rune); 1865 } else { 1866 applyArtBackground(dst, src); 1867 if (next_dst) |next| applyArtBackground(next, src); 1868 } 1869 } 1870 } 1871 return out; 1872 } 1873 }; 1874 1875 pub const CopyableText = struct { 1876 pub const Choice = struct { 1877 selector: []const u8 = "", 1878 text: []const u8, 1879 copyable_text: ?[]const u8 = null, 1880 label: []const u8 = "", 1881 bottom_label: []const u8 = "", 1882 }; 1883 1884 box: wgt.Box(Widget), 1885 session: *ui.Session, 1886 choices: []Choice, 1887 selected: usize = 0, 1888 1889 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1890 if (choices.len == 0) return error.MissingCopyableText; 1891 1892 const owned_choices = try allocator.dupe(Choice, choices); 1893 errdefer allocator.free(owned_choices); 1894 1895 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1896 errdefer box.deinit(allocator); 1897 1898 if (choices.len > 1) for (choices) |choice| { 1899 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none }); 1900 errdefer selector_box.deinit(allocator); 1901 selector_box.getFocus().mode = .all; 1902 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 } }); 1903 }; 1904 1905 var text_input = try wgt.TextInput.init(allocator, .{ 1906 .border_style = .single, 1907 .round_corners = true, 1908 .label = choices[initial].label, 1909 .bottom_label = choices[initial].bottom_label, 1910 .read_only = true, 1911 .render_content = session.is_terminal, 1912 .visible_width = null, 1913 }); 1914 errdefer text_input.deinit(allocator); 1915 text_input.getFocus().mode = .all; 1916 try text_input.setContent(allocator, choices[initial].text); 1917 box.getFocus().child_id = text_input.getFocus().id; 1918 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1919 1920 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1921 } 1922 1923 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1924 const aa = session.page_arena.allocator(); 1925 var choices: [2]Choice = undefined; 1926 var count: usize = 0; 1927 var ssh_index: ?usize = null; 1928 switch (handle.location) { 1929 .repo => |identity| { 1930 if (session.data.git_http_port) |port| { 1931 const url = try std.fmt.allocPrint(aa, "http://localhost:{d}/repo/{s}", .{ port, identity }); 1932 choices[count] = .{ 1933 .selector = "http", 1934 .text = url, 1935 .copyable_text = try std.fmt.allocPrint(aa, "git clone {s}", .{url}), 1936 .label = " clone ", 1937 }; 1938 count += 1; 1939 } 1940 if (session.data.git_ssh_port) |port| { 1941 ssh_index = count; 1942 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/repo/{s}", .{ port, identity }); 1943 choices[count] = .{ 1944 .selector = "ssh", 1945 .text = url, 1946 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1947 .label = " clone ", 1948 }; 1949 count += 1; 1950 } 1951 }, 1952 .fork => |f| if (session.data.git_ssh_port) |port| { 1953 const url = try std.fmt.allocPrint(aa, "ssh://localhost:{d}/fork/{s}/patch:{s}", .{ port, f.identity, f.id }); 1954 choices[count] = .{ 1955 .text = url, 1956 .copyable_text = try std.fmt.allocPrint(aa, "{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1957 .label = " clone ", 1958 }; 1959 count += 1; 1960 }, 1961 } 1962 if (count == 0) return null; 1963 // a viewer who can push starts on the ssh url 1964 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1965 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1966 } 1967 1968 pub fn minWidth(self: *const CopyableText) usize { 1969 var width = maxWidth(self.choices) + 2; 1970 if (self.choices.len > 1) { 1971 for (self.choices) |choice| width += choice.selector.len + 2; 1972 } 1973 return width; 1974 } 1975 1976 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1977 self.box.deinit(allocator); 1978 allocator.free(self.choices); 1979 } 1980 1981 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1982 self.clearGrid(); 1983 1984 if (self.choices.len > 1) { 1985 if (root_focus.grandchild_id) |id| { 1986 for (0..self.choices.len) |i| { 1987 if (id != self.selector(i).getFocus().id) continue; 1988 if (i != self.selected) try self.select(allocator, i); 1989 root_focus.setFocus(self.textInput().getFocus().id); 1990 break; 1991 } 1992 } 1993 1994 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 1995 for (0..self.choices.len) |i| { 1996 self.selector(i).options.border_style = if (i == self.selected) 1997 (if (focused) .double else .single) 1998 else 1999 .hidden; 2000 } 2001 } 2002 self.textInput().options.border_style = .single; 2003 self.textInput().cursor = 0; 2004 self.textInput().scroll_offset = 0; 2005 try self.box.build(allocator, constraint, root_focus); 2006 2007 if (!self.session.is_terminal) { 2008 const aa = self.session.arena.allocator(); 2009 const id = self.textInput().getFocus().id; 2010 try self.session.text_inputs.put(aa, id, self.textInput()); 2011 } 2012 } 2013 2014 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2015 switch (key) { 2016 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2017 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2018 .enter => if (self.session.is_terminal) self.show(), 2019 .mouse => |mouse| { 2020 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2021 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2022 try self.select(allocator, i); 2023 root_focus.setFocus(self.textInput().getFocus().id); 2024 return; 2025 }; 2026 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2027 }, 2028 else => {}, 2029 } 2030 } 2031 2032 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2033 self.selected = selected; 2034 const choice = self.choices[selected]; 2035 const text_input = self.textInput(); 2036 text_input.options.label = choice.label; 2037 text_input.options.bottom_label = choice.bottom_label; 2038 try text_input.setContent(allocator, choice.text); 2039 } 2040 2041 fn show(self: *CopyableText) void { 2042 const choice = self.choices[self.selected]; 2043 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2044 } 2045 2046 fn textInput(self: *CopyableText) *wgt.TextInput { 2047 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2048 } 2049 2050 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2051 return &self.box.children.values()[index].widget.text_box; 2052 } 2053 2054 fn maxWidth(choices: []const Choice) usize { 2055 var width: usize = 0; 2056 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2057 return width; 2058 } 2059 2060 pub fn clearGrid(self: *CopyableText) void { 2061 self.box.clearGrid(); 2062 } 2063 2064 pub fn getGrid(self: CopyableText) ?Grid { 2065 return self.box.getGrid(); 2066 } 2067 2068 pub fn getFocus(self: *CopyableText) *Focus { 2069 return self.box.getFocus(); 2070 } 2071 }; 2072