Clamp scrolling to document bounds w/scroll past end

This commit is contained in:
ForLoveOfCats 2020-01-09 14:18:21 -05:00
parent 13c2f55019
commit d771faa0e9
2 changed files with 28 additions and 12 deletions

View File

@ -127,7 +127,7 @@ end
function DocView:get_scrollable_size()
return self:get_line_height() * #self.doc.lines + style.padding.y * 2
return self:get_line_height() * (#self.doc.lines-1) + style.padding.y * 2
end
@ -236,6 +236,7 @@ function DocView:scroll_to_line(line, ignore_if_visible, instant)
self.scroll.y = self.scroll.to.y
end
end
self:clamp_scroll()
end
@ -248,6 +249,7 @@ function DocView:scroll_to_make_visible(line, col)
local xoffset = self:get_col_x_offset(line, col)
local max = xoffset - self.size.x + gw + self.size.x / 5
self.scroll.to.x = math.max(0, max)
self:clamp_scroll()
end

View File

@ -50,17 +50,18 @@ end
function View:get_scrollbar_rect()
local sz = self:get_scrollable_size()
if sz <= self.size.y then
return 0, 0, 0, 0
end
local h = math.max(20, self.size.y * self.size.y / sz)
local bar_width = style.scrollbar_size
local scrollable = self:get_scrollable_size() + self.size.y - style.padding.y
local scale_y = self.size.y / scrollable
local top_y_offset = math.ceil(scale_y * self.scroll.y)
return
self.position.x + self.size.x - style.scrollbar_size,
self.position.y + self.scroll.y * (self.size.y - h) / (sz - self.size.y),
style.scrollbar_size,
h
end
self.position.x + self.size.x - bar_width,
self.position.y + top_y_offset,
bar_width,
math.max(20, self.size.y * scale_y + style.padding.y)
end
function View:scrollbar_overlaps_point(x, y)
@ -82,10 +83,22 @@ function View:on_mouse_released(button, x, y)
end
function View:clamp_scroll()
local max_scroll = self:get_scrollable_size() - style.padding.y
if self.scroll.to.y < 0 or max_scroll <= style.padding.y then
self.scroll.to.y = 0
elseif self.scroll.to.y > max_scroll then
self.scroll.to.y = max_scroll
end
end
function View:on_mouse_moved(x, y, dx, dy)
if self.dragging_scrollbar then
local delta = self:get_scrollable_size() / self.size.y * dy
local scrollable = self:get_scrollable_size() + self.size.y - style.padding.y
local delta = scrollable / self.size.y * dy
self.scroll.to.y = self.scroll.to.y + delta
self:clamp_scroll()
end
self.hovered_scrollbar = self:scrollbar_overlaps_point(x, y)
end
@ -99,6 +112,7 @@ end
function View:on_mouse_wheel(y)
if self.scrollable then
self.scroll.to.y = self.scroll.to.y + y * -config.mouse_wheel_scroll
self:clamp_scroll()
end
end