Skip to content

Commit

Permalink
Make DocView aware of scrollbars sizes (lite-xl#1177)
Browse files Browse the repository at this point in the history
* Make `DocView:scroll_to_make_visible` aware of vertical scrollbar width

* Make `DocView` aware of horizontal scrollbar size
  • Loading branch information
Guldoman authored and takase1121 committed Aug 15, 2023
1 parent 6e013c1 commit 8bf5cad
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions data/core/docview.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -258,17 +260,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
Expand Down

0 comments on commit 8bf5cad

Please sign in to comment.