From 2c03d90fc896761cdb137ddb058c5c2a2a05612e Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 8 Mar 2024 11:47:29 +0400 Subject: [PATCH] Fix the new time picker. --- Telegram/Resources/langs/lang.strings | 1 + .../business/settings_working_hours.cpp | 41 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 2489020b8e..2805ebaf67 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -2205,6 +2205,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_hours_closed" = "Closed"; "lng_hours_open_full" = "Open 24 hours"; "lng_hours_next_day" = "{time} (Next day)"; +"lng_hours_on_next_day" = "Next day {time}"; "lng_hours_time_zone_title" = "Choose Time Zone"; "lng_hours_add_button" = "Add a Set of Hours"; "lng_hours_opening" = "Opening Time"; diff --git a/Telegram/SourceFiles/settings/business/settings_working_hours.cpp b/Telegram/SourceFiles/settings/business/settings_working_hours.cpp index db30dd8474..525d069a4f 100644 --- a/Telegram/SourceFiles/settings/business/settings_working_hours.cpp +++ b/Telegram/SourceFiles/settings/business/settings_working_hours.cpp @@ -94,20 +94,20 @@ private: return wrap(time); } const auto wrapped = wrap(time - kDay); - const auto result = tr::lng_hours_next_day(tr::now, lt_time, wrapped); + const auto result = tr::lng_hours_on_next_day(tr::now, lt_time, wrapped); const auto i = result.indexOf(wrapped); return (i >= 0) ? (result.left(i) + wrapped) : result; } [[nodiscard]] QString FormatTimeMinute(TimeId time) { const auto wrap = [](TimeId value) { - return QString::number(value / 60).rightJustified(2, u'0'); + return QString::number((value / 60) % 60).rightJustified(2, u'0'); }; if (time < kDay) { return wrap(time); } const auto wrapped = wrap(time - kDay); - const auto result = tr::lng_hours_next_day(tr::now, lt_time, wrapped); + const auto result = tr::lng_hours_on_next_day(tr::now, lt_time, wrapped); const auto i = result.indexOf(wrapped); return (i >= 0) ? (wrapped + result.right(result.size() - i - wrapped.size())) @@ -174,7 +174,7 @@ void EditTimeBox( }; const auto hoursCount = (high - low + 3600) / 3600; - const auto hoursStartIndex = (value - low) / 3600; + const auto hoursStartIndex = (value / 3600) - (low / 3600); const auto hoursPaint = [=](QPainter &p, QRectF rect, int index) { p.drawText( rect, @@ -185,6 +185,23 @@ void EditTimeBox( const auto minutes = content->lifetime().make_state< rpl::variable >(nullptr); + + // hours->value() is valid only after size is set. + const auto separator = u":"_q; + const auto separatorWidth = st::boxTextFont->width(separator); + rpl::combine( + content->sizeValue(), + minutes->value() + ) | rpl::start_with_next([=](QSize s, Ui::VerticalDrumPicker *minutes) { + const auto half = (s.width() - separatorWidth) / 2; + hours->setGeometry(0, 0, half, s.height()); + if (minutes) { + minutes->setGeometry(half + separatorWidth, 0, half, s.height()); + } + }, content->lifetime()); + + Ui::SendPendingMoveResizeEvents(hours); + const auto minutesStart = content->lifetime().make_state(); hours->value() | rpl::start_with_next([=](int hoursIndex) { const auto start = std::max(low, (hoursIndex + (low / 3600)) * 3600); @@ -196,13 +213,13 @@ void EditTimeBox( - ((start / 60) % 60)), 0, (minutesCount - 1)) - : std::clamp((value - start) / 60, 0, minutesCount - 1); + : std::clamp((value / 60) - (start / 60), 0, minutesCount - 1); *minutesStart = start; const auto minutesPaint = [=](QPainter &p, QRectF rect, int index) { p.drawText( rect, - FormatTimeMinute((((start / 60) + index) % 60) * 60), + FormatTimeMinute(((start / 60) + index) * 60), style::al_left); }; const auto updated = picker( @@ -214,18 +231,6 @@ void EditTimeBox( minutes->current()->show(); }, hours->lifetime()); - const auto separator = u":"_q; - const auto separatorWidth = st::boxTextFont->width(separator); - - rpl::combine( - content->sizeValue(), - minutes->value() - ) | rpl::start_with_next([=](QSize s, Ui::VerticalDrumPicker *minutes) { - const auto half = (s.width() - separatorWidth) / 2; - hours->setGeometry(0, 0, half, s.height()); - minutes->setGeometry(half + separatorWidth, 0, half, s.height()); - }, content->lifetime()); - content->paintRequest( ) | rpl::start_with_next([=](const QRect &r) { auto p = QPainter(content);