2019-01-04 08:52:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
--- A notification action.
|
|
|
|
--
|
|
|
|
-- A notification can have multiple actions to chose from. This module allows
|
2019-07-17 06:26:44 +02:00
|
|
|
-- to manage such actions. An action object can be shared by multiple
|
|
|
|
-- notifications.
|
2019-01-04 08:52:02 +01:00
|
|
|
--
|
|
|
|
-- @author Emmanuel Lepage Vallee <elv1313@gmail.com>
|
|
|
|
-- @copyright 2019 Emmanuel Lepage Vallee
|
2019-06-09 00:55:04 +02:00
|
|
|
-- @coreclassmod naughty.action
|
2019-01-04 08:52:02 +01:00
|
|
|
---------------------------------------------------------------------------
|
|
|
|
local gtable = require("gears.table" )
|
|
|
|
local gobject = require("gears.object")
|
|
|
|
|
|
|
|
local action = {}
|
|
|
|
|
|
|
|
--- Create a new action.
|
2019-06-07 20:59:34 +02:00
|
|
|
-- @constructorfct naughty.action
|
2019-01-04 08:52:02 +01:00
|
|
|
-- @tparam table args The arguments.
|
|
|
|
-- @tparam string args.name The name.
|
|
|
|
-- @tparam string args.position The position.
|
|
|
|
-- @tparam string args.icon The icon.
|
|
|
|
-- @tparam naughty.notification args.notification The notification object.
|
|
|
|
-- @tparam boolean args.selected If this action is currently selected.
|
|
|
|
-- @return A new action.
|
|
|
|
|
|
|
|
-- The action name.
|
|
|
|
-- @property name
|
|
|
|
-- @tparam string name The name.
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @propertydefault This is provided by DBus.
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @propemits true false
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
-- If the action is selected.
|
|
|
|
--
|
2019-07-17 06:26:44 +02:00
|
|
|
-- Only a single action can be selected per notification. This is useful to
|
|
|
|
-- implement keyboard navigation.
|
2019-01-04 08:52:02 +01:00
|
|
|
--
|
|
|
|
-- @property selected
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=false] boolean selected
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @propemits true false
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
--- The action position (index).
|
|
|
|
-- @property position
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam integer position
|
|
|
|
-- @propertydefault This is provided by DBus.
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @propemits true false
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @negativeallowed false
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
--- The action icon.
|
|
|
|
-- @property icon
|
2022-08-22 08:02:26 +02:00
|
|
|
-- @tparam[opt=nil] image|string|nil icon
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @propemits true false
|
2019-01-04 08:52:02 +01:00
|
|
|
|
2019-03-05 04:47:36 +01:00
|
|
|
--- If the action should hide the label and only display the icon.
|
2019-07-07 22:04:17 +02:00
|
|
|
--
|
|
|
|
-- @DOC_wibox_nwidget_actionlist_icon_only_EXAMPLE@
|
|
|
|
--
|
2019-03-05 04:47:36 +01:00
|
|
|
-- @property icon_only
|
2021-12-21 06:54:15 +01:00
|
|
|
-- @tparam[opt=false] boolean icon_only
|
2020-01-13 08:32:28 +01:00
|
|
|
-- @propemits true false
|
2019-03-05 04:47:36 +01:00
|
|
|
|
2019-01-04 08:52:02 +01:00
|
|
|
--- When a notification is invoked.
|
2021-09-17 02:25:46 +02:00
|
|
|
--
|
|
|
|
-- Note that it is possible to call `:invoke()` without a notification object.
|
|
|
|
-- It is possible the `notification` parameter will be nil.
|
|
|
|
--
|
2019-01-04 08:52:02 +01:00
|
|
|
-- @signal invoked
|
2021-09-17 02:25:46 +02:00
|
|
|
-- @tparam naughty.action action The action.
|
|
|
|
-- @tparam naughty.notification|nil notification The notification, if known.
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
function action:get_selected()
|
|
|
|
return self._private.selected
|
|
|
|
end
|
|
|
|
|
|
|
|
function action:set_selected(value)
|
|
|
|
self._private.selected = value
|
|
|
|
self:emit_signal("property::selected", value)
|
2019-07-17 06:26:44 +02:00
|
|
|
self:emit_signal("_changed")
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
--TODO deselect other actions from the same notification
|
|
|
|
end
|
|
|
|
|
|
|
|
function action:get_position()
|
|
|
|
return self._private.position
|
|
|
|
end
|
|
|
|
|
|
|
|
function action:set_position(value)
|
|
|
|
self._private.position = value
|
|
|
|
self:emit_signal("property::position", value)
|
2019-07-17 06:26:44 +02:00
|
|
|
self:emit_signal("_changed")
|
2019-01-04 08:52:02 +01:00
|
|
|
|
|
|
|
--TODO make sure the position is unique
|
|
|
|
end
|
|
|
|
|
2019-07-17 06:26:44 +02:00
|
|
|
for _, prop in ipairs { "name", "icon", "icon_only" } do
|
2019-01-04 08:52:02 +01:00
|
|
|
action["get_"..prop] = function(self)
|
|
|
|
return self._private[prop]
|
|
|
|
end
|
|
|
|
|
|
|
|
action["set_"..prop] = function(self, value)
|
|
|
|
self._private[prop] = value
|
|
|
|
self:emit_signal("property::"..prop, value)
|
2019-07-17 06:26:44 +02:00
|
|
|
self:emit_signal("_changed")
|
2019-01-04 08:52:02 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-17 06:26:44 +02:00
|
|
|
--TODO v4.5, remove this.
|
|
|
|
function action.set_notification()
|
|
|
|
-- It didn't work because it prevented actions defined in the rules to be
|
|
|
|
-- in multiple notifications at once.
|
|
|
|
assert(
|
|
|
|
false,
|
|
|
|
"Setting a notification object was a bad idea and is now forbidden"
|
|
|
|
)
|
2019-07-09 07:35:03 +02:00
|
|
|
end
|
|
|
|
|
2019-01-04 08:52:02 +01:00
|
|
|
--- Execute this action.
|
2019-06-09 00:55:04 +02:00
|
|
|
--
|
|
|
|
-- This only emits the `invoked` signal.
|
|
|
|
--
|
|
|
|
-- @method invoke
|
2019-07-17 06:26:44 +02:00
|
|
|
-- @tparam[opt={}] naughty.notification notif A notification object on which
|
|
|
|
-- the action was invoked. If a notification is shared by many object (like
|
|
|
|
-- a "mute" or "snooze" action added to all notification), calling `:invoke()`
|
|
|
|
-- without adding the `notif` context will cause unexpected results.
|
2022-07-05 10:37:14 +02:00
|
|
|
-- @noreturn
|
2019-07-17 06:26:44 +02:00
|
|
|
function action:invoke(notif)
|
|
|
|
self:emit_signal("invoked", notif)
|
2019-01-04 08:52:02 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function new(_, args)
|
|
|
|
args = args or {}
|
|
|
|
local ret = gobject { enable_properties = true }
|
|
|
|
|
|
|
|
gtable.crush(ret, action, true)
|
|
|
|
|
|
|
|
local default = {
|
|
|
|
-- See "table 1" of the spec about the default name
|
|
|
|
name = args.name or "default",
|
|
|
|
selected = args.selected == true,
|
|
|
|
position = args.position,
|
|
|
|
icon = args.icon,
|
|
|
|
notification = args.notification,
|
2019-07-07 22:04:17 +02:00
|
|
|
icon_only = args.icon_only or false,
|
2019-01-04 08:52:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rawset(ret, "_private", default)
|
|
|
|
|
2019-07-07 22:04:17 +02:00
|
|
|
gtable.crush(ret, args)
|
|
|
|
|
2019-01-04 08:52:02 +01:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
2019-06-07 20:59:34 +02:00
|
|
|
--@DOC_object_COMMON@
|
|
|
|
|
2019-01-04 08:52:02 +01:00
|
|
|
return setmetatable(action, {__call = new})
|