2020-10-10 12:09:55 +00:00
|
|
|
#include "modules/simpleclock.hpp"
|
2022-04-06 06:37:19 +00:00
|
|
|
|
2020-10-10 12:09:55 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
|
2022-11-24 11:28:52 +00:00
|
|
|
: ALabel(config, "clock", id, "{:%H:%M}", 60) {
|
2020-10-10 12:09:55 +00:00
|
|
|
thread_ = [this] {
|
|
|
|
dp.emit();
|
2022-03-31 15:14:29 +00:00
|
|
|
auto now = std::chrono::system_clock::now();
|
|
|
|
/* difference with projected wakeup time */
|
|
|
|
auto diff = now.time_since_epoch() % interval_;
|
|
|
|
/* sleep until the next projected time */
|
|
|
|
thread_.sleep_for(interval_ - diff);
|
2020-10-10 12:09:55 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto waybar::modules::Clock::update() -> void {
|
2022-04-06 06:37:19 +00:00
|
|
|
tzset(); // Update timezone information
|
2020-10-10 12:09:55 +00: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);
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.set_markup(text);
|
2020-10-10 12:09:55 +00:00
|
|
|
|
|
|
|
if (tooltipEnabled()) {
|
|
|
|
if (config_["tooltip-format"].isString()) {
|
|
|
|
auto tooltip_format = config_["tooltip-format"].asString();
|
|
|
|
auto tooltip_text = fmt::format(tooltip_format, localtime);
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.set_tooltip_text(tooltip_text);
|
2020-10-10 12:09:55 +00:00
|
|
|
} else {
|
2022-11-24 11:28:52 +00:00
|
|
|
label_.set_tooltip_text(text);
|
2020-10-10 12:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Call parent update
|
2022-11-24 11:28:52 +00:00
|
|
|
ALabel::update();
|
2020-10-10 12:09:55 +00:00
|
|
|
}
|