╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
291 } 292 pub fn atTop(self: *View) bool { 293 const box = self.inner(); 294 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 295 } 296 fn inner(self: *View) *wgt.Box(ui.Widget) { 297 return &self.scroll.child.box; 298 } 299 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 300 _ = allocator; 301 const sc = &self.scroll; 302 switch (key) { 303 // scroll horizontally within the diff 304 .arrow_left => if (!self.moveInRow(root_focus, false) and sc.x > 0) { 305 sc.x -= 1; 306 self.scroll.clampToContent(); 307 }, 308 .arrow_right => if (!self.moveInRow(root_focus, true)) { 309 sc.x += 1; 310 self.scroll.clampToContent(); 311 }, 312 .arrow_up => self.moveDiff(root_focus, -1), 313 .arrow_down => self.moveDiff(root_focus, 1), 314 .page_up => self.pageDiff(root_focus, -10), 315 .page_down => self.pageDiff(root_focus, 10), 316 .home => self.jumpDiff(root_focus, false), 317 .end => self.jumpDiff(root_focus, true), 318 // Enter / a click on a "previous"/"next" row follow its "a:" link in 319 // the host (it reloads the adjacent window), so they don't reach here. 320 .mouse => |mouse| switch (mouse.action) { 321 .scroll => |dir| self.moveDiff(root_focus, if (dir == .up) -1 else 1), 322 else => {}, 323 }, 324 else => {}, 325 } 326 } 327 328 // step across the selected row's boxes, false when the row has none that way. 329 pub fn moveInRow(self: *View, root_focus: *Focus, right: bool) bool { 330 const box = self.inner(); 331 const selected = box.getFocus().child_id orelse return false; 332 return switch ((box.children.getPtr(selected) orelse return false).widget) { 333 .box => |*row| ui.widget.moveInRow(row, root_focus, right), 334 else => false, 335 }; 336 } 337 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