Calendar scrolling opportunity

This commit is contained in:
Viktar Lukashonak 2022-08-06 13:55:20 +03:00
parent 061ad13082
commit e9e5780aae
No known key found for this signature in database
GPG Key ID: 08A413AA87200A6F
2 changed files with 47 additions and 33 deletions

View File

@ -25,8 +25,9 @@ class Clock : public ALabel {
std::locale locale_; std::locale locale_;
std::vector<const date::time_zone*> time_zones_; std::vector<const date::time_zone*> time_zones_;
int current_time_zone_idx_; int current_time_zone_idx_;
date::year_month_day cached_calendar_ymd_ = date::January / 1 / 0; date::year_month_day calendar_cached_ymd_{date::January / 1 / 0};
std::string cached_calendar_text_; date::months calendar_shift_{0}, calendar_shift_init_{0};
std::string calendar_cached_text_;
bool is_calendar_in_tooltip_; bool is_calendar_in_tooltip_;
bool is_timezoned_list_in_tooltip_; bool is_timezoned_list_in_tooltip_;
@ -39,6 +40,5 @@ class Clock : public ALabel {
bool is_timezone_fixed(); bool is_timezone_fixed();
auto timezones_text(std::chrono::system_clock::time_point* now) -> std::string; auto timezones_text(std::chrono::system_clock::time_point* now) -> std::string;
}; };
} // namespace modules } // namespace modules
} // namespace waybar } // namespace waybar

View File

@ -61,6 +61,13 @@ waybar::modules::Clock::Clock(const std::string& id, const Json::Value& config)
} }
} }
if (is_calendar_in_tooltip_) {
if (config_["on-scroll"][kCalendarPlaceholder].isInt()) {
calendar_shift_init_ =
date::months{config_["on-scroll"].get(kCalendarPlaceholder, 0).asInt()};
}
}
if (config_["locale"].isString()) { if (config_["locale"].isString()) {
locale_ = std::locale(config_["locale"].asString()); locale_ = std::locale(config_["locale"].asString());
} else { } else {
@ -89,8 +96,8 @@ bool waybar::modules::Clock::is_timezone_fixed() {
auto waybar::modules::Clock::update() -> void { auto waybar::modules::Clock::update() -> void {
auto time_zone = current_timezone(); auto time_zone = current_timezone();
auto now = std::chrono::system_clock::now(); auto now = std::chrono::system_clock::now();
waybar_time wtime = {locale_, waybar_time wtime = {locale_, date::make_zoned(time_zone, date::floor<std::chrono::seconds>(now) +
date::make_zoned(time_zone, date::floor<std::chrono::seconds>(now))}; calendar_shift_)};
std::string text = ""; std::string text = "";
if (!is_timezone_fixed()) { if (!is_timezone_fixed()) {
// As date dep is not fully compatible, prefer fmt // As date dep is not fully compatible, prefer fmt
@ -104,14 +111,10 @@ 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 = ""; 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);
}
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 = text =
fmt::format(tooltip_format, wtime, fmt::arg(kCalendarPlaceholder.c_str(), calendar_lines), fmt::format(tooltip_format, wtime, fmt::arg(kCalendarPlaceholder.c_str(), calendar_lines),
@ -131,20 +134,30 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
} }
auto dir = AModule::getScrollDir(e); auto dir = AModule::getScrollDir(e);
if (dir != SCROLL_DIR::UP && dir != SCROLL_DIR::DOWN) {
return true;
}
if (time_zones_.size() == 1) {
return true;
}
auto nr_zones = time_zones_.size(); // Shift calendar date
if (dir == SCROLL_DIR::UP) { if (calendar_shift_init_.count() > 0) {
size_t new_idx = current_time_zone_idx_ + 1; if (dir == SCROLL_DIR::UP)
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx; calendar_shift_ += calendar_shift_init_;
else
calendar_shift_ -= calendar_shift_init_;
} else { } else {
current_time_zone_idx_ = // Change time zone
current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1; if (dir != SCROLL_DIR::UP && dir != SCROLL_DIR::DOWN) {
return true;
}
if (time_zones_.size() == 1) {
return true;
}
auto nr_zones = time_zones_.size();
if (dir == SCROLL_DIR::UP) {
size_t new_idx = current_time_zone_idx_ + 1;
current_time_zone_idx_ = new_idx == nr_zones ? 0 : new_idx;
} else {
current_time_zone_idx_ =
current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
}
} }
update(); update();
@ -153,13 +166,14 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string { auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string {
const auto daypoint = date::floor<date::days>(wtime.ztime.get_local_time()); const auto daypoint = date::floor<date::days>(wtime.ztime.get_local_time());
const auto ymd = date::year_month_day(daypoint); const auto ymd{date::year_month_day{daypoint}};
if (cached_calendar_ymd_ == ymd) {
return cached_calendar_text_;
}
const date::year_month ym(ymd.year(), ymd.month()); if (calendar_cached_ymd_ == ymd) return calendar_cached_text_;
const auto curr_day = ymd.day();
const auto curr_day{(calendar_shift_init_.count() > 0 && calendar_shift_.count() != 0)
? date::day{0}
: ymd.day()};
const date::year_month ym{ymd.year(), ymd.month()};
const auto week_format{config_["format-calendar-weekdays"].isString() const auto week_format{config_["format-calendar-weekdays"].isString()
? config_["format-calendar-weekdays"].asString() ? config_["format-calendar-weekdays"].asString()
: ""}; : ""};
@ -243,8 +257,8 @@ auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::str
} }
auto result = os.str(); auto result = os.str();
cached_calendar_ymd_ = ymd; calendar_cached_ymd_ = ymd;
cached_calendar_text_ = result; calendar_cached_text_ = result;
return result; return result;
} }