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