mirror of
https://github.com/airstruck/luigi.git
synced 2026-01-10 16:28:23 +00:00
add Widget:defineAttribute, fixes #16
This commit is contained in:
@@ -53,7 +53,7 @@ end
|
|||||||
-- look for properties in attributes, Widget, style, and theme
|
-- look for properties in attributes, Widget, style, and theme
|
||||||
local function metaIndex (self, property)
|
local function metaIndex (self, property)
|
||||||
-- look in widget's own attributes
|
-- look in widget's own attributes
|
||||||
local A = Attribute[property]
|
local A = self.attributeDescriptors[property] or Attribute[property]
|
||||||
if A then
|
if A then
|
||||||
local value = A.get and A.get(self, property)
|
local value = A.get and A.get(self, property)
|
||||||
or self.attributes[property]
|
or self.attributes[property]
|
||||||
@@ -76,7 +76,7 @@ end
|
|||||||
|
|
||||||
-- setting attributes triggers special behavior
|
-- setting attributes triggers special behavior
|
||||||
local function metaNewIndex (self, property, value)
|
local function metaNewIndex (self, property, value)
|
||||||
local A = Attribute[property]
|
local A = self.attributeDescriptors[property] or Attribute[property]
|
||||||
if A then
|
if A then
|
||||||
if A.set then
|
if A.set then
|
||||||
A.set(self, value, property)
|
A.set(self, value, property)
|
||||||
@@ -121,6 +121,7 @@ local function metaCall (Widget, layout, self)
|
|||||||
self.position = { x = nil, y = nil }
|
self.position = { x = nil, y = nil }
|
||||||
self.dimensions = { width = nil, height = nil }
|
self.dimensions = { width = nil, height = nil }
|
||||||
self.attributes = {}
|
self.attributes = {}
|
||||||
|
self.attributeDescriptors = {}
|
||||||
|
|
||||||
setmetatable(self, { __index = metaIndex, __newindex = metaNewIndex })
|
setmetatable(self, { __index = metaIndex, __newindex = metaNewIndex })
|
||||||
|
|
||||||
@@ -138,6 +139,26 @@ local function metaCall (Widget, layout, self)
|
|||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--[[--
|
||||||
|
Define a custom attribute for this widget.
|
||||||
|
|
||||||
|
@tparam string name
|
||||||
|
The name of the attribute.
|
||||||
|
|
||||||
|
@tparam table descriptor
|
||||||
|
A table, optionally containing `get` and `set` functions (see `Attribute`).
|
||||||
|
|
||||||
|
@treturn Widget
|
||||||
|
Return this widget for chaining.
|
||||||
|
--]]--
|
||||||
|
function Widget:defineAttribute (name, descriptor)
|
||||||
|
self.attributeDescriptors[name] = descriptor
|
||||||
|
local value = rawget(self, name)
|
||||||
|
rawset(self, name, nil)
|
||||||
|
self[name] = value
|
||||||
|
return self
|
||||||
|
end
|
||||||
|
|
||||||
--[[--
|
--[[--
|
||||||
Fire an event on this widget and each ancestor.
|
Fire an event on this widget and each ancestor.
|
||||||
|
|
||||||
|
|||||||
@@ -15,22 +15,39 @@ standard themes, the widget's value should be indicated in some other way.
|
|||||||
@widget radio
|
@widget radio
|
||||||
--]]--
|
--]]--
|
||||||
|
|
||||||
-- TODO: make `group` a first-class attribute
|
|
||||||
local groups = {}
|
local groups = {}
|
||||||
|
|
||||||
return function (self)
|
local function remove (t, value)
|
||||||
local groupName = self.group or 'default'
|
for i, v in t do
|
||||||
|
if v == value then return table.remove(t, i) end
|
||||||
if not groups[groupName] then
|
|
||||||
groups[groupName] = {}
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local group = groups[groupName]
|
local function setGroup (self, value)
|
||||||
|
-- remove the widget from the old group
|
||||||
|
local oldValue = self.attributes.group
|
||||||
|
local oldGroup = oldValue and groups[oldValue]
|
||||||
|
if oldGroup then
|
||||||
|
remove(oldGroup, self)
|
||||||
|
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'
|
||||||
|
if not groups[value] then
|
||||||
|
groups[value] = {}
|
||||||
|
end
|
||||||
|
local group = groups[value]
|
||||||
group[#group + 1] = self
|
group[#group + 1] = self
|
||||||
|
self.attributes.group = value
|
||||||
|
end
|
||||||
|
|
||||||
|
return function (self)
|
||||||
|
self:defineAttribute('group', { set = setGroup })
|
||||||
|
|
||||||
self:onPress(function ()
|
self:onPress(function ()
|
||||||
for _, widget in ipairs(group) do
|
for _, widget in ipairs(groups[self.group]) do
|
||||||
widget.value = widget == self
|
widget.value = widget == self
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user