Add and use gears.timer.delayed_call
This function calls a callback at the end of the current main loop iteration. Signed-off-by: Uli Schlachter <psychon@znc.in>
This commit is contained in:
parent
27988ecdde
commit
6fc15f4afd
|
@ -4,10 +4,14 @@
|
||||||
-- @release @AWESOME_VERSION@
|
-- @release @AWESOME_VERSION@
|
||||||
---------------------------------------------------------------------------
|
---------------------------------------------------------------------------
|
||||||
|
|
||||||
local setmetatable = setmetatable
|
local capi = { awesome = awesome }
|
||||||
|
local ipairs = ipairs
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
|
local setmetatable = setmetatable
|
||||||
|
local table = table
|
||||||
local tonumber = tonumber
|
local tonumber = tonumber
|
||||||
local traceback = debug.traceback
|
local traceback = debug.traceback
|
||||||
|
local unpack = unpack or table.unpack -- v5.1: unpack, v5.2: table.unpack
|
||||||
local glib = require("lgi").GLib
|
local glib = require("lgi").GLib
|
||||||
local object = require("gears.object")
|
local object = require("gears.object")
|
||||||
|
|
||||||
|
@ -93,6 +97,25 @@ local function new(args)
|
||||||
return ret
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local delayed_calls = {}
|
||||||
|
capi.awesome.connect_signal("refresh", function()
|
||||||
|
for _, callback in ipairs(delayed_calls) do
|
||||||
|
local success, message = pcall(unpack(callback))
|
||||||
|
if not success then
|
||||||
|
print(message)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
delayed_calls = {}
|
||||||
|
end)
|
||||||
|
|
||||||
|
--- Call the given function at the end of the current main loop iteration
|
||||||
|
-- @param callback The function that should be called
|
||||||
|
-- @param ... Arguments to the callback function
|
||||||
|
function timer.delayed_call(callback, ...)
|
||||||
|
assert(type(callback) == "function", "callback must be a function, got: " .. type(callback))
|
||||||
|
table.insert(delayed_calls, { callback, ... })
|
||||||
|
end
|
||||||
|
|
||||||
function timer.mt:__call(...)
|
function timer.mt:__call(...)
|
||||||
return new(...)
|
return new(...)
|
||||||
end
|
end
|
||||||
|
|
|
@ -16,6 +16,7 @@ local debug = require("gears.debug")
|
||||||
local object = require("gears.object")
|
local object = require("gears.object")
|
||||||
local sort = require("gears.sort")
|
local sort = require("gears.sort")
|
||||||
local surface = require("gears.surface")
|
local surface = require("gears.surface")
|
||||||
|
local timer = require("gears.timer")
|
||||||
|
|
||||||
local drawables = setmetatable({}, { __mode = 'k' })
|
local drawables = setmetatable({}, { __mode = 'k' })
|
||||||
local wallpaper = nil
|
local wallpaper = nil
|
||||||
|
@ -241,14 +242,13 @@ function drawable.new(d, widget_arg)
|
||||||
ret._redraw_pending = false
|
ret._redraw_pending = false
|
||||||
ret._do_redraw = function()
|
ret._do_redraw = function()
|
||||||
ret._redraw_pending = false
|
ret._redraw_pending = false
|
||||||
capi.awesome.disconnect_signal("refresh", ret._do_redraw)
|
|
||||||
do_redraw(ret)
|
do_redraw(ret)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Connect our signal when we need a redraw
|
-- Connect our signal when we need a redraw
|
||||||
ret.draw = function()
|
ret.draw = function()
|
||||||
if not ret._redraw_pending then
|
if not ret._redraw_pending then
|
||||||
capi.awesome.connect_signal("refresh", ret._do_redraw)
|
timer.delayed_call(ret._do_redraw)
|
||||||
ret._redraw_pending = true
|
ret._redraw_pending = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue