forked from Alexays/Waybar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add simpleclock as fallback when hhdate is not available
ref Alexays#668
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#pragma once | ||
|
||
#include <fmt/format.h> | ||
#if FMT_VERSION < 60000 | ||
#include <fmt/time.h> | ||
#else | ||
#include <fmt/chrono.h> | ||
#endif | ||
#include "ALabel.hpp" | ||
#include "util/sleeper_thread.hpp" | ||
|
||
namespace waybar::modules { | ||
|
||
class Clock : public ALabel { | ||
public: | ||
Clock(const std::string&, const Json::Value&); | ||
~Clock() = default; | ||
auto update() -> void; | ||
|
||
private: | ||
util::SleeperThread thread_; | ||
}; | ||
|
||
} // namespace waybar::modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "modules/simpleclock.hpp" | ||
#include <time.h> | ||
|
||
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config) | ||
: ALabel(config, "clock", id, "{:%H:%M}", 60) { | ||
thread_ = [this] { | ||
dp.emit(); | ||
auto now = std::chrono::system_clock::now(); | ||
auto timeout = std::chrono::floor<std::chrono::seconds>(now + interval_); | ||
auto diff = std::chrono::seconds(timeout.time_since_epoch().count() % interval_.count()); | ||
thread_.sleep_until(timeout - diff); | ||
}; | ||
} | ||
|
||
auto waybar::modules::Clock::update() -> void { | ||
tzset(); // Update timezone information | ||
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); | ||
|
||
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); | ||
} | ||
} | ||
// Call parent update | ||
ALabel::update(); | ||
} |