From fd2ece278b0941ec6673489e88868120e86b834a Mon Sep 17 00:00:00 2001 From: Matthias Deiml Date: Sun, 12 Mar 2023 23:58:46 +0100 Subject: feat(ui): add scroll_delta to win_viewport event #19270 scroll_delta contains how much the top line of a window moved since the last time win_viewport was emitted. It is expected to be used to implement smooth scrolling. For this purpose it only counts "virtual" or "displayed" so folds should count as one line. Because of this it adds extra information that cannot be computed from the topline parameter. Fixes #19227 --- src/nvim/api/ui_events.in.h | 2 +- src/nvim/buffer_defs.h | 1 + src/nvim/window.c | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/api/ui_events.in.h b/src/nvim/api/ui_events.in.h index a08e8dbfeb..b93f521ca3 100644 --- a/src/nvim/api/ui_events.in.h +++ b/src/nvim/api/ui_events.in.h @@ -114,7 +114,7 @@ void msg_set_pos(Integer grid, Integer row, Boolean scrolled, String sep_char) FUNC_API_SINCE(6) FUNC_API_COMPOSITOR_IMPL FUNC_API_CLIENT_IGNORE; void win_viewport(Integer grid, Window win, Integer topline, Integer botline, Integer curline, - Integer curcol, Integer line_count) + Integer curcol, Integer line_count, Integer scroll_delta) FUNC_API_SINCE(7) FUNC_API_CLIENT_IGNORE; void win_extmark(Integer grid, Window win, Integer ns_id, Integer mark_id, Integer row, Integer col) diff --git a/src/nvim/buffer_defs.h b/src/nvim/buffer_defs.h index 6122136a75..4bdf5aac64 100644 --- a/src/nvim/buffer_defs.h +++ b/src/nvim/buffer_defs.h @@ -1222,6 +1222,7 @@ struct window_S { colnr_T w_valid_leftcol; // last known w_leftcol bool w_viewport_invalid; + linenr_T w_viewport_last_topline; // topline when the viewport was last updated // w_cline_height is the number of physical lines taken by the buffer line // that the cursor is on. We use this to avoid extra calls to plines_win(). diff --git a/src/nvim/window.c b/src/nvim/window.c index 38b7326363..39e9efbabb 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -993,10 +993,22 @@ void ui_ext_win_viewport(win_T *wp) // interact with incomplete final line? Diff filler lines? botline = wp->w_buffer->b_ml.ml_line_count; } + int scroll_delta = 0; + if (wp->w_viewport_last_topline > line_count) { + scroll_delta -= wp->w_viewport_last_topline - line_count; + wp->w_viewport_last_topline = line_count; + } + if (wp->w_topline < wp->w_viewport_last_topline) { + scroll_delta -= plines_m_win(wp, wp->w_topline, wp->w_viewport_last_topline - 1); + } else if (wp->w_topline > wp->w_viewport_last_topline + && wp->w_topline <= line_count) { + scroll_delta += plines_m_win(wp, wp->w_viewport_last_topline, wp->w_topline - 1); + } ui_call_win_viewport(wp->w_grid_alloc.handle, wp->handle, wp->w_topline - 1, botline, wp->w_cursor.lnum - 1, wp->w_cursor.col, - line_count); + line_count, scroll_delta); wp->w_viewport_invalid = false; + wp->w_viewport_last_topline = wp->w_topline; } } @@ -5038,6 +5050,7 @@ static win_T *win_alloc(win_T *after, bool hidden) new_wp->w_floating = 0; new_wp->w_float_config = FLOAT_CONFIG_INIT; new_wp->w_viewport_invalid = true; + new_wp->w_viewport_last_topline = 1; new_wp->w_ns_hl = -1; -- cgit