diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 1a804bc64..022d24de6 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -3844,8 +3844,10 @@ rendering and event handling framework. The class defines the following attributes: -:visible: Specifies that the view should be painted. -:active: Specifies that the view should receive events, if also visible. +:visible: Specifies that the view should be painted. This can be a boolean or + a function that returns a boolean. +:active: Specifies that the view should receive events, if also visible. This + can be a boolean or a function that returns a boolean. :view_id: Specifies an identifier to easily identify the view among subviews. This is reserved for use by script writers and should not be set by library widgets for their internal subviews. diff --git a/library/lua/gui.lua b/library/lua/gui.lua index d817a7a0e..fb274ebec 100644 --- a/library/lua/gui.lua +++ b/library/lua/gui.lua @@ -2,7 +2,10 @@ local _ENV = mkmodule('gui') +local utils = require('utils') + local dscreen = dfhack.screen +local getval = utils.getval USE_GRAPHICS = dscreen.inGraphicsMode() @@ -514,7 +517,7 @@ end function View:renderSubviews(dc) for _,child in ipairs(self.subviews) do - if child.visible then + if getval(child.visible) then child:render(dc) end end @@ -556,7 +559,7 @@ local function should_send_input_to_focus_owner(view, focus_owner) end iter = focus_owner while iter do - if not iter.visible or not iter.active then + if not getval(iter.visible) or not getval(iter.active) then return false end iter = iter.parent_view @@ -576,8 +579,8 @@ function View:inputToSubviews(keys) for i=#children,1,-1 do local child = children[i] - if child.visible and child.active and child ~= focus_owner and - child:onInput(keys) then + if getval(child.visible) and getval(child.active) + and child ~= focus_owner and child:onInput(keys) then return true end end diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 9d42edd45..3779f8954 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -6,6 +6,7 @@ local gui = require('gui') local utils = require('utils') local dscreen = dfhack.screen +local getval = utils.getval local function show_view(view,vis) if view then @@ -13,14 +14,6 @@ local function show_view(view,vis) end end -local function getval(obj) - if type(obj) == 'function' then - return obj() - else - return obj - end -end - local function map_opttab(tab,idx) if tab then return tab[idx] @@ -109,7 +102,7 @@ function Panel:postUpdateLayout() for _,subview in ipairs(self.subviews) do if not subview.frame then goto continue end subview.frame.t = y - if subview.visible then + if getval(subview.visible) then y = y + (subview.frame.h or 0) + gap end ::continue:: @@ -137,7 +130,7 @@ ResizingPanel = defclass(ResizingPanel, Panel) function ResizingPanel:postUpdateLayout(frame_body) local w, h = 0, 0 for _,s in ipairs(self.subviews) do - if s.visible then + if getval(s.visible) then w = math.max(w, (s.frame and s.frame.l or 0) + (s.frame and s.frame.w or frame_body.width)) h = math.max(h, (s.frame and s.frame.t or 0) + @@ -265,7 +258,7 @@ function EditField:onRenderBody(dc) dc:pen(self.text_pen or COLOR_LIGHTCYAN):fill(0,0,dc.width-1,0) local cursor_char = '_' - if not self.active or not self.focus or gui.blink_visible(300) then + if not getval(self.active) or not self.focus or gui.blink_visible(300) then cursor_char = (self.cursor > #self.text) and ' ' or self.text:sub(self.cursor, self.cursor) end @@ -907,8 +900,8 @@ TooltipLabel.ATTRS{ text_pen=COLOR_GREY, } -function TooltipLabel:preUpdateLayout() - self.visible = getval(self.show_tooltip) +function TooltipLabel:init() + self.visible = self.show_tooltip end ----------------- @@ -1217,7 +1210,7 @@ function List:onRenderBody(dc) local cur_dpen = self.text_pen local active_pen = current and cur_pen or cur_dpen - if not self.active then + if not getval(self.active) then cur_pen = self.inactive_pen or self.cursor_pen end diff --git a/library/lua/utils.lua b/library/lua/utils.lua index 00e31929f..2e03a8616 100644 --- a/library/lua/utils.lua +++ b/library/lua/utils.lua @@ -2,6 +2,14 @@ local _ENV = mkmodule('utils') local df = df +function getval(obj) + if type(obj) == 'function' then + return obj() + else + return obj + end +end + -- Comparator function function compare(a,b) if a < b then