improve textdialog

This commit is contained in:
ruki
2020-11-28 20:41:03 +08:00
parent 1f9076fbd7
commit 0903f700ca
8 changed files with 53 additions and 32 deletions

View File

@@ -40,7 +40,6 @@ function boxdialog:init(name, bounds, title)
self:text():bounds().ey = self._TEXT_EY self:text():bounds().ey = self._TEXT_EY
self:text():invalidate(true) self:text():invalidate(true)
self:text():option_set("selectable", false) self:text():option_set("selectable", false)
self:text():option_set("scrollable", false)
-- insert box -- insert box
self:panel():insert(self:box()) self:panel():insert(self:box())

View File

@@ -45,7 +45,6 @@ function inputdialog:init(name, bounds, title)
self:text():bounds().ey = 1 self:text():bounds().ey = 1
self:text():invalidate(true) self:text():invalidate(true)
self:text():option_set("selectable", false) self:text():option_set("selectable", false)
self:text():option_set("scrollable", false)
-- text changed -- text changed
self:text():action_set(action.ac_on_text_changed, function (v) self:text():action_set(action.ac_on_text_changed, function (v)

View File

@@ -122,6 +122,7 @@ function mconfdialog:helpdialog()
if not self._HELPDIALOG then if not self._HELPDIALOG then
local helpdialog = textdialog:new("mconfdialog.help", self:bounds(), "help") local helpdialog = textdialog:new("mconfdialog.help", self:bounds(), "help")
helpdialog:button_add("exit", "< Exit >", function (v) helpdialog:quit() end) helpdialog:button_add("exit", "< Exit >", function (v) helpdialog:quit() end)
helpdialog:option_set("scrollable", true)
self._HELPDIALOG = helpdialog self._HELPDIALOG = helpdialog
end end
return self._HELPDIALOG return self._HELPDIALOG
@@ -132,6 +133,7 @@ function mconfdialog:resultdialog()
if not self._RESULTDIALOG then if not self._RESULTDIALOG then
local resultdialog = textdialog:new("mconfdialog.result", self:bounds(), "result") local resultdialog = textdialog:new("mconfdialog.result", self:bounds(), "result")
resultdialog:button_add("exit", "< Exit >", function (v) resultdialog:quit() end) resultdialog:button_add("exit", "< Exit >", function (v) resultdialog:quit() end)
resultdialog:option_set("scrollable", true)
self._RESULTDIALOG = resultdialog self._RESULTDIALOG = resultdialog
end end
return self._RESULTDIALOG return self._RESULTDIALOG

View File

@@ -35,7 +35,7 @@ function scrollbar:init(name, bounds, vertical)
view.init(self, name, bounds) view.init(self, name, bounds)
-- init bar attribute -- init bar attribute
self:charattr_set("black") self:charattr_set("black on black")
-- init bar vertical -- init bar vertical
self:vertical_set(vertical) self:vertical_set(vertical)
@@ -165,14 +165,18 @@ function scrollbar:on_draw(transparent)
if self:vertical() then if self:vertical() then
local sb = math.floor(self:height() * pb) local sb = math.floor(self:height() * pb)
local se = math.ceil(self:height() * pe) local se = math.ceil(self:height() * pe)
if se > sb and se - sb < self:height() then if se > sb and se - sb <= self:height() then
self:canvas():attr(charattr):move(0, sb):putchar(char, se - sb, true) for x = 0, self:width() - 1 do
self:canvas():attr(charattr):move(x, sb):putchar(char, se - sb, true)
end
end end
else else
local sb = math.floor(self:width() * pb) local sb = math.floor(self:width() * pb)
local se = math.ceil(self:width() * pe) local se = math.ceil(self:width() * pe)
if se > sb and se - sb < self:width() then if se > sb and se - sb <= self:width() then
self:canvas():attr(charattr):move(sb, 0):putchar(char, se - sb) for y = 0, self:height() - 1 do
self:canvas():attr(charattr):move(sb, y):putchar(char, se - sb)
end
end end
end end
end end

View File

@@ -25,7 +25,6 @@ local label = require("ltui/label")
local event = require("ltui/event") local event = require("ltui/event")
local curses = require("ltui/curses") local curses = require("ltui/curses")
local action = require("ltui/action") local action = require("ltui/action")
local scrollbar = require("ltui/scrollbar")
-- define module -- define module
local textarea = textarea or label() local textarea = textarea or label()
@@ -39,9 +38,6 @@ function textarea:init(name, bounds, text)
-- mark as selectable -- mark as selectable
self:option_set("selectable", true) self:option_set("selectable", true)
-- mark as scrollable
self:option_set("scrollable", true)
-- init start line -- init start line
self._STARTLINE = 0 self._STARTLINE = 0
self._LINECOUNT = 0 self._LINECOUNT = 0
@@ -61,17 +57,6 @@ function textarea:on_draw(transparent)
if strs and #strs > 0 and textattr then if strs and #strs > 0 and textattr then
self:canvas():attr(textattr):move(0, 0):putstrs(strs, self._STARTLINE + 1) self:canvas():attr(textattr):move(0, 0):putstrs(strs, self._STARTLINE + 1)
end end
-- draw scrollable
if self:option("scrollable") then
local tb = self._STARTLINE
local fator = self:height() / self._LINECOUNT
local sb = math.min(math.floor(tb * fator), self:height() - 1)
local se = math.min(sb + math.ceil(self:height() * fator), self:height())
if se > sb and se - sb < self:height() then
self:canvas():attr("black"):move(self:width() - 1, sb):putchar(' ', se - sb, true)
end
end
end end
-- set text -- set text

View File

@@ -19,13 +19,14 @@
-- --
-- load modules -- load modules
local log = require("ltui/base/log") local log = require("ltui/base/log")
local rect = require("ltui/rect") local rect = require("ltui/rect")
local event = require("ltui/event") local event = require("ltui/event")
local dialog = require("ltui/dialog") local dialog = require("ltui/dialog")
local curses = require("ltui/curses") local curses = require("ltui/curses")
local textarea = require("ltui/textarea") local textarea = require("ltui/textarea")
local action = require("ltui/action") local scrollbar = require("ltui/scrollbar")
local action = require("ltui/action")
-- define module -- define module
local textdialog = textdialog or dialog() local textdialog = textdialog or dialog()
@@ -36,9 +37,15 @@ function textdialog:init(name, bounds, title)
-- init window -- init window
dialog.init(self, name, bounds, title) dialog.init(self, name, bounds, title)
-- mark as scrollable, disabled by default
self:option_set("scrollable", false)
-- insert text -- insert text
self:panel():insert(self:text()) self:panel():insert(self:text())
-- insert scrollbar
self:panel():insert(self:scrollbar())
-- select buttons by default -- select buttons by default
self:panel():select(self:buttons()) self:panel():select(self:buttons())
@@ -48,6 +55,24 @@ function textdialog:init(name, bounds, title)
end) end)
end end
-- enable or disable scrollbar
function textdialog:option_set(name, value)
if name == "scrollable" then
local oldvalue = self:option(name)
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)
end
-- get text -- get text
function textdialog:text() function textdialog:text()
if not self._TEXT then if not self._TEXT then
@@ -56,6 +81,15 @@ function textdialog:text()
return self._TEXT return self._TEXT
end end
-- get scrollbar
function textdialog:scrollbar()
if not self._SCROLLBAR then
self._SCROLLBAR = scrollbar:new("textdialog.scrollbar", rect:new(self:panel():width() - 1, 0, 1, self:panel():height() - 1))
self._SCROLLBAR:show(false)
end
return self._SCROLLBAR
end
-- on event -- on event
function textdialog:on_event(e) function textdialog:on_event(e)

View File

@@ -47,9 +47,6 @@ function textedit:init(name, bounds, text)
self:option_set("mouseable", true) self:option_set("mouseable", true)
self:action_set(action.ac_on_clicked, function () return true end) self:action_set(action.ac_on_clicked, function () return true end)
-- disable scrollbar
self:option_set("scrollable", false)
-- enable multiple line -- enable multiple line
self:option_set("multiline", true) self:option_set("multiline", true)
end end

View File

@@ -86,6 +86,7 @@ function demo:dialog_help()
dialog_help:text():text_set(helptext) dialog_help:text():text_set(helptext)
end end
dialog_help:button_add("exit", "< Exit >", function (v) self:remove(dialog_help) 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 self._DIALOG_HELP = dialog_help
end end
return dialog_help return dialog_help