summaryrefslogtreecommitdiff
path: root/xmake/core/ui/textdialog.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2020-11-29 22:36:04 +0800
committerruki <[email protected]>2020-11-29 22:36:04 +0800
commite4058387bfd67c02aa0e065705655b1e4b0aafbf (patch)
treed2dfd95343e3161a9e4dc55691e37abbca69b430 /xmake/core/ui/textdialog.lua
parentd48118e840fc0be69f3d78a3894e4bf76b8d801b (diff)
support scrollbar
Diffstat (limited to 'xmake/core/ui/textdialog.lua')
-rw-r--r--xmake/core/ui/textdialog.lua70
1 files changed, 62 insertions, 8 deletions
diff --git a/xmake/core/ui/textdialog.lua b/xmake/core/ui/textdialog.lua
index 52ee51cbd..cc4243740 100644
--- a/xmake/core/ui/textdialog.lua
+++ b/xmake/core/ui/textdialog.lua
@@ -19,13 +19,14 @@
--
-- load modules
-local log = require("ui/log")
-local rect = require("ui/rect")
-local event = require("ui/event")
-local dialog = require("ui/dialog")
-local curses = require("ui/curses")
-local textarea = require("ui/textarea")
-local action = require("ui/action")
+local log = require("ui/log")
+local rect = require("ui/rect")
+local event = require("ui/event")
+local dialog = require("ui/dialog")
+local curses = require("ui/curses")
+local textarea = require("ui/textarea")
+local scrollbar = require("ui/scrollbar")
+local action = require("ui/action")
-- define module
local textdialog = textdialog or dialog()
@@ -36,18 +37,62 @@ function textdialog:init(name, bounds, title)
-- init window
dialog.init(self, name, bounds, title)
+ -- mark as scrollable, disabled by default
+ self:option_set("scrollable", false)
+
-- insert text
self:panel():insert(self:text())
+ -- insert scrollbar
+ self:panel():insert(self:scrollbar())
+
-- select buttons by default
self:panel():select(self:buttons())
-- on resize for panel
self:panel():action_add(action.ac_on_resized, function (v)
- self:text():bounds_set(rect:new(0, 0, v:width(), v:height() - 1))
+ if self:option("scrollable") then
+ self:text():bounds_set(rect:new(0, 0, v:width() - 1, v:height() - 1))
+ self:scrollbar():bounds_set(rect:new(v:width() - 1, 0, 1, v:height() - 1))
+ else
+ self:text():bounds_set(rect:new(0, 0, v:width(), v:height() - 1))
+ end
+ end)
+
+ -- show scrollbar?
+ self:text():action_add(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_add(action.ac_on_scrolled, function (v, progress)
+ if self:scrollbar():state("visible") then
+ self:scrollbar():progress_set(progress)
+ 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)
+ else
+ self:text():bounds():resize(self:panel():width(), self:panel():height() - 1)
+ end
+ end
+ end
+ dialog.option_set(self, name, value)
+end
+
-- get text
function textdialog:text()
if not self._TEXT then
@@ -56,6 +101,15 @@ function textdialog:text()
return self._TEXT
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
function textdialog:on_event(e)