From acbc248f23facfe484e45adf86b31dafccef62f2 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Tue, 11 Oct 2022 21:10:15 +0300 Subject: [PATCH] Replaced UsernameBox with generic box. --- Telegram/SourceFiles/boxes/username_box.cpp | 226 ++++++++---------- Telegram/SourceFiles/boxes/username_box.h | 54 +---- .../settings/settings_information.cpp | 2 +- .../SourceFiles/settings/settings_main.cpp | 2 +- 4 files changed, 110 insertions(+), 174 deletions(-) diff --git a/Telegram/SourceFiles/boxes/username_box.cpp b/Telegram/SourceFiles/boxes/username_box.cpp index 82042702e..077075682 100644 --- a/Telegram/SourceFiles/boxes/username_box.cpp +++ b/Telegram/SourceFiles/boxes/username_box.cpp @@ -7,28 +7,73 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/username_box.h" +#include "base/timer.h" #include "boxes/peers/edit_peer_common.h" -#include "lang/lang_keys.h" -#include "ui/widgets/buttons.h" -#include "ui/widgets/labels.h" -#include "ui/widgets/fields/special_fields.h" -#include "ui/toast/toast.h" -#include "ui/text/text_utilities.h" -#include "ui/painter.h" -#include "main/main_session.h" #include "data/data_session.h" #include "data/data_user.h" +#include "lang/lang_keys.h" +#include "main/main_session.h" +#include "mtproto/sender.h" +#include "settings/settings_common.h" +#include "ui/layers/generic_box.h" +#include "ui/painter.h" +#include "ui/text/text_utilities.h" +#include "ui/toast/toast.h" +#include "ui/widgets/buttons.h" +#include "ui/widgets/fields/special_fields.h" +#include "ui/widgets/labels.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" +#include "styles/style_settings.h" -#include -#include +namespace { -UsernameBox::UsernameBox(QWidget*, not_null session) +class UsernameEditor final : public Ui::RpWidget { +public: + UsernameEditor(not_null, not_null session); + + void setInnerFocus(); + void save(); + + [[nodiscard]] rpl::producer<> closeRequests() const; + +protected: + void paintEvent(QPaintEvent *e) override; + void resizeEvent(QResizeEvent *e) override; + +private: + void updateFail(const QString &error); + void checkFail(const QString &error); + + + void check(); + void changed(); + + QString getName() const; + + const not_null _session; + const style::font &_font; + const style::margins &_padding; + MTP::Sender _api; + + object_ptr _username; + + mtpRequestId _saveRequestId = 0; + mtpRequestId _checkRequestId = 0; + QString _sentUsername, _checkUsername, _errorText, _goodText; + + base::Timer _checkTimer; + + rpl::event_stream<> _closeRequests; + +}; + +UsernameEditor::UsernameEditor( + not_null, + not_null session) : _session(session) , _font(st::normalFont) , _padding(st::usernamePadding) -, _textCenterTop((_font->height - _font->height) / 2) , _api(&_session->mtp()) , _username( this, @@ -36,52 +81,30 @@ UsernameBox::UsernameBox(QWidget*, not_null session) rpl::single(qsl("@username")), session->user()->username(), QString()) -, _about( - this, - tr::lng_username_description(Ui::Text::RichLangValue), - st::defaultBoxLabel) -, _link(this, QString(), st::defaultLinkButton) , _checkTimer([=] { check(); }) { -} - -void UsernameBox::prepare() { _goodText = _session->user()->username().isEmpty() ? QString() : tr::lng_username_available(tr::now); - setTitle(tr::lng_username_title()); - - addButton(tr::lng_settings_save(), [=] { save(); }); - addButton(tr::lng_cancel(), [=] { closeBox(); }); - connect(_username, &Ui::MaskedInputField::changed, [=] { changed(); }); connect(_username, &Ui::MaskedInputField::submitted, [=] { save(); }); - _link->addClickHandler([=] { linkClick(); }); - _about->resizeToWidth( - st::boxWideWidth - _padding.left() - _padding.right()); - _about->heightValue( - ) | rpl::start_with_next([=](int height) { - setDimensions( - st::boxWideWidth, - (_padding.top() - + _username->height() - + st::usernameSkip - + height - + 3 * _font->height - + _padding.bottom())); - }, lifetime()); - - updateLinkText(); + resize( + width(), + (_padding.top() + + _username->height() + + st::usernameSkip)); } -void UsernameBox::setInnerFocus() { +rpl::producer<> UsernameEditor::closeRequests() const { + return _closeRequests.events(); +} + +void UsernameEditor::setInnerFocus() { _username->setFocusFast(); } -void UsernameBox::paintEvent(QPaintEvent *e) { - BoxContent::paintEvent(e); - +void UsernameEditor::paintEvent(QPaintEvent *e) { Painter p(this); const auto textTop = _username->y() @@ -112,59 +135,16 @@ void UsernameBox::paintEvent(QPaintEvent *e) { tr::lng_username_choose(tr::now)); } p.setPen(st::boxTextFg); - - const auto linkTop = _username->y() - + _username->height() - + st::usernameSkip - + _about->height() - + _font->height - + _textCenterTop; - if (_link->isHidden()) { - p.drawTextLeft( - _padding.left(), - linkTop, - width(), - tr::lng_username_link_willbe(tr::now)); - p.setPen(st::usernameDefaultFg); - const auto link = _session->createInternalLinkFull(qsl("username")); - p.drawTextLeft( - _padding.left(), - linkTop - + _font->height - + _textCenterTop, - width(), - link); - } else { - p.drawTextLeft( - _padding.left(), - linkTop, - width(), - tr::lng_username_link(tr::now)); - } } -void UsernameBox::resizeEvent(QResizeEvent *e) { - BoxContent::resizeEvent(e); - +void UsernameEditor::resizeEvent(QResizeEvent *e) { _username->resize( width() - _padding.left() - _padding.right(), _username->height()); _username->moveToLeft(_padding.left(), _padding.top()); - - _about->moveToLeft( - _padding.left(), - _username->y() + _username->height() + st::usernameSkip); - - const auto linkTop = _about->y() - + _about->height() - + _font->height - + _textCenterTop; - _link->moveToLeft( - _padding.left(), - linkTop + _font->height + _textCenterTop); } -void UsernameBox::save() { +void UsernameEditor::save() { if (_saveRequestId) { return; } @@ -175,14 +155,14 @@ void UsernameBox::save() { )).done([=](const MTPUser &result) { _saveRequestId = 0; _session->data().processUser(result); - closeBox(); + _closeRequests.fire({}); }).fail([=](const MTP::Error &error) { _saveRequestId = 0; updateFail(error.type()); }).send(); } -void UsernameBox::check() { +void UsernameEditor::check() { _api.request(base::take(_checkRequestId)).cancel(); const auto name = getName(); @@ -210,8 +190,7 @@ void UsernameBox::check() { }).send(); } -void UsernameBox::changed() { - updateLinkText(); +void UsernameEditor::changed() { const auto name = getName(); if (name.isEmpty()) { if (!_errorText.isEmpty() || !_goodText.isEmpty()) { @@ -252,15 +231,7 @@ void UsernameBox::changed() { } } -void UsernameBox::linkClick() { - QGuiApplication::clipboard()->setText( - _session->createInternalLinkFull(getName())); - Ui::Toast::Show( - Ui::BoxShow(this).toastParent(), - tr::lng_username_copied(tr::now)); -} - -void UsernameBox::updateFail(const QString &error) { +void UsernameEditor::updateFail(const QString &error) { const auto self = _session->user(); if ((error == qstr("USERNAME_NOT_MODIFIED")) || (_sentUsername == self->username())) { @@ -269,7 +240,7 @@ void UsernameBox::updateFail(const QString &error) { TextUtilities::SingleLine(self->lastName), TextUtilities::SingleLine(self->nameOrPhone), TextUtilities::SingleLine(_sentUsername)); - closeBox(); + _closeRequests.fire({}); } else if (error == qstr("USERNAME_INVALID")) { _username->setFocus(); _username->showError(); @@ -286,7 +257,7 @@ void UsernameBox::updateFail(const QString &error) { } } -void UsernameBox::checkFail(const QString &error) { +void UsernameEditor::checkFail(const QString &error) { if (error == qstr("USERNAME_INVALID")) { _errorText = tr::lng_username_invalid(tr::now); update(); @@ -300,24 +271,35 @@ void UsernameBox::checkFail(const QString &error) { } } -QString UsernameBox::getName() const { +QString UsernameEditor::getName() const { return _username->text().replace('@', QString()).trimmed(); } -void UsernameBox::updateLinkText() { - const auto uname = getName(); - _link->setText(_font->elided( - _session->createInternalLinkFull(uname), - st::boxWideWidth - _padding.left() - _padding.right())); - if (uname.isEmpty()) { - if (!_link->isHidden()) { - _link->hide(); - update(); - } - } else { - if (_link->isHidden()) { - _link->show(); - update(); - } - } +} // namespace + +void UsernamesBox( + not_null box, + not_null session) { + box->setTitle(tr::lng_username_title()); + + const auto container = box->verticalLayout(); + + const auto editor = box->addRow( + object_ptr(box, session), + {}); + editor->closeRequests( + ) | rpl::start_with_next([=] { + box->closeBox(); + }, editor->lifetime()); + + container->add(object_ptr( + container, + object_ptr( + container, + tr::lng_username_description(Ui::Text::RichLangValue), + st::boxDividerLabel), + st::settingsDividerLabelPadding)); + + box->addButton(tr::lng_settings_save(), [=] { editor->save(); }); + box->addButton(tr::lng_cancel(), [=] { box->closeBox(); }); } diff --git a/Telegram/SourceFiles/boxes/username_box.h b/Telegram/SourceFiles/boxes/username_box.h index 922c12c02..62ebc8a08 100644 --- a/Telegram/SourceFiles/boxes/username_box.h +++ b/Telegram/SourceFiles/boxes/username_box.h @@ -7,60 +7,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once -#include "boxes/abstract_box.h" -#include "base/timer.h" -#include "mtproto/sender.h" - namespace Ui { -class UsernameInput; -class LinkButton; -class FlatLabel; +class GenericBox; } // namespace Ui namespace Main { class Session; } // namespace Main -class UsernameBox : public Ui::BoxContent { -public: - UsernameBox(QWidget*, not_null session); - -protected: - void prepare() override; - void setInnerFocus() override; - - void paintEvent(QPaintEvent *e) override; - void resizeEvent(QResizeEvent *e) override; - -private: - void updateFail(const QString &error); - - void checkFail(const QString &error); - - void save(); - - void check(); - void changed(); - - void linkClick(); - - QString getName() const; - void updateLinkText(); - - const not_null _session; - const style::font &_font; - const style::margins &_padding; - const int _textCenterTop; - MTP::Sender _api; - - object_ptr _username; - object_ptr _about; - object_ptr _link; - - mtpRequestId _saveRequestId = 0; - mtpRequestId _checkRequestId = 0; - QString _sentUsername, _checkUsername, _errorText, _goodText; - - base::Timer _checkTimer; - -}; +void UsernamesBox( + not_null box, + not_null session); diff --git a/Telegram/SourceFiles/settings/settings_information.cpp b/Telegram/SourceFiles/settings/settings_information.cpp index e1d9d77f5..e4e6cfa31 100644 --- a/Telegram/SourceFiles/settings/settings_information.cpp +++ b/Telegram/SourceFiles/settings/settings_information.cpp @@ -450,7 +450,7 @@ void SetupRows( std::move(label), std::move(value), tr::lng_context_copy_mention(tr::now), - [=] { controller->show(Box(session)); }, + [=] { controller->show(Box(UsernamesBox, session)); }, { &st::settingsIconMention, kIconLightOrange }); AddSkip(container); diff --git a/Telegram/SourceFiles/settings/settings_main.cpp b/Telegram/SourceFiles/settings/settings_main.cpp index d893c12fe..5168f3ad8 100644 --- a/Telegram/SourceFiles/settings/settings_main.cpp +++ b/Telegram/SourceFiles/settings/settings_main.cpp @@ -200,7 +200,7 @@ void Cover::initViewers() { _username->overrideLinkClickHandler([=] { const auto username = _user->userName(); if (username.isEmpty()) { - _controller->show(Box(&_user->session())); + _controller->show(Box(UsernamesBox, &_user->session())); } else { QGuiApplication::clipboard()->setText( _user->session().createInternalLinkFull(username));