2010-09-28 16:23:00 +02:00
|
|
|
---------------------------------------------------------------------------
|
2016-05-17 06:44:20 +02:00
|
|
|
-- The object oriented programming base class used by various Awesome
|
|
|
|
-- widgets and components.
|
|
|
|
--
|
|
|
|
-- It provide basic observer pattern, signaling and dynamic properties.
|
|
|
|
--
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @author Uli Schlachter
|
|
|
|
-- @copyright 2010 Uli Schlachter
|
2019-06-06 09:40:17 +02:00
|
|
|
-- @utillib gears.object
|
2010-09-28 16:23:00 +02:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local pairs = pairs
|
|
|
|
local type = type
|
|
|
|
local error = error
|
2016-03-13 08:42:57 +01:00
|
|
|
local properties = require("gears.object.properties")
|
2010-09-28 16:23:00 +02:00
|
|
|
|
2016-03-13 08:42:57 +01:00
|
|
|
local object = { properties = properties, mt = {} }
|
2010-09-28 16:23:00 +02:00
|
|
|
|
2015-02-20 15:45:53 +01:00
|
|
|
--- Verify that obj is indeed a valid object as returned by new()
|
2010-09-28 16:23:00 +02:00
|
|
|
local function check(obj)
|
|
|
|
if type(obj) ~= "table" or type(obj._signals) ~= "table" then
|
2016-06-04 18:23:48 +02:00
|
|
|
error("called on non-object")
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-29 21:38:06 +02:00
|
|
|
--- Find a given signal
|
2016-05-17 06:38:46 +02:00
|
|
|
-- @tparam table obj The object to search in
|
|
|
|
-- @tparam string name The signal to find
|
|
|
|
-- @treturn table The signal table
|
2016-06-04 18:23:48 +02:00
|
|
|
local function find_signal(obj, name)
|
2010-09-28 16:23:00 +02:00
|
|
|
check(obj)
|
|
|
|
if not obj._signals[name] then
|
2016-06-04 18:23:48 +02:00
|
|
|
assert(type(name) == "string", "name must be a string, got: " .. type(name))
|
|
|
|
obj._signals[name] = {
|
2015-06-14 14:21:23 +02:00
|
|
|
strong = {},
|
2015-11-19 18:11:51 +01:00
|
|
|
weak = setmetatable({}, { __mode = "kv" })
|
2015-06-14 14:21:23 +02:00
|
|
|
}
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
2016-06-04 18:23:48 +02:00
|
|
|
return obj._signals[name]
|
|
|
|
end
|
|
|
|
|
|
|
|
function object.add_signal()
|
2017-03-15 06:08:40 +01:00
|
|
|
require("gears.debug").deprecate("Use signals without explicitly adding them. This is now done implicitly.",
|
|
|
|
{deprecated_in=4})
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2016-05-17 06:38:46 +02:00
|
|
|
--- Connect to a signal.
|
2021-03-27 20:23:16 +01:00
|
|
|
--
|
2017-01-01 21:21:42 +01:00
|
|
|
--@DOC_text_gears_object_signal_EXAMPLE@
|
2021-03-27 20:23:16 +01:00
|
|
|
--
|
|
|
|
-- @tparam string name The name of the signal.
|
|
|
|
-- @tparam function func The callback to call when the signal is emitted.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @method connect_signal
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:connect_signal(name, func)
|
2015-01-11 11:04:33 +01:00
|
|
|
assert(type(func) == "function", "callback must be a function, got: " .. type(func))
|
2016-06-04 18:23:48 +02:00
|
|
|
local sig = find_signal(self, name)
|
2015-06-14 14:21:23 +02:00
|
|
|
assert(sig.weak[func] == nil, "Trying to connect a strong callback which is already connected weakly")
|
|
|
|
sig.strong[func] = true
|
|
|
|
end
|
|
|
|
|
2017-07-16 05:15:29 +02:00
|
|
|
-- Register a global signal receiver.
|
|
|
|
function object:_connect_everything(callback)
|
|
|
|
table.insert(self._global_receivers, callback)
|
|
|
|
end
|
|
|
|
|
2015-11-19 18:11:51 +01:00
|
|
|
local function make_the_gc_obey(func)
|
|
|
|
if _VERSION <= "Lua 5.1" then
|
|
|
|
-- Lua 5.1 only has the behaviour we want if a userdata is used as the
|
|
|
|
-- value in a weak table. Thus, do some magic so that we get a userdata.
|
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
-- luacheck: globals newproxy getfenv setfenv
|
2015-11-19 18:11:51 +01:00
|
|
|
local userdata = newproxy(true)
|
|
|
|
getmetatable(userdata).__gc = function() end
|
|
|
|
-- Now bind the lifetime of userdata to the lifetime of func. For this,
|
|
|
|
-- we mess with the function's environment and add a table for all the
|
|
|
|
-- various userdata that it should keep alive.
|
|
|
|
local key = "_secret_key_used_by_gears_object_in_Lua51"
|
|
|
|
local old_env = getfenv(func)
|
|
|
|
if old_env[key] then
|
|
|
|
-- Assume the code in the else branch added this and the function
|
|
|
|
-- already has its own, private environment
|
|
|
|
table.insert(old_env[key], userdata)
|
|
|
|
else
|
|
|
|
-- No table yet, add it
|
|
|
|
local new_env = { [key] = { userdata } }
|
|
|
|
setmetatable(new_env, { __index = old_env, __newindex = old_env })
|
|
|
|
setfenv(func, new_env)
|
|
|
|
end
|
|
|
|
assert(_G[key] == nil, "Something broke, things escaped to _G")
|
|
|
|
return userdata
|
|
|
|
end
|
|
|
|
-- Lua 5.2+ already behaves the way we want with functions directly, no magic
|
|
|
|
return func
|
|
|
|
end
|
|
|
|
|
2021-03-27 20:23:16 +01:00
|
|
|
--- Connect to a signal weakly.
|
|
|
|
--
|
|
|
|
-- This allows the callback function to be garbage collected and
|
|
|
|
-- automatically disconnects the signal when that happens.
|
|
|
|
-- **Warning:**
|
|
|
|
-- Only use this function if you really, really, really know what you
|
|
|
|
-- are doing.
|
|
|
|
--
|
|
|
|
-- @tparam string name The name of the signal.
|
|
|
|
-- @tparam function func The callback to call when the signal is emitted.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @method weak_connect_signal
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2015-06-14 14:21:23 +02:00
|
|
|
function object:weak_connect_signal(name, func)
|
|
|
|
assert(type(func) == "function", "callback must be a function, got: " .. type(func))
|
2016-06-04 18:23:48 +02:00
|
|
|
local sig = find_signal(self, name)
|
2015-06-14 14:21:23 +02:00
|
|
|
assert(sig.strong[func] == nil, "Trying to connect a weak callback which is already connected strongly")
|
2015-11-19 18:11:51 +01:00
|
|
|
sig.weak[func] = make_the_gc_obey(func)
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2024-01-11 11:09:54 +01:00
|
|
|
--- Disconnect from a signal.
|
2021-03-27 20:23:16 +01:00
|
|
|
-- @tparam string name The name of the signal.
|
|
|
|
-- @tparam function func The callback that should be disconnected.
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @method disconnect_signal
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:disconnect_signal(name, func)
|
2016-06-04 18:23:48 +02:00
|
|
|
local sig = find_signal(self, name)
|
2015-06-14 14:21:23 +02:00
|
|
|
sig.weak[func] = nil
|
|
|
|
sig.strong[func] = nil
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2016-05-17 06:38:46 +02:00
|
|
|
--- Emit a signal.
|
2014-05-19 15:15:39 +02:00
|
|
|
--
|
2016-05-17 06:38:46 +02:00
|
|
|
-- @tparam string name The name of the signal
|
2010-09-28 16:23:00 +02:00
|
|
|
-- @param ... Extra arguments for the callback functions. Each connected
|
2021-03-27 20:23:16 +01:00
|
|
|
-- function receives the object as first argument and then any extra
|
|
|
|
-- arguments that are given to emit_signal()
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @method emit_signal
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2012-11-19 13:40:37 +01:00
|
|
|
function object:emit_signal(name, ...)
|
2016-06-04 18:23:48 +02:00
|
|
|
local sig = find_signal(self, name)
|
2015-06-14 14:21:23 +02:00
|
|
|
for func in pairs(sig.strong) do
|
|
|
|
func(self, ...)
|
|
|
|
end
|
|
|
|
for func in pairs(sig.weak) do
|
2012-11-19 13:40:37 +01:00
|
|
|
func(self, ...)
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
2017-07-16 05:15:29 +02:00
|
|
|
for _, func in ipairs(self._global_receivers) do
|
|
|
|
func(name, self, ...)
|
|
|
|
end
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2020-03-15 09:02:31 +01:00
|
|
|
function object._setup_class_signals(t, args)
|
|
|
|
args = args or {}
|
2019-05-06 05:00:58 +02:00
|
|
|
local conns = {}
|
|
|
|
|
|
|
|
function t.connect_signal(name, func)
|
|
|
|
assert(name)
|
|
|
|
conns[name] = conns[name] or {}
|
|
|
|
table.insert(conns[name], func)
|
|
|
|
end
|
|
|
|
|
2020-03-15 09:02:31 +01:00
|
|
|
-- A variant of emit_signal which stops once a condition is met.
|
|
|
|
if args.allow_chain_of_responsibility then
|
|
|
|
function t._emit_signal_if(name, condition, ...)
|
|
|
|
assert(name)
|
|
|
|
for _, func in pairs(conns[name] or {}) do
|
|
|
|
if condition(...) then return end
|
|
|
|
func(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-05-06 05:00:58 +02:00
|
|
|
--- Emit a notification signal.
|
|
|
|
-- @tparam string name The signal name.
|
|
|
|
-- @param ... The signal callback arguments
|
|
|
|
function t.emit_signal(name, ...)
|
|
|
|
assert(name)
|
|
|
|
for _, func in pairs(conns[name] or {}) do
|
|
|
|
func(...)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Disconnect a signal from a source.
|
|
|
|
-- @tparam string name The name of the signal
|
|
|
|
-- @tparam function func The attached function
|
|
|
|
-- @treturn boolean If the disconnection was successful
|
|
|
|
function t.disconnect_signal(name, func)
|
|
|
|
for k, v in ipairs(conns[name] or {}) do
|
|
|
|
if v == func then
|
|
|
|
table.remove(conns[name], k)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return conns
|
|
|
|
end
|
|
|
|
|
2016-05-17 06:34:09 +02:00
|
|
|
local function get_miss(self, key)
|
|
|
|
local class = rawget(self, "_class")
|
|
|
|
|
|
|
|
if rawget(self, "get_"..key) then
|
|
|
|
return rawget(self, "get_"..key)(self)
|
|
|
|
elseif class and class["get_"..key] then
|
|
|
|
return class["get_"..key](self)
|
|
|
|
elseif class then
|
|
|
|
return class[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
local function set_miss(self, key, value)
|
|
|
|
local class = rawget(self, "_class")
|
|
|
|
|
|
|
|
if rawget(self, "set_"..key) then
|
|
|
|
return rawget(self, "set_"..key)(self, value)
|
|
|
|
elseif class and class["set_"..key] then
|
|
|
|
return class["set_"..key](self, value)
|
|
|
|
elseif rawget(self, "_enable_auto_signals") then
|
|
|
|
local changed = class[key] ~= value
|
|
|
|
class[key] = value
|
|
|
|
|
|
|
|
if changed then
|
|
|
|
self:emit_signal("property::"..key, value)
|
|
|
|
end
|
2016-06-04 15:47:09 +02:00
|
|
|
elseif (not rawget(self, "get_"..key))
|
|
|
|
and not (class and class["get_"..key]) then
|
2016-05-17 06:34:09 +02:00
|
|
|
return rawset(self, key, value)
|
2016-06-04 15:47:09 +02:00
|
|
|
else
|
|
|
|
error("Cannot set '" .. tostring(key) .. "' on " .. tostring(self)
|
|
|
|
.. " because it is read-only")
|
2016-05-17 06:34:09 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-04 18:23:48 +02:00
|
|
|
--- Returns a new object. You can call `:emit_signal()`, `:disconnect_signal()`
|
|
|
|
-- and `:connect_signal()` on the resulting object.
|
2016-05-17 06:34:09 +02:00
|
|
|
--
|
|
|
|
-- Note that `args.enable_auto_signals` is only supported when
|
|
|
|
-- `args.enable_properties` is true.
|
|
|
|
--
|
2016-05-17 07:17:22 +02:00
|
|
|
--@DOC_text_gears_object_properties_EXAMPLE@
|
2016-05-17 06:34:09 +02:00
|
|
|
-- @tparam[opt={}] table args The arguments
|
|
|
|
-- @tparam[opt=false] boolean args.enable_properties Automatically call getters and setters
|
|
|
|
-- @tparam[opt=false] boolean args.enable_auto_signals Generate "property::xxxx" signals
|
|
|
|
-- when an unknown property is set.
|
|
|
|
-- @tparam[opt=nil] table args.class
|
|
|
|
-- @treturn table A new object
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @constructorfct gears.object
|
|
|
|
|
2016-05-17 06:34:09 +02:00
|
|
|
local function new(args)
|
|
|
|
args = args or {}
|
2010-09-28 16:23:00 +02:00
|
|
|
local ret = {}
|
|
|
|
|
2016-05-17 06:34:09 +02:00
|
|
|
-- Automatic signals cannot work without both miss handlers.
|
|
|
|
assert(not (args.enable_auto_signals and args.enable_properties ~= true))
|
|
|
|
|
2010-09-28 16:23:00 +02:00
|
|
|
-- Copy all our global functions to our new object
|
2012-06-12 10:13:46 +02:00
|
|
|
for k, v in pairs(object) do
|
2010-09-28 16:23:00 +02:00
|
|
|
if type(v) == "function" then
|
|
|
|
ret[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
ret._signals = {}
|
|
|
|
|
2017-07-16 05:15:29 +02:00
|
|
|
ret._global_receivers = {}
|
|
|
|
|
2016-05-17 06:34:09 +02:00
|
|
|
local mt = {}
|
|
|
|
|
|
|
|
-- Look for methods in another table
|
|
|
|
ret._class = args.class
|
|
|
|
ret._enable_auto_signals = args.enable_auto_signals
|
|
|
|
|
|
|
|
-- To catch all changes, a proxy is required
|
|
|
|
if args.enable_auto_signals then
|
|
|
|
ret._class = ret._class and setmetatable({}, {__index = args.class}) or {}
|
|
|
|
end
|
|
|
|
|
|
|
|
if args.enable_properties then
|
|
|
|
-- Check got existing get_xxxx and set_xxxx
|
|
|
|
mt.__index = get_miss
|
|
|
|
mt.__newindex = set_miss
|
|
|
|
elseif args.class then
|
|
|
|
-- Use the class table a miss handler
|
|
|
|
mt.__index = ret._class
|
|
|
|
end
|
|
|
|
|
|
|
|
return setmetatable(ret, mt)
|
2010-09-28 16:23:00 +02:00
|
|
|
end
|
|
|
|
|
2016-02-07 13:29:46 +01:00
|
|
|
function object.mt.__call(_, ...)
|
2012-06-12 10:13:46 +02:00
|
|
|
return new(...)
|
|
|
|
end
|
|
|
|
|
2015-07-23 21:42:54 +02:00
|
|
|
--- Helper function to get the module name out of `debug.getinfo`.
|
|
|
|
-- @usage
|
|
|
|
-- local mt = {}
|
|
|
|
-- mt.__tostring = function(o)
|
|
|
|
-- return require("gears.object").modulename(2)
|
|
|
|
-- end
|
|
|
|
-- return setmetatable(ret, mt)
|
|
|
|
--
|
|
|
|
-- @tparam[opt=2] integer level Level for `debug.getinfo(level, "S")`.
|
|
|
|
-- Typically 2 or 3.
|
2016-05-23 05:56:45 +02:00
|
|
|
-- @treturn string The module name, e.g. "wibox.container.background".
|
2019-06-08 01:08:05 +02:00
|
|
|
-- @staticfct gears.object.modulename
|
2015-07-23 21:42:54 +02:00
|
|
|
function object.modulename(level)
|
|
|
|
return debug.getinfo(level, "S").source:gsub(".*/lib/", ""):gsub("/", "."):gsub("%.lua", "")
|
|
|
|
end
|
|
|
|
|
2012-06-12 10:13:46 +02:00
|
|
|
return setmetatable(object, object.mt)
|
2010-09-28 16:23:00 +02:00
|
|
|
|
2011-09-11 16:50:01 +02:00
|
|
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|