From e9991ca68067ee835af43868a193e8e6bd5dd420 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Thu, 26 Nov 2026 11:37:09 -0300 Subject: mouse, TagState, keys, draw, Pick, exec, panes, Trail: the mouse never ends a picker or a prompt as a side effect: a click elsewhere leaves a state with a field waiting for the keyboard, the wheel scrolls the results, B1 on a row moves the cursor and B3 lands it, and only an act on the panes the picker lives in ends it first (UX DESIGN O2, O3, binding; the user: "Using the mouse just closes the picker? That is extremely bad the mouse shoudn't do implicit actions like this and I should be able to at least scroll on the resuls (doing a right click Look should also work though)"); THE WAY IT WAS: handleMouse's deferred check ended any state whose host was not `v.active` after any event (motion and the wheel too: from a held pane, whose picker the made pane hosted, a bare pointer motion ended it, UX7's t5), and W5's hostTagAt ended a state on any press on its tag; bandPress returned for the wheel, and its B3 landed through TagState.point and landHere whatever step the row was in (an act's row set the list's cursor; a theme's row rolled the picker up and wore nothing); THE CHANGE: TagState.waits, one bool (in the view, so in the dump and the digest), cleared by toRest: an event that moves the keyboard off the host, or B1 anywhere off the head and the band (the host's own tag, its body under the band, a header, a grip), leaves a state with a field (Help, a picker, a text prompt, `:`, rename) up and waiting, and ends a level, the registers or describe-key first, as Esc does (they hold no typed text; W5's hostTagAt branch deleted); while it waits, keys go where the keyboard is (keys.handleKey) and Esc ends it first, the pane's own Esc ladder from the next; B1 on its head or band, or a key that moves the keyboard to its pane (`C-w … ⏎`, `SPC w k`), gives it back; the waiting box shows its pane's mode and the field no caret (draw.zig: `typing` and the head's caret, behaviour only); TagState.pending echoes keys typed meanwhile on the notice row; bandPress: the wheel moves a picker's cursor by `wheel_rows` (Pick.wheel; the band's eleven and the preview follow as for a key) and Help's; B1 on a row is Pick.press (the list's cursor, or the act step's), B3 Enter there (a row landed, Shift-B3 below as S-Enter, an act applied, a source switched, a theme worn, an action applied); B2 and B3 on the head's words and B2 on a row's run with a picker or prompt up; B2 or B3 on the text a picker previews lands it first (Pick.previewsAt, landHere), then acts in the landed file; O3's guard, Pick.yieldFor: the live picker, waiting or not, ends as Esc does when Get, Undo, Redo, Save (acme's Put), Del or Keep is run on the pane it asks from or previews in (exec.runBuiltinFrom), when that pane is removed (panes.removePane) or a walk step replaces its file (Trail.show: a trail word, a Look landing there); anything else (Wrap, Snarf, a Look opening elsewhere) leaves it up; RUN (TEST FIRST): four mouse.zig tests, each failing on the change before (its `waits` checks taken out to compile there) and passing now: "the mouse never ends a picker as a side effect ..." (motion and the wheel over the preview and the window row, the wheel over the rows ±1 and the preview following, B1 on the third row and Up/Down in the field, B3 on the preview's text landing three.zig in the held pane's reader, Shift-B3 on a row opening a pane below: fails at mouse.zig:2121, the wheel moved no cursor, 0 for 1), "B1 in another pane: a picker waits ..." (B1 on the preview: waiting, the held box its mode, no caret in the head, `j` the reader's; B1 on the head back, `w` in the field; `C-w k ⏎` back; B1 under the band, `i`, Esc ends the picker with insert mode kept, the next Esc normal: fails at mouse.zig:2179, the picker ended), "B1 on the asking pane's own tag: the picker waits ...; B2 Del ends it first ... and closes the pane" (B2 Wrap leaves it up; B2 Del closes the held pane and the reader made for it: fails at mouse.zig:2238, the picker ended), "the act step and the rows under the mouse ..." (B2 Wrap in a row's text, B1 on `y Copy` sets the act step's cursor to 6, B2 on it copies `…one.zig:2`, the picker up: fails at mouse.zig:2286, act_at 0 for 6); TagState's W5 test, renamed, now asks Help to stay up after B2 Wrap in its tag and to wait after a click in the other pane, and an `s` prompt to wait the same; a user hit it with every click while a picker was up, and with any pointer motion from a held pane; GATES (Debug tui): core-test 1172/1346 (174 skipped: + 4; one program's 7 cached), shell-test 1387/1390 (3 skipped: + 4); the dump changes (TagState.waits): the dump fuzz and the soak run on the last change of this line; load 1.8 to 4.2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE --- core/Pick.zig | 58 ++++++++- core/TagState.zig | 36 +++++- core/Trail.zig | 3 + core/draw.zig | 7 +- core/exec.zig | 7 ++ core/keys.zig | 16 ++- core/mouse.zig | 349 +++++++++++++++++++++++++++++++++++++++++++++--------- core/panes.zig | 3 + 8 files changed, 408 insertions(+), 71 deletions(-) (limited to 'core') diff --git a/core/Pick.zig b/core/Pick.zig index 10f78208..7bceab39 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -1942,13 +1942,69 @@ pub fn keyAtRest(p: *Pardes, k: Key) void { p.resume_at = 0; } -/// B3 on a picker's row (mouse.zig bandPress): Enter on it. +/// Enter on the live picker's current row: B3 on its text where it +/// previews (mouse.zig, UX DESIGN O3). pub fn landHere(p: *Pardes) void { const pk = live(p) orelse return; if (pk.source == .lists) return reopenListed(p, pk); if (pk.spine == .rows) land(p, .walk); } +/// A press on row `row` of the live picker's band (mouse.zig bandPress, UX +/// DESIGN O2): B1 (`enter` false) puts the cursor on it, the list's or the +/// act step's; B3 does what Enter does there (`below`: Shift-B3, as +/// S-Enter): the row landed, the act applied, the source switched to, the +/// theme worn, the action applied. A row of the act step or the source +/// spine is its key's, its first byte (actStep, steps). +pub fn press(p: *Pardes, row: Band.Row, enter: bool, below: bool) void { + const pk = live(p) orelse return; + const s = &p.view().tag_state; + if ((pk.spine == .acts or pk.spine == .sources) and row.key > 0) { + if (enter) return key(p, .{ .cp = row.text[0] }); + if (pk.spine == .acts) pk.act_at = @intCast(row.index); + return; + } + if (pk.spine != .rows or row.index >= count(p, pk)) return; + s.rows.at = row.index; + if (!enter) return; + if (pk.source == .lists) return reopenListed(p, pk); + if (pk.source == .themes or pk.source == .actions) return key(p, .{ .cp = Key.enter }); + land(p, if (below) .below else .walk); +} + +/// The wheel over the live picker's head or band (UX DESIGN O2): its +/// cursor `delta` rows on, the band's eleven following it and the preview +/// with them, as the keys move it. +pub fn wheel(p: *Pardes, delta: i32) void { + const pk = live(p) orelse return; + const s = &p.view().tag_state; + const n = @min(count(p, pk), std.math.maxInt(u16)); + if (n == 0 or pk.spine != .rows) return; + s.rows.at = @intCast(std.math.clamp(@as(i64, s.rows.at) + delta, 0, @as(i64, @intCast(n)) - 1)); + if (fuzzy(pk)) pk.names.reachTo(@as(usize, s.rows.at) + Band.Cursor.shown); +} + +/// Whether screen row `row` of pane `id` is text the live picker previews: +/// its landing pane's body, a place shown in it (UX DESIGN O3). B2 and B3 +/// there land it first: acting on the file is taking it. +pub fn previewsAt(p: *const Pardes, id: usize, row: u16) bool { + const pk = live(p) orelse return false; + const pane = p.panes[id] orelse return false; + if (pane.serial != pk.landing or !pk.previewed or pk.source == .themes or pk.source == .actions) return false; + return row >= p.bodyTop(pane, p.view().rects[id]); +} + +/// The live picker, waiting or not, ended as Esc ends it when what is about +/// to run changes or closes pane `id`, the pane it asks from or previews in +/// (UX DESIGN O3: Del, Get, Undo, Put, Keep, a walk step there, a Look that +/// lands there, the pane closed): the preview's own text back, a pane made +/// for it gone with it. Anything else leaves it up. +pub fn yieldFor(p: *Pardes, id: usize) void { + const pk = live(p) orelse return; + const pane = p.panes[id] orelse return; + if (pane.serial == pk.owner or pane.serial == pk.landing) TagState.toRest(p); +} + /// Enter in Lists (UX L'3): the kept picker under its cursor unrolled again /// as it was left, where Lists was asked from, and the newest now. fn reopenListed(p: *Pardes, pk: *Pick) void { diff --git a/core/TagState.zig b/core/TagState.zig index 11c3458b..622cb949 100644 --- a/core/TagState.zig +++ b/core/TagState.zig @@ -81,6 +81,14 @@ miss: u8 = 0, /// The leaf each group ran last, by its key (bindings.groups): the cursor /// starts there and the rolled level underlines it (SPEC §4.3). used: [bindings.ngroups]u8 = @splat(0), +/// A state with a field (Help, a picker, a text prompt, `:`, rename) whose +/// pane lost the keyboard to a click elsewhere (UX DESIGN O2): unrolled as +/// it was, its field drawn with no caret and its pane's box saying its +/// mode; keys go where the keyboard is, Esc ends it first, and a click on +/// its head or band, or a key that moves the keyboard back to its pane, +/// gives it the keyboard again. A level, the registers and describe-key, +/// which hold no typed text, end on such a click instead. +waits: bool = false, /// `.full` while the whole window previews a theme (a themes picker's /// row, `:theme `'s): its own again at rest. No pane dims out of rest /// (UX D9), so no pane's own bit is read. @@ -234,6 +242,7 @@ pub fn toRest(p: *Pardes) void { const s = &v.tag_state; if (s.kind == .prompt) Prompt.leave(p); s.kind = .rest; + s.waits = false; // The whole window previewed `:theme `'s row (Command.preview): its // own theme again (a themes picker's went as it rolled up). if (s.previewing.eql(.full)) @import("colors.zig").previewTheme(p, null); @@ -386,9 +395,10 @@ pub fn asking(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?Head { /// The keys of the root current in view `v` while its level waits for its /// delay (UX D12: the tag is at rest until then), as the notice row's right -/// end echoes them at once: `SPC f`. +/// end echoes them at once: `SPC f`; all along while a state waits for the +/// keyboard (`waits`), whose head and band stay where the level would show. pub fn pending(p: *const Pardes, v: *const View, buf: []u8) ?[]const u8 { - if (v.tag_state.kind != .rest or shown(p, v)) return null; + if (if (v.tag_state.kind == .rest) shown(p, v) else !v.tag_state.waits) return null; const cur = current(p, v) orelse return null; return bindings.chord(cur.root, cur.path, buf); } @@ -1389,7 +1399,7 @@ test "a dump's tag state that indexes past what it names is at rest when loaded, try std.testing.expect(s.kind == .rest); } -test "a click that gives the keyboard to another pane ends a level as Esc does; one in the level's own tag ends it too, then does what it does there at rest: B1 gives the tag the keyboard, B2 runs the word (UX DESIGN M3)" { +test "a click that gives the keyboard to another pane ends a level as Esc does; one in the level's own tag ends it too, then does what it does there at rest; Help and a text prompt, which hold a field, wait instead, and a word run in their pane's tag leaves them up (UX DESIGN M3, O2)" { const config = @import("config.zig"); const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); defer p.deinit(); @@ -1422,15 +1432,17 @@ test "a click that gives the keyboard to another pane ends a level as Esc does; click(p, word, rl.y); try std.testing.expect(hostOf(p) == null and p.view().prefix == .none); try std.testing.expectEqual(Pane.Focus.tag, p.panes[low].?.focus); - // Help, a typing state: B2 on the word ends it, then runs the word. + // Help, which holds a field: B2 on the word runs it, Help up. p.panes[low].?.focus = .body; for (" ?") |c| p.update(.{ .key = .{ .cp = c } }); try std.testing.expect(p.view().tag_state.kind == .help and hostOf(p) == low); const wrapped = p.settings.wrap; p.update(.{ .mouse = .{ .button = .middle, .kind = .press, .col = word, .row = rl.y } }); p.update(.{ .mouse = .{ .button = .middle, .kind = .release, .col = word, .row = rl.y } }); - try std.testing.expect(p.view().tag_state.kind == .rest and hostOf(p) == null); + try std.testing.expect(p.view().tag_state.kind == .help and hostOf(p) == low); try std.testing.expect(p.settings.wrap != wrapped); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(p.view().tag_state.kind == .rest); // SPC's level again, for the clicks elsewhere. p.update(.{ .key = .{ .cp = ' ' } }); p.advance(p.clock.now_ns); @@ -1453,7 +1465,19 @@ test "a click that gives the keyboard to another pane ends a level as Esc does; for (" ?") |c| p.update(.{ .key = .{ .cp = c } }); try std.testing.expect(p.view().tag_state.kind == .help); click(p, rt.x + config.GUTTER + 1, rt.y + pardes.BOX_H); - try std.testing.expect(p.view().tag_state.kind == .rest and hostOf(p) == null); + try std.testing.expect(p.view().tag_state.kind == .help and p.view().tag_state.waits and hostOf(p) == low); + try std.testing.expectEqual(top, p.view().active); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(p.view().tag_state.kind == .rest and !p.view().tag_state.waits); + // A text prompt, `s` in the lower pane's text: it waits the same. + p.view().active = low; + p.update(.{ .key = .{ .cp = '%', .text = "%" } }); + p.update(.{ .key = .{ .cp = 's', .text = "s" } }); + try std.testing.expect(p.view().tag_state.kind == .prompt and hostOf(p) == low); + click(p, rt.x + config.GUTTER + 1, rt.y + pardes.BOX_H); + try std.testing.expect(p.view().tag_state.kind == .prompt and p.view().tag_state.waits and hostOf(p) == low); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(p.view().tag_state.kind == .rest); } test "a rolled level that wraps has its hints on its last row at every width that leaves it a row to spare: `]` and `g` as SPC (UX D3)" { diff --git a/core/Trail.zig b/core/Trail.zig index f6cfec9a..6bed619b 100644 --- a/core/Trail.zig +++ b/core/Trail.zig @@ -242,6 +242,9 @@ pub fn replaceStep(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { /// pane is held, as Keep holds it, and the step opens where a held pane's /// go, in its reader. fn show(p: *Pardes, id: usize, path: []const u8, at: Spot) !usize { + // A step in the pane a picker asks from or previews in ends the picker + // first, as Esc does (UX DESIGN O3): the step is from its own text. + pardes.Pick.yieldFor(p, id); const pane = p.panes[id].?; if (pane.file) |f| if (std.mem.eql(u8, f.path, path)) { look.clearNavigationSelection(p, pane); diff --git a/core/draw.zig b/core/draw.zig index 3caf27ca..a94c8e60 100644 --- a/core/draw.zig +++ b/core/draw.zig @@ -521,8 +521,9 @@ fn renderPane(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: // In window mode the box holds the pane's letter instead of its mode. const label: ?u8 = if (p.view().window_pick != null) layout.windowLabel(p, id) else null; // A tag in a typing state is typed in: its box says so (walk UI SPEC - // §3.2), whatever the body's mode. - const typing = p.view().tag_state.kind != .rest and TagState.hostOf(p) == id; + // §3.2), whatever the body's mode; not while it waits for the keyboard + // (UX DESIGN O2). + const typing = p.view().tag_state.kind != .rest and !p.view().tag_state.waits and TagState.hostOf(p) == id; s.set(r.x, tag_y, if (label) |*letter| @as(*const [1]u8, letter) else if (typing) config.tag_insert else switch (pane.body.mode) { .normal => config.tag_normal, .insert => config.tag_insert, @@ -1065,7 +1066,7 @@ fn joinBand(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: R for (0..rows) |y| joinCells(s, r.x + TAG_TEXT_INSET, y0 + @as(u16, @intCast(y)), storage[y * columns ..][0..columns], 0, r.w - TAG_TEXT_INSET, .{ .rgb = fg }); // A head typed in: its caret a bar at the field's, the keyboard's // where the pane has it. - if (head.caret) |c| if (active) { + if (head.caret) |c| if (active and !p.view().tag_state.waits) { const at = modal.positionAt(shown.text, shown.fromReal(c)); if (at.row < drawn.head) { const col = panes.File.rawDisplayCol(tab, lines[at.row], @min(at.col, lines[at.row].len)); diff --git a/core/exec.zig b/core/exec.zig index 44cfea64..a367ea94 100644 --- a/core/exec.zig +++ b/core/exec.zig @@ -979,6 +979,13 @@ pub fn runBuiltinFrom(p: *Pardes, b: Builtin, id: usize, txt: []const u8, arg: ? var said: pardes.Kind.Buf(96) = .{}; return p.reportFailure(id, said.print("{s} is GUI-only, invalid here", .{@tagName(b)})); } + // A word that changes or closes the pane a picker asks from or + // previews in ends the picker first, as Esc does (UX DESIGN O3); then + // it runs as at rest (on nothing, the pane made for the picker gone). + switch (b) { + .Get, .Undo, .Redo, .Save, .Del, .Keep => pardes.Pick.yieldFor(p, id), + else => {}, + } // No pane at all (the window Delcol emptied): a word runs all the same, // with a stand-in that is no pane (New, Newcol, Exit are what make sense). const pane = p.panes[id] orelse (if (builtins.registry.scope(b) == .session or panes.anyPane(p) == null) panes.standIn(p) orelse return else return); diff --git a/core/keys.zig b/core/keys.zig index 05349d59..61ebaab9 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -197,6 +197,13 @@ pub fn handleKey(p: *Pardes, key: Key) void { // A root this key strikes starts its which-key delay; one it ends // takes its level away (TagState). defer TagState.follow(p); + // A key that moves the keyboard to the pane a waiting state hangs + // under (`C-w`'s pick, `SPC w k`) gives the state the keyboard back (UX + // DESIGN O2). + const keyboard = p.view().active; + defer if (p.view().tag_state.waits and p.view().window_pick == null and p.view().active != keyboard and TagState.hostOf(p) == p.view().active) { + p.view().tag_state.waits = false; + }; // A double-click is two clicks with nothing between them. p.view().last_click = null; if (p.macro.recording != 0 and p.macro.replaying == 0) Macro.append(&p.macro.recorded, p.gpa, key) catch |err| p.reportError(.at(p.view().active), "macro", err); @@ -229,8 +236,13 @@ pub fn handleKey(p: *Pardes, key: Key) void { return; } p.view().answering = 0; - // A tag in a typing state takes every key (TagState: Help, describe-key). - if (p.view().tag_state.kind != .rest) return tagStateKey(p, key); + // A tag in a typing state takes every key (TagState: Help, describe-key); + // one waiting for the keyboard (UX DESIGN O2) only Esc, which ends it + // first: the others go where the keyboard is. + if (p.view().tag_state.kind != .rest) { + if (!p.view().tag_state.waits) return tagStateKey(p, key); + if (hit(key, config.escape)) return TagState.toRest(p); + } Prompt.keyAtRest(p, key); @import("Pick.zig").keyAtRest(p, key); // Esc takes a hover's ghost lines away (SPEC §3.5) before anything else diff --git a/core/mouse.zig b/core/mouse.zig index 2e55ba95..dc730923 100644 --- a/core/mouse.zig +++ b/core/mouse.zig @@ -503,16 +503,22 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void { pub fn handleMouse(p: *Pardes, event: Mouse) void { const v = p.view(); - // A click that gives the keyboard to another pane ends the level or - // typing state of the tag out of rest as Esc does, its text's prefix - // with it (SPEC §3.4.6: only the pane with the keyboard leaves rest); - // one in its band or its own tag keeps it. + // The mouse never ends a state as a side effect (UX DESIGN O2): an + // event that gives the keyboard to another pane leaves a state with a + // field (Help, a picker, a text prompt, `:`, rename) up, waiting for it + // (TagState.waits); a level, the registers or describe-key, which hold + // no typed text, end as Esc ends them, the text's prefix with them + // (SPEC §3.4.6). The pointer's motion and the wheel move no keyboard. const host = pardes.TagState.hostOf(p); defer if (host) |was| if (v.active != was) { - const to = v.active; - v.active = was; - pardes.TagState.toRest(p); - v.active = to; + if (v.tag_state.kind != .rest and v.tag_state.kind != .describe) { + v.tag_state.waits = true; + } else { + const to = v.active; + v.active = was; + pardes.TagState.toRest(p); + v.active = to; + } }; var m = event; // A click goes somewhere else: the question it leaves unanswered is @@ -611,6 +617,13 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { return; } + // B1 off the head and the band gives the keyboard where it gives it at + // rest (a tag's text, a body, a header): a state with a field waits for + // it; a level, the registers and describe-key end first (UX DESIGN O2). + // B1 on them gives it back (bandPress). + if (m.kind == .press and m.button == config.select_button and host != null and bandPoint(p, mcol, mrow) == null) { + if (v.tag_state.kind != .rest and v.tag_state.kind != .describe) v.tag_state.waits = true else pardes.TagState.toRest(p); + } const hovered = paneAt(p, mcol, mrow); // any press that is NOT the execute+select chord abandons a pending @@ -726,16 +739,14 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { if (m.kind == .press) { tagline.exitHeader(p); } - // A press on a tag out of rest ends its state as Esc does, the bodies - // back, then does what a press there does at rest: the tag is drawn as - // at rest, and its words act as they do then (UX DESIGN M3). A pane made - // for a picker goes with it, and the press with it: it was the Esc. - if (m.kind == .press) if (hostTagAt(p, mcol, mrow)) |id| { - const serial = p.panes[id].?.serial; - pardes.TagState.toRest(p); - if (p.paneBySerial(serial) == null) return; + if (m.kind == .press) if (bandPoint(p, mcol, mrow)) |point| return bandPress(p, m, point); + // B2 or B3 on the text a picker previews lands it first, as Enter + // does: acting on that file is taking it (UX DESIGN O3); the press then + // does what it does at rest, in the file landed. + if (m.kind == .press and (m.button == config.exec_button or m.button == config.look_button)) if (hovered) |id| if (pardes.Pick.previewsAt(p, id, mrow)) { + pardes.Pick.landHere(p); + p.layOut(); }; - if (m.kind == .press) if (bandPoint(p, mcol, mrow)) |point| return bandPress(p, m.button, point); switch (m.button) { .none, .back, .forward => {}, // hover and navigation handled above @@ -929,18 +940,6 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void { } } -/// The pane out of rest whose tag (its rows at rest, Band.covered) a press -/// is on: a window's layer hit, or a grid cell. -fn hostTagAt(p: *Pardes, mcol: u16, mrow: u16) ?usize { - const v = p.view(); - const id = pardes.TagState.hostOf(p) orelse return null; - if (v.pointer_tag_hit) |hit| return if (hit.kind == .pane and hit.id == id) id else null; - if (paneAt(p, mcol, mrow) != id) return null; - const pane = p.panes[id].?; - const top = p.tagTop(pane, v.rects[id]); - return if (mrow >= top and mrow < top + pardes.Band.covered(p, pane, id).tag) id else null; -} - /// A head's row (`head`) or a band's under it, drawn over the body: a /// covered row belongs to them, never to the body under it (UX DESIGN I1, /// M2). @@ -968,12 +967,30 @@ fn bandPoint(p: *Pardes, mcol: u16, mrow: u16) ?BandPoint { return .{ .id = id, .line = mrow - top - at.head, .col = col }; } -/// A press on a band (Band.zig), read-only text: the select button puts -/// the rows' cursor on the row it is on; execute runs the word under it -/// and look looks at it, as on a tag, the tag back at rest first. -fn bandPress(p: *Pardes, button: Mouse.Button, point: BandPoint) void { +/// A press on a band (Band.zig), read-only text, and the wheel over it +/// (UX DESIGN O2). The select button gives a state waiting for the +/// keyboard its keyboard back (TagState.waits), puts the field's caret +/// where it is in the head's field, and the rows' cursor on the row it is +/// on. Execute runs the word under it and look looks at it, as on a tag: a +/// level back at rest first, a state with a field kept up (what the word +/// does may end it: Pick.yieldFor); B3 on a picker's row is Enter there, +/// Shift-B3 S-Enter. The wheel moves a picker's cursor or Help's. +fn bandPress(p: *Pardes, m: Mouse, point: BandPoint) void { const v = p.view(); const arena = p.scratch.allocator(); + const s = &v.tag_state; + const field = s.kind != .rest and s.kind != .describe; + if (m.button == .wheel_up or m.button == .wheel_down) { + const delta = if (m.button == .wheel_up) -config.wheel_rows else config.wheel_rows; + if (s.kind == .pick) pardes.Pick.wheel(p, delta); + if (s.kind == .help and s.nhits > 0) s.rows.at = @intCast(std.math.clamp(@as(i32, s.rows.at) + delta, 0, @as(i32, s.nhits) - 1)); + return; + } + if (m.button == config.select_button and s.waits) { + s.waits = false; + v.active = point.id; + p.panes[point.id].?.focus = .body; + } // The head's rows: the select button in its field puts the field's // caret there (UX DESIGN M6); its words run and are looked at as a // tag's. @@ -982,47 +999,41 @@ fn bandPress(p: *Pardes, button: Mouse.Button, point: BandPoint) void { const shown = tagline.wrapTag(p.settings.tab, arena, h.text, tagline.tagWidth(p, point.id)) catch return; const text = modal.lineSlice(shown.text, point.line); const raw = panes.File.rawAtDisplay(p.settings.tab, text, point.col); - if (button == config.select_button) { + if (m.button == config.select_button) { const caret = h.caret orelse return; - const field = &v.tag_state.field; const at = shown.toReal(modal.lineStartOffset(shown.text, point.line) + modal.graphemeStart(text, raw)); - const start = caret - field.caret; - if (at >= start and at <= start + field.len) field.caret = @intCast(at - start); + const start = caret - s.field.caret; + if (at >= start and at <= start + s.field.len) s.field.caret = @intCast(at - start); return; } - if (button != config.exec_button and button != config.look_button) return; + if (m.button != config.exec_button and m.button != config.look_button) return; const b = tagline.wordBoundsAtCol(text, raw) orelse return; if (b.lo == b.hi) return; const word = text[b.lo..b.hi]; - if (v.tag_state.kind == .pick and button == config.exec_button) return exec.runBuiltin(p, config.exec_cmd, v.active, "", word); - pardes.TagState.toRest(p); - return exec.runBuiltin(p, if (button == config.exec_button) config.exec_cmd else config.look_cmd, v.active, "", word); + if (!field) pardes.TagState.toRest(p); + return exec.runBuiltin(p, if (m.button == config.exec_button) config.exec_cmd else config.look_cmd, point.id, "", word); } - if (button == config.select_button) { - const band = (pardes.Band.of(p, arena, p.panes[point.id].?, point.id) catch return) orelse return; - const ln = band.line(arena, p.settings.tab, point.line) catch return; + const band = (pardes.Band.of(p, arena, p.panes[point.id].?, point.id) catch return) orelse return; + const ln = band.line(arena, p.settings.tab, point.line) catch return; + if (m.button == config.select_button) { for (ln.parts) |part| if (point.col >= part.col -| 2 and point.col < part.col + part.width) { - pardes.TagState.point(p, part.row.index); + if (s.kind == .pick) pardes.Pick.press(p, part.row, false, false) else pardes.TagState.point(p, part.row.index); }; return; } - if (button != config.exec_button and button != config.look_button) return; - const word = (pardes.Band.wordAt(p, arena, point.id, point.line, point.col) catch return) orelse return; - const kept = arena.dupe(u8, word) catch return; - // A picker's band: B2 runs the word with the picker live (an act's word - // acts, Pick.execWord); B3 on a row lands on it. - if (v.tag_state.kind == .pick) { - if (button == config.exec_button) return exec.runBuiltin(p, config.exec_cmd, v.active, "", kept); - const band = (pardes.Band.of(p, arena, p.panes[point.id].?, point.id) catch return) orelse return; - const ln = band.line(arena, p.settings.tab, point.line) catch return; - for (ln.parts) |part| if (point.col >= part.col and point.col < part.col + part.width) { - pardes.TagState.point(p, part.row.index); - return pardes.Pick.landHere(p); + if (m.button != config.exec_button and m.button != config.look_button) return; + // A picker's band: B3 on a row is Enter there (Pick.press); B2 runs the + // word under it with the picker up (an act's word acts, Pick.execWord). + if (s.kind == .pick and m.button == config.look_button) { + for (ln.parts) |part| if (point.col >= part.col -| 2 and point.col < part.col + part.width) { + return pardes.Pick.press(p, part.row, true, m.shift); }; return; } - pardes.TagState.toRest(p); - exec.runBuiltin(p, if (button == config.exec_button) config.exec_cmd else config.look_cmd, v.active, "", kept); + const word = (pardes.Band.wordAt(p, arena, point.id, point.line, point.col) catch return) orelse return; + const kept = arena.dupe(u8, word) catch return; + if (s.kind != .pick) pardes.TagState.toRest(p); + exec.runBuiltin(p, if (m.button == config.exec_button) config.exec_cmd else config.look_cmd, point.id, "", kept); } /// Index of the pane pair in column `c` whose seam (see seamRowOf) is screen @@ -2068,3 +2079,223 @@ test "one column's words at the window row's left and the window's at its right clickAt(p, .middle, row.words + @as(u16, "Find Grep ".len) + 2, 0); try std.testing.expectEqual(@as(usize, 2), p.view().ncol); } + +/// A picker asked from a held pane with three places in `dir`'s files, +/// previewed in a reader made under it (UX DESIGN O1): the asker's id. +fn heldPicker(p: *Pardes, tmp: *std.testing.TmpDir) !usize { + for ([_][]const u8{ "one.zig", "two.zig", "three.zig" }) |name| try tmp.dir.writeFile(std.testing.io, .{ .sub_path = name, .data = "const a = 1;\nconst b = 2;\nconst c = 3;\nconst d = 4;\n" }); + var buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)]; + _ = try p.setTestFile("held\n"); + const held = p.view().active; + pardes.Trail.keep(p.panes[held].?); + var rows: [3 * (std.Io.Dir.max_path_bytes + 32)]u8 = undefined; + const list = try std.fmt.bufPrint(&rows, "{s}/one.zig:2:1 Wrap b\n{s}/two.zig:3:1 c\n{s}/three.zig:4:1 d\n", .{ dir, dir, dir }); + pardes.Pick.open(p, held, .list, "Refs", "", list); + p.update(.tick); + p.sync(); + return held; +} + +/// Screen row `i` of the band under pane `id`'s head, and a column on its +/// rows' text. +fn bandRow(p: *Pardes, id: usize, i: u16) [2]u16 { + const r = p.view().rects[id]; + const at = pardes.Band.covered(p, p.panes[id].?, id); + return .{ r.x + TAG_TEXT_INSET + 8, r.y + at.tag + at.head + i }; +} + +test "the mouse never ends a picker as a side effect (UX DESIGN O2): the pointer's motion and the wheel anywhere leave it up; the wheel over its rows moves its cursor `wheel_rows` and the preview follows; B1 on a row puts the cursor there and keeps the keyboard in the field; B2 or B3 on the preview's text lands it first; Shift-B3 on a row opens it below" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const held = try heldPicker(p, &tmp); + const pk = pardes.Pick.live(p).?; + const reader = p.paneBySerial(pk.landing).?; + try std.testing.expect(reader != held and pk.made); + const s = &p.view().tag_state; + const rr = p.view().rects[reader]; + // Motion and the wheel over the reader, over the held pane's body, and + // over the window row: nothing ends. + for ([_][2]u16{ .{ rr.x + 6, rr.y + 3 }, .{ 6, 0 } }) |at| { + p.update(.{ .mouse = .{ .button = .none, .kind = .motion, .col = at[0], .row = at[1] } }); + p.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = at[0], .row = at[1] } }); + try std.testing.expect(pardes.Pick.live(p) == pk and !s.waits); + } + try std.testing.expectEqual(held, p.view().active); + // The wheel over the rows: the cursor down, the next row previewed on + // the frame after. + const row0 = bandRow(p, held, 0); + p.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = row0[0], .row = row0[1] } }); + try std.testing.expectEqual(@as(u16, config.wheel_rows), s.rows.at); + p.update(.tick); + try std.testing.expect(std.mem.endsWith(u8, p.panes[reader].?.file.?.path, "two.zig")); + p.update(.{ .mouse = .{ .button = .wheel_up, .kind = .press, .col = row0[0], .row = row0[1] } }); + try std.testing.expectEqual(@as(u16, 0), s.rows.at); + // B1 on the third row: the cursor there, the picker up, the keyboard + // its field's. + const row2 = bandRow(p, held, 2); + clickAt(p, .left, row2[0], row2[1]); + try std.testing.expect(pardes.Pick.live(p) == pk and !s.waits); + try std.testing.expectEqual(@as(u16, 2), s.rows.at); + try std.testing.expectEqual(held, p.view().active); + p.update(.{ .key = .{ .cp = pardes.Key.up } }); + try std.testing.expectEqual(@as(u16, 1), s.rows.at); + p.update(.{ .key = .{ .cp = pardes.Key.down } }); + p.update(.tick); + p.sync(); + // B3 on the preview's text: it lands first, the held pane's reader + // showing that file at that line, the picker gone. + const r2 = p.view().rects[reader]; + try std.testing.expect(std.mem.endsWith(u8, p.panes[reader].?.file.?.path, "three.zig")); + clickAt(p, .right, r2.x + config.GUTTER + 6, r2.y + p.panes[reader].?.tag_rows); + try std.testing.expect(pardes.Pick.live(p) == null); + const landed = pardes.Trail.readerOf(p, p.panes[held].?).?; + try std.testing.expect(std.mem.endsWith(u8, p.panes[landed].?.file.?.path, "three.zig")); + // Shift-B3 on a row: that place in a new pane below. + p.view().active = held; + var n: usize = 0; + for (p.panes) |slot| n += @intFromBool(slot != null); + pardes.Pick.open(p, held, .list, "Refs", "", pk.rows); + p.sync(); + const first = bandRow(p, held, 0); + p.update(.{ .mouse = .{ .button = .right, .kind = .press, .col = first[0], .row = first[1], .shift = true } }); + p.update(.{ .mouse = .{ .button = .right, .kind = .release, .col = first[0], .row = first[1], .shift = true } }); + try std.testing.expect(pardes.Pick.live(p) == null); + var now: usize = 0; + for (p.panes) |slot| now += @intFromBool(slot != null); + try std.testing.expectEqual(n + 1, now); + try std.testing.expect(std.mem.endsWith(u8, p.panes[p.view().active].?.file.?.path, "one.zig")); +} + +test "B1 in another pane: a picker waits (UX DESIGN O2), unrolled as it was, its box its pane's mode and no caret in its field; keys go where the keyboard is; B1 on its head, or C-w back to its pane, gives it the keyboard; Esc anywhere ends it first, then the pane's own Esc" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const held = try heldPicker(p, &tmp); + const pk = pardes.Pick.live(p).?; + const reader = p.paneBySerial(pk.landing).?; + const s = &p.view().tag_state; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + // B1 on the preview: the keyboard there, the picker waiting. + const rr = p.view().rects[reader]; + clickAt(p, .left, rr.x + config.GUTTER + 3, rr.y + p.panes[reader].?.tag_rows + 1); + try std.testing.expectEqual(reader, p.view().active); + try std.testing.expect(pardes.Pick.live(p) == pk and s.waits); + try std.testing.expectEqual(@as(?usize, held), pardes.TagState.hostOf(p)); + const surface = try p.render(a); + const hr = p.view().rects[held]; + try std.testing.expectEqualStrings(config.tag_normal, surface.at(hr.x, hr.y).grapheme()); + try std.testing.expect(surface.cursor == null or surface.cursor.?.y >= rr.y); + // A key goes to the reader: `j` moves its caret, the field untouched. + const row = p.panes[reader].?.body.cur_row; + p.update(.{ .key = .{ .cp = 'j', .text = "j" } }); + try std.testing.expectEqual(row + 1, p.panes[reader].?.body.cur_row); + try std.testing.expectEqual(@as(u16, 0), s.field.len); + // B1 on the head: the keyboard back in the field. + const head = bandRow(p, held, 0); + clickAt(p, .left, head[0], head[1] - 1); + try std.testing.expect(!s.waits and p.view().active == held); + p.update(.{ .key = .{ .cp = 'w', .text = "w" } }); + try std.testing.expectEqualStrings("w", s.field.text()); + // C-w to the held pane from the reader gives it back too. + clickAt(p, .left, rr.x + config.GUTTER + 3, rr.y + p.panes[reader].?.tag_rows + 1); + try std.testing.expect(s.waits); + p.update(.{ .key = .{ .cp = 'w', .ctrl = true } }); + p.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + p.update(.{ .key = .{ .cp = pardes.Key.enter } }); + try std.testing.expect(!s.waits and p.view().active == held and pardes.Pick.live(p) == pk); + // The keyboard to the held pane's own text under the band, in insert + // mode; Esc ends the picker first, the text still inserting; the next + // Esc is the text's. + const at = pardes.Band.covered(p, p.panes[held].?, held); + clickAt(p, .left, hr.x + config.GUTTER + 3, hr.y + at.tag + at.over); + try std.testing.expect(s.waits and p.view().active == held); + p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); + try std.testing.expect(p.panes[held].?.body.mode == .insert); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(pardes.Pick.live(p) == null and s.kind == .rest and !s.waits); + try std.testing.expect(p.panes[held].?.body.mode == .insert); + p.update(.{ .key = .{ .cp = pardes.Key.escape } }); + try std.testing.expect(p.panes[held].?.body.mode == .normal); +} + +test "B1 on the asking pane's own tag: the picker waits, the tag's text has the keyboard; a word there that changes nothing leaves it up; B2 Del ends it first, as Esc does, and closes the pane (UX DESIGN O2, O3)" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const held = try heldPicker(p, &tmp); + const s = &p.view().tag_state; + p.panes[held].?.tag.own = try p.gpa.dupe(u8, "Wrap"); + p.sync(); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const tag = try tagline.tagText(p, frame.allocator(), p.panes[held].?); + const hr = p.view().rects[held]; + const col = struct { + fn of(q: *Pardes, t: []const u8, x: u16, word: []const u8) u16 { + return x + TAG_TEXT_INSET + 1 + @as(u16, @intCast(panes.File.displayWidth(q.settings.tab, t[0..std.mem.indexOf(u8, t, word).?]))); + } + }.of; + clickAt(p, .left, col(p, tag, hr.x, "Wrap"), hr.y); + try std.testing.expect(pardes.Pick.live(p) != null and s.waits); + try std.testing.expectEqual(Pane.Focus.tag, p.panes[held].?.focus); + const wrapped = p.settings.wrap; + clickAt(p, .middle, col(p, tag, hr.x, "Wrap"), hr.y); + try std.testing.expect(p.settings.wrap != wrapped and pardes.Pick.live(p) != null); + var n: usize = 0; + for (p.panes) |slot| n += @intFromBool(slot != null); + clickAt(p, .middle, col(p, tag, hr.x, "Del"), hr.y); + try std.testing.expect(pardes.Pick.live(p) == null and s.kind == .rest); + try std.testing.expect(p.panes[held] == null); + var now: usize = 0; + for (p.panes) |slot| now += @intFromBool(slot != null); + // The held pane and the reader made for the picker, both gone. + try std.testing.expectEqual(n - 2, now); +} + +test "the act step and the rows under the mouse (UX DESIGN O2): B2 on an act's word acts and the picker stays, B2 on a row's word runs it and the picker stays, B1 on an act puts the act step's cursor on it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const held = try heldPicker(p, &tmp); + const pk = pardes.Pick.live(p).?; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + // B2 on `Wrap` in the first row's text. + const band = (try pardes.Band.of(p, a, p.panes[held].?, held)).?; + const ln = try band.line(a, p.settings.tab, 0); + const r = p.view().rects[held]; + const top = bandRow(p, held, 0)[1]; + const wrap_col: u16 = r.x + TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, ln.text[0..std.mem.indexOf(u8, ln.text, "Wrap").?]))) + 1; + const wrapped = p.settings.wrap; + clickAt(p, .middle, wrap_col, top); + try std.testing.expect(p.settings.wrap != wrapped and pardes.Pick.live(p) == pk); + // M-l: the act step; B1 on `Copy` puts its cursor there, B2 on it + // copies the place, the picker up. + p.update(.{ .key = .{ .cp = 'l', .alt = true } }); + _ = frame.reset(.retain_capacity); + const acts = (try pardes.Band.of(p, a, p.panes[held].?, held)).?; + var copy: ?[2]u16 = null; + for (0..acts.height) |y| { + const l = try acts.line(a, p.settings.tab, @intCast(y)); + if (std.mem.indexOf(u8, l.text, "y Copy")) |at| copy = .{ r.x + TAG_TEXT_INSET + @as(u16, @intCast(panes.File.displayWidth(p.settings.tab, l.text[0..at]))) + 3, top + @as(u16, @intCast(y)) }; + } + clickAt(p, .left, copy.?[0], copy.?[1]); + try std.testing.expect(pk.spine == .acts and pardes.Pick.live(p) == pk); + try std.testing.expectEqual(@as(u8, 6), pk.act_at); + clickAt(p, .middle, copy.?[0], copy.?[1]); + try std.testing.expect(pardes.Pick.live(p) == pk); + try std.testing.expect(std.mem.endsWith(u8, p.registers.text(p.gpa, pardes.Registers.default).?, "one.zig:2\n")); +} diff --git a/core/panes.zig b/core/panes.zig index 16b1d388..f4f528b7 100644 --- a/core/panes.zig +++ b/core/panes.zig @@ -1495,6 +1495,9 @@ pub fn paneDir(pane: *const Pane) []const u8 { /// none by recency. pub fn removePane(p: *Pardes, id: usize, to: ?usize) error{OutOfMemory}!void { const v = p.view(); + // The pane a picker asks from or previews in: the picker ends first, + // as Esc ends it (UX DESIGN O3), a pane made for it gone with it. + pardes.Pick.yieldFor(p, id); const pane = p.panes[id] orelse return; // What is docked to it closes with it, first, each giving its rows // back to it: then its rows, theirs among them, go where a pane's go. -- cgit v1.3