improve scrollbar

This commit is contained in:
ruki
2020-11-28 21:32:23 +08:00
parent 42b9a690af
commit 49526b95cb
3 changed files with 27 additions and 18 deletions

View File

@@ -149,30 +149,30 @@ function scrollbar:on_draw(transparent)
-- draw background
view.on_draw(self, transparent)
-- compute bar range
-- draw bar
local char = self:char()
local charattr = self:charattr_val()
local progress = self:progress()
local stepwidth = self:stepwidth()
local pb = progress
local pe = progress + stepwidth
if pe > 1 then
pb = 1 - stepwidth
pe = 1
end
-- draw bar
if self:vertical() then
local sb = math.floor(self:height() * pb)
local se = math.ceil(self:height() * pe)
local sn = math.ceil(self:height() * self:stepwidth())
local sb = math.floor(self:height() * self:progress())
local se = sb + sn
if se > self:height() then
sb = self:height() - sn
se = self:height()
end
if se > sb and se - sb <= self:height() then
for x = 0, self:width() - 1 do
self:canvas():attr(charattr):move(x, sb):putchar(char, se - sb, true)
end
end
else
local sb = math.floor(self:width() * pb)
local se = math.ceil(self:width() * pe)
local sn = math.ceil(self:width() * self:stepwidth())
local sb = math.floor(self:width() * self:progress())
local se = sb + sn
if se > self:width() then
sb = self:width() - sn
se = self:width()
end
if se > sb and se - sb <= self:width() then
for y = 0, self:height() - 1 do
self:canvas():attr(charattr):move(sb, y):putchar(char, se - sb)

View File

@@ -74,9 +74,11 @@ function textarea:scroll(lines)
if self._STARTLINE < 0 then
self._STARTLINE = 0
end
if self._STARTLINE > self._LINECOUNT - self:height() then
self._STARTLINE = self._LINECOUNT - self:height()
local startline_end = self._LINECOUNT - self:height()
if self._STARTLINE > startline_end then
self._STARTLINE = startline_end
end
self:action_on(action.ac_on_scrolled, self._STARTLINE / startline_end)
self:invalidate()
end
end
@@ -84,7 +86,9 @@ end
-- scroll to end
function textarea:scroll_to_end()
if self._LINECOUNT > self:height() then
self._STARTLINE = self._LINECOUNT - self:height()
local startline_end = self._LINECOUNT - self:height()
self._STARTLINE = startline_end
self:action_on(action.ac_on_scrolled, self._STARTLINE / startline_end)
self:invalidate()
end
end

View File

@@ -58,6 +58,11 @@ function textdialog:init(name, bounds, title)
self:text():bounds_set(rect:new(0, 0, v:width(), v:height() - 1))
end
end)
-- on scroll for text and scrollbar
self:text():action_set(action.ac_on_scrolled, function (v, progress)
self:scrollbar():progress_set(progress)
end)
end
-- enable or disable scrollbar