waybar/src/modules/clock.cpp

28 lines
812 B
C++
Raw Normal View History

2018-08-08 23:54:58 +02:00
#include "modules/clock.hpp"
2018-08-09 13:30:11 +02:00
waybar::modules::Clock::Clock(Json::Value config)
: _config(config)
2018-08-08 23:54:58 +02:00
{
2018-08-15 14:48:08 +02:00
_label.set_name("clock");
2018-08-15 21:00:04 +02:00
int interval = _config["interval"] ? _config["inveral"].asInt() : 60;
_thread = [this, interval] {
2018-08-08 23:54:58 +02:00
auto now = waybar::chrono::clock::now();
2018-08-15 21:00:04 +02:00
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Clock::update));
auto timeout = std::chrono::floor<std::chrono::seconds>(now
+ std::chrono::seconds(interval));
2018-08-08 23:54:58 +02:00
_thread.sleep_until(timeout);
};
};
2018-08-09 12:05:48 +02:00
auto waybar::modules::Clock::update() -> void
{
2018-08-15 20:53:27 +02:00
auto localtime = fmt::localtime(std::time(nullptr));
2018-08-09 13:30:11 +02:00
auto format =
2018-08-15 20:53:27 +02:00
_config["format"] ? _config["format"].asString() : "{:%H:%M}";
_label.set_text(fmt::format(format, localtime));
2018-08-09 12:05:48 +02:00
}
2018-08-08 23:54:58 +02:00
waybar::modules::Clock::operator Gtk::Widget &() {
return _label;
}