From adb2f8838380c9dc6b81760071d44d6c06d8af4d Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Thu, 26 May 2016 00:52:11 -0400 Subject: [PATCH] objects: Add a table for private elements Provide a standardized place for them rather than having each widgets use their own conventions. --- lib/wibox/widget/base.lua | 31 +++++++++++++++++++------------ spec/wibox/test_utils.lua | 5 +++-- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/wibox/widget/base.lua b/lib/wibox/widget/base.lua index de5844c6d..7ce9ec952 100644 --- a/lib/wibox/widget/base.lua +++ b/lib/wibox/widget/base.lua @@ -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 -- that calls the callback of kind `kind` on the widget. local function get_cache(widget, kind) - if not widget._widget_caches[kind] then - widget._widget_caches[kind] = cache.new(function(...) + if not widget._private.widget_caches[kind] then + widget._private.widget_caches[kind] = cache.new(function(...) return protected_call(widget[kind], widget, ...) end) end - return widget._widget_caches[kind] + return widget._private.widget_caches[kind] end -- Special value to skip the dependency recording that is normally done by @@ -177,7 +177,7 @@ local clear_caches function clear_caches(widget) local deps = widget_dependencies[widget] or {} widget_dependencies[widget] = {} - widget._widget_caches = {} + widget._private.widget_caches = {} for w in pairs(deps) do clear_caches(w) end @@ -424,7 +424,11 @@ end -- Only available when the declarative system is used 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 --- Set a declarative widget hierarchy description. @@ -503,6 +507,9 @@ function base.make_widget(proxy, widget_name, args) ret:emit_signal("widget::redraw_needed") end) + -- Create a table used to store the widgets internal data + rawset(ret, "_private", {}) + -- No buttons yet ret.widget_buttons = {} @@ -513,7 +520,7 @@ function base.make_widget(proxy, widget_name, args) ret.opacity = 1 -- Differentiate tables from widgets - ret.is_widget = true + rawset(ret, "is_widget", true) -- Size is not restricted/forced ret._forced_width = nil @@ -528,12 +535,12 @@ function base.make_widget(proxy, widget_name, args) end) 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) - end - ret.layout = function(_, _, width, height) + end) + rawset(ret, "layout", function(_, _, width, height) return { base.place_widget_at(proxy, 0, 0, width, height) } - end + end) proxy:connect_signal("widget::layout_changed", function() ret:emit_signal("widget::layout_changed") end) @@ -550,11 +557,11 @@ function base.make_widget(proxy, widget_name, args) -- Add functions for k, v in pairs(base.widget) do - ret[k] = v + rawset(ret, k, v) end -- 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 orig_string = tostring(ret) mt.__tostring = function() diff --git a/spec/wibox/test_utils.lua b/spec/wibox/test_utils.lua index 04e6e0f13..47a1138a8 100644 --- a/spec/wibox/test_utils.lua +++ b/spec/wibox/test_utils.lua @@ -82,11 +82,12 @@ assert:register("assertion", "widget_layout", widget_layout, "assertion.widget_l return { widget_stub = function(width, height) local w = object() + w._private = {} w:add_signal("widget::redraw_needed") w:add_signal("widget::layout_changed") w.is_widget = true - w.visible = true - w.opacity = 1 + w._private.visible = true + w._private.opacity = 1 if width or height then w.fit = function() return width or 10, height or 10