Waybar/src/modules/clock.cpp

27 lines
825 B
C++
Raw Normal View History

2018-08-08 21:54:58 +00:00
#include "modules/clock.hpp"
2018-08-09 11:30:11 +00:00
waybar::modules::Clock::Clock(Json::Value config)
2018-08-16 12:29:41 +00:00
: config_(std::move(config))
2018-08-08 21:54:58 +00:00
{
2018-08-16 12:29:41 +00:00
label_.set_name("clock");
uint32_t interval = config_["interval"] ? config_["inveral"].asUInt() : 60;
thread_ = [this, interval] {
2018-08-08 21:54:58 +00:00
auto now = waybar::chrono::clock::now();
2018-08-15 19:00:04 +00: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-16 12:29:41 +00:00
thread_.sleep_until(timeout);
2018-08-08 21:54:58 +00:00
};
};
2018-08-09 10:05:48 +00:00
auto waybar::modules::Clock::update() -> void
{
2018-08-15 18:53:27 +00:00
auto localtime = fmt::localtime(std::time(nullptr));
2018-08-16 12:29:41 +00:00
auto format = config_["format"] ? config_["format"].asString() : "{:%H:%M}";
label_.set_text(fmt::format(format, localtime));
2018-08-09 10:05:48 +00:00
}
2018-08-08 21:54:58 +00:00
waybar::modules::Clock::operator Gtk::Widget &() {
2018-08-16 12:29:41 +00:00
return label_;
2018-08-08 21:54:58 +00:00
}