diff --git a/docs/common/object.ldoc b/docs/common/object.ldoc index 8eb477e1c2..8d66a0e46d 100644 --- a/docs/common/object.ldoc +++ b/docs/common/object.ldoc @@ -37,3 +37,13 @@ -- @method weak_connect_signal -- @noreturn -- @baseclass gears.object + +--- Check if the callback is connected to the signal. +-- +-- This function check both kind of signal connection (strong and weak). +-- +-- @tparam string name The name of the signal. +-- @tparam function func The callback to check if connected. +-- @treturn boolean Whether the signal is connected. +-- @method is_signal_connected +-- @baseclass gears.object diff --git a/lib/awful/popup.lua b/lib/awful/popup.lua index 71ca6a6a12..96614fbae6 100644 --- a/lib/awful/popup.lua +++ b/lib/awful/popup.lua @@ -22,6 +22,7 @@ -- @supermodule wibox --------------------------------------------------------------------------- local wibox = require( "wibox" ) +local gdebug = require( "gears.debug" ) local gtable = require( "gears.table" ) local placement = require( "awful.placement" ) local xresources= require("beautiful.xresources") @@ -297,17 +298,28 @@ function popup:unbind_to_widget(widget) widget:disconnect_signal("button::press", self._private.show_fct) end ---- Hide the popup when right clicked. +--- Hide the popup when clicked inside. -- --- @property hide_on_right_click --- @tparam[opt=false] boolean hide_on_right_click +-- @property hide_on_click +-- @tparam[opt=false] boolean hide_on_click -- @propemits true false - -function popup:set_hide_on_right_click(value) +function popup:set_hide_on_click(value) self[value and "connect_signal" or "disconnect_signal"]( - self, "button::press", self._private.hide_fct + self, + "button::press", + self._private.hide_fct + ) + self:emit_signal("property::hide_on_click", value) +end +function popup:set_hide_on_right_click(value) + gdebug.deprecate( + "Use popup:set_hide_on_click() instead of set_hide_on_right_click", + { deprecated_in = 4 } ) - self:emit_signal("property::hide_on_right_click", value) + self:set_hide_on_click(value) +end +function popup:get_hide_on_click() + return self:is_signal_connected("button::press", self._private.hide_fct) end --- The popup minimum width. @@ -453,8 +465,8 @@ end -- @tparam string|table args.preferred_positions -- @tparam string|table args.preferred_anchors -- @tparam table|number args.offset The X and Y offset compared to the parent object --- @tparam boolean args.hide_on_right_click Whether or not to hide the popup on --- right clicks. +-- @tparam boolean args.hide_on_click Whether or not to hide the popup on +-- clicks. -- @constructorfct awful.popup local function create_popup(_, args) assert(args) @@ -509,12 +521,20 @@ local function create_popup(_, args) -- when set_position is called before the limits for _,v in ipairs{"minimum_width", "minimum_height", "maximum_height", "maximum_width", "offset", "placement","preferred_positions", - "preferred_anchors", "hide_on_right_click"} do + "preferred_anchors", "hide_on_right_click", "hide_on_click"} do if args[v] ~= nil then w["set_"..v](w, args[v]) end end + -- TODO : remove when we are ready to break backward compatibility + if args.hide_on_right_click ~= nil then + gdebug.deprecate( + "Use the hide_on_click parameter instead of hide_on_right_click", + { deprecated_in = 4 } + ) + end + -- Default to visible if args.visible ~= false then w.visible = true diff --git a/lib/gears/object.lua b/lib/gears/object.lua index 35beeb2236..45620cd8ee 100644 --- a/lib/gears/object.lua +++ b/lib/gears/object.lua @@ -60,6 +60,23 @@ function object:connect_signal(name, func) sig.strong[func] = true end +--- Check if the callback is connected to the signal. +-- +-- This function check both kind of signal connection (strong and weak). +-- +-- @tparam string name The name of the signal. +-- @tparam function func The callback to check if connected. +-- @treturn boolean Whether the signal is connected. +-- @method is_signal_connected +function object:is_signal_connected(name, func) + assert( + type(func) == "function", + "callback must be a function, got: " .. type(func) + ) + local sig = find_signal(self, name) + return not not (sig and (sig.strong[func] or sig.weak[func])) +end + -- Register a global signal receiver. function object:_connect_everything(callback) table.insert(self._global_receivers, callback) diff --git a/spec/gears/object_spec.lua b/spec/gears/object_spec.lua index 7c6eb57f72..fa8250029b 100644 --- a/spec/gears/object_spec.lua +++ b/spec/gears/object_spec.lua @@ -185,6 +185,20 @@ describe("gears.object", function() object{enable_auto_signals=true, enable_properties=false} end) end) + + it("is_signal_connected", function() + local cb = function()end + assert.is_false(obj:is_signal_connected("signal", cb)) + obj:connect_signal("signal", cb) + assert.is_true(obj:is_signal_connected("signal", cb)) + end) + + it("is_weak_signal_connected", function() + local cb = function()end + assert.is_false(obj:is_signal_connected("signal", cb)) + obj:weak_connect_signal("signal", cb) + assert.is_true(obj:is_signal_connected("signal", cb)) + end) end) -- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 diff --git a/tests/test-awful-popup-hide-on-click.lua b/tests/test-awful-popup-hide-on-click.lua new file mode 100644 index 0000000000..8ce6b3c01c --- /dev/null +++ b/tests/test-awful-popup-hide-on-click.lua @@ -0,0 +1,38 @@ +local runner = require "_runner" +local base = require "wibox.widget.base" +local popup = require "awful.popup" + +local widget = base.make_widget() +local p = popup { widget = widget } + +local function click(x, y) + return function() + mouse.coords { x = x, y = y } + assert(mouse.coords().x == x and mouse.coords().y == y) + root.fake_input("button_press", 1) + awesome.sync() + root.fake_input("button_release", 1) + awesome.sync() + return true + end +end + +runner.run_steps { + function() + assert(p.visible) + assert(not p.hide_on_click) + return true + end, + click(p.x, p.y), + function() + assert(p.visible) + p.hide_on_click = true + return true + end, + click(p.x, p.y), + function() + assert(not p.visible) + assert(p.hide_on_click) + return true + end, +}