aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authoruser <user@clank>2026-08-27 15:09:02 +0200
committeruser <user@clank>2026-08-27 15:09:02 +0200
commit2ee881452aa4b19701a071f0333a15265e3b314e (patch)
tree82fc4fc20ebcff8c27fbebe170534a9bc9213f6e /src
parentmacos-remote-build: tolerate notarization ticket propagation lag (diff)
downloadslosh-2ee881452aa4b19701a071f0333a15265e3b314e.tar.gz
slosh-2ee881452aa4b19701a071f0333a15265e3b314e.tar.xz
slosh-2ee881452aa4b19701a071f0333a15265e3b314e.zip
selection: drag past an edge scrolls the viewport under it
Text taller than the viewport was not selectable in one gesture: the drag stopped dead at the pane's border. Two faults, one visible and one waiting. The visible one: DRAG_SELECT motion subtracted the content origin in uint16_t, so a pointer above or left of the pane underflowed, the grid lookup failed, and the selection froze. The waiting one: the anchor was a pair of *viewport* coordinates re-resolved on every extend, so the moment anything scrolled mid-drag the anchor would have drifted with the viewport, re-anchoring to whatever slid under the press. The anchor is now a tracked grid ref -- the thing lib-vt documents as being for 'long-lived anchors such as selections' -- so it follows its cell across scrolling, pruning and reflow, and only the near end of the selection moves. If a program flooding output prunes the anchored row out of scrollback entirely, the selection keeps what it had; there is no cell left to anchor a new answer to. Motion clamps into the content rect (a pointer past an edge is pointing at the nearest cell), and holding past the top or bottom edge scrolls on a clock rather than on motion, because a resting pointer sends no events and 'keep going' is exactly what resting it there means. app_tick() does the step, app_next_deadline_ms() wakes the poll loop for it -- the same mechanism toasts and hover guides already use -- and both front ends call the tick, so the behaviour exists under the server and the headless harness alike. Rows per step is how far past the edge the pointer sits: one row past creeps, further hurries, so the hand already dragging is also the throttle. The metronome is select_scroll_ms, a knob so the test can configure the clock down instead of sleeping. The test drags to one row above the pane and simply holds: the viewport walks to the top of scrollback (clamping makes the endpoint exact), the copy starts at a line that was never on screen with the press, and still ends at the pressed row -- that last check is the tracked anchor's.
Diffstat (limited to 'src')
-rw-r--r--src/app.c60
-rw-r--r--src/config.c10
-rw-r--r--src/headless.c4
-rw-r--r--src/pane.c32
-rw-r--r--src/server.c8
5 files changed, 104 insertions, 10 deletions
diff --git a/src/app.c b/src/app.c
index e72d2a7..0c8e43d 100644
--- a/src/app.c
+++ b/src/app.c
@@ -293,11 +293,62 @@ int app_next_deadline_ms(app_t *a) {
if (soonest < 0 || due < soonest) soonest = due;
}
+ /* A selection drag holding past the pane's top or bottom edge scrolls on a
+ * clock, not on motion: a pointer resting there sends no events, and "keep
+ * going" is exactly what resting it there means. */
+ if (a->drag.kind == DRAG_SELECT && a->ptr_valid) {
+ node_t *n = pane_by_id(a, a->drag.src);
+ if (n && ((int)a->ptr_y < (int)n->content.y ||
+ (int)a->ptr_y >= (int)n->content.y + (int)n->content.h)) {
+ int64_t due = a->drag.scroll_due;
+ int64_t now = now_ms_();
+ if (due < now) due = now;
+ if (soonest < 0 || due < soonest) soonest = due;
+ }
+ }
+
if (soonest < 0) return -1;
int64_t in = soonest - now_ms_();
return in <= 0 ? 0 : (int)in;
}
+/* Extend a pane's selection toward the pointer, clamped into its content:
+ * the nearest cell is what a pointer past an edge is pointing at. */
+static void select_extend_to_pointer(app_t *a, node_t *n) {
+ if (!n->content.w || !n->content.h) return;
+ int lx = (int)a->ptr_x - (int)n->content.x;
+ int ly = (int)a->ptr_y - (int)n->content.y;
+ lx = lx < 0 ? 0 : lx >= (int)n->content.w ? n->content.w - 1 : lx;
+ ly = ly < 0 ? 0 : ly >= (int)n->content.h ? n->content.h - 1 : ly;
+ pane_select_extend(n->pane, (uint16_t)lx, (uint16_t)ly);
+}
+
+/* The auto-scroll step behind app_next_deadline_ms's DRAG_SELECT deadline.
+ * Rows per step is how far past the edge the pointer sits -- one row past
+ * creeps, further hurries -- which is rate control by the hand that is
+ * already there. The step scrolls, then re-extends the selection: the anchor
+ * is a tracked grid ref (pane.c), so only the near end moves. True when
+ * anything changed, so a caller knows a repaint is owed. */
+bool app_tick(app_t *a) {
+ if (a->drag.kind != DRAG_SELECT || !a->ptr_valid) return false;
+ node_t *n = pane_by_id(a, a->drag.src);
+ if (!n || !n->content.h) return false;
+ int ly = (int)a->ptr_y - (int)n->content.y;
+ int over = ly < 0 ? ly
+ : ly >= (int)n->content.h ? ly - (int)n->content.h + 1
+ : 0;
+ if (!over) return false;
+ int64_t now = now_ms_();
+ if (now < a->drag.scroll_due) return false;
+ /* Advance the clock whether or not the scroll had anywhere to go: a
+ * pointer parked past an edge of a fully scrolled pane must not turn the
+ * deadline into a busy loop. */
+ a->drag.scroll_due = now + CFG.select_scroll_ms;
+ pane_scroll(n->pane, over);
+ select_extend_to_pointer(a, n);
+ return true;
+}
+
size_t app_toast_count(app_t *a) {
toasts_expire(a);
return a->ntoasts;
@@ -1280,6 +1331,7 @@ static void do_action(app_t *a, const char *action, const input_event_t *ev) {
pane_select_word(n->pane, local.mx, local.my, CFG.word_separators);
a->drag.kind = DRAG_SELECT;
a->drag.src = n->id;
+ a->drag.scroll_due = 0; /* the first edge contact may scroll at once */
return;
}
@@ -1659,10 +1711,12 @@ void app_event(app_t *a, const input_event_t *ev) {
}
}
if (a->drag.kind == DRAG_SELECT) {
+ /* Clamped, not refused: the old unsigned subtraction underflowed
+ * for a pointer above or left of the pane, and the selection froze
+ * at the border. Past an edge the drag keeps the nearest cell, and
+ * app_tick scrolls the viewport after it. */
node_t *n = pane_by_id(a, a->drag.src);
- if (n)
- pane_select_extend(n->pane, (uint16_t)(ev->mx - n->content.x),
- (uint16_t)(ev->my - n->content.y));
+ if (n) select_extend_to_pointer(a, n);
} else if (a->drag.kind == DRAG_EDGE &&
(a->drag.c_nv || a->drag.c_nh)) {
/* One axis to each: the row boundary takes the vertical movement
diff --git a/src/config.c b/src/config.c
index 5d9d688..8ca9fcf 100644
--- a/src/config.c
+++ b/src/config.c
@@ -891,6 +891,10 @@ void config_defaults(config_t *c) {
* -- and any key or click ends it early. */
c->splash_ms = 900;
c->hover_delay_ms = 250;
+ /* Slow enough to pick a line at one row past the edge, fast enough to cross
+ * ten thousand lines of scrollback without regretting the gesture -- and the
+ * distance past the edge scales the step, so the hand can hurry it. */
+ c->select_scroll_ms = 50;
c->double_click_ms = 400;
/* Quotes, brackets, and the punctuation that ends a clause or a list.
*
@@ -1609,6 +1613,9 @@ char *config_render(const config_t *c) {
cb_add(&b, "splash_ms %u // the logo, on attach; 0 for never\n",
c->splash_ms);
cb_add(&b, "hover_delay_ms %u\n", c->hover_delay_ms);
+ cb_add(&b,
+ "select_scroll_ms %u // drag past an edge: ms per scroll step\n",
+ c->select_scroll_ms);
cb_add(&b, "double_click_ms %u\n", c->double_click_ms);
cb_qstr(&b, "word_separators", c->word_separators,
"// what a double-click's word stops at");
@@ -1857,6 +1864,7 @@ static const char *const KNOWN_TOP[] = {
"scroll_lines",
"scrollback",
"scrollback_bytes",
+ "select_scroll_ms",
"shader_dir",
"shaders",
"shell",
@@ -2170,6 +2178,8 @@ static bool load_into(config_t *c, const char *path, int depth, char *err,
(uint16_t)kdl_arg_int(kdl_child(root, "splash_ms"), 0, c->splash_ms);
c->hover_delay_ms = (uint16_t)kdl_arg_int(kdl_child(root, "hover_delay_ms"),
0, c->hover_delay_ms);
+ c->select_scroll_ms = (uint16_t)kdl_arg_int(
+ kdl_child(root, "select_scroll_ms"), 0, c->select_scroll_ms);
c->double_click_ms = (uint16_t)kdl_arg_int(kdl_child(root, "double_click_ms"),
0, c->double_click_ms);
const char *ws = kdl_arg(kdl_child(root, "word_separators"), 0, NULL);
diff --git a/src/headless.c b/src/headless.c
index 399a7ea..ad58371 100644
--- a/src/headless.c
+++ b/src/headless.c
@@ -54,6 +54,10 @@ static void settle(app_t *a, int quiet, screen_t *snap) {
}
if (snap) app_compose(a, snap);
app_reap(a);
+ /* The same event-less work the server's loop does: without it a settle
+ * would freeze a selection drag that is holding past a pane's edge, and
+ * the auto-scroll would exist everywhere except under the tests. */
+ app_tick(a);
if (ms_now() - last >= quiet) break;
}
}
diff --git a/src/pane.c b/src/pane.c
index 08e86fb..1fbf6c5 100644
--- a/src/pane.c
+++ b/src/pane.c
@@ -71,9 +71,14 @@ struct pane {
* `dump-layout` writes down and what a restore then tries to run. */
char label[1024];
- /* Selection anchor, in viewport coordinates, while a drag is in progress. */
+ /* Selection anchor, while a drag is in progress. A *tracked* grid ref
+ * rather than a pair of viewport coordinates, because the viewport moves
+ * while the drag is alive -- dragging past an edge scrolls it -- and an
+ * anchor named by viewport coordinates would drift with every scroll,
+ * silently re-anchoring to whatever slid under the press. Allocated on the
+ * first selection and reused (ghostty_tracked_grid_ref_set) after that. */
bool selecting;
- uint16_t sel_x, sel_y;
+ GhosttyTrackedGridRef sel_anchor;
osc_scan_t scan;
char status[256];
@@ -397,6 +402,7 @@ void pane_free(pane_t *p) {
if (p->menc) ghostty_mouse_encoder_free(p->menc);
if (p->kev) ghostty_key_event_free(p->kev);
if (p->kenc) ghostty_key_encoder_free(p->kenc);
+ ghostty_tracked_grid_ref_free(p->sel_anchor);
if (p->cells) ghostty_render_state_row_cells_free(p->cells);
if (p->rows) ghostty_render_state_row_iterator_free(p->rows);
if (p->rstate) ghostty_render_state_free(p->rstate);
@@ -933,17 +939,29 @@ static bool grid_ref_at(pane_t *p, uint16_t x, uint16_t y,
}
void pane_select_start(pane_t *p, uint16_t x, uint16_t y) {
- p->selecting = true;
- p->sel_x = x;
- p->sel_y = y;
+ GhosttyPoint pt = {.tag = GHOSTTY_POINT_TAG_VIEWPORT,
+ .value.coordinate = {.x = x, .y = y}};
+ bool ok;
+ if (p->sel_anchor)
+ ok = ghostty_tracked_grid_ref_set(p->sel_anchor, p->term, pt) ==
+ GHOSTTY_SUCCESS;
+ else
+ ok = ghostty_terminal_grid_ref_track(p->term, pt, &p->sel_anchor) ==
+ GHOSTTY_SUCCESS;
+ p->selecting = ok; /* no anchor, no drag: extend would have nothing to hold */
pane_select_clear(p);
}
void pane_select_extend(pane_t *p, uint16_t x, uint16_t y) {
- if (!p->selecting) return;
+ if (!p->selecting || !p->sel_anchor) return;
GhosttyGridRef a, b;
- if (!grid_ref_at(p, p->sel_x, p->sel_y, &a) || !grid_ref_at(p, x, y, &b))
+ /* The anchor can stop existing mid-drag: a program flooding output while
+ * you select can push the anchored row out of a full scrollback. Keeping
+ * the selection we had is the honest answer -- there is no cell left to
+ * anchor a new one to. */
+ if (ghostty_tracked_grid_ref_snapshot(p->sel_anchor, &a) != GHOSTTY_SUCCESS)
return;
+ if (!grid_ref_at(p, x, y, &b)) return;
GhosttySelection sel = GHOSTTY_INIT_SIZED(GhosttySelection);
sel.start = a;
sel.end = b;
diff --git a/src/server.c b/src/server.c
index 36a55d6..3a7a366 100644
--- a/src/server.c
+++ b/src/server.c
@@ -706,6 +706,14 @@ int server_run(const char *name, const char *const argv[], uint16_t cols,
app_reap(s.app);
+ /* The event-less work: a selection drag held past a pane's edge scrolls
+ * on a clock (app_next_deadline_ms is what woke the poll for it), and a
+ * step that moved anything is a frame the client is owed. */
+ if (app_tick(s.app) && !pending_paint) {
+ pending_paint = true;
+ next_frame = now_ms();
+ }
+
if (app_detach_requested(s.app)) {
app_clear_detach(s.app);
drop_display(&s, EXIT_DETACHED);