From 6fc15f4afdbb49cddb986fe35c61996875e5ca46 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sun, 11 Jan 2015 10:58:49 +0100 Subject: [PATCH] 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 --- lib/gears/timer.lua.in | 25 ++++++++++++++++++++++++- lib/wibox/drawable.lua.in | 4 ++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/lib/gears/timer.lua.in b/lib/gears/timer.lua.in index 9e7eabdf..2ff3ed03 100644 --- a/lib/gears/timer.lua.in +++ b/lib/gears/timer.lua.in @@ -4,10 +4,14 @@ -- @release @AWESOME_VERSION@ --------------------------------------------------------------------------- -local setmetatable = setmetatable +local capi = { awesome = awesome } +local ipairs = ipairs local pairs = pairs +local setmetatable = setmetatable +local table = table local tonumber = tonumber local traceback = debug.traceback +local unpack = unpack or table.unpack -- v5.1: unpack, v5.2: table.unpack local glib = require("lgi").GLib local object = require("gears.object") @@ -93,6 +97,25 @@ local function new(args) return ret 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(...) return new(...) end diff --git a/lib/wibox/drawable.lua.in b/lib/wibox/drawable.lua.in index 36ac58e5..48282771 100644 --- a/lib/wibox/drawable.lua.in +++ b/lib/wibox/drawable.lua.in @@ -16,6 +16,7 @@ local debug = require("gears.debug") local object = require("gears.object") local sort = require("gears.sort") local surface = require("gears.surface") +local timer = require("gears.timer") local drawables = setmetatable({}, { __mode = 'k' }) local wallpaper = nil @@ -241,14 +242,13 @@ function drawable.new(d, widget_arg) ret._redraw_pending = false ret._do_redraw = function() ret._redraw_pending = false - capi.awesome.disconnect_signal("refresh", ret._do_redraw) do_redraw(ret) end -- Connect our signal when we need a redraw ret.draw = function() if not ret._redraw_pending then - capi.awesome.connect_signal("refresh", ret._do_redraw) + timer.delayed_call(ret._do_redraw) ret._redraw_pending = true end end