From 7fedf2f80160d4cb96b77ae58137573b78cbfeee Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 11 Oct 2020 15:44:20 +0200 Subject: selection: auto-scroll: selection keeps scrolling while mouse is outside grid MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Moving the mouse outside the grid while we have an on-going selection now starts a timer. The interval of this timer depends on the mouse’s distance from the grid - the further away the mouse is, the shorter interval. On each timer timeout, we scroll one line, and update the selection. Thus, the shorter the interval, the faster we scroll. The timer is canceled as soon as the mouse enters the grid again, or the selection is either canceled or finalized. The timer FD is created and destroyed on-demand. Most of the logic is now in selection.c. The exception is the calculation of the timer interval, which depends on the mouse’s position. Thus, this is done in input.c. The scroll+selection update logic needs to know a) which direction we’re scrolling in, and b) which *column* the selection should be updated with. If the mouse is outside the grid’s left or right margins, the stored mouse column will be -1. I.e. we don’t know whether the mouse is on the left or right side of the grid. This is why the caller, that starts the timer, must provide this value. The same applies to top and bottom margins, but since we already have the scroll *direction*, which row value to use can be derived from this. --- terminal.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'terminal.h') diff --git a/terminal.h b/terminal.h index b6fb2aec..0a71abb4 100644 --- a/terminal.h +++ b/terminal.h @@ -182,6 +182,7 @@ enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; enum selection_kind { SELECTION_NONE, SELECTION_NORMAL, SELECTION_BLOCK }; enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT}; +enum selection_scroll_direction {SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; struct ptmx_buffer { void *data; @@ -346,6 +347,12 @@ struct terminal { struct coord start; struct coord end; bool ongoing; + + struct { + int fd; + int col; + enum selection_scroll_direction direction; + } auto_scroll; } selection; bool is_searching; -- cgit v1.3 From 4ad7fdc19c386fada4e1f8401d6da95f2930c201 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Sun, 11 Oct 2020 18:18:18 +0200 Subject: selection: auto-scroll: add SELECTION_SCROLL_NOT as a scroll ‘direction’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- input.c | 29 ++++++++++++++++++++++------- selection.c | 11 +++++++++-- terminal.h | 2 +- 3 files changed, 32 insertions(+), 10 deletions(-) (limited to 'terminal.h') diff --git a/input.c b/input.c index b862408b..a486ca8b 100644 --- a/input.c +++ b/input.c @@ -1327,16 +1327,32 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, /* Cursor is inside the grid, i.e. *not* in the margins */ const bool cursor_is_on_grid = seat->mouse.col >= 0 && seat->mouse.row >= 0; - const bool scroll_up = y < term->margins.top; - const bool scroll_down = y > term->height - term->margins.bottom; + enum selection_scroll_direction auto_scroll_direction + = y < term->margins.top ? SELECTION_SCROLL_UP + : y > term->height - term->margins.bottom ? SELECTION_SCROLL_DOWN + : SELECTION_SCROLL_NOT; - if (!scroll_up && !scroll_down) + if (auto_scroll_direction == SELECTION_SCROLL_NOT) selection_stop_scroll_timer(term); /* Update selection */ if (!term->is_searching) { - if (scroll_up || scroll_down) { - int distance = scroll_up + if (auto_scroll_direction != SELECTION_SCROLL_NOT) { + /* + * Start ‘selection auto-scrolling’ + * + * The speed of the scrolling is proportional to the + * distance between the mouse and the grid; the + * further away the mouse is, the fast we scroll. + * + * Note that the speed is measures in ‘intervals (in + * ns) between each timed scroll of a single line’. + * + * Thus, the further away the mouse is, the smaller + * interval value we use. + */ + + int distance = auto_scroll_direction == SELECTION_SCROLL_UP ? term->margins.top - y : y - (term->height - term->margins.bottom); @@ -1346,8 +1362,7 @@ wl_pointer_motion(void *data, struct wl_pointer *wl_pointer, selection_start_scroll_timer( term, 400000000 / (divisor > 0 ? divisor : 1), - scroll_up ? SELECTION_SCROLL_UP : SELECTION_SCROLL_DOWN, - selection_col); + auto_scroll_direction, selection_col); } if (cursor_is_on_new_cell || term->selection.end.row < 0) diff --git a/selection.c b/selection.c index c774c713..20ed011c 100644 --- a/selection.c +++ b/selection.c @@ -817,6 +817,9 @@ fdm_scroll_timer(struct fdm *fdm, int fd, int events, void *data) } switch (term->selection.auto_scroll.direction) { + case SELECTION_SCROLL_NOT: + return true; + case SELECTION_SCROLL_UP: for (uint64_t i = 0; i < expiration_count; i++) cmd_scrollback_up(term, 1); @@ -838,6 +841,8 @@ void selection_start_scroll_timer(struct terminal *term, int interval_ns, enum selection_scroll_direction direction, int col) { + assert(direction != SELECTION_SCROLL_NOT); + if (!term->selection.ongoing) return; @@ -875,7 +880,6 @@ selection_start_scroll_timer(struct terminal *term, int interval_ns, term->selection.auto_scroll.direction = direction; term->selection.auto_scroll.col = col; - return; err: @@ -886,11 +890,14 @@ err: void selection_stop_scroll_timer(struct terminal *term) { - if (term->selection.auto_scroll.fd < 0) + if (term->selection.auto_scroll.fd < 0) { + assert(term->selection.auto_scroll.direction == SELECTION_SCROLL_NOT); return; + } fdm_del(term->fdm, term->selection.auto_scroll.fd); term->selection.auto_scroll.fd = -1; + term->selection.auto_scroll.direction = SELECTION_SCROLL_NOT; } static void diff --git a/terminal.h b/terminal.h index 0a71abb4..544d67a7 100644 --- a/terminal.h +++ b/terminal.h @@ -182,7 +182,7 @@ enum cursor_style { CURSOR_BLOCK, CURSOR_UNDERLINE, CURSOR_BAR }; enum selection_kind { SELECTION_NONE, SELECTION_NORMAL, SELECTION_BLOCK }; enum selection_direction {SELECTION_UNDIR, SELECTION_LEFT, SELECTION_RIGHT}; -enum selection_scroll_direction {SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; +enum selection_scroll_direction {SELECTION_SCROLL_NOT, SELECTION_SCROLL_UP, SELECTION_SCROLL_DOWN}; struct ptmx_buffer { void *data; -- cgit v1.3