Formalize type attribute and support it in styles

This commit is contained in:
airstruck
2015-12-02 15:48:21 -05:00
parent a902385d55
commit f03b5c7bd4
29 changed files with 302 additions and 344 deletions

View File

@@ -11,8 +11,26 @@ Setting attributes can have side effects. For example, setting
to recalculate their size and position.
--]]--
local ROOT = (...):gsub('[^.]*$', '')
local Attribute = {}
function Attribute.type (widget, value)
local oldType = widget.attributes.type
widget.attributes.type = value
if value and not widget.hasType then
widget.hasType = true
local Widget = require(ROOT .. 'widget')
local decorate = Widget.typeDecorators[value]
if decorate then
decorate(widget)
end
end
end
--[[--
widget identifier.

View File

@@ -58,6 +58,26 @@ function Layout:constructor (data, master)
Widget(self, data)
end
local function clearWidget (widget)
widget.textData = nil
widget.fontData = nil
widget.position = {}
widget.dimensions = {}
widget.type = widget.type
end
local function reset (self)
if not self.root then return end
local widget = self.root:getNextNeighbor()
clearWidget(self.root)
while widget ~= self.root do
clearWidget(widget)
widget = widget:getNextNeighbor()
end
end
--[[--
Set the master layout for this layout.
@@ -77,6 +97,7 @@ function Layout:setMaster (layout)
return layout:addWidget(...)
end
reset(self)
return self
end
@@ -94,6 +115,7 @@ function Layout:setStyle (rules)
end
self.style = Style(rules or {}, { 'style' })
reset(self)
return self
end
@@ -108,6 +130,8 @@ function Layout:setTheme (rules)
rules = rules()
end
self.theme = Style(rules or {}, { 'type' })
reset(self)
return self
end
--[[--

View File

@@ -74,9 +74,13 @@ end
local attributeNames = {}
for name in pairs(Attribute) do
attributeNames[#attributeNames + 1] = name
if name ~= 'type' then -- type must be handled last
attributeNames[#attributeNames + 1] = name
end
end
attributeNames[#attributeNames + 1] = 'type'
--[[--
Widget pseudo-constructor.
@@ -109,17 +113,6 @@ local function metaCall (Widget, layout, self)
self[property] = value
end
self.type = self.type or 'generic'
self.fontData = Font(self.font, self.size, self.color)
-- layout:addWidget(self)
local decorate = Widget.typeDecorators[self.type]
if decorate then
decorate(self)
end
for k, v in ipairs(self) do
self[k] = v.isWidget and v or metaCall(Widget, self.layout, v)
self[k].parent = self

View File

@@ -9,6 +9,8 @@ not be explicitly created.
--]]--
local ROOT = (...):gsub('[^.]*.[^.]*.[^.]*$', '')
local Backend = require(ROOT .. 'backend')
local Layout, Event
local function addLayoutChildren (self)
@@ -141,12 +143,14 @@ local function registerLayoutEvents (self)
end
end)
menuLayout:onEnter(activate)
menuLayout:onPressEnter(activate)
end
local function initialize (self)
if not self.fontData then
self.fontData = Backend.Font(self.font, self.size)
end
local pad = self.padding or 0
local isSubmenu = self.parentMenu and self.parentMenu.parentMenu
local text, key, icon = self.text or '', self.key or '', self.icon

View File

@@ -178,6 +178,10 @@ local function insertText (self, newText)
end
return function (self)
if not self.fontData then
self.fontData = Backend.Font(self.font, self.size)
end
self.value = self.value or self.text or ''
self.text = ''
self.highlight = self.highlight or { 0x80, 0x80, 0x80 }