aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authoruser <user@clank>2026-11-22 13:41:39 +0100
committeruser <user@clank>2026-11-22 13:41:39 +0100
commitf134d8fe6fa9c8d469f254275675dfea6c610c46 (patch)
tree796788e2448c5baaaff12191a0df4a1ad2a27606
parentfeat: add live cross-tab pane gallery (diff)
downloadslosh-f134d8fe6fa9c8d469f254275675dfea6c610c46.tar.gz
slosh-f134d8fe6fa9c8d469f254275675dfea6c610c46.tar.xz
slosh-f134d8fe6fa9c8d469f254275675dfea6c610c46.zip
fix: commands naming a background pane do not move the human's view
minimize, zoom, float, float placement and rerun addressed by id used to set a->cur to the pane's tab -- a script touching a pane in another tab yanked the screen there. They now lay out and refocus within the pane's own tab only. rerun also pointed the *current* tab's focus at the rerun pane, so keys went to a pane in another tab and its title showed beside the wrong tab name (and could dangle once that pane closed). Focus now belongs to the pane's tab, and the reply answers the id that was run rather than whatever is focused. apply-layout: a failed parse no longer leaves focus:false armed for the next, unrelated layout, and a request with neither path nor kdl is refused.
-rw-r--r--docs/scripting.md4
-rw-r--r--src/app_layout.c31
-rw-r--r--src/app_session.c12
-rw-r--r--src/cmd.c10
-rw-r--r--tests/test_focus_isolation.py114
5 files changed, 150 insertions, 21 deletions
diff --git a/docs/scripting.md b/docs/scripting.md
index eb0c812..24bcb96 100644
--- a/docs/scripting.md
+++ b/docs/scripting.md
@@ -29,12 +29,12 @@ Panes and tabs are addressed by **id**, so a background tab is scriptable.
| `resize` | `cols` `rows`, and optionally `cell_w` `cell_h` |
| `split` | `dir:"cols"\|"rows"`, `id` for which pane to split — and, to make a pane that is *for* something: `command` to run instead of a shell, `cwd` instead of that pane's own directory, `purpose` (declared, so the pane cannot relabel itself), `focus:false` to build it without pulling the view. The new pane lands in the split pane's tab, whether or not that tab is on screen, and the reply is its `id` either way |
| `focus` | `id` |
-| `close` `rerun` | `id`, or 0 for the focused pane |
+| `close` `rerun` | `id`, or 0 for the focused pane. Rerunning by id in another tab keeps the active tab and answers the pane's id |
| `clear-shaders` | `id`, or 0 for the focused pane; answers `cleared:0\|1`. The way back from a pane that painted itself unreadable |
| `new-tab` `select-tab` `close-tab` `move-tab` | tabs, by `id` or `index` |
| `gallery` | toggle a live, paginated grid of the focused pane in each tab; answers `shown`. Does not change the active tab or any tab's focus until a tile is entered |
| `move-pane` | `id` of the pane, `tab` id to move it into (`0` for a tab of its own, with an optional `name`), `dir:"cols"\|"rows"`, `beside` a pane in that tab to land next to (default: whatever it has focused), `focus:false` to leave focus — and the view — where it is. The pane keeps running: same pty, same scrollback |
-| `float` | bare: toggle a pane [floating](panes.md#floating-a-pane) (`id`, or 0 for the focused one). With any of `x` `y` `w` `h` it *places*: floats first when tiled, re-places when floating, never un-floats; omitted fields keep their value |
+| `float` | bare: toggle a pane [floating](panes.md#floating-a-pane) (`id`, or 0 for the focused one). With any of `x` `y` `w` `h` it *places*: floats first when tiled, re-places when floating, never un-floats; omitted fields keep their value. An id in another tab does not switch the active tab |
| `new-float` | a fresh floating shell over the current tab, centred, in the focused pane's directory; answers `id` |
| `set-name` | `target:"tab"` (the default, because that is what this verb has always meant) or `"pane"`, `id`, `name`. A pane accepts 0 for the focused one. A pane's name wins over the title the program sets, so this is how a program that keeps announcing something stale gets overruled; an empty `name` clears it and hands the label back. Refusals say `no such pane` or `no such tab`, so a mistyped target is visible in the reply |
| `set-purpose` | `target:"pane"\|"tab"`, `id` (or 0 for the focused pane, and the tab you are in), `purpose`. An empty `purpose` clears the slot *and* unlocks it, handing the label back to the program |
diff --git a/src/app_layout.c b/src/app_layout.c
index f2119f8..d156941 100644
--- a/src/app_layout.c
+++ b/src/app_layout.c
@@ -1290,14 +1290,13 @@ bool app_minimize(app_t *a, uint32_t id) {
if (!next) return false;
n->minimized = true;
- a->cur = ti;
/* A zoom naming this pane must end with it: left standing, the layout would
* solo a pane the strip now holds — filling the tab and put away at once. */
if (a->tabs[ti].zoom == n->id) a->tabs[ti].zoom = 0;
/* Focus has to leave, or the rule that a focused pane is never minimised
* would undo this on the next frame. */
if (a->tabs[ti].focus == n) a->tabs[ti].focus = next;
- layout(a);
+ layout_tab(a, ti); /* an explicit id in another tab must not select it */
return true;
}
@@ -1306,13 +1305,11 @@ bool app_toggle_zoom(app_t *a, uint32_t id) {
if (!n) return false;
size_t ti = tab_of(a, n);
if (ti == (size_t)-1) return false;
- a->cur = ti;
- /* Zooming focuses what it zoomed: the pane filling the tab is the only one
- * you could be typing into, and leaving focus elsewhere would mean keys
- * going somewhere invisible. */
+ /* Zooming focuses the pane *within its own tab*. It need not pull the
+ * current view to a background tab when addressed by id. */
a->tabs[ti].zoom = a->tabs[ti].zoom == n->id ? 0 : n->id;
a->tabs[ti].focus = n;
- layout(a);
+ layout_tab(a, ti);
return true;
}
@@ -1366,11 +1363,11 @@ bool app_toggle_float(app_t *a, uint32_t id) {
/* w == 0 (a pane too small to measure): float_clamp's own centred
* default answers the same question at two thirds of the area. */
}
- /* Either way you are now looking at it, and it holds the keyboard: the
- * pane that just changed planes is the one the verb was about. */
- a->cur = ti;
+ /* In the viewed tab it takes focus; in a background tab it only updates
+ * that tab's focus. A control command naming another pane is not a request
+ * to move the human's view there. */
a->tabs[ti].focus = n;
- layout(a);
+ layout_tab(a, ti);
return true;
}
@@ -1403,7 +1400,7 @@ bool app_float_place(app_t *a, uint32_t id, long x, long y, long w, long h) {
if (x >= 0) want.x = (uint16_t)x;
if (y >= 0) want.y = (uint16_t)y;
n->float_rect = want;
- layout(a);
+ layout_tab(a, tab_of(a, n));
return true;
}
@@ -2182,6 +2179,8 @@ void app_hover_path_update(app_t *a, const input_event_t *ev,
bool app_rerun_pane(app_t *a, uint32_t id) {
node_t *n = id ? pane_by_id(a, id) : (a->ntabs ? cur(a)->focus : NULL);
if (!n) return false;
+ size_t ti = tab_of(a, n);
+ if (ti == (size_t)-1) return false;
bool ok =
pane_suspended(n->pane) ? pane_start(n->pane) : pane_restart(n->pane);
if (!ok) {
@@ -2189,8 +2188,12 @@ bool app_rerun_pane(app_t *a, uint32_t id) {
return false;
}
/* The new pty is born at the pane's current size: a dead pane is still laid
- * out and still resized, pane_resize() just had no pty to tell. */
- cur(a)->focus = n;
+ * out and still resized, pane_resize() just had no pty to tell. Focus belongs
+ * to the pane's tab, not whichever tab happens to be on screen: pointing the
+ * current tab's focus at a background pane sent keys there and showed its
+ * title beside the *current* tab's name. It could even dangle if that pane
+ * then closed. */
+ a->tabs[ti].focus = n;
return true;
}
diff --git a/src/app_session.c b/src/app_session.c
index 2c132ce..c5b163b 100644
--- a/src/app_session.c
+++ b/src/app_session.c
@@ -257,7 +257,12 @@ void app_layout_keep_view(app_t *a) { a->layout_keep_view = true; }
bool app_apply_layout_text_at(app_t *a, const char *text, bool replace,
const char *base, char *err, size_t errcap) {
kdl_node_t *root = kdl_parse(text, err, errcap);
- if (!root) return false;
+ if (!root) {
+ /* `focus:false` is armed before parsing; a failed parse must not pass
+ * that request's keep-view intent to the next, unrelated layout. */
+ a->layout_keep_view = false;
+ return false;
+ }
bool ok = app_apply_layout(a, root, replace, base, err, errcap);
kdl_free(root);
return ok;
@@ -278,7 +283,10 @@ bool app_apply_layout_file(app_t *a, const char *path, bool replace, char *err,
char pathbuf[1024], dirbuf[1024];
const char *file = path_expand(path, pathbuf, sizeof pathbuf);
kdl_node_t *root = kdl_parse_file(file, err, errcap);
- if (!root) return false;
+ if (!root) {
+ a->layout_keep_view = false;
+ return false;
+ }
bool ok = app_apply_layout(
a, root, replace, path_dir(file, dirbuf, sizeof dirbuf), err, errcap);
kdl_free(root);
diff --git a/src/cmd.c b/src/cmd.c
index 8dc5e93..b0e554a 100644
--- a/src/cmd.c
+++ b/src/cmd.c
@@ -283,9 +283,12 @@ static char *cmd_json(app_t *a, screen_t *s, input_parser_t *in,
: jerr("no room for another pane");
}
if (strcmp(cmd, "rerun") == 0) {
- if (!app_rerun_pane(a, (uint32_t)jv_geti(req, "id", 0)))
- return jerr("cannot run it again");
- return jok_int("id", app_focused_pane_id(a));
+ uint32_t id = (uint32_t)jv_geti(req, "id", 0);
+ if (!id) id = app_focused_pane_id(a);
+ if (!app_rerun_pane(a, id)) return jerr("cannot run it again");
+ /* A background rerun does not take keyboard focus. Answer with the pane
+ * that was run, not the unrelated pane still focused on screen. */
+ return jok_int("id", id);
}
if (strcmp(cmd, "close") == 0) {
if (!app_close_pane(a, (uint32_t)jv_geti(req, "id", 0)))
@@ -410,6 +413,7 @@ static char *cmd_json(app_t *a, screen_t *s, input_parser_t *in,
const char *path = jv_gets(req, "path", NULL);
const char *text = jv_gets(req, "kdl", NULL);
bool replace = jv_getb(req, "replace", false);
+ if (!text && !path) return jerr("need a path or kdl");
/* `focus:false` builds the tabs without going to them. A layout that names
* an `active` tab still wins -- that is a statement about where to be. */
if (!jv_getb(req, "focus", true)) app_layout_keep_view(a);
diff --git a/tests/test_focus_isolation.py b/tests/test_focus_isolation.py
new file mode 100644
index 0000000..b093e38
--- /dev/null
+++ b/tests/test_focus_isolation.py
@@ -0,0 +1,114 @@
+#!/usr/bin/env python3
+"""Background operations cannot reassign the visible tab's keyboard focus."""
+
+import sys
+
+from harness import Session, check, report
+
+SH = ["/bin/sh", "-c", "stty raw -echo; cat"]
+
+
+def setup(s):
+ s.settle()
+ front = s.focused()
+ s.api("set-name", target="tab", id=front["tab_id"], name="foreground-tab")
+ s.api("set-name", target="pane", id=front["id"], name="foreground-pane")
+ made = s.api(
+ "apply-layout",
+ focus=False,
+ kdl='layout { tab name="background-tab" { pane suspended=true } }',
+ )
+ check("background layout was created", made.get("ok") is True, str(made))
+ back = next(p for p in s.panes() if p["tab_id"] != front["tab_id"])
+ s.api("set-name", target="pane", id=back["id"], name="background-pane")
+ return front, back
+
+
+def still_here(s, front, label):
+ panes = s.panes()
+ tabs = s.tabs()
+ snap = s.snapshot()
+ focused = [p for p in panes if p["focused"]]
+ check(
+ label + ": one focused pane in active tab",
+ len(focused) == 1
+ and focused[0]["id"] == front["id"]
+ and any(t["active"] and t["id"] == front["tab_id"] for t in tabs),
+ f"{focused} {tabs}",
+ )
+ status = snap.line(snap.rows - 2)
+ check(
+ label + ": title stays on foreground pane",
+ "foreground-pane" in status and "background-pane" not in status,
+ repr(status),
+ )
+
+
+def test_rerun_background_suspended_pane():
+ with Session(SH, cols=90, rows=18) as s:
+ front, back = setup(s)
+ answer = s.api("rerun", id=back["id"])
+ check(
+ "background rerun returns its target id",
+ answer.get("ok") is True and answer.get("id") == back["id"],
+ str(answer),
+ )
+ still_here(s, front, "background rerun")
+ s.api(
+ "raw",
+ id=back["id"],
+ data="\x1b]2;background-updated\x07\x1b]5577;1;status;working\x1b\\",
+ )
+ s.until(
+ lambda _: any(
+ p["id"] == back["id"] and p["status"] == "working" for p in s.panes()
+ )
+ )
+ still_here(s, front, "background title and status update")
+ s.api("close", id=back["id"])
+ still_here(s, front, "closing rerun background tab")
+
+
+def test_failed_background_layout_does_not_change_next_request():
+ with Session(SH, cols=90, rows=18) as s:
+ s.settle()
+ failed = s.api("apply-layout", focus=False, kdl="layout {")
+ check("malformed background layout refused", failed.get("ok") is False)
+ made = s.api("apply-layout", kdl='layout { tab name="next" { pane } }')
+ check("subsequent default layout succeeded", made.get("ok") is True)
+ check(
+ "failed request did not keep view on subsequent layout",
+ next(t for t in s.tabs() if t["active"])["name"] == "next",
+ str(s.tabs()),
+ )
+ failed = s.api("apply-layout", focus=False, path="/nonexistent/slosh.layout")
+ check("missing background layout refused", failed.get("ok") is False)
+ failed = s.api("apply-layout", focus=False)
+ check("background layout without a source refused", failed.get("ok") is False)
+ s.api("apply-layout", kdl='layout { tab name="after" { pane } }')
+ check(
+ "failed file and missing source do not leak focus:false",
+ next(t for t in s.tabs() if t["active"])["name"] == "after",
+ str(s.tabs()),
+ )
+
+
+def test_float_background_pane():
+ with Session(SH, cols=90, rows=18) as s:
+ front, back = setup(s)
+ made = s.api("split", id=back["id"], focus=False)
+ check("background split succeeded", made.get("ok") is True, str(made))
+ target = made["id"]
+ answer = s.api("float", id=target)
+ check("background float succeeded", answer.get("ok") is True, str(answer))
+ still_here(s, front, "background float")
+ answer = s.api("float", id=target, x=8, y=5, w=24, h=8)
+ check("background placement succeeded", answer.get("ok") is True, str(answer))
+ still_here(s, front, "background placement")
+
+
+if __name__ == "__main__":
+ test_rerun_background_suspended_pane()
+ test_float_background_pane()
+ test_failed_background_layout_does_not_change_next_request()
+ sys.exit(report())