diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 5ce76bd32..7e3a0c64c 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -233,8 +233,6 @@ PRIVATE boxes/phone_banned_box.h boxes/pin_messages_box.cpp boxes/pin_messages_box.h - boxes/rate_call_box.cpp - boxes/rate_call_box.h boxes/self_destruction_box.cpp boxes/self_destruction_box.h boxes/send_files_box.cpp diff --git a/Telegram/SourceFiles/boxes/rate_call_box.cpp b/Telegram/SourceFiles/boxes/rate_call_box.cpp deleted file mode 100644 index adbc02ec8..000000000 --- a/Telegram/SourceFiles/boxes/rate_call_box.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* -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 "boxes/rate_call_box.h" - -#include "lang/lang_keys.h" -#include "ui/boxes/confirm_box.h" -#include "ui/widgets/labels.h" -#include "ui/widgets/buttons.h" -#include "ui/widgets/input_fields.h" -#include "mainwindow.h" -#include "main/main_session.h" -#include "core/application.h" -#include "core/core_settings.h" -#include "apiwrap.h" -#include "styles/style_layers.h" -#include "styles/style_calls.h" - -namespace { - -constexpr auto kMaxRating = 5; -constexpr auto kRateCallCommentLengthMax = 200; - -} // namespace - -RateCallBox::RateCallBox( - QWidget*, - not_null session, - uint64 callId, - uint64 callAccessHash) -: _session(session) -, _api(&_session->mtp()) -, _callId(callId) -, _callAccessHash(callAccessHash) { -} - -void RateCallBox::prepare() { - setTitle(tr::lng_call_rate_label()); - addButton(tr::lng_cancel(), [this] { closeBox(); }); - - for (auto i = 0; i < kMaxRating; ++i) { - _stars.emplace_back(this, st::callRatingStar); - _stars.back()->setClickedCallback([this, value = i + 1] { ratingChanged(value); }); - _stars.back()->show(); - } - - updateMaxHeight(); -} - -void RateCallBox::resizeEvent(QResizeEvent *e) { - BoxContent::resizeEvent(e); - - auto starsWidth = (_stars.size() * st::callRatingStar.width); - auto starLeft = (width() - starsWidth) / 2; - auto starTop = st::callRatingStarTop; - for (auto &star : _stars) { - star->moveToLeft(starLeft, starTop); - starLeft += star->width(); - } - if (_comment) { - _comment->moveToLeft(st::callRatingPadding.left(), _stars.back()->bottomNoMargins() + st::callRatingCommentTop); - } -} - -void RateCallBox::ratingChanged(int value) { - Expects(value > 0 && value <= kMaxRating); - if (!_rating) { - clearButtons(); - addButton(tr::lng_send_button(), [this] { send(); }); - addButton(tr::lng_cancel(), [this] { closeBox(); }); - } - _rating = value; - - for (auto i = 0; i < kMaxRating; ++i) { - _stars[i]->setIconOverride((i < value) ? &st::callRatingStarFilled : nullptr); - _stars[i]->setRippleColorOverride((i < value) ? &st::lightButtonBgOver : nullptr); - } - if (value < kMaxRating) { - if (!_comment) { - _comment.create( - this, - st::callRatingComment, - Ui::InputField::Mode::MultiLine, - tr::lng_call_rate_comment()); - _comment->show(); - _comment->setSubmitSettings(Core::App().settings().sendSubmitWay()); - _comment->setMaxLength(kRateCallCommentLengthMax); - _comment->resize(width() - (st::callRatingPadding.left() + st::callRatingPadding.right()), _comment->height()); - - updateMaxHeight(); - connect(_comment, &Ui::InputField::resized, [=] { commentResized(); }); - connect(_comment, &Ui::InputField::submitted, [=] { send(); }); - connect(_comment, &Ui::InputField::cancelled, [=] { closeBox(); }); - } - _comment->setFocusFast(); - } else if (_comment) { - _comment.destroy(); - updateMaxHeight(); - } -} - -void RateCallBox::setInnerFocus() { - if (_comment) { - _comment->setFocusFast(); - } else { - setFocus(); - } -} - -void RateCallBox::commentResized() { - updateMaxHeight(); - update(); -} - -void RateCallBox::send() { - Expects(_rating > 0 && _rating <= kMaxRating); - - if (_requestId) { - return; - } - auto comment = _comment ? _comment->getLastText().trimmed() : QString(); - _requestId = _api.request(MTPphone_SetCallRating( - MTP_flags(0), - MTP_inputPhoneCall(MTP_long(_callId), MTP_long(_callAccessHash)), - MTP_int(_rating), - MTP_string(comment) - )).done([=](const MTPUpdates &updates) { - _session->api().applyUpdates(updates); - closeBox(); - }).fail([=](const MTP::Error &error) { closeBox(); }).send(); -} - -void RateCallBox::updateMaxHeight() { - auto newHeight = st::callRatingPadding.top() + st::callRatingStarTop + _stars.back()->heightNoMargins() + st::callRatingPadding.bottom(); - if (_comment) { - newHeight += st::callRatingCommentTop + _comment->height(); - } - setDimensions(st::boxWideWidth, newHeight); -} diff --git a/Telegram/SourceFiles/calls/calls_call.cpp b/Telegram/SourceFiles/calls/calls_call.cpp index 3ba339a74..1428bade8 100644 --- a/Telegram/SourceFiles/calls/calls_call.cpp +++ b/Telegram/SourceFiles/calls/calls_call.cpp @@ -13,7 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "apiwrap.h" #include "lang/lang_keys.h" #include "ui/boxes/confirm_box.h" -#include "boxes/rate_call_box.h" +#include "ui/boxes/rate_call_box.h" #include "calls/calls_instance.h" #include "base/openssl_help.h" #include "base/random.h" @@ -571,7 +571,31 @@ bool Call::handleUpdate(const MTPPhoneCall &call) { } } if (data.is_need_rating() && _id && _accessHash) { - Ui::show(Box(&_user->session(), _id, _accessHash)); + const auto session = &_user->session(); + const auto callId = _id; + const auto callAccessHash = _accessHash; + const auto box = Ui::show(Box( + Core::App().settings().sendSubmitWay())); + const auto sender = box->lifetime().make_state( + &session->mtp()); + box->sends( + ) | rpl::take( + 1 // Instead of keeping requestId. + ) | rpl::start_with_next([=](const Ui::RateCallBox::Result &r) { + sender->request(MTPphone_SetCallRating( + MTP_flags(0), + MTP_inputPhoneCall( + MTP_long(callId), + MTP_long(callAccessHash)), + MTP_int(r.rating), + MTP_string(r.comment) + )).done([=](const MTPUpdates &updates) { + session->api().applyUpdates(updates); + box->closeBox(); + }).fail([=] { + box->closeBox(); + }).send(); + }, box->lifetime()); } const auto reason = data.vreason(); if (reason && reason->type() == mtpc_phoneCallDiscardReasonDisconnect) { diff --git a/Telegram/SourceFiles/calls/calls_instance.cpp b/Telegram/SourceFiles/calls/calls_instance.cpp index f171e2c57..a72d4b732 100644 --- a/Telegram/SourceFiles/calls/calls_instance.cpp +++ b/Telegram/SourceFiles/calls/calls_instance.cpp @@ -31,9 +31,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "platform/platform_specific.h" #include "ui/toast/toast.h" #include "base/unixtime.h" -#include "mainwidget.h" #include "mtproto/mtproto_config.h" -#include "boxes/rate_call_box.h" #include "app.h" // App::quitting #include diff --git a/Telegram/SourceFiles/ui/boxes/rate_call_box.cpp b/Telegram/SourceFiles/ui/boxes/rate_call_box.cpp new file mode 100644 index 000000000..f82994ff4 --- /dev/null +++ b/Telegram/SourceFiles/ui/boxes/rate_call_box.cpp @@ -0,0 +1,144 @@ +/* +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/rate_call_box.h" + +#include "lang/lang_keys.h" +#include "ui/widgets/buttons.h" +#include "ui/widgets/input_fields.h" +#include "styles/style_layers.h" +#include "styles/style_calls.h" + +namespace Ui { + +namespace { + +constexpr auto kMaxRating = 5; +constexpr auto kRateCallCommentLengthMax = 200; + +} // namespace + +RateCallBox::RateCallBox(QWidget*, InputSubmitSettings sendWay) +: _sendWay(sendWay) { +} + +void RateCallBox::prepare() { + setTitle(tr::lng_call_rate_label()); + addButton(tr::lng_cancel(), [=] { closeBox(); }); + + for (auto i = 0; i < kMaxRating; ++i) { + _stars.emplace_back(this, st::callRatingStar); + _stars.back()->setClickedCallback([this, value = i + 1] { + ratingChanged(value); + }); + _stars.back()->show(); + } + + updateMaxHeight(); +} + +void RateCallBox::resizeEvent(QResizeEvent *e) { + BoxContent::resizeEvent(e); + + const auto starsWidth = (_stars.size() * st::callRatingStar.width); + auto starLeft = (width() - starsWidth) / 2; + const auto starTop = st::callRatingStarTop; + for (auto &star : _stars) { + star->moveToLeft(starLeft, starTop); + starLeft += star->width(); + } + if (_comment) { + _comment->moveToLeft( + st::callRatingPadding.left(), + _stars.back()->bottomNoMargins() + st::callRatingCommentTop); + } +} + +void RateCallBox::ratingChanged(int value) { + Expects(value > 0 && value <= kMaxRating); + if (!_rating) { + clearButtons(); + addButton(tr::lng_send_button(), [=] { send(); }); + addButton(tr::lng_cancel(), [=] { closeBox(); }); + } + _rating = value; + + for (auto i = 0; i < kMaxRating; ++i) { + _stars[i]->setIconOverride((i < value) + ? &st::callRatingStarFilled + : nullptr); + _stars[i]->setRippleColorOverride((i < value) + ? &st::lightButtonBgOver + : nullptr); + } + if (value < kMaxRating) { + if (!_comment) { + _comment.create( + this, + st::callRatingComment, + Ui::InputField::Mode::MultiLine, + tr::lng_call_rate_comment()); + _comment->show(); + _comment->setSubmitSettings(_sendWay); + _comment->setMaxLength(kRateCallCommentLengthMax); + _comment->resize( + width() + - st::callRatingPadding.left() + - st::callRatingPadding.right(), + _comment->height()); + + updateMaxHeight(); + connect(_comment, &InputField::resized, [=] { + commentResized(); + }); + connect(_comment, &InputField::submitted, [=] { send(); }); + connect(_comment, &InputField::cancelled, [=] { closeBox(); }); + } + _comment->setFocusFast(); + } else if (_comment) { + _comment.destroy(); + updateMaxHeight(); + } +} + +void RateCallBox::setInnerFocus() { + if (_comment) { + _comment->setFocusFast(); + } else { + setFocus(); + } +} + +void RateCallBox::commentResized() { + updateMaxHeight(); + update(); +} + +void RateCallBox::send() { + Expects(_rating > 0 && _rating <= kMaxRating); + _sends.fire({ + .rating = _rating, + .comment = _comment ? _comment->getLastText().trimmed() : QString(), + }); +} + +void RateCallBox::updateMaxHeight() { + auto newHeight = st::callRatingPadding.top() + + st::callRatingStarTop + + _stars.back()->heightNoMargins() + + st::callRatingPadding.bottom(); + if (_comment) { + newHeight += st::callRatingCommentTop + _comment->height(); + } + setDimensions(st::boxWideWidth, newHeight); +} + +rpl::producer RateCallBox::sends() const { + return _sends.events(); +} + +} // namespace Ui diff --git a/Telegram/SourceFiles/boxes/rate_call_box.h b/Telegram/SourceFiles/ui/boxes/rate_call_box.h similarity index 68% rename from Telegram/SourceFiles/boxes/rate_call_box.h rename to Telegram/SourceFiles/ui/boxes/rate_call_box.h index 99ca677d8..952465447 100644 --- a/Telegram/SourceFiles/boxes/rate_call_box.h +++ b/Telegram/SourceFiles/ui/boxes/rate_call_box.h @@ -8,25 +8,23 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "boxes/abstract_box.h" -#include "mtproto/sender.h" namespace Ui { -class InputField; -class FlatLabel; -class IconButton; -} // namespace Ui -namespace Main { -class Session; -} // namespace Main +class InputField; +class IconButton; +enum class InputSubmitSettings; class RateCallBox : public Ui::BoxContent { public: - RateCallBox( - QWidget*, - not_null session, - uint64 callId, - uint64 callAccessHash); + RateCallBox(QWidget*, InputSubmitSettings sendWay); + + struct Result { + int rating = 0; + QString comment; + }; + + [[nodiscard]] rpl::producer sends() const; protected: void prepare() override; @@ -40,16 +38,14 @@ private: void send(); void commentResized(); - const not_null _session; - MTP::Sender _api; - - uint64 _callId = 0; - uint64 _callAccessHash = 0; + const InputSubmitSettings _sendWay; int _rating = 0; std::vector> _stars; object_ptr _comment = { nullptr }; - mtpRequestId _requestId = 0; + rpl::event_stream _sends; }; + +} // namespace Ui diff --git a/Telegram/cmake/td_ui.cmake b/Telegram/cmake/td_ui.cmake index 1f9eaf700..4b141a6d8 100644 --- a/Telegram/cmake/td_ui.cmake +++ b/Telegram/cmake/td_ui.cmake @@ -116,6 +116,8 @@ PRIVATE ui/boxes/country_select_box.h ui/boxes/edit_invite_link.cpp ui/boxes/edit_invite_link.h + ui/boxes/rate_call_box.cpp + ui/boxes/rate_call_box.h ui/boxes/report_box.cpp ui/boxes/report_box.h ui/boxes/single_choice_box.cpp