╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
338 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 339 // line count unless there is a new hunk visible (in which case it is a 340 // hunk count). on the web `delta` is always a hunk count. 341 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) void { 342 const box = self.inner(); 343 const keys = box.children.keys(); 344 if (keys.len == 0) return; 345 const cur: usize = if (box.getFocus().child_id) |c| (box.children.getIndex(c) orelse 0) else 0; 346 const target = @as(isize, @intCast(cur)) + delta; 347 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 348 349 if (self.session.is_terminal) { 350 if (in_range and self.hunkVisible(@intCast(target))) { 351 root_focus.setFocus(keys[@intCast(target)]); 352 return; 353 } 354 355 const sc = &self.scroll; 356 sc.y += delta * 5; // magnify because it's a line count 357 self.scroll.clampToContent(); 358 if (in_range and self.hunkVisible(@intCast(target))) { 359 root_focus.setFocus(keys[@intCast(target)]); 360 } 361 } else { 362 if (in_range) { 363 root_focus.setFocus(keys[@intCast(target)]); 364 } 365 } 366 } 367 368 // page a fixed number of lines, then focus the leading visible hunk 369 // (bottom-most when paging down, top-most when paging up). in the 370 // terminal, `delta` is a line count, and on the web it's a hunk count. 371 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) void { 372 if (self.session.is_terminal) { 373 const sc = &self.scroll; 374 sc.y += delta * 5; // magnify because it's a line count 375 self.scroll.clampToContent(); 376 self.focusVisible(root_focus, delta > 0); 377 } else { 378 const box = self.inner(); 379 const keys = box.children.keys(); 380 if (keys.len == 0) return; 381 const cur: isize = if (box.getFocus().child_id) |c| @intCast(box.children.getIndex(c) orelse 0) else 0; 382 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 383 root_focus.setFocus(keys[target]); 384 } 385 } 386 387 // jump to the first or last hunk. on the web the browser scrolls to the 388 // focused hunk; on the terminal pin the scroll to the top/bottom too. 389 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 390 const box = self.inner(); 391 const keys = box.children.keys(); 392 if (keys.len == 0) return; 393 if (self.session.is_terminal) { 394 const sc = &self.scroll; 395 sc.y = if (to_end) std.math.maxInt(isize) else 0; 396 self.scroll.clampToContent(); 397 } 398 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 399 } 400 401 // whether hunk `index` is at least partly within the diff viewport, per the 402 // last build's layout rects (content space) and the current scroll offset. 403 fn hunkVisible(self: *View, index: usize) bool { 404 const box = self.inner(); 405 const sc = &self.scroll; 406 const vp = sc.grid orelse return false; 407 const r = box.children.values()[index].rect orelse return false; 408 const top = sc.y; 409 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 410 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 411 } 412 413 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 414 // the bottom-most visible (for downward motion), else the top-most. 415 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 416 const box = self.inner(); 417 const sc = &self.scroll; 418 const vp = sc.grid orelse return; 419 const top = sc.y; 420 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 421 var chosen: ?usize = null; 422 for (box.children.keys(), box.children.values()) |id, *child| { 423 const r = child.rect orelse continue; // content-space layout rect 424 const r_top = r.y; 425 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 426 if (r_bot <= top or r_top >= bottom) continue; // not visible 427 chosen = id; 428 if (!prefer_last) break; // first visible 429 } 430 if (chosen) |id| root_focus.setFocus(id); 431 } 432 }; 433