2018-08-08 21:54:58 +00:00
|
|
|
#include "modules/clock.hpp"
|
2019-10-04 21:25:09 +00:00
|
|
|
#include <time.h>
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2018-12-18 16:30:54 +00:00
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2019-05-22 10:06:24 +00:00
|
|
|
: ALabel(config, "clock", id, "{:%H:%M}", 60) {
|
2018-11-23 10:57:37 +00:00
|
|
|
thread_ = [this] {
|
2018-08-20 12:50:45 +00:00
|
|
|
dp.emit();
|
2019-03-07 11:34:12 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
2018-11-23 10:57:37 +00:00
|
|
|
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
|
2019-05-22 09:51:33 +00:00
|
|
|
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count());
|
|
|
|
thread_.sleep_until(timeout - diff);
|
2018-08-08 21:54:58 +00:00
|
|
|
};
|
2018-08-18 09:43:48 +00:00
|
|
|
}
|
2018-08-08 21:54:58 +00:00
|
|
|
|
2019-04-18 15:52:00 +00:00
|
|
|
auto waybar::modules::Clock::update() -> void {
|
2019-07-05 21:46:38 +00:00
|
|
|
// Run user-provided update handler if configured
|
|
|
|
if (config_["on-update"].isString()) {
|
|
|
|
AModule::update();
|
|
|
|
}
|
|
|
|
|
2019-10-04 21:25:09 +00:00
|
|
|
tzset(); // Update timezone information
|
2019-05-19 12:30:19 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now));
|
2019-02-24 08:25:34 +00:00
|
|
|
auto text = fmt::format(format_, localtime);
|
|
|
|
label_.set_markup(text);
|
2019-04-18 15:52:00 +00:00
|
|
|
|
2019-02-24 08:25:34 +00:00
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
auto tooltip_text = fmt::format(tooltip_format, localtime);
|
|
|
|
label_.set_tooltip_text(tooltip_text);
|
|
|
|
} else {
|
|
|
|
label_.set_tooltip_text(text);
|
|
|
|
}
|
|
|
|
}
|
2018-08-09 10:05:48 +00:00
|
|
|
}
|