client: Add a `:grant()` and `:deny()` method for permissions.

This is a lower level API than what most people will end up using
(the rules), but it is useful enough to expose to the public API.
This commit is contained in:
Emmanuel Lepage Vallee 2019-11-17 19:48:25 -05:00 committed by Emmanuel Lepage-Vallee
parent 5818de41ce
commit 668ed6135c
3 changed files with 53 additions and 2 deletions

View File

@ -15,6 +15,7 @@ local grect = require("gears.geometry").rectangle
local gmath = require("gears.math")
local gtable = require("gears.table")
local amousec = require("awful.mouse.client")
local pcommon = require("awful.permissions._common")
local pairs = pairs
local type = type
local ipairs = ipairs
@ -1485,6 +1486,22 @@ function client.object.activate(c, args)
end
end
--- Grant a permission for a client.
--
-- @method grant
-- @tparam string permission The permission name (just the name, no `request::`).
-- @tparam string context The reason why this permission is requested.
-- @see awful.permissions
--- Deny a permission for a client.
--
-- @method deny
-- @tparam string permission The permission name (just the name, no `request::`).
-- @tparam string context The reason why this permission is requested.
-- @see awful.permissions
pcommon.setup_grant(client.object, "client")
--- Return true if the client is active (has focus).
--
-- This property is **READ ONLY**. Use `c:activate { context = "myreason" }`

View File

@ -19,11 +19,19 @@ local default_permissions = {
}
}
function module.check(class, request, context)
function module.check(object, class, request, context)
if not default_permissions[class] then return true end
if not default_permissions[class][request] then return true end
if default_permissions[class][request][context] == nil then return true end
local ret = nil
if object._private.permissions and object._private.permissions[request] then
ret = object._private.permissions[request][context]
end
if ret ~= nil then return ret end
return default_permissions[class][request][context]
end
@ -53,4 +61,22 @@ function module._deprecated_autofocus_in_use()
module.set("client", "autoactivate", "history" , true)
end
local function set_object_permission_common(self, request, context, v)
self._private.permissions = self._private.permissions or {}
if not self._private.permissions[request] then
self._private.permissions[request] = {}
end
self._private.permissions[request][context] = v
end
-- Add the grant and deny methods to the objects.
function module.setup_grant(class, classname) -- luacheck: no unused
function class.grant(self, request, context)
set_object_permission_common(self, request, context, true)
end
function class.deny(self, request, context)
set_object_permission_common(self, request, context, false)
end
end
return module

View File

@ -146,6 +146,8 @@ end
-- @tparam[opt=false] boolean hints.switch_to_tags Select all tags associated
-- with the client.
function permissions.activate(c, context, hints) -- luacheck: no unused args
if not pcommon.check(c, "client", "activate", context) then return end
hints = hints or {}
if c.focusable == false and not hints.force then
@ -333,6 +335,8 @@ local context_mapper = {
-- @tparam string context The context
-- @tparam[opt={}] table hints The hints to pass to the handler
function permissions.geometry(c, context, hints)
if not pcommon.check(c, "client", "geometry", context) then return end
local layout = c.screen.selected_tag and c.screen.selected_tag.layout or nil
-- Setting the geometry will not work unless the client is floating.
@ -403,6 +407,8 @@ end
-- @tparam string context The context
-- @tparam[opt={}] table hints The hints to pass to the handler
function permissions.merge_maximization(c, context, hints)
if not pcommon.check(c, "client", "geometry", context) then return end
if context ~= "client_maximize_horizontal" and context ~= "client_maximize_vertical" then
return
end
@ -484,6 +490,8 @@ end
-- @tparam string context The context
-- @tparam[opt={}] table hints The hints to pass to the handler
function permissions.client_geometry_requests(c, context, hints)
if not pcommon.check(c, "client", "geometry", context) then return end
if context == "ewmh" and hints then
if c.immobilized_horizontal then
hints = gtable.clone(hints)
@ -679,7 +687,7 @@ local activate_context_map = {
--
-- @signalhandler awful.permissions.autoactivate
function permissions.autoactivate(c, context, args)
if not pcommon.check("client", "autoactivate", context) then return end
if not pcommon.check(c, "client", "autoactivate", context) then return end
local ctx = activate_context_map[context] and
activate_context_map[context] or context