From e9e5780aaeb4a02d054b1434e5c33a0054a6ddf2 Mon Sep 17 00:00:00 2001 From: Viktar Lukashonak Date: Sat, 6 Aug 2022 13:55:20 +0300 Subject: [PATCH] Calendar scrolling opportunity --- include/modules/clock.hpp | 6 ++-- src/modules/clock.cpp | 74 +++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/include/modules/clock.hpp b/include/modules/clock.hpp index efc7186..08ab05e 100644 --- a/include/modules/clock.hpp +++ b/include/modules/clock.hpp @@ -25,8 +25,9 @@ class Clock : public ALabel { std::locale locale_; std::vector time_zones_; int current_time_zone_idx_; - date::year_month_day cached_calendar_ymd_ = date::January / 1 / 0; - std::string cached_calendar_text_; + date::year_month_day calendar_cached_ymd_{date::January / 1 / 0}; + date::months calendar_shift_{0}, calendar_shift_init_{0}; + std::string calendar_cached_text_; bool is_calendar_in_tooltip_; bool is_timezoned_list_in_tooltip_; @@ -39,6 +40,5 @@ class Clock : public ALabel { bool is_timezone_fixed(); auto timezones_text(std::chrono::system_clock::time_point* now) -> std::string; }; - } // namespace modules } // namespace waybar diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 467536e..71b24c1 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -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()) { locale_ = std::locale(config_["locale"].asString()); } else { @@ -89,8 +96,8 @@ bool waybar::modules::Clock::is_timezone_fixed() { auto waybar::modules::Clock::update() -> void { auto time_zone = current_timezone(); auto now = std::chrono::system_clock::now(); - waybar_time wtime = {locale_, - date::make_zoned(time_zone, date::floor(now))}; + waybar_time wtime = {locale_, date::make_zoned(time_zone, date::floor(now) + + calendar_shift_)}; std::string text = ""; if (!is_timezone_fixed()) { // As date dep is not fully compatible, prefer fmt @@ -104,14 +111,10 @@ auto waybar::modules::Clock::update() -> void { if (tooltipEnabled()) { if (config_["tooltip-format"].isString()) { - std::string calendar_lines = ""; - std::string timezoned_time_lines = ""; - if (is_calendar_in_tooltip_) { - calendar_lines = calendar_text(wtime); - } - if (is_timezoned_list_in_tooltip_) { - timezoned_time_lines = timezones_text(&now); - } + std::string calendar_lines{""}; + std::string timezoned_time_lines{""}; + if (is_calendar_in_tooltip_) calendar_lines = calendar_text(wtime); + if (is_timezoned_list_in_tooltip_) timezoned_time_lines = timezones_text(&now); auto tooltip_format = config_["tooltip-format"].asString(); text = 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); - 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; + // Shift calendar date + if (calendar_shift_init_.count() > 0) { + if (dir == SCROLL_DIR::UP) + calendar_shift_ += calendar_shift_init_; + else + calendar_shift_ -= calendar_shift_init_; } else { - current_time_zone_idx_ = - current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1; + // Change time zone + 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(); @@ -153,13 +166,14 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) { auto waybar::modules::Clock::calendar_text(const waybar_time& wtime) -> std::string { const auto daypoint = date::floor(wtime.ztime.get_local_time()); - const auto ymd = date::year_month_day(daypoint); - if (cached_calendar_ymd_ == ymd) { - return cached_calendar_text_; - } + const auto ymd{date::year_month_day{daypoint}}; - const date::year_month ym(ymd.year(), ymd.month()); - const auto curr_day = ymd.day(); + if (calendar_cached_ymd_ == ymd) return calendar_cached_text_; + + 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() ? 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(); - cached_calendar_ymd_ = ymd; - cached_calendar_text_ = result; + calendar_cached_ymd_ = ymd; + calendar_cached_text_ = result; return result; }