objects: Add a table for private elements

Provide a standardized place for them rather than having each
widgets use their own conventions.
This commit is contained in:
Emmanuel Lepage Vallee 2016-05-26 00:52:11 -04:00
parent efcbda0a78
commit adb2f88383
2 changed files with 22 additions and 14 deletions

View File

@ -143,12 +143,12 @@ local widget_dependencies = setmetatable({}, { __mode = "kv" })
-- Get the cache of the given kind for this widget. This returns a gears.cache -- Get the cache of the given kind for this widget. This returns a gears.cache
-- that calls the callback of kind `kind` on the widget. -- that calls the callback of kind `kind` on the widget.
local function get_cache(widget, kind) local function get_cache(widget, kind)
if not widget._widget_caches[kind] then if not widget._private.widget_caches[kind] then
widget._widget_caches[kind] = cache.new(function(...) widget._private.widget_caches[kind] = cache.new(function(...)
return protected_call(widget[kind], widget, ...) return protected_call(widget[kind], widget, ...)
end) end)
end end
return widget._widget_caches[kind] return widget._private.widget_caches[kind]
end end
-- Special value to skip the dependency recording that is normally done by -- Special value to skip the dependency recording that is normally done by
@ -177,7 +177,7 @@ local clear_caches
function clear_caches(widget) function clear_caches(widget)
local deps = widget_dependencies[widget] or {} local deps = widget_dependencies[widget] or {}
widget_dependencies[widget] = {} widget_dependencies[widget] = {}
widget._widget_caches = {} widget._private.widget_caches = {}
for w in pairs(deps) do for w in pairs(deps) do
clear_caches(w) clear_caches(w)
end end
@ -424,7 +424,11 @@ end
-- Only available when the declarative system is used -- Only available when the declarative system is used
local function get_children_by_id(self, name) local function get_children_by_id(self, name)
return self._by_id[name] or {} if rawget(self, "_private") then
return self._private.by_id[name] or {}
else
return rawget(self, "_by_id")[name] or {}
end
end end
--- Set a declarative widget hierarchy description. --- Set a declarative widget hierarchy description.
@ -503,6 +507,9 @@ function base.make_widget(proxy, widget_name, args)
ret:emit_signal("widget::redraw_needed") ret:emit_signal("widget::redraw_needed")
end) end)
-- Create a table used to store the widgets internal data
rawset(ret, "_private", {})
-- No buttons yet -- No buttons yet
ret.widget_buttons = {} ret.widget_buttons = {}
@ -513,7 +520,7 @@ function base.make_widget(proxy, widget_name, args)
ret.opacity = 1 ret.opacity = 1
-- Differentiate tables from widgets -- Differentiate tables from widgets
ret.is_widget = true rawset(ret, "is_widget", true)
-- Size is not restricted/forced -- Size is not restricted/forced
ret._forced_width = nil ret._forced_width = nil
@ -528,12 +535,12 @@ function base.make_widget(proxy, widget_name, args)
end) end)
if proxy then if proxy then
ret.fit = function(_, context, width, height) rawset(ret, "fit", function(_, context, width, height)
return base.fit_widget(ret, context, proxy, width, height) return base.fit_widget(ret, context, proxy, width, height)
end end)
ret.layout = function(_, _, width, height) rawset(ret, "layout", function(_, _, width, height)
return { base.place_widget_at(proxy, 0, 0, width, height) } return { base.place_widget_at(proxy, 0, 0, width, height) }
end end)
proxy:connect_signal("widget::layout_changed", function() proxy:connect_signal("widget::layout_changed", function()
ret:emit_signal("widget::layout_changed") ret:emit_signal("widget::layout_changed")
end) end)
@ -550,11 +557,11 @@ function base.make_widget(proxy, widget_name, args)
-- Add functions -- Add functions
for k, v in pairs(base.widget) do for k, v in pairs(base.widget) do
ret[k] = v rawset(ret, k, v)
end end
-- Add __tostring method to metatable. -- Add __tostring method to metatable.
ret.widget_name = widget_name or object.modulename(3) rawset(ret, "widget_name", widget_name or object.modulename(3))
local mt = getmetatable(ret) or {} local mt = getmetatable(ret) or {}
local orig_string = tostring(ret) local orig_string = tostring(ret)
mt.__tostring = function() mt.__tostring = function()

View File

@ -82,11 +82,12 @@ assert:register("assertion", "widget_layout", widget_layout, "assertion.widget_l
return { return {
widget_stub = function(width, height) widget_stub = function(width, height)
local w = object() local w = object()
w._private = {}
w:add_signal("widget::redraw_needed") w:add_signal("widget::redraw_needed")
w:add_signal("widget::layout_changed") w:add_signal("widget::layout_changed")
w.is_widget = true w.is_widget = true
w.visible = true w._private.visible = true
w.opacity = 1 w._private.opacity = 1
if width or height then if width or height then
w.fit = function() w.fit = function()
return width or 10, height or 10 return width or 10, height or 10