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::vector<const date::time_zone*> 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

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()) {
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<std::chrono::seconds>(now))};
waybar_time wtime = {locale_, date::make_zoned(time_zone, date::floor<std::chrono::seconds>(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,6 +134,15 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
}
auto dir = AModule::getScrollDir(e);
// 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 {
// Change time zone
if (dir != SCROLL_DIR::UP && dir != SCROLL_DIR::DOWN) {
return true;
}
@ -146,6 +158,7 @@ bool waybar::modules::Clock::handleScroll(GdkEventScroll* e) {
current_time_zone_idx_ =
current_time_zone_idx_ == 0 ? nr_zones - 1 : current_time_zone_idx_ - 1;
}
}
update();
return true;
@ -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<date::days>(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;
}