Merge pull request #4447 from guttermonk/clock-fix

Clock Module: Independent clock tooltip formatting and fix for blank line at the end of the timezone list
This commit is contained in:
Alexis Rouillard
2025-10-01 14:18:28 +02:00
committed by GitHub
3 changed files with 45 additions and 3 deletions

View File

@ -62,6 +62,7 @@ class Clock final : public ALabel {
std::vector<const date::time_zone*> tzList_; // time zones list std::vector<const date::time_zone*> tzList_; // time zones list
int tzCurrIdx_; // current time zone index for tzList_ int tzCurrIdx_; // current time zone index for tzList_
std::string tzText_{""}; // time zones text to print std::string tzText_{""}; // time zones text to print
std::string tzTooltipFormat_{""}; // optional timezone tooltip format
util::SleeperThread thread_; util::SleeperThread thread_;
// ordinal date in tooltip // ordinal date in tooltip

View File

@ -39,6 +39,12 @@ $XDG_CONFIG_HOME/waybar/config ++
:[ A list of timezones (as in *timezone*) to use for time display, changed using :[ A list of timezones (as in *timezone*) to use for time display, changed using
the scroll wheel. Do not specify *timezone* option when *timezones* is specified. the scroll wheel. Do not specify *timezone* option when *timezones* is specified.
"" represents the system's local timezone "" represents the system's local timezone
|[ *timezone-tooltip-format*
:[ string
:[
:[ Format to use for displaying timezones in the tooltip. When set, this allows showing
timezone information (like timezone abbreviations) in the tooltip while keeping the
main display clean. Uses the same format options as *format*
|[ *locale* |[ *locale*
:[ string :[ string
:[ :[
@ -229,6 +235,25 @@ View all valid format options in *strftime(3)* or have a look https://en.cpprefe
} }
``` ```
4. Show timezone in tooltip only
```
"clock": {
"interval": 60,
"format": "{:%H:%M}",
"timezone-tooltip-format": "{:%H:%M %Z}",
"timezones": [
"",
"America/Chicago",
"America/Los_Angeles",
"Europe/Paris",
"UTC"
],
"tooltip": true,
"tooltip-format": "{tz_list}"
}
```
# STYLE # STYLE
- *#clock* - *#clock*

View File

@ -30,6 +30,7 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
cldMonShift_{year(1900) / January}, cldMonShift_{year(1900) / January},
tzInTooltip_{m_tlpFmt_.find("{" + kTZPlaceholder + "}") != std::string::npos}, tzInTooltip_{m_tlpFmt_.find("{" + kTZPlaceholder + "}") != std::string::npos},
tzCurrIdx_{0}, tzCurrIdx_{0},
tzTooltipFormat_{config_["timezone-tooltip-format"].isString() ? config_["timezone-tooltip-format"].asString() : ""},
ordInTooltip_{m_tlpFmt_.find("{" + kOrdPlaceholder + "}") != std::string::npos} { ordInTooltip_{m_tlpFmt_.find("{" + kOrdPlaceholder + "}") != std::string::npos} {
m_tlpText_ = m_tlpFmt_; m_tlpText_ = m_tlpFmt_;
@ -188,11 +189,26 @@ auto waybar::modules::Clock::getTZtext(sys_seconds now) -> std::string {
if (tzList_.size() == 1) return ""; if (tzList_.size() == 1) return "";
std::stringstream os; std::stringstream os;
bool first = true;
for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) { for (size_t tz_idx{0}; tz_idx < tzList_.size(); ++tz_idx) {
if (static_cast<int>(tz_idx) == tzCurrIdx_) continue; // Skip local timezone (nullptr) - never show it in tooltip
const auto* tz = tzList_[tz_idx] != nullptr ? tzList_[tz_idx] : local_zone(); if (tzList_[tz_idx] == nullptr) continue;
// Skip current timezone unless timezone-tooltip-format is specified
if (static_cast<int>(tz_idx) == tzCurrIdx_ && tzTooltipFormat_.empty()) continue;
const auto* tz = tzList_[tz_idx];
auto zt{zoned_time{tz, now}}; auto zt{zoned_time{tz, now}};
os << fmt_lib::vformat(m_locale_, format_, fmt_lib::make_format_args(zt)) << '\n';
// Add newline before each entry except the first
if (!first) {
os << '\n';
}
first = false;
// Use timezone-tooltip-format if specified, otherwise use format_
const std::string& fmt = tzTooltipFormat_.empty() ? format_ : tzTooltipFormat_;
os << fmt_lib::vformat(m_locale_, fmt, fmt_lib::make_format_args(zt));
} }
return os.str(); return os.str();