Merge pull request #1333 from dartkron/list_of_times
Feature Clock: show list of time in other timezones in a tooltip
This commit is contained in:
		
						commit
						1aa7587cac
					
				| 
						 | 
					@ -11,6 +11,7 @@ struct waybar_time;
 | 
				
			||||||
namespace modules {
 | 
					namespace modules {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const std::string kCalendarPlaceholder = "calendar";
 | 
					const std::string kCalendarPlaceholder = "calendar";
 | 
				
			||||||
 | 
					const std::string KTimezonedTimeListPlaceholder = "timezoned_time_list";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Clock : public ALabel {
 | 
					class Clock : public ALabel {
 | 
				
			||||||
 public:
 | 
					 public:
 | 
				
			||||||
| 
						 | 
					@ -26,6 +27,7 @@ class Clock : public ALabel {
 | 
				
			||||||
  date::year_month_day cached_calendar_ymd_ = date::January/1/0;
 | 
					  date::year_month_day cached_calendar_ymd_ = date::January/1/0;
 | 
				
			||||||
  std::string cached_calendar_text_;
 | 
					  std::string cached_calendar_text_;
 | 
				
			||||||
  bool is_calendar_in_tooltip_;
 | 
					  bool is_calendar_in_tooltip_;
 | 
				
			||||||
 | 
					  bool is_timezoned_list_in_tooltip_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  bool handleScroll(GdkEventScroll* e);
 | 
					  bool handleScroll(GdkEventScroll* e);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -34,6 +36,7 @@ class Clock : public ALabel {
 | 
				
			||||||
  auto first_day_of_week() -> date::weekday;
 | 
					  auto first_day_of_week() -> date::weekday;
 | 
				
			||||||
  const date::time_zone* current_timezone();
 | 
					  const date::time_zone* current_timezone();
 | 
				
			||||||
  bool is_timezone_fixed();
 | 
					  bool is_timezone_fixed();
 | 
				
			||||||
 | 
					  auto timezones_text(std::chrono::_V2::system_clock::time_point *now) -> std::string;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}  // namespace modules
 | 
					}  // namespace modules
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -96,6 +96,7 @@ View all valid format options in *strftime(3)*.
 | 
				
			||||||
# FORMAT REPLACEMENTS
 | 
					# FORMAT REPLACEMENTS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
*{calendar}*: Current month calendar
 | 
					*{calendar}*: Current month calendar
 | 
				
			||||||
 | 
					*{timezoned_time_list}*: List of time in the rest timezones, if more than one timezone is set in the config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# EXAMPLES
 | 
					# EXAMPLES
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -23,7 +23,8 @@ using waybar::waybar_time;
 | 
				
			||||||
waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
 | 
					waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
 | 
				
			||||||
    : ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true),
 | 
					    : ALabel(config, "clock", id, "{:%H:%M}", 60, false, false, true),
 | 
				
			||||||
      current_time_zone_idx_(0),
 | 
					      current_time_zone_idx_(0),
 | 
				
			||||||
      is_calendar_in_tooltip_(false)
 | 
					      is_calendar_in_tooltip_(false),
 | 
				
			||||||
 | 
					      is_timezoned_list_in_tooltip_(false)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
  if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
 | 
					  if (config_["timezones"].isArray() && !config_["timezones"].empty()) {
 | 
				
			||||||
    for (const auto& zone_name: config_["timezones"]) {
 | 
					    for (const auto& zone_name: config_["timezones"]) {
 | 
				
			||||||
| 
						 | 
					@ -64,6 +65,9 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
 | 
				
			||||||
    if (trimmed_format.find("{" + kCalendarPlaceholder + "}") != std::string::npos) {
 | 
					    if (trimmed_format.find("{" + kCalendarPlaceholder + "}") != std::string::npos) {
 | 
				
			||||||
      is_calendar_in_tooltip_ = true;
 | 
					      is_calendar_in_tooltip_ = true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    if (trimmed_format.find("{" + KTimezonedTimeListPlaceholder + "}") != std::string::npos) {
 | 
				
			||||||
 | 
					      is_timezoned_list_in_tooltip_ = true;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (config_["locale"].isString()) {
 | 
					  if (config_["locale"].isString()) {
 | 
				
			||||||
| 
						 | 
					@ -108,11 +112,15 @@ auto waybar::modules::Clock::update() -> void {
 | 
				
			||||||
  if (tooltipEnabled()) {
 | 
					  if (tooltipEnabled()) {
 | 
				
			||||||
    if (config_["tooltip-format"].isString()) {
 | 
					    if (config_["tooltip-format"].isString()) {
 | 
				
			||||||
      std::string calendar_lines = "";
 | 
					      std::string calendar_lines = "";
 | 
				
			||||||
 | 
					      std::string timezoned_time_lines = "";
 | 
				
			||||||
      if (is_calendar_in_tooltip_) {
 | 
					      if (is_calendar_in_tooltip_) {
 | 
				
			||||||
        calendar_lines = calendar_text(wtime);
 | 
					        calendar_lines = calendar_text(wtime);
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					      if (is_timezoned_list_in_tooltip_) {
 | 
				
			||||||
 | 
					        timezoned_time_lines = timezones_text(&now);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
      auto tooltip_format = config_["tooltip-format"].asString();
 | 
					      auto tooltip_format = config_["tooltip-format"].asString();
 | 
				
			||||||
      text = fmt::format(tooltip_format, wtime, fmt::arg(kCalendarPlaceholder.c_str(), calendar_lines));
 | 
					      text = fmt::format(tooltip_format, wtime, fmt::arg(kCalendarPlaceholder.c_str(), calendar_lines), fmt::arg(KTimezonedTimeListPlaceholder.c_str(), timezoned_time_lines));
 | 
				
			||||||
      label_.set_tooltip_markup(text);
 | 
					      label_.set_tooltip_markup(text);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
| 
						 | 
					@ -211,6 +219,26 @@ auto waybar::modules::Clock::weekdays_header(const date::weekday& first_dow, std
 | 
				
			||||||
  os << "\n";
 | 
					  os << "\n";
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					auto waybar::modules::Clock::timezones_text(std::chrono::_V2::system_clock::time_point *now) -> std::string {
 | 
				
			||||||
 | 
					  if (time_zones_.size() == 1) {
 | 
				
			||||||
 | 
					    return "";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  std::stringstream os;
 | 
				
			||||||
 | 
					  waybar_time wtime;
 | 
				
			||||||
 | 
					  for (size_t time_zone_idx = 0; time_zone_idx < time_zones_.size(); ++time_zone_idx) {
 | 
				
			||||||
 | 
					    if (static_cast<int>(time_zone_idx) == current_time_zone_idx_) {
 | 
				
			||||||
 | 
					      continue;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    const date::time_zone* timezone = time_zones_[time_zone_idx];
 | 
				
			||||||
 | 
					    if (!timezone) {
 | 
				
			||||||
 | 
					      timezone = date::current_zone();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    wtime = {locale_, date::make_zoned(timezone, date::floor<std::chrono::seconds>(*now))};
 | 
				
			||||||
 | 
					    os << fmt::format(format_, wtime) << "\n";
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  return os.str();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef HAVE_LANGINFO_1STDAY
 | 
					#ifdef HAVE_LANGINFO_1STDAY
 | 
				
			||||||
template <auto fn>
 | 
					template <auto fn>
 | 
				
			||||||
using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
 | 
					using deleter_from_fn = std::integral_constant<decltype(fn), fn>;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue