create "pseudo-widget" for option groups

This commit is contained in:
airstruck
2016-01-28 21:28:30 -05:00
parent 2e91a62426
commit e53f470bb9
4 changed files with 37 additions and 9 deletions

View File

@@ -89,11 +89,12 @@ return { id = 'mainWindow',
{ type = 'check', text = 'Vertical controls', id = 'flowToggle', },
{ type = 'label', text = 'Some radio widgets' },
{
{ type = 'radio', text = 'One fish' },
{ type = 'radio', text = 'Two fish' },
{ type = 'radio', text = 'Red fish' },
{ type = 'radio', text = 'Blue fish' },
{ type = 'radio', group = 'fish', text = 'One fish' },
{ type = 'radio', group = 'fish', text = 'Two fish' },
{ type = 'radio', group = 'fish', text = 'Red fish' },
{ type = 'radio', group = 'fish', text = 'Blue fish' },
},
{ type = 'label', id = 'fishStatus' },
}
},
},

View File

@@ -96,6 +96,10 @@ layout.sans2:onPress(function()
layout.stepper.font = false
end)
layout.fish:onChange(function()
layout.fishStatus.text = 'Selected: ' .. layout.fish.selected.text
end)
-- show the main layout
layout:show()

View File

@@ -236,6 +236,10 @@ local function metaCall (Widget, layout, self)
return self
end
function Widget:getMasterLayout ()
return self.layout.master or self.layout
end
--[[--
Define a custom attribute for this widget.

View File

@@ -27,21 +27,26 @@ local function setGroup (self, value)
local oldGroup = oldValue and groups[oldValue]
if oldGroup then
remove(oldGroup, self)
if #oldGroup < 1 then
groups[oldValue] = nil
end
-- TODO: is it safe to remove these?
if #oldGroup < 1 then groups[oldValue] = nil end
end
-- add the widget to the new group, or 'default' if no group specified
value = value or 'default'
-- add the widget to the new group, or 'defaultGroup' if no group specified
value = value or 'defaultGroup'
if not groups[value] then
groups[value] = {}
end
local group = groups[value]
group[#group + 1] = self
self.attributes.group = value
local layout = self:getMasterLayout()
if not layout[value] then
layout:createWidget { id = value, items = group }
end
self.groupWidget = layout[value]
end
return function (self)
--[[--
Special Attributes
@@ -64,6 +69,14 @@ in the same group change to `false`.
@section end
--]]--
-- when we bubble events, send them to the group widget
local bubbleEvent = self.bubbleEvent
function self:bubbleEvent (...)
local result = bubbleEvent(self, ...)
if result ~= nil then return result end
return self.groupWidget:bubbleEvent(...)
end
self:onPress(function (event)
if event.button ~= 'left' then return end
for _, widget in ipairs(groups[self.group]) do
@@ -71,5 +84,11 @@ in the same group change to `false`.
end
end)
self:onChange(function (event)
-- change event is only sent to group widget once.
if not self.value then return false end
self.groupWidget.selected = self
end)
self.value = not not self.value
end