From 209e09ab66faea646189640a46c28acff3c734ba Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 1 Apr 2022 18:43:20 +0300 Subject: [PATCH] Moved box with vertical drum picker for time to separated module. --- Telegram/SourceFiles/menu/menu_ttl.cpp | 146 ++++-------------- .../SourceFiles/ui/boxes/time_picker_box.cpp | 107 +++++++++++++ .../SourceFiles/ui/boxes/time_picker_box.h | 20 +++ Telegram/cmake/td_ui.cmake | 2 + 4 files changed, 160 insertions(+), 115 deletions(-) create mode 100644 Telegram/SourceFiles/ui/boxes/time_picker_box.cpp create mode 100644 Telegram/SourceFiles/ui/boxes/time_picker_box.h diff --git a/Telegram/SourceFiles/menu/menu_ttl.cpp b/Telegram/SourceFiles/menu/menu_ttl.cpp index df6bf2db3..e9ddbefa0 100644 --- a/Telegram/SourceFiles/menu/menu_ttl.cpp +++ b/Telegram/SourceFiles/menu/menu_ttl.cpp @@ -7,13 +7,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "menu/menu_ttl.h" -#include "base/event_filter.h" #include "lang/lang_keys.h" +#include "ui/boxes/time_picker_box.h" #include "ui/layers/generic_box.h" #include "ui/text/format_values.h" #include "ui/ui_utility.h" #include "ui/widgets/labels.h" -#include "ui/widgets/vertical_drum_picker.h" #if 0 #include "ui/boxes/choose_time.h" #include "ui/widgets/menu/menu_action.h" @@ -21,7 +20,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_dialogs.h" // dialogsScamFont #include "styles/style_menu_icons.h" #endif -#include "styles/style_chat.h" #include "styles/style_layers.h" namespace TTLMenu { @@ -37,118 +35,7 @@ constexpr auto kTTLDurHours3 = crl::time(24 * 7); constexpr auto kTTLDurSeconds3 = kTTLDurHours3 * 3600; constexpr auto kTTLDurHours4 = crl::time(24 * 30); constexpr auto kTTLDurSeconds4 = kTTLDurHours4 * 3600; -#endif -void SetupPickerAndConfirm( - not_null box, - Fn callback, - TimeId startTtlPeriod) { - const auto ttls = std::vector{ - (86400 * 1), - (86400 * 2), - (86400 * 3), - (86400 * 4), - (86400 * 5), - (86400 * 6), - (86400 * 7 * 1), - (86400 * 7 * 2), - (86400 * 7 * 3), - (86400 * 30 * 1), - (86400 * 30 * 2), - (86400 * 30 * 3), - (86400 * 30 * 4), - (86400 * 30 * 5), - (86400 * 30 * 6), - (86400 * 30 * 12), - }; - const auto phrases = ranges::views::all( - ttls - ) | ranges::views::transform(Ui::FormatTTL) | ranges::to_vector; - - const auto startIndex = [&] { - const auto it = ranges::find(ttls, startTtlPeriod); - return (it == end(ttls)) ? 0 : std::distance(begin(ttls), it); - }(); - - const auto content = box->addRow(object_ptr( - box, - st::historyMessagesTTLPickerHeight)); - - const auto font = st::boxTextFont; - const auto maxPhraseWidth = [&] { - const auto maxPhrase = ranges::max_element( - phrases, - std::less<>(), - [&](const QString &s) { return font->width(s); }); - return font->width(*maxPhrase); - }(); - const auto itemHeight = st::historyMessagesTTLPickerItemHeight; - auto paintCallback = [=]( - Painter &p, - int index, - float64 y, - float64 distanceFromCenter, - int outerWidth) { - const auto r = QRectF(0, y, outerWidth, itemHeight); - const auto progress = std::abs(distanceFromCenter); - p.setOpacity(1. - progress); - p.setFont(font); - p.setPen(st::defaultFlatLabel.textFg); - p.drawText(r, phrases[index], style::al_center); - }; - - const auto picker = Ui::CreateChild( - content, - std::move(paintCallback), - phrases.size(), - itemHeight, - startIndex); - - content->sizeValue( - ) | rpl::start_with_next([=](const QSize &s) { - picker->resize(maxPhraseWidth, s.height()); - picker->moveToLeft((s.width() - picker->width()) / 2, 0); - }, content->lifetime()); - - content->paintRequest( - ) | rpl::start_with_next([=](const QRect &r) { - Painter p(content); - - p.fillRect(r, Qt::transparent); - - const auto lineRect = QRect( - 0, - content->height() / 2, - content->width(), - st::defaultInputField.borderActive); - p.fillRect(lineRect.translated(0, itemHeight / 2), st::activeLineFg); - p.fillRect(lineRect.translated(0, -itemHeight / 2), st::activeLineFg); - }, content->lifetime()); - - base::install_event_filter(content, [=](not_null e) { - if ((e->type() == QEvent::MouseButtonPress) - || (e->type() == QEvent::MouseButtonRelease) - || (e->type() == QEvent::MouseMove)) { - picker->handleMouseEvent(static_cast(e.get())); - } else if (e->type() == QEvent::Wheel) { - picker->handleWheelEvent(static_cast(e.get())); - } - return base::EventFilterResult::Continue; - }); - base::install_event_filter(box, [=](not_null e) { - if (e->type() == QEvent::KeyPress) { - picker->handleKeyEvent(static_cast(e.get())); - } - return base::EventFilterResult::Continue; - }); - - box->addButton(tr::lng_settings_save(), [=] { - callback(ttls[picker->index()]); - box->getDelegate()->hideLayer(); - }); -} - -#if 0 class IconWithText final : public Ui::Menu::Action { public: using Ui::Menu::Action::Action; @@ -273,7 +160,36 @@ void TTLBox(not_null box, Args args) { box, std::move(args.about), st::boxLabel)); - SetupPickerAndConfirm(box, args.callback, args.startTtl); + + const auto ttls = std::vector{ + (86400 * 1), + (86400 * 2), + (86400 * 3), + (86400 * 4), + (86400 * 5), + (86400 * 6), + (86400 * 7 * 1), + (86400 * 7 * 2), + (86400 * 7 * 3), + (86400 * 30 * 1), + (86400 * 30 * 2), + (86400 * 30 * 3), + (86400 * 30 * 4), + (86400 * 30 * 5), + (86400 * 30 * 6), + (86400 * 30 * 12), + }; + const auto phrases = ranges::views::all( + ttls + ) | ranges::views::transform(Ui::FormatTTL) | ranges::to_vector; + + const auto pickerTtl = TimePickerBox(box, ttls, phrases, args.startTtl); + + box->addButton(tr::lng_settings_save(), [=] { + args.callback(pickerTtl()); + box->getDelegate()->hideLayer(); + }); + box->setTitle(tr::lng_manage_messages_ttl_title()); box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); diff --git a/Telegram/SourceFiles/ui/boxes/time_picker_box.cpp b/Telegram/SourceFiles/ui/boxes/time_picker_box.cpp new file mode 100644 index 000000000..b3ff7dc67 --- /dev/null +++ b/Telegram/SourceFiles/ui/boxes/time_picker_box.cpp @@ -0,0 +1,107 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "ui/boxes/time_picker_box.h" + +#include "base/event_filter.h" +#include "lang/lang_keys.h" +#include "ui/layers/generic_box.h" +#include "ui/ui_utility.h" +#include "ui/widgets/vertical_drum_picker.h" +#include "styles/style_chat.h" +#include "styles/style_layers.h" + +namespace Ui { + +Fn TimePickerBox( + not_null box, + std::vector values, + std::vector phrases, + TimeId startValue) { + Expects(phrases.size() == values.size()); + + const auto startIndex = [&] { + const auto it = ranges::find(values, startValue); + return (it == end(values)) ? 0 : std::distance(begin(values), it); + }(); + + const auto content = box->addRow(object_ptr( + box, + st::historyMessagesTTLPickerHeight)); + + const auto font = st::boxTextFont; + const auto maxPhraseWidth = [&] { + const auto maxPhrase = ranges::max_element( + phrases, + std::less<>(), + [&](const QString &s) { return font->width(s); }); + return font->width(*maxPhrase); + }(); + const auto itemHeight = st::historyMessagesTTLPickerItemHeight; + auto paintCallback = [=]( + Painter &p, + int index, + float64 y, + float64 distanceFromCenter, + int outerWidth) { + const auto r = QRectF(0, y, outerWidth, itemHeight); + const auto progress = std::abs(distanceFromCenter); + p.setOpacity(1. - progress); + p.setFont(font); + p.setPen(st::defaultFlatLabel.textFg); + p.drawText(r, phrases[index], style::al_center); + }; + + const auto picker = Ui::CreateChild( + content, + std::move(paintCallback), + phrases.size(), + itemHeight, + startIndex); + + content->sizeValue( + ) | rpl::start_with_next([=](const QSize &s) { + picker->resize(maxPhraseWidth, s.height()); + picker->moveToLeft((s.width() - picker->width()) / 2, 0); + }, content->lifetime()); + + content->paintRequest( + ) | rpl::start_with_next([=](const QRect &r) { + Painter p(content); + + p.fillRect(r, Qt::transparent); + + const auto lineRect = QRect( + 0, + content->height() / 2, + content->width(), + st::defaultInputField.borderActive); + p.fillRect(lineRect.translated(0, itemHeight / 2), st::activeLineFg); + p.fillRect(lineRect.translated(0, -itemHeight / 2), st::activeLineFg); + }, content->lifetime()); + + base::install_event_filter(content, [=](not_null e) { + if ((e->type() == QEvent::MouseButtonPress) + || (e->type() == QEvent::MouseButtonRelease) + || (e->type() == QEvent::MouseMove)) { + picker->handleMouseEvent(static_cast(e.get())); + } else if (e->type() == QEvent::Wheel) { + picker->handleWheelEvent(static_cast(e.get())); + } + return base::EventFilterResult::Continue; + }); + base::install_event_filter(box, [=](not_null e) { + if (e->type() == QEvent::KeyPress) { + picker->handleKeyEvent(static_cast(e.get())); + } + return base::EventFilterResult::Continue; + }); + + return [=] { return values[picker->index()]; }; +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/ui/boxes/time_picker_box.h b/Telegram/SourceFiles/ui/boxes/time_picker_box.h new file mode 100644 index 000000000..407816b4a --- /dev/null +++ b/Telegram/SourceFiles/ui/boxes/time_picker_box.h @@ -0,0 +1,20 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +namespace Ui { + +class GenericBox; + +Fn TimePickerBox( + not_null box, + std::vector values, + std::vector phrases, + TimeId startValue); + +} // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index a6ab0269a..b8195cb31 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -155,6 +155,8 @@ PRIVATE ui/boxes/report_box.h ui/boxes/single_choice_box.cpp ui/boxes/single_choice_box.h + ui/boxes/time_picker_box.cpp + ui/boxes/time_picker_box.h ui/chat/attach/attach_abstract_single_file_preview.cpp ui/chat/attach/attach_abstract_single_file_preview.h