improve textdialog

This commit is contained in:
ruki
2020-11-28 22:02:56 +08:00
parent 50434c9365
commit cd2a4c4bd6
3 changed files with 22 additions and 7 deletions

View File

@@ -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)

View File

@@ -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)
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)

View File

@@ -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