diff options
Diffstat (limited to 'src/layout.zig')
| -rw-r--r-- | src/layout.zig | 94 |
1 files changed, 88 insertions, 6 deletions
diff --git a/src/layout.zig b/src/layout.zig index a2211dfd..6345c91a 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -27,8 +27,19 @@ pub const MovePlacement = struct { above_h: u16, }; +pub const Dir = enum { left, right, up, down }; + /// Move the keyboard to the nearest pane in `dir`; false when there is none. -pub fn focusDir(p: *Pardes, from: usize, dir: enum { left, right, up, down }) bool { +pub fn focusDir(p: *Pardes, from: usize, dir: Dir) bool { + const b = nearest(p, from, dir) orelse return false; + p.active = b; + // a count typed before the hop was meant for the pane you left + p.panes[b].?.body.normal.clear(); + return true; +} + +/// The pane next to `from` in `dir`, by the screen. +pub fn nearest(p: *const Pardes, from: usize, dir: Dir) ?usize { const a = p.rects[from]; var best: ?usize = null; var best_d: i32 = 0; @@ -55,11 +66,60 @@ pub fn focusDir(p: *Pardes, from: usize, dir: enum { left, right, up, down }) bo best_d = d; } } - const b = best orelse return false; - p.active = b; - // a count typed before the hop was meant for the pane you left - p.panes[b].?.body.normal.clear(); - return true; + return best; +} + +/// Window mode's labels, one a pane in reading order (columns left to +/// right, each top to bottom); h, j, k and l move instead. ponytail: a +/// pane past the 22nd has no label and is reached by moving. +pub const window_labels = "asdfgqwertyuiopzxcvbnm"; + +/// The letter window mode shows on pane `id`. +pub fn windowLabel(p: *const Pardes, id: usize) ?u8 { + var n: usize = 0; + for (0..p.ncol) |c| for (p.col_panes[c][0..p.col_n[c]]) |pane| { + if (n == window_labels.len) return null; + if (pane == id) return window_labels[n]; + n += 1; + }; + return null; +} + +/// Window mode (SPC w, Ctrl-w): h/j/k/l (or the arrows) move an outline +/// from pane to pane and nothing else gets the keys; Enter or Esc focuses +/// the outlined pane, `:` its tag, and a pane's label letter that one. Up past a +/// column's top pane goes on to its tag, as the Up word does. Any other +/// key changes nothing. +pub fn windowKey(p: *Pardes, key: pardes.Key) void { + const at = p.window_pick orelse return; + if (p.panes[at] == null) { + p.window_pick = null; + return; + } + if (key.ctrl or key.alt) return; + for (config.window_keys) |wk| if (pardes.hit(key, &.{ wk.letter, wk.arrow })) { + const dir: Dir = switch (wk.cmd) { + .Left => .left, + .Down => .down, + .Up => .up, + else => .right, + }; + if (nearest(p, at, dir)) |next| { + p.window_pick = next; + } else if (dir == .up) { + p.window_pick = null; + p.active = at; + pardes.exec.runBuiltin(p, .Up, at, "", null); + } + return; + }; + const chosen = if (key.cp == pardes.Key.enter or key.cp == pardes.Key.escape or key.cp == ':') at else for (0..p.panes.len) |id| { + if (p.panes[id] != null and key.cp < 128 and windowLabel(p, id) == @as(u8, @intCast(key.cp))) break id; + } else return; + p.window_pick = null; + p.active = chosen; + p.panes[chosen].?.body.normal.clear(); + if (key.cp == ':') tagline.enterTag(p, p.panes[chosen].?); } /// Column `c`'s serial, handed out the first time it is asked for. @@ -266,6 +326,28 @@ pub fn setColumnWeights(p: *Pardes, col: usize, heights: *const [MAX_PANES]u16) } } +/// Give a newly opened report enough rows to read, borrowing only space +/// above the other panes' usable minima. Keep their tags and bodies visible. +pub fn givePaneRows(p: *Pardes, id: usize, wanted: u16, context: usize) void { + const at = findPane(p, id) orelse return; + compute(p); + var heights: [MAX_PANES]u16 = @splat(0); + for (p.col_panes[at.col][0..p.col_n[at.col]]) |other| heights[other] = p.rects[other].h; + var missing = wanted -| heights[id]; + for (p.col_panes[at.col][0..p.col_n[at.col]]) |other| { + if (other == id or p.panes[other].?.collapsed) continue; + const body_rows: u16 = if (other == context) 6 else 3; + const minimum = @max(minRows(p, other), p.panes[other].?.tag_rows + body_rows); + const take = @min(missing, heights[other] -| minimum); + heights[other] -= take; + heights[id] += take; + missing -= take; + if (missing == 0) break; + } + setColumnWeights(p, at.col, &heights); + p.sync(); +} + pub fn applyRowSplit(p: *Pardes, cc: usize, k: usize, cur_y: u16) void { if (k + 1 >= p.col_n[cc]) return; const a = p.panes[p.col_panes[cc][k]] orelse return; |
