Merge pull request #2462 from myk002/myk_dynamic_view_props

[gui.View] give visible and active attributes the ability to have dynamic values
develop
Myk 2022-12-04 15:55:35 -08:00 committed by GitHub
commit 5cfda380a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 27 additions and 20 deletions

@ -44,6 +44,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## API ## API
## Lua ## Lua
- ``gui.View``: ``visible`` and ``active`` can now be functions that return a boolean
## Internals ## Internals

@ -3844,8 +3844,10 @@ rendering and event handling framework.
The class defines the following attributes: The class defines the following attributes:
:visible: Specifies that the view should be painted. :visible: Specifies that the view should be painted. This can be a boolean or
:active: Specifies that the view should receive events, if also visible. 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. :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 This is reserved for use by script writers and should not be set by
library widgets for their internal subviews. library widgets for their internal subviews.

@ -2,7 +2,10 @@
local _ENV = mkmodule('gui') local _ENV = mkmodule('gui')
local utils = require('utils')
local dscreen = dfhack.screen local dscreen = dfhack.screen
local getval = utils.getval
USE_GRAPHICS = dscreen.inGraphicsMode() USE_GRAPHICS = dscreen.inGraphicsMode()
@ -514,7 +517,7 @@ end
function View:renderSubviews(dc) function View:renderSubviews(dc)
for _,child in ipairs(self.subviews) do for _,child in ipairs(self.subviews) do
if child.visible then if getval(child.visible) then
child:render(dc) child:render(dc)
end end
end end
@ -556,7 +559,7 @@ local function should_send_input_to_focus_owner(view, focus_owner)
end end
iter = focus_owner iter = focus_owner
while iter do 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 return false
end end
iter = iter.parent_view iter = iter.parent_view
@ -576,8 +579,8 @@ function View:inputToSubviews(keys)
for i=#children,1,-1 do for i=#children,1,-1 do
local child = children[i] local child = children[i]
if child.visible and child.active and child ~= focus_owner and if getval(child.visible) and getval(child.active)
child:onInput(keys) then and child ~= focus_owner and child:onInput(keys) then
return true return true
end end
end end

@ -6,6 +6,7 @@ local gui = require('gui')
local utils = require('utils') local utils = require('utils')
local dscreen = dfhack.screen local dscreen = dfhack.screen
local getval = utils.getval
local function show_view(view,vis) local function show_view(view,vis)
if view then if view then
@ -13,14 +14,6 @@ local function show_view(view,vis)
end end
end end
local function getval(obj)
if type(obj) == 'function' then
return obj()
else
return obj
end
end
local function map_opttab(tab,idx) local function map_opttab(tab,idx)
if tab then if tab then
return tab[idx] return tab[idx]
@ -109,7 +102,7 @@ function Panel:postUpdateLayout()
for _,subview in ipairs(self.subviews) do for _,subview in ipairs(self.subviews) do
if not subview.frame then goto continue end if not subview.frame then goto continue end
subview.frame.t = y subview.frame.t = y
if subview.visible then if getval(subview.visible) then
y = y + (subview.frame.h or 0) + gap y = y + (subview.frame.h or 0) + gap
end end
::continue:: ::continue::
@ -137,7 +130,7 @@ ResizingPanel = defclass(ResizingPanel, Panel)
function ResizingPanel:postUpdateLayout(frame_body) function ResizingPanel:postUpdateLayout(frame_body)
local w, h = 0, 0 local w, h = 0, 0
for _,s in ipairs(self.subviews) do 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) + w = math.max(w, (s.frame and s.frame.l or 0) +
(s.frame and s.frame.w or frame_body.width)) (s.frame and s.frame.w or frame_body.width))
h = math.max(h, (s.frame and s.frame.t or 0) + 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) dc:pen(self.text_pen or COLOR_LIGHTCYAN):fill(0,0,dc.width-1,0)
local cursor_char = '_' 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 cursor_char = (self.cursor > #self.text) and ' ' or
self.text:sub(self.cursor, self.cursor) self.text:sub(self.cursor, self.cursor)
end end
@ -907,8 +900,8 @@ TooltipLabel.ATTRS{
text_pen=COLOR_GREY, text_pen=COLOR_GREY,
} }
function TooltipLabel:preUpdateLayout() function TooltipLabel:init()
self.visible = getval(self.show_tooltip) self.visible = self.show_tooltip
end end
----------------- -----------------
@ -1217,7 +1210,7 @@ function List:onRenderBody(dc)
local cur_dpen = self.text_pen local cur_dpen = self.text_pen
local active_pen = current and cur_pen or cur_dpen 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 cur_pen = self.inactive_pen or self.cursor_pen
end end

@ -2,6 +2,14 @@ local _ENV = mkmodule('utils')
local df = df local df = df
function getval(obj)
if type(obj) == 'function' then
return obj()
else
return obj
end
end
-- Comparator function -- Comparator function
function compare(a,b) function compare(a,b)
if a < b then if a < b then