╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
336 sc.y += delta * 5; // magnify because it's a line count 337 self.scroll.clampToContent(); 338 self.focusVisible(root_focus, delta > 0); 339 } else { 340 const box = self.inner(); 341 const keys = box.children.keys(); 342 if (keys.len == 0) return; 343 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(box.children.getIndex(g) orelse 0) else 0; 344 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 345 root_focus.setFocus(keys[target]); 346 } 347 } 348 349 // jump to the first or last hunk. on the web the browser scrolls to the 350 // focused hunk; on the terminal pin the scroll to the top/bottom too. 351 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 352 const box = self.inner(); 353 const keys = box.children.keys(); 354 if (keys.len == 0) return; 355 if (self.session.is_terminal) { 356 const sc = &self.scroll; 357 sc.y = if (to_end) std.math.maxInt(isize) else 0; 358 self.scroll.clampToContent(); 359 } 360 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 361 } 362 363 // whether hunk `index` is at least partly within the diff viewport, per the 364 // last build's layout rects (content space) and the current scroll offset. 365 fn hunkVisible(self: *View, index: usize) bool { 366 const box = self.inner(); 367 const sc = &self.scroll; 368 const vp = sc.grid orelse return false; 369 const r = box.children.values()[index].rect orelse return false; 370 const top = sc.y; 371 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 372 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 373 } 374 375 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 376 // the bottom-most visible (for downward motion), else the top-most. 377 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 378 const box = self.inner(); 379 const sc = &self.scroll; 380 const vp = sc.grid orelse return; 381 const top = sc.y; 382 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 383 var chosen: ?usize = null; 384 for (box.children.keys(), box.children.values()) |id, *child| { 385 const r = child.rect orelse continue; // content-space layout rect 386 const r_top = r.y; 387 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 388 if (r_bot <= top or r_top >= bottom) continue; // not visible 389 chosen = id; 390 if (!prefer_last) break; // first visible 391 } 392 if (chosen) |id| root_focus.setFocus(id); 393 } 394 }; 395