From 9d3a9547b13d94724bac37613fcb024c5988c3d5 Mon Sep 17 00:00:00 2001 From: Otto Modinos Date: Thu, 11 Jan 2018 16:00:07 +0200 Subject: [PATCH 1/3] Add set/get for textclock attributes To allow "declarative" construction of clocks. --- lib/wibox/widget/textclock.lua | 58 ++++++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/lib/wibox/widget/textclock.lua b/lib/wibox/widget/textclock.lua index 829ee98cf..4b6c5cd3d 100644 --- a/lib/wibox/widget/textclock.lua +++ b/lib/wibox/widget/textclock.lua @@ -10,12 +10,47 @@ local setmetatable = setmetatable local os = os local textbox = require("wibox.widget.textbox") local timer = require("gears.timer") +local xtable = require("gears.table") local glib = require("lgi").GLib local DateTime = glib.DateTime local TimeZone = glib.TimeZone local textclock = { mt = {} } +--- Set the clock's format +-- @tparam string format The new time format. This can contain pango markup +function textclock:set_format(format) + self._private.format = format + self:force_update() +end + +function textclock:get_format() + return self._private.format +end + +--- Set the clock's timezone +-- @tparam string timezone +function textclock:set_timezone(tzid) + self._private.tzid = tzid + self._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local() + self:force_update() +end + +function textclock:get_timezone() + return self._private.tzid +end + +--- Set the clock's timeout +-- @tparam number How often the clock is updated +function textclock:set_timeout(timeout) + self._private.timeout = timeout or self._private.timeout + self:force_update() +end + +function textclock:get_timeout() + return self._private.timeout +end + --- Force a textclock to update now. function textclock:force_update() self._timer:emit_signal("timeout") @@ -30,31 +65,34 @@ end --- Create a textclock widget. It draws the time it is in a textbox. -- -- @tparam[opt=" %a %b %d, %H:%M "] string format The time format. --- @tparam[opt=60] number timeout How often update the time (in seconds). +-- @tparam[opt=60] number timeout How often to update the time (in seconds). -- @tparam[opt=local timezone] string timezone The timezone to use, -- e.g. "Z" for UTC, "±hh:mm" or "Europe/Amsterdam". See -- https://developer.gnome.org/glib/stable/glib-GTimeZone.html#g-time-zone-new. -- @treturn table A textbox widget. -- @function wibox.widget.textclock -function textclock.new(format, timeout, timezone) - format = format or " %a %b %d, %H:%M " - timeout = timeout or 60 - timezone = timezone and TimeZone.new(timezone) or TimeZone.new_local() - +function textclock.new(format, timeout, tzid) local w = textbox() - w.force_update = textclock.force_update + xtable.crush(w, textclock, true) + + w._private.format = format or " %a %b %d, %H:%M " + w._private.timeout = timeout or 60 + w._private.tzid = tzid + w._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local() + function w._private.textclock_update_cb() - local str = DateTime.new_now(timezone):format(format) + local str = DateTime.new_now(w._private.timezone):format(w._private.format) if str == nil then require("gears.debug").print_warning("textclock: " .. "g_date_time_format() failed for format " - .. "'" .. format .. "'") + .. "'" .. w._private.format .. "'") end w:set_markup(str) - w._timer.timeout = calc_timeout(timeout) + w._timer.timeout = calc_timeout(w._private.timeout) w._timer:again() return true -- Continue the timer end + w._timer = timer.weak_start_new(timeout, w._private.textclock_update_cb) w:force_update() return w From 1e685635a83a97fb2c4b5cbbab01dde0297f214c Mon Sep 17 00:00:00 2001 From: Otto Modinos Date: Sun, 21 Jan 2018 01:57:59 +0200 Subject: [PATCH 2/3] textclock: rename timeout to refresh --- lib/wibox/widget/textclock.lua | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/wibox/widget/textclock.lua b/lib/wibox/widget/textclock.lua index 4b6c5cd3d..ae0f4d180 100644 --- a/lib/wibox/widget/textclock.lua +++ b/lib/wibox/widget/textclock.lua @@ -40,15 +40,15 @@ function textclock:get_timezone() return self._private.tzid end ---- Set the clock's timeout +--- Set the clock's refresh rate -- @tparam number How often the clock is updated -function textclock:set_timeout(timeout) - self._private.timeout = timeout or self._private.timeout +function textclock:set_refresh(refresh) + self._private.refresh = refresh or self._private.refresh self:force_update() end -function textclock:get_timeout() - return self._private.timeout +function textclock:get_refresh() + return self._private.refresh end --- Force a textclock to update now. @@ -65,18 +65,18 @@ end --- Create a textclock widget. It draws the time it is in a textbox. -- -- @tparam[opt=" %a %b %d, %H:%M "] string format The time format. --- @tparam[opt=60] number timeout How often to update the time (in seconds). +-- @tparam[opt=60] number refresh How often to update the time (in seconds). -- @tparam[opt=local timezone] string timezone The timezone to use, -- e.g. "Z" for UTC, "±hh:mm" or "Europe/Amsterdam". See -- https://developer.gnome.org/glib/stable/glib-GTimeZone.html#g-time-zone-new. -- @treturn table A textbox widget. -- @function wibox.widget.textclock -function textclock.new(format, timeout, tzid) +function textclock.new(format, refresh, tzid) local w = textbox() xtable.crush(w, textclock, true) w._private.format = format or " %a %b %d, %H:%M " - w._private.timeout = timeout or 60 + w._private.refresh = refresh or 60 w._private.tzid = tzid w._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local() @@ -88,12 +88,12 @@ function textclock.new(format, timeout, tzid) .. "'" .. w._private.format .. "'") end w:set_markup(str) - w._timer.timeout = calc_timeout(w._private.timeout) + w._timer.timeout = calc_timeout(w._private.refresh) w._timer:again() return true -- Continue the timer end - w._timer = timer.weak_start_new(timeout, w._private.textclock_update_cb) + w._timer = timer.weak_start_new(refresh, w._private.textclock_update_cb) w:force_update() return w end From 3687d198f26d49f71d5260e59d611cc88d005ed1 Mon Sep 17 00:00:00 2001 From: Otto Modinos Date: Sun, 21 Jan 2018 02:18:54 +0200 Subject: [PATCH 3/3] textclock: mimick other widgets for docs/naming --- lib/wibox/widget/textclock.lua | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/wibox/widget/textclock.lua b/lib/wibox/widget/textclock.lua index ae0f4d180..f7c8bf78a 100644 --- a/lib/wibox/widget/textclock.lua +++ b/lib/wibox/widget/textclock.lua @@ -10,7 +10,7 @@ local setmetatable = setmetatable local os = os local textbox = require("wibox.widget.textbox") local timer = require("gears.timer") -local xtable = require("gears.table") +local gtable = require("gears.table") local glib = require("lgi").GLib local DateTime = glib.DateTime local TimeZone = glib.TimeZone @@ -18,7 +18,9 @@ local TimeZone = glib.TimeZone local textclock = { mt = {} } --- Set the clock's format +-- @property format -- @tparam string format The new time format. This can contain pango markup + function textclock:set_format(format) self._private.format = format self:force_update() @@ -29,7 +31,9 @@ function textclock:get_format() end --- Set the clock's timezone +-- @property timezone -- @tparam string timezone + function textclock:set_timezone(tzid) self._private.tzid = tzid self._private.timezone = tzid and TimeZone.new(tzid) or TimeZone.new_local() @@ -41,7 +45,9 @@ function textclock:get_timezone() end --- Set the clock's refresh rate --- @tparam number How often the clock is updated +-- @property refresh +-- @tparam number How often the clock is updated, in seconds + function textclock:set_refresh(refresh) self._private.refresh = refresh or self._private.refresh self:force_update() @@ -71,9 +77,9 @@ end -- https://developer.gnome.org/glib/stable/glib-GTimeZone.html#g-time-zone-new. -- @treturn table A textbox widget. -- @function wibox.widget.textclock -function textclock.new(format, refresh, tzid) +local function new(format, refresh, tzid) local w = textbox() - xtable.crush(w, textclock, true) + gtable.crush(w, textclock, true) w._private.format = format or " %a %b %d, %H:%M " w._private.refresh = refresh or 60 @@ -99,7 +105,7 @@ function textclock.new(format, refresh, tzid) end function textclock.mt:__call(...) - return textclock.new(...) + return new(...) end --@DOC_widget_COMMON@