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