diff --git a/src/ltui/textarea.lua b/src/ltui/textarea.lua index 54de807..98e1ddd 100644 --- a/src/ltui/textarea.lua +++ b/src/ltui/textarea.lua @@ -67,9 +67,14 @@ function textarea:text_set(text) return label.text_set(self, text) end +-- is scrollable? +function textarea:scrollable() + return self._LINECOUNT > self:height() +end + -- scroll function textarea:scroll(lines) - if self._LINECOUNT > self:height() then + if self:scrollable() then self._STARTLINE = self._STARTLINE + lines if self._STARTLINE < 0 then self._STARTLINE = 0 @@ -85,7 +90,7 @@ end -- scroll to end function textarea:scroll_to_end() - if self._LINECOUNT > self:height() then + if self:scrollable() then local startline_end = self._LINECOUNT - self:height() self._STARTLINE = startline_end self:action_on(action.ac_on_scrolled, self._STARTLINE / startline_end) diff --git a/src/ltui/textdialog.lua b/src/ltui/textdialog.lua index d88599f..cfec53c 100644 --- a/src/ltui/textdialog.lua +++ b/src/ltui/textdialog.lua @@ -59,9 +59,22 @@ function textdialog:init(name, bounds, title) end end) + -- show scrollbar? + self:text():action_set(action.ac_on_text_changed, function (v) + if self:option("scrollable") then + if v:scrollable() then + self:scrollbar():show(true) + else + self:scrollbar():show(false) + end + end + end) + -- on scroll for text and scrollbar self:text():action_set(action.ac_on_scrolled, function (v, progress) - self:scrollbar():progress_set(progress) + if self:option("scrollable") then + self:scrollbar():progress_set(progress) + end end) end @@ -72,12 +85,9 @@ function textdialog:option_set(name, value) if value ~= oldvalue then if value then self:text():bounds():resize(self:panel():width() - 1, self:panel():height() - 1) - self:scrollbar():show(true) else self:text():bounds():resize(self:panel():width(), self:panel():height() - 1) - self:scrollbar():show(false) end - self:invalidate() end end dialog.option_set(self, name, value) diff --git a/tests/dialog.lua b/tests/dialog.lua index 1e8371d..68dec0f 100644 --- a/tests/dialog.lua +++ b/tests/dialog.lua @@ -76,6 +76,7 @@ function demo:dialog_help() local dialog_help = self._DIALOG_HELP if not dialog_help then dialog_help = textdialog:new("dialog.help", rect {1, 1, self:width() - 1, self:height() - 1}, "README") + dialog_help:option_set("scrollable", true) local helptext = nil local file = io.open("./LICENSE.md", 'r') if file then @@ -86,7 +87,6 @@ function demo:dialog_help() dialog_help:text():text_set(helptext) end dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) end) - dialog_help:option_set("scrollable", true) self._DIALOG_HELP = dialog_help end return dialog_help