diff --git a/src/ltui/scrollbar.lua b/src/ltui/scrollbar.lua index 8f22290..94fd51e 100644 --- a/src/ltui/scrollbar.lua +++ b/src/ltui/scrollbar.lua @@ -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) diff --git a/src/ltui/textarea.lua b/src/ltui/textarea.lua index 296e3e3..54de807 100644 --- a/src/ltui/textarea.lua +++ b/src/ltui/textarea.lua @@ -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 diff --git a/src/ltui/textdialog.lua b/src/ltui/textdialog.lua index 56c0920..d88599f 100644 --- a/src/ltui/textdialog.lua +++ b/src/ltui/textdialog.lua @@ -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