╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
220 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 221 } 222 223 pub const View = struct { 224 scroll: wgt.Scroll(ui.Widget), 225 session: *ui.Session, 226 227 pub fn initEmpty(allocator: std.mem.Allocator, session: *ui.Session) !View { 228 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 229 errdefer box.deinit(allocator); 230 return .{ 231 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }), 232 .session = session, 233 }; 234 } 235 236 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 237 var view = try initEmpty(allocator, session); 238 errdefer view.deinit(allocator); 239 const box = view.inner(); 240 try data.appendWindow(allocator, session, box); 241 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 242 box.getFocus().child_id = box.children.keys()[0]; 243 return view; 244 } 245 246 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 247 self.scroll.deinit(allocator); 248 } 249 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 250 self.clearGrid(); 251 try self.scroll.build(allocator, constraint, root_focus); 252 } 253 pub fn clearGrid(self: *View) void { 254 self.scroll.clearGrid(); 255 } 256 pub fn getGrid(self: View) ?Grid { 257 return self.scroll.getGrid(); 258 } 259 pub fn getFocus(self: *View) *Focus { 260 return self.scroll.getFocus(); 261 } 262 pub fn atTop(self: *View) bool { 263 const box = self.inner(); 264 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 265 } 266 fn inner(self: *View) *wgt.Box(ui.Widget) { 267 return &self.scroll.child.box; 268 } 269 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 270 _ = allocator; 271 const sc = &self.scroll; 272 switch (key) { 273 // scroll horizontally within the diff 274 .arrow_left => if (!self.moveInRow(root_focus, false) and sc.x > 0) { 275 sc.x -= 1; 276 self.scroll.clampToContent(); 277 }, 278 .arrow_right => if (!self.moveInRow(root_focus, true)) { 279 sc.x += 1; 280 self.scroll.clampToContent(); 281 }, 282 .arrow_up => self.moveDiff(root_focus, -1), 283 .arrow_down => self.moveDiff(root_focus, 1), 284 .page_up => self.pageDiff(root_focus, -10), 285 .page_down => self.pageDiff(root_focus, 10), 286 .home => self.jumpDiff(root_focus, false), 287 .end => self.jumpDiff(root_focus, true), 288 // Enter / a click on a "previous"/"next" row follow its "a:" link in 289 // the host (it reloads the adjacent window), so they don't reach here. 290 .mouse => |mouse| switch (mouse.action) { 291 .scroll => |dir| self.moveDiff(root_focus, if (dir == .up) -1 else 1), 292 else => {}, 293 }, 294 else => {}, 295 } 296 } 297 298 // step across the selected row's boxes, false when the row has none that way. 299 pub fn moveInRow(self: *View, root_focus: *Focus, right: bool) bool { 300 const box = self.inner(); 301 const selected = box.getFocus().child_id orelse return false; 302 return switch ((box.children.getPtr(selected) orelse return false).widget) { 303 .box => |*row| ui.widget.moveInRow(row, root_focus, right), 304 else => false, 305 }; 306 } 307 308 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 309 // line count unless there is a new hunk visible (in which case it is a 310 // hunk count). on the web `delta` is always a hunk count. 311 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) void { 312 const box = self.inner(); 313 const keys = box.children.keys(); 314 if (keys.len == 0) return; 315 const cur: usize = if (box.getFocus().child_id) |c| (box.children.getIndex(c) orelse 0) else 0; 316 const target = @as(isize, @intCast(cur)) + delta; 317 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 318 319 if (self.session.is_terminal) { 320 if (in_range and self.hunkVisible(@intCast(target))) { 321 root_focus.setFocus(keys[@intCast(target)]); 322 return; 323 } 324 325 const sc = &self.scroll; 326 sc.y += delta * 5; // magnify because it's a line count 327 self.scroll.clampToContent(); 328 if (in_range and self.hunkVisible(@intCast(target))) { 329 root_focus.setFocus(keys[@intCast(target)]); 330 } 331 } else { 332 if (in_range) { 333 root_focus.setFocus(keys[@intCast(target)]); 334 } 335 } 336 } 337 338 // page a fixed number of lines, then focus the leading visible hunk 339 // (bottom-most when paging down, top-most when paging up). in the 340 // terminal, `delta` is a line count, and on the web it's a hunk count. 341 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) void { 342 if (self.session.is_terminal) { 343 const sc = &self.scroll; 344 sc.y += delta * 5; // magnify because it's a line count 345 self.scroll.clampToContent(); 346 self.focusVisible(root_focus, delta > 0); 347 } else { 348 const box = self.inner(); 349 const keys = box.children.keys(); 350 if (keys.len == 0) return; 351 const cur: isize = if (box.getFocus().child_id) |c| @intCast(box.children.getIndex(c) orelse 0) else 0; 352 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 353 root_focus.setFocus(keys[target]); 354 } 355 } 356 357 // jump to the first or last hunk. on the web the browser scrolls to the 358 // focused hunk; on the terminal pin the scroll to the top/bottom too. 359 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 360 const box = self.inner(); 361 const keys = box.children.keys(); 362 if (keys.len == 0) return; 363 if (self.session.is_terminal) { 364 const sc = &self.scroll; 365 sc.y = if (to_end) std.math.maxInt(isize) else 0; 366 self.scroll.clampToContent(); 367 } 368 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 369 } 370 371 // whether hunk `index` is at least partly within the diff viewport, per the 372 // last build's layout rects (content space) and the current scroll offset. 373 fn hunkVisible(self: *View, index: usize) bool { 374 const box = self.inner(); 375 const sc = &self.scroll; 376 const vp = sc.grid orelse return false; 377 const r = box.children.values()[index].rect orelse return false; 378 const top = sc.y; 379 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 380 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 381 } 382 383 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 384 // the bottom-most visible (for downward motion), else the top-most. 385 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 386 const box = self.inner(); 387 const sc = &self.scroll; 388 const vp = sc.grid orelse return; 389 const top = sc.y; 390 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 391 var chosen: ?usize = null; 392 for (box.children.keys(), box.children.values()) |id, *child| { 393 const r = child.rect orelse continue; // content-space layout rect 394 const r_top = r.y; 395 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 396 if (r_bot <= top or r_top >= bottom) continue; // not visible 397 chosen = id; 398 if (!prefer_last) break; // first visible 399 } 400 if (chosen) |id| root_focus.setFocus(id); 401 } 402 }; 403