2018-07-24 14:27:41 +02:00
|
|
|
--DOC_GEN_OUTPUT --DOC_HIDE
|
2016-05-17 07:17:22 +02:00
|
|
|
local gears = require("gears") --DOC_HIDE
|
|
|
|
|
2016-12-27 21:39:08 +01:00
|
|
|
-- Create a class for this object. It will be used as a backup source for
|
|
|
|
-- methods and accessors. It is also possible to set them directly on the
|
|
|
|
-- object.
|
2016-05-17 07:17:22 +02:00
|
|
|
local class = {}
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
function class:get_foo()
|
|
|
|
print("In get foo", self._foo or "bar")
|
|
|
|
return self._foo or "bar"
|
|
|
|
end
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
function class:set_foo(value)
|
|
|
|
print("In set foo", value)
|
|
|
|
|
|
|
|
-- In case it is necessary to bypass the object property system, use
|
|
|
|
-- `rawset`
|
|
|
|
rawset(self, "_foo", value)
|
|
|
|
|
|
|
|
-- When using custom accessors, the signals need to be handled manually
|
|
|
|
self:emit_signal("property::foo", value)
|
|
|
|
end
|
|
|
|
|
|
|
|
function class:method(a, b, c)
|
2018-07-25 16:03:26 +02:00
|
|
|
print("In a method", a, b, c)
|
2016-05-17 07:17:22 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local o = gears.object {
|
|
|
|
class = class,
|
|
|
|
enable_properties = true,
|
|
|
|
enable_auto_signals = true,
|
|
|
|
}
|
|
|
|
|
|
|
|
print(o.foo)
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
o.foo = 42
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
print(o.foo)
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
o:method(1, 2, 3)
|
|
|
|
|
2016-12-27 21:39:08 +01:00
|
|
|
-- Random properties can also be added, the signal will be emitted automatically.
|
2016-05-17 07:17:22 +02:00
|
|
|
|
|
|
|
o:connect_signal("property::something", function(obj, value)
|
2016-12-02 20:30:10 +01:00
|
|
|
assert(obj == o)
|
|
|
|
print("In the connection handler!", value)
|
2016-05-17 07:17:22 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
print(o.something)
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
o.something = "a cow"
|
2016-11-21 22:38:23 +01:00
|
|
|
|
2016-05-17 07:17:22 +02:00
|
|
|
print(o.something)
|
2016-12-31 13:59:38 +01:00
|
|
|
|
|
|
|
--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|