waybar/src/modules/clock.cpp

36 lines
1.2 KiB
C++
Raw Normal View History

2018-08-08 23:54:58 +02:00
#include "modules/clock.hpp"
2018-12-18 17:30:54 +01:00
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
2019-04-18 17:52:00 +02:00
: ALabel(config, "{:%H:%M}", 60) {
2018-08-16 14:29:41 +02:00
label_.set_name("clock");
2018-12-18 17:30:54 +01:00
if (!id.empty()) {
label_.get_style_context()->add_class(id);
}
2018-11-23 11:57:37 +01:00
thread_ = [this] {
2018-08-20 14:50:45 +02:00
dp.emit();
2019-03-07 12:34:12 +01:00
auto now = std::chrono::system_clock::now();
2018-11-23 11:57:37 +01:00
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_);
auto time_point =
std::chrono::time_point_cast<std::chrono::seconds>(timeout).time_since_epoch().count() %
2019-04-18 17:52:00 +02:00
interval_.count();
thread_.sleep_until(timeout - std::chrono::seconds(time_point));
2018-08-08 23:54:58 +02:00
};
}
2018-08-08 23:54:58 +02:00
2019-04-18 17:52:00 +02:00
auto waybar::modules::Clock::update() -> void {
2019-05-19 14:30:19 +02:00
auto now = std::chrono::system_clock::now();
auto localtime = fmt::localtime(std::chrono::system_clock::to_time_t(now));
auto text = fmt::format(format_, localtime);
label_.set_markup(text);
2019-04-18 17:52:00 +02: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 12:05:48 +02:00
}