Waybar/src/modules/clock.cpp

28 lines
791 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)
: _config(config)
2018-08-08 21:54:58 +00:00
{
2018-08-09 08:50:16 +00:00
_label.get_style_context()->add_class("clock");
2018-08-08 21:54:58 +00:00
_thread = [this] {
Glib::signal_idle().connect_once(sigc::mem_fun(*this, &Clock::update));
2018-08-08 21:54:58 +00:00
auto now = waybar::chrono::clock::now();
auto timeout =
std::chrono::floor<std::chrono::minutes>(now + std::chrono::minutes(1));
_thread.sleep_until(timeout);
};
};
2018-08-09 10:05:48 +00:00
auto waybar::modules::Clock::update() -> void
{
auto t = std::time(nullptr);
auto localtime = std::localtime(&t);
2018-08-09 11:30:11 +00:00
auto format =
_config["format"] ? _config["format"].asString() : "{:02}:{:02}";
_label.set_text(fmt::format(format, localtime->tm_hour, localtime->tm_min));
2018-08-09 10:05:48 +00:00
}
2018-08-08 21:54:58 +00:00
waybar::modules::Clock::operator Gtk::Widget &() {
return _label;
}