╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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