Make `DocView` aware of scrollbars sizes (#1177)

* Make `DocView:scroll_to_make_visible` aware of vertical scrollbar width

* Make `DocView` aware of horizontal scrollbar size
This commit is contained in:
Guldoman 2023-08-04 16:56:49 +02:00 committed by takase1121
parent c279ef0034
commit 86cfbe5f3b
No known key found for this signature in database
GPG Key ID: 60EEFFC68EB3031B
1 changed files with 11 additions and 6 deletions

View File

@ -112,7 +112,8 @@ end
function DocView:get_scrollable_size()
if not config.scroll_past_end then
return self:get_line_height() * (#self.doc.lines) + style.padding.y * 2
local _, _, _, h_scroll = self.h_scrollbar:get_track_rect()
return self:get_line_height() * (#self.doc.lines) + style.padding.y * 2 + h_scroll
end
return self:get_line_height() * (#self.doc.lines - 1) + self.size.y
end
@ -244,7 +245,8 @@ function DocView:scroll_to_line(line, ignore_if_visible, instant)
if not (ignore_if_visible and line > min and line < max) then
local x, y = self:get_line_screen_position(line)
local ox, oy = self:get_content_offset()
self.scroll.to.y = math.max(0, y - oy - self.size.y / 2)
local _, _, _, scroll_h = self.h_scrollbar:get_track_rect()
self.scroll.to.y = math.max(0, y - oy - (self.size.y - scroll_h) / 2)
if instant then
self.scroll.y = self.scroll.to.y
end
@ -253,17 +255,20 @@ end
function DocView:scroll_to_make_visible(line, col)
local ox, oy = self:get_content_offset()
local _, oy = self:get_content_offset()
local _, ly = self:get_line_screen_position(line, col)
local lh = self:get_line_height()
self.scroll.to.y = common.clamp(self.scroll.to.y, ly - oy - self.size.y + lh * 2, ly - oy - lh)
local _, _, _, scroll_h = self.h_scrollbar:get_track_rect()
self.scroll.to.y = common.clamp(self.scroll.to.y, ly - oy - self.size.y + scroll_h + lh * 2, ly - oy - lh)
local gw = self:get_gutter_width()
local xoffset = self:get_col_x_offset(line, col)
local xmargin = 3 * self:get_font():get_width(' ')
local xsup = xoffset + gw + xmargin
local xinf = xoffset - xmargin
if xsup > self.scroll.x + self.size.x then
self.scroll.to.x = xsup - self.size.x
local _, _, scroll_w = self.v_scrollbar:get_track_rect()
local size_x = math.max(0, self.size.x - scroll_w)
if xsup > self.scroll.x + size_x then
self.scroll.to.x = xsup - size_x
elseif xinf < self.scroll.x then
self.scroll.to.x = math.max(0, xinf)
end