From 2d37920a4cb57378a0639a34d9ba52f2743ed971 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 18 Oct 2021 03:46:24 +0300 Subject: [PATCH] Improved code style in some peer boxes. --- .../SourceFiles/boxes/add_contact_box.cpp | 511 ++++++++++++------ Telegram/SourceFiles/boxes/add_contact_box.h | 22 +- 2 files changed, 361 insertions(+), 172 deletions(-) diff --git a/Telegram/SourceFiles/boxes/add_contact_box.cpp b/Telegram/SourceFiles/boxes/add_contact_box.cpp index fe4f99ca8..ed2c37db4 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.cpp +++ b/Telegram/SourceFiles/boxes/add_contact_box.cpp @@ -8,15 +8,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/add_contact_box.h" #include "lang/lang_keys.h" -#include "mtproto/sender.h" -#include "base/flat_set.h" #include "base/random.h" #include "boxes/confirm_box.h" #include "boxes/peer_list_controllers.h" #include "boxes/peers/add_participants_box.h" #include "boxes/peers/edit_participant_box.h" #include "boxes/peers/edit_participants_box.h" -#include "core/file_utilities.h" #include "core/application.h" #include "chat_helpers/emoji_suggestions_widget.h" #include "countries/countries_instance.h" // Countries::ExtractPhoneCode. @@ -36,8 +33,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_changes.h" #include "data/data_cloud_file.h" -#include "mainwidget.h" -#include "mainwindow.h" #include "apiwrap.h" #include "api/api_invite_links.h" #include "main/main_session.h" @@ -45,6 +40,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "styles/style_layers.h" #include "styles/style_boxes.h" #include "styles/style_dialogs.h" +#include "styles/style_widgets.h" #include #include @@ -73,7 +69,7 @@ void ChatCreateDone( const MTPUpdates &updates) { navigation->session().api().applyUpdates(updates); - auto success = base::make_optional(&updates) + const auto success = base::make_optional(&updates) | [](auto updates) -> std::optional*> { switch (updates->type()) { case mtpc_updates: @@ -280,7 +276,8 @@ void AddContactBox::prepare() { setTabOrder(_last, _first); } const auto readyToAdd = !_phone->getLastText().isEmpty() - && (!_first->getLastText().isEmpty() || !_last->getLastText().isEmpty()); + && (!_first->getLastText().isEmpty() + || !_last->getLastText().isEmpty()); setTitle(readyToAdd ? tr::lng_confirm_contact_data() : tr::lng_enter_contact_data()); @@ -290,11 +287,21 @@ void AddContactBox::prepare() { connect(_last, &Ui::InputField::submitted, [=] { submit(); }); connect(_phone, &Ui::PhoneInput::submitted, [=] { submit(); }); - setDimensions(st::boxWideWidth, st::contactPadding.top() + _first->height() + st::contactSkip + _last->height() + st::contactPhoneSkip + _phone->height() + st::contactPadding.bottom() + st::boxPadding.bottom()); + setDimensions( + st::boxWideWidth, + st::contactPadding.top() + + _first->height() + + st::contactSkip + + _last->height() + + st::contactPhoneSkip + + _phone->height() + + st::contactPadding.bottom() + + st::boxPadding.bottom()); } void AddContactBox::setInnerFocus() { - if ((_first->getLastText().isEmpty() && _last->getLastText().isEmpty()) || !_phone->isEnabled()) { + if ((_first->getLastText().isEmpty() && _last->getLastText().isEmpty()) + || !_phone->isEnabled()) { (_invertOrder ? _last : _first)->setFocusFast(); _phone->finishAnimating(); } else { @@ -309,8 +316,18 @@ void AddContactBox::paintEvent(QPaintEvent *e) { if (_retrying) { p.setPen(st::boxTextFg); p.setFont(st::boxTextFont); - auto textHeight = height() - st::contactPadding.top() - st::contactPadding.bottom() - st::boxPadding.bottom(); - p.drawText(QRect(st::boxPadding.left(), st::contactPadding.top(), width() - st::boxPadding.left() - st::boxPadding.right(), textHeight), tr::lng_contact_not_joined(tr::now, lt_name, _sentName), style::al_topleft); + const auto textHeight = height() + - st::contactPadding.top() + - st::contactPadding.bottom() + - st::boxPadding.bottom(); + p.drawText( + QRect( + st::boxPadding.left(), + st::contactPadding.top(), + width() - st::boxPadding.left() - st::boxPadding.right(), + textHeight), + tr::lng_contact_not_joined(tr::now, lt_name, _sentName), + style::al_topleft); } else { st::contactUserIcon.paint( p, @@ -328,18 +345,25 @@ void AddContactBox::paintEvent(QPaintEvent *e) { void AddContactBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); - _first->resize(width() - st::boxPadding.left() - st::contactPadding.left() - st::boxPadding.right(), _first->height()); + _first->resize( + width() + - st::boxPadding.left() + - st::contactPadding.left() + - st::boxPadding.right(), + _first->height()); _last->resize(_first->width(), _last->height()); _phone->resize(_first->width(), _last->height()); - if (_invertOrder) { - _last->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), st::contactPadding.top()); - _first->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _last->y() + _last->height() + st::contactSkip); - _phone->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _first->y() + _first->height() + st::contactPhoneSkip); - } else { - _first->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), st::contactPadding.top()); - _last->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _first->y() + _first->height() + st::contactSkip); - _phone->moveToLeft(st::boxPadding.left() + st::contactPadding.left(), _last->y() + _last->height() + st::contactPhoneSkip); - } + const auto left = st::boxPadding.left() + st::contactPadding.left(); + const auto &firstRow = _invertOrder ? _last : _first; + const auto &secondRow = _invertOrder ? _first : _last; + const auto &thirdRow = _phone; + firstRow->moveToLeft(left, st::contactPadding.top()); + secondRow->moveToLeft( + left, + firstRow->y() + firstRow->height() + st::contactSkip); + thirdRow->moveToLeft( + left, + secondRow->y() + secondRow->height() + st::contactPhoneSkip); } void AddContactBox::submit() { @@ -361,9 +385,11 @@ void AddContactBox::save() { return; } - auto firstName = TextUtilities::PrepareForSending(_first->getLastText()); - auto lastName = TextUtilities::PrepareForSending(_last->getLastText()); - auto phone = _phone->getLastText().trimmed(); + auto firstName = TextUtilities::PrepareForSending( + _first->getLastText()); + auto lastName = TextUtilities::PrepareForSending( + _last->getLastText()); + const auto phone = _phone->getLastText().trimmed(); if (firstName.isEmpty() && lastName.isEmpty()) { if (_invertOrder) { _last->setFocus(); @@ -501,9 +527,15 @@ void GroupInfoBox::prepare() { _description->setSubmitSettings( Core::App().settings().sendSubmitWay()); - connect(_description, &Ui::InputField::resized, [=] { descriptionResized(); }); - connect(_description, &Ui::InputField::submitted, [=] { submit(); }); - connect(_description, &Ui::InputField::cancelled, [=] { closeBox(); }); + connect(_description, &Ui::InputField::resized, [=] { + descriptionResized(); + }); + connect(_description, &Ui::InputField::submitted, [=] { + submit(); + }); + connect(_description, &Ui::InputField::cancelled, [=] { + closeBox(); + }); Ui::Emoji::SuggestionsController::Init( getDelegate()->outerContainer(), @@ -530,17 +562,34 @@ void GroupInfoBox::setInnerFocus() { void GroupInfoBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); - _photo->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::boxPadding.top() + st::newGroupInfoPadding.top()); + _photo->moveToLeft( + st::boxPadding.left() + st::newGroupInfoPadding.left(), + st::boxPadding.top() + st::newGroupInfoPadding.top()); - auto nameLeft = st::defaultUserpicButton.size.width() + const auto nameLeft = st::defaultUserpicButton.size.width() + st::newGroupNamePosition.x(); - _title->resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right() - nameLeft, _title->height()); - _title->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left() + nameLeft, st::boxPadding.top() + st::newGroupInfoPadding.top() + st::newGroupNamePosition.y()); + _title->resize( + width() + - st::boxPadding.left() + - st::newGroupInfoPadding.left() + - st::boxPadding.right() + - nameLeft, + _title->height()); + _title->moveToLeft( + st::boxPadding.left() + st::newGroupInfoPadding.left() + nameLeft, + st::boxPadding.top() + + st::newGroupInfoPadding.top() + + st::newGroupNamePosition.y()); if (_description) { - _description->resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _description->height()); - auto descriptionLeft = st::boxPadding.left() + _description->resize( + width() + - st::boxPadding.left() + - st::newGroupInfoPadding.left() + - st::boxPadding.right(), + _description->height()); + const auto descriptionLeft = st::boxPadding.left() + st::newGroupInfoPadding.left(); - auto descriptionTop = st::boxPadding.top() + const auto descriptionTop = st::boxPadding.top() + st::newGroupInfoPadding.top() + st::defaultUserpicButton.size.height() + st::newGroupDescriptionPadding.top(); @@ -654,7 +703,9 @@ void GroupInfoBox::submit() { } } -void GroupInfoBox::createChannel(const QString &title, const QString &description) { +void GroupInfoBox::createChannel( + const QString &title, + const QString &description) { Expects(!_creationRequestId); const auto flags = (_type == Type::Megagroup) @@ -677,11 +728,13 @@ void GroupInfoBox::createChannel(const QString &title, const QString &descriptio case mtpc_updatesCombined: return &updates->c_updatesCombined().vchats().v; } - LOG(("API Error: unexpected update cons %1 (GroupInfoBox::createChannel)").arg(updates->type())); + LOG(("API Error: unexpected update cons %1 " + "(GroupInfoBox::createChannel)").arg(updates->type())); return std::nullopt; } | [](auto chats) { - return (!chats->empty() && chats->front().type() == mtpc_channel) + return (!chats->empty() + && chats->front().type() == mtpc_channel) ? base::make_optional(chats) : std::nullopt; } @@ -700,7 +753,8 @@ void GroupInfoBox::createChannel(const QString &title, const QString &descriptio checkInviteLink(); }; if (!success) { - LOG(("API Error: channel not found in updates (GroupInfoBox::creationDone)")); + LOG(("API Error: channel not found in updates " + "(GroupInfoBox::creationDone)")); closeBox(); } }).fail([this](const MTP::Error &error) { @@ -813,7 +867,8 @@ SetupChannelBox::SetupChannelBox( st::setupChannelLink, nullptr, channel->username, - channel->session().createInternalLink(QString())) { + channel->session().createInternalLink(QString())) +, _checkTimer([=] { check(); }) { } void SetupChannelBox::prepare() { @@ -825,7 +880,8 @@ void SetupChannelBox::prepare() { _channel->inputChannel, MTP_string("preston") )).fail([=](const MTP::Error &error) { - firstCheckFail(error); + _checkRequestId = 0; + firstCheckFail(error.type()); }).send(); addButton(tr::lng_settings_save(), [=] { save(); }); @@ -836,10 +892,9 @@ void SetupChannelBox::prepare() { connect(_link, &Ui::MaskedInputField::changed, [=] { handleChange(); }); _link->setVisible(_privacyGroup->value() == Privacy::Public); - _checkTimer.setSingleShot(true); - connect(&_checkTimer, &QTimer::timeout, [=] { check(); }); - - _privacyGroup->setChangedCallback([this](Privacy value) { privacyChanged(value); }); + _privacyGroup->setChangedCallback([=](Privacy value) { + privacyChanged(value); + }); _channel->session().changes().peerUpdates( _channel, @@ -866,9 +921,20 @@ void SetupChannelBox::setInnerFocus() { } void SetupChannelBox::updateMaxHeight() { - auto newHeight = st::boxPadding.top() + st::newGroupPadding.top() + _public->heightNoMargins() + _aboutPublicHeight + st::newGroupSkip + _private->heightNoMargins() + _aboutPrivate.countHeight(_aboutPublicWidth) + st::newGroupSkip + st::newGroupPadding.bottom(); - if (!_channel->isMegagroup() || _privacyGroup->value() == Privacy::Public) { - newHeight += st::newGroupLinkPadding.top() + _link->height() + st::newGroupLinkPadding.bottom(); + auto newHeight = st::boxPadding.top() + + st::newGroupPadding.top() + + _public->heightNoMargins() + + _aboutPublicHeight + + st::newGroupSkip + + _private->heightNoMargins() + + _aboutPrivate.countHeight(_aboutPublicWidth) + + st::newGroupSkip + + st::newGroupPadding.bottom(); + if (!_channel->isMegagroup() + || _privacyGroup->value() == Privacy::Public) { + newHeight += st::newGroupLinkPadding.top() + + _link->height() + + st::newGroupLinkPadding.bottom(); } setDimensions(st::boxWideWidth, newHeight); } @@ -894,17 +960,43 @@ void SetupChannelBox::paintEvent(QPaintEvent *e) { p.fillRect(e->rect(), st::boxBg); p.setPen(st::newGroupAboutFg); - QRect aboutPublic(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadio.diameter + st::defaultBoxCheckbox.textPosition.x(), _public->bottomNoMargins(), _aboutPublicWidth, _aboutPublicHeight); - _aboutPublic.drawLeft(p, aboutPublic.x(), aboutPublic.y(), aboutPublic.width(), width()); + const auto aboutPublic = QRect( + st::boxPadding.left() + + st::newGroupPadding.left() + + st::defaultRadio.diameter + + st::defaultBoxCheckbox.textPosition.x(), + _public->bottomNoMargins(), + _aboutPublicWidth, + _aboutPublicHeight); + _aboutPublic.drawLeft( + p, + aboutPublic.x(), + aboutPublic.y(), + aboutPublic.width(), + width()); - QRect aboutPrivate(st::boxPadding.left() + st::newGroupPadding.left() + st::defaultRadio.diameter + st::defaultBoxCheckbox.textPosition.x(), _private->bottomNoMargins(), _aboutPublicWidth, _aboutPublicHeight); - _aboutPrivate.drawLeft(p, aboutPrivate.x(), aboutPrivate.y(), aboutPrivate.width(), width()); + const auto aboutPrivate = QRect( + st::boxPadding.left() + + st::newGroupPadding.left() + + st::defaultRadio.diameter + + st::defaultBoxCheckbox.textPosition.x(), + _private->bottomNoMargins(), + _aboutPublicWidth, + _aboutPublicHeight); + _aboutPrivate.drawLeft( + p, + aboutPrivate.x(), + aboutPrivate.y(), + aboutPrivate.width(), + width()); if (!_channel->isMegagroup() || !_link->isHidden()) { p.setPen(st::boxTextFg); p.setFont(st::newGroupLinkFont); p.drawTextLeft( - st::boxPadding.left() + st::newGroupPadding.left() + st::defaultInputField.textMargins.left(), + st::boxPadding.left() + + st::newGroupPadding.left() + + st::defaultInputField.textMargins.left(), _link->y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop, width(), (_link->isHidden() @@ -916,20 +1008,29 @@ void SetupChannelBox::paintEvent(QPaintEvent *e) { if (!_channel->isMegagroup()) { QTextOption option(style::al_left); option.setWrapMode(QTextOption::WrapAnywhere); - p.setFont(_linkOver ? st::boxTextFont->underline() : st::boxTextFont); + p.setFont(_linkOver + ? st::boxTextFont->underline() + : st::boxTextFont); p.setPen(st::defaultLinkButton.color); - auto inviteLinkText = _channel->inviteLink().isEmpty() ? tr::lng_group_invite_create(tr::now) : _channel->inviteLink(); + const auto inviteLinkText = _channel->inviteLink().isEmpty() + ? tr::lng_group_invite_create(tr::now) + : _channel->inviteLink(); p.drawText(_invitationLink, inviteLinkText, option); } } else { + const auto top = _link->y() + - st::newGroupLinkPadding.top() + + st::newGroupLinkTop + + st::newGroupLinkFont->ascent + - st::boxTextFont->ascent; if (!_errorText.isEmpty()) { p.setPen(st::boxTextFgError); p.setFont(st::boxTextFont); - p.drawTextRight(st::boxPadding.right(), _link->y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop + st::newGroupLinkFont->ascent - st::boxTextFont->ascent, width(), _errorText); + p.drawTextRight(st::boxPadding.right(), top, width(), _errorText); } else if (!_goodText.isEmpty()) { p.setPen(st::boxTextFgGood); p.setFont(st::boxTextFont); - p.drawTextRight(st::boxPadding.right(), _link->y() - st::newGroupLinkPadding.top() + st::newGroupLinkTop + st::newGroupLinkFont->ascent - st::boxTextFont->ascent, width(), _goodText); + p.drawTextRight(st::boxPadding.right(), top, width(), _goodText); } } } @@ -937,12 +1038,32 @@ void SetupChannelBox::paintEvent(QPaintEvent *e) { void SetupChannelBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); - _public->moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), st::boxPadding.top() + st::newGroupPadding.top()); - _private->moveToLeft(st::boxPadding.left() + st::newGroupPadding.left(), _public->bottomNoMargins() + _aboutPublicHeight + st::newGroupSkip); + const auto left = st::boxPadding.left() + st::newGroupPadding.left(); + _public->moveToLeft( + left, + st::boxPadding.top() + st::newGroupPadding.top()); + _private->moveToLeft( + left, + _public->bottomNoMargins() + _aboutPublicHeight + st::newGroupSkip); - _link->resize(width() - st::boxPadding.left() - st::newGroupLinkPadding.left() - st::boxPadding.right(), _link->height()); - _link->moveToLeft(st::boxPadding.left() + st::newGroupLinkPadding.left(), _private->bottomNoMargins() + _aboutPrivate.countHeight(_aboutPublicWidth) + st::newGroupSkip + st::newGroupPadding.bottom() + st::newGroupLinkPadding.top()); - _invitationLink = QRect(_link->x(), _link->y() + (_link->height() / 2) - st::boxTextFont->height, _link->width(), 2 * st::boxTextFont->height); + _link->resize( + width() + - st::boxPadding.left() + - st::newGroupLinkPadding.left() + - st::boxPadding.right(), + _link->height()); + _link->moveToLeft( + st::boxPadding.left() + st::newGroupLinkPadding.left(), + _private->bottomNoMargins() + + _aboutPrivate.countHeight(_aboutPublicWidth) + + st::newGroupSkip + + st::newGroupPadding.bottom() + + st::newGroupLinkPadding.top()); + _invitationLink = QRect( + _link->x(), + _link->y() + (_link->height() / 2) - st::boxTextFont->height, + _link->width(), + 2 * st::boxTextFont->height); } void SetupChannelBox::mouseMoveEvent(QMouseEvent *e) { @@ -982,9 +1103,13 @@ void SetupChannelBox::save() { _channel->inputChannel, MTP_string(_sentUsername) )).done([=](const MTPBool &result) { - updateDone(result); + _channel->setName( + TextUtilities::SingleLine(_channel->name), + _sentUsername); + closeBox(); }).fail([=](const MTP::Error &error) { - updateFail(error); + _saveRequestId = 0; + updateFail(error.type()); }).send(); }; if (_saveRequestId) { @@ -1007,38 +1132,45 @@ void SetupChannelBox::save() { } void SetupChannelBox::handleChange() { - QString name = _link->text().trimmed(); + const auto name = _link->text().trimmed(); if (name.isEmpty()) { if (!_errorText.isEmpty() || !_goodText.isEmpty()) { _errorText = _goodText = QString(); update(); } - _checkTimer.stop(); + _checkTimer.cancel(); } else { - int32 len = name.size(); - for (int32 i = 0; i < len; ++i) { - QChar ch = name.at(i); - if ((ch < 'A' || ch > 'Z') && (ch < 'a' || ch > 'z') && (ch < '0' || ch > '9') && ch != '_') { - if (_errorText != tr::lng_create_channel_link_bad_symbols(tr::now)) { - _errorText = tr::lng_create_channel_link_bad_symbols(tr::now); + const auto len = int(name.size()); + for (auto i = 0; i < len; ++i) { + const auto ch = name.at(i); + if ((ch < 'A' || ch > 'Z') + && (ch < 'a' || ch > 'z') + && (ch < '0' || ch > '9') + && ch != '_') { + const auto badSymbols = + tr::lng_create_channel_link_bad_symbols(tr::now); + if (_errorText != badSymbols) { + _errorText = badSymbols; update(); } - _checkTimer.stop(); + _checkTimer.cancel(); return; } } if (name.size() < kMinUsernameLength) { - if (_errorText != tr::lng_create_channel_link_too_short(tr::now)) { - _errorText = tr::lng_create_channel_link_too_short(tr::now); + const auto tooShort = + tr::lng_create_channel_link_too_short(tr::now); + if (_errorText != tooShort) { + _errorText = tooShort; update(); } - _checkTimer.stop(); + _checkTimer.cancel(); } else { if (!_errorText.isEmpty() || !_goodText.isEmpty()) { _errorText = _goodText = QString(); update(); } - _checkTimer.start(UsernameCheckTimeout); + _checkTimer.callOnce(UsernameCheckTimeout); } } } @@ -1047,16 +1179,25 @@ void SetupChannelBox::check() { if (_checkRequestId) { _channel->session().api().request(_checkRequestId).cancel(); } - QString link = _link->text().trimmed(); + const auto link = _link->text().trimmed(); if (link.size() >= kMinUsernameLength) { _checkUsername = link; _checkRequestId = _api.request(MTPchannels_CheckUsername( _channel->inputChannel, MTP_string(link) )).done([=](const MTPBool &result) { - checkDone(result); + _checkRequestId = 0; + _errorText = (mtpIsTrue(result) + || _checkUsername == _channel->username) + ? QString() + : tr::lng_create_channel_link_occupied(tr::now); + _goodText = _errorText.isEmpty() + ? tr::lng_create_channel_link_available(tr::now) + : QString(); + update(); }).fail([=](const MTP::Error &error) { - checkFail(error); + _checkRequestId = 0; + checkFail(error.type()); }).send(); } } @@ -1090,26 +1231,20 @@ void SetupChannelBox::privacyChanged(Privacy value) { update(); } -void SetupChannelBox::updateDone(const MTPBool &result) { - _channel->setName(TextUtilities::SingleLine(_channel->name), _sentUsername); - closeBox(); -} - -void SetupChannelBox::updateFail(const MTP::Error &error) { - _saveRequestId = 0; - QString err(error.type()); - if (err == "USERNAME_NOT_MODIFIED" - || _sentUsername == _channel->username) { +void SetupChannelBox::updateFail(const QString &error) { + if ((error == "USERNAME_NOT_MODIFIED") + || (_sentUsername == _channel->username)) { _channel->setName( TextUtilities::SingleLine(_channel->name), TextUtilities::SingleLine(_sentUsername)); closeBox(); - } else if (err == "USERNAME_INVALID") { + } else if (error == "USERNAME_INVALID") { _link->setFocus(); _link->showError(); _errorText = tr::lng_create_channel_link_invalid(tr::now); update(); - } else if (err == "USERNAME_OCCUPIED" || err == "USERNAMES_UNAVAILABLE") { + } else if ((error == "USERNAME_OCCUPIED") + || (error == "USERNAMES_UNAVAILABLE")) { _link->setFocus(); _link->showError(); _errorText = tr::lng_create_channel_link_occupied(tr::now); @@ -1119,33 +1254,21 @@ void SetupChannelBox::updateFail(const MTP::Error &error) { } } -void SetupChannelBox::checkDone(const MTPBool &result) { - _checkRequestId = 0; - QString newError = (mtpIsTrue(result) || _checkUsername == _channel->username) ? QString() : tr::lng_create_channel_link_occupied(tr::now); - QString newGood = newError.isEmpty() ? tr::lng_create_channel_link_available(tr::now) : QString(); - if (_errorText != newError || _goodText != newGood) { - _errorText = newError; - _goodText = newGood; - update(); - } -} - -void SetupChannelBox::checkFail(const MTP::Error &error) { - _checkRequestId = 0; - QString err(error.type()); - if (err == qstr("CHANNEL_PUBLIC_GROUP_NA")) { +void SetupChannelBox::checkFail(const QString &error) { + if (error == qstr("CHANNEL_PUBLIC_GROUP_NA")) { Ui::hideLayer(); - } else if (err == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) { + } else if (error == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) { if (_existing) { showRevokePublicLinkBoxForEdit(); } else { _tooMuchUsernames = true; _privacyGroup->setValue(Privacy::Private); } - } else if (err == qstr("USERNAME_INVALID")) { + } else if (error == qstr("USERNAME_INVALID")) { _errorText = tr::lng_create_channel_link_invalid(tr::now); update(); - } else if (err == qstr("USERNAME_OCCUPIED") && _checkUsername != _channel->username) { + } else if (error == qstr("USERNAME_OCCUPIED") + && _checkUsername != _channel->username) { _errorText = tr::lng_create_channel_link_occupied(tr::now); update(); } else { @@ -1171,12 +1294,10 @@ void SetupChannelBox::showRevokePublicLinkBoxForEdit() { Ui::LayerOption::KeepOther); } -void SetupChannelBox::firstCheckFail(const MTP::Error &error) { - _checkRequestId = 0; - const auto &type = error.type(); - if (type == qstr("CHANNEL_PUBLIC_GROUP_NA")) { +void SetupChannelBox::firstCheckFail(const QString &error) { + if (error == qstr("CHANNEL_PUBLIC_GROUP_NA")) { Ui::hideLayer(); - } else if (type == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) { + } else if (error == qstr("CHANNELS_ADMIN_PUBLIC_TOO_MUCH")) { if (_existing) { showRevokePublicLinkBoxForEdit(); } else { @@ -1192,8 +1313,16 @@ void SetupChannelBox::firstCheckFail(const MTP::Error &error) { EditNameBox::EditNameBox(QWidget*, not_null user) : _user(user) , _api(&_user->session().mtp()) -, _first(this, st::defaultInputField, tr::lng_signup_firstname(), _user->firstName) -, _last(this, st::defaultInputField, tr::lng_signup_lastname(), _user->lastName) +, _first( + this, + st::defaultInputField, + tr::lng_signup_firstname(), + _user->firstName) +, _last( + this, + st::defaultInputField, + tr::lng_signup_lastname(), + _user->lastName) , _invertOrder(langFirstNameGoesSecond()) { } @@ -1241,19 +1370,28 @@ void EditNameBox::submit() { void EditNameBox::resizeEvent(QResizeEvent *e) { BoxContent::resizeEvent(e); - _first->resize(width() - st::boxPadding.left() - st::newGroupInfoPadding.left() - st::boxPadding.right(), _first->height()); + _first->resize( + width() + - st::boxPadding.left() + - st::newGroupInfoPadding.left() + - st::boxPadding.right(), + _first->height()); _last->resize(_first->size()); + const auto left = st::boxPadding.left() + st::newGroupInfoPadding.left(); + const auto skip = st::contactSkip; if (_invertOrder) { - _last->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::contactPadding.top()); - _first->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _last->y() + _last->height() + st::contactSkip); + _last->moveToLeft(left, st::contactPadding.top()); + _first->moveToLeft(left, _last->y() + _last->height() + skip); } else { - _first->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), st::contactPadding.top()); - _last->moveToLeft(st::boxPadding.left() + st::newGroupInfoPadding.left(), _first->y() + _first->height() + st::contactSkip); + _first->moveToLeft(left, st::contactPadding.top()); + _last->moveToLeft(left, _first->y() + _first->height() + skip); } } void EditNameBox::save() { - if (_requestId) return; + if (_requestId) { + return; + } auto first = TextUtilities::PrepareForSending(_first->getLastText()); auto last = TextUtilities::PrepareForSending(_last->getLastText()); @@ -1279,29 +1417,27 @@ void EditNameBox::save() { MTP_string(first), MTP_string(last), MTPstring() - )).done([=](const MTPUser &result) { - saveSelfDone(result); + )).done([=](const MTPUser &user) { + _user->owner().processUser(user); + closeBox(); }).fail([=](const MTP::Error &error) { - saveSelfFail(error); + _requestId = 0; + saveSelfFail(error.type()); }).send(); } -void EditNameBox::saveSelfDone(const MTPUser &user) { - _user->owner().processUsers(MTP_vector(1, user)); - closeBox(); -} - -void EditNameBox::saveSelfFail(const MTP::Error &error) { - auto err = error.type(); - auto first = TextUtilities::SingleLine(_first->getLastText().trimmed()); - auto last = TextUtilities::SingleLine(_last->getLastText().trimmed()); - if (err == "NAME_NOT_MODIFIED") { - _user->setName(first, last, QString(), TextUtilities::SingleLine(_user->username)); +void EditNameBox::saveSelfFail(const QString &error) { + if (error == "NAME_NOT_MODIFIED") { + _user->setName( + TextUtilities::SingleLine(_first->getLastText().trimmed()), + TextUtilities::SingleLine(_last->getLastText().trimmed()), + QString(), + TextUtilities::SingleLine(_user->username)); closeBox(); - } else if (err == "FIRSTNAME_INVALID") { + } else if (error == "FIRSTNAME_INVALID") { _first->setFocus(); _first->showError(); - } else if (err == "LASTNAME_INVALID") { + } else if (error == "LASTNAME_INVALID") { _last->setFocus(); _last->showError(); } else { @@ -1316,8 +1452,11 @@ RevokePublicLinkBox::Inner::Inner( : TWidget(parent) , _session(session) , _api(&_session->mtp()) -, _rowHeight(st::contactsPadding.top() + st::contactsPhotoSize + st::contactsPadding.bottom()) -, _revokeWidth(st::normalFont->width(tr::lng_channels_too_much_public_revoke(tr::now))) +, _rowHeight(st::contactsPadding.top() + + st::contactsPhotoSize + + st::contactsPadding.bottom()) +, _revokeWidth(st::normalFont->width( + tr::lng_channels_too_much_public_revoke(tr::now))) , _revokeCallback(std::move(revokeCallback)) { setMouseTracking(true); @@ -1367,7 +1506,9 @@ RevokePublicLinkBox::RevokePublicLinkBox( } void RevokePublicLinkBox::prepare() { - _innerTop = st::boxPadding.top() + _aboutRevoke->height() + st::boxPadding.top(); + _innerTop = st::boxPadding.top() + + _aboutRevoke->height() + + st::boxPadding.top(); _inner = setInnerWidget(object_ptr(this, _session, [=] { const auto callback = _revokeCallback; closeBox(); @@ -1392,11 +1533,21 @@ void RevokePublicLinkBox::Inner::mouseMoveEvent(QMouseEvent *e) { } void RevokePublicLinkBox::Inner::updateSelected() { - auto point = mapFromGlobal(QCursor::pos()); + const auto point = mapFromGlobal(QCursor::pos()); PeerData *selected = nullptr; auto top = _rowsTop; for (const auto &row : _rows) { - auto revokeLink = style::rtlrect(width() - st::contactsPadding.right() - st::contactsCheckPosition.x() - _revokeWidth, top + st::contactsPadding.top() + (st::contactsPhotoSize - st::normalFont->height) / 2, _revokeWidth, st::normalFont->height, width()); + const auto revokeLink = style::rtlrect( + width() + - st::contactsPadding.right() + - st::contactsCheckPosition.x() + - _revokeWidth, + top + + st::contactsPadding.top() + + (st::contactsPhotoSize - st::normalFont->height) / 2, + _revokeWidth, + st::normalFont->height, + width()); if (revokeLink.contains(point)) { selected = row.peer; break; @@ -1405,7 +1556,9 @@ void RevokePublicLinkBox::Inner::updateSelected() { } if (selected != _selected) { _selected = selected; - setCursor((_selected || _pressed) ? style::cur_pointer : style::cur_default); + setCursor((_selected || _pressed) + ? style::cur_pointer + : style::cur_default); update(); } } @@ -1418,21 +1571,26 @@ void RevokePublicLinkBox::Inner::mousePressEvent(QMouseEvent *e) { } void RevokePublicLinkBox::Inner::mouseReleaseEvent(QMouseEvent *e) { - auto pressed = base::take(_pressed); - setCursor((_selected || _pressed) ? style::cur_pointer : style::cur_default); + const auto pressed = base::take(_pressed); + setCursor((_selected || _pressed) + ? style::cur_pointer + : style::cur_default); if (pressed && pressed == _selected) { - auto text_method = pressed->isMegagroup() + const auto textMethod = pressed->isMegagroup() ? tr::lng_channels_too_much_public_revoke_confirm_group : tr::lng_channels_too_much_public_revoke_confirm_channel; - auto text = text_method( + const auto text = textMethod( tr::now, lt_link, _session->createInternalLink(pressed->userName()), lt_group, pressed->name); - auto confirmText = tr::lng_channels_too_much_public_revoke(tr::now); + const auto confirmText = tr::lng_channels_too_much_public_revoke( + tr::now); auto callback = crl::guard(this, [=](Fn &&close) { - if (_revokeRequestId) return; + if (_revokeRequestId) { + return; + } _revokeRequestId = _api.request(MTPchannels_UpdateUsername( pressed->asChannel()->inputChannel, MTP_string() @@ -1464,18 +1622,33 @@ void RevokePublicLinkBox::resizeEvent(QResizeEvent *e) { _aboutRevoke->moveToLeft(st::boxPadding.left(), st::boxPadding.top()); } -void RevokePublicLinkBox::Inner::paintChat(Painter &p, const ChatRow &row, bool selected) const { - auto peer = row.peer; - peer->paintUserpicLeft(p, row.userpic, st::contactsPadding.left(), st::contactsPadding.top(), width(), st::contactsPhotoSize); +void RevokePublicLinkBox::Inner::paintChat( + Painter &p, + const ChatRow &row, + bool selected) const { + const auto peer = row.peer; + peer->paintUserpicLeft( + p, + row.userpic, + st::contactsPadding.left(), + st::contactsPadding.top(), + width(), + st::contactsPhotoSize); p.setPen(st::contactsNameFg); - int32 namex = st::contactsPadding.left() + st::contactsPhotoSize + st::contactsPadding.left(); - int32 namew = width() - namex - st::contactsPadding.right() - (_revokeWidth + st::contactsCheckPosition.x() * 2); + const auto namex = st::contactsPadding.left() + + st::contactsPhotoSize + + st::contactsPadding.left(); + auto namew = width() + - namex + - st::contactsPadding.right() + - (_revokeWidth + st::contactsCheckPosition.x() * 2); const auto badgeStyle = Ui::PeerBadgeStyle{ &st::dialogsVerifiedIcon, - &st::attentionButtonFg }; + &st::attentionButtonFg + }; namew -= Ui::DrawPeerBadgeGetWidth( peer, p, @@ -1487,14 +1660,32 @@ void RevokePublicLinkBox::Inner::paintChat(Painter &p, const ChatRow &row, bool namew, width(), badgeStyle); - row.name.drawLeftElided(p, namex, st::contactsPadding.top() + st::contactsNameTop, namew, width()); + row.name.drawLeftElided( + p, + namex, + st::contactsPadding.top() + st::contactsNameTop, + namew, + width()); p.setFont(selected ? st::linkOverFont : st::linkFont); - p.setPen(selected ? st::defaultLinkButton.overColor : st::defaultLinkButton.color); - p.drawTextRight(st::contactsPadding.right() + st::contactsCheckPosition.x(), st::contactsPadding.top() + (st::contactsPhotoSize - st::normalFont->height) / 2, width(), tr::lng_channels_too_much_public_revoke(tr::now), _revokeWidth); + p.setPen(selected + ? st::defaultLinkButton.overColor + : st::defaultLinkButton.color); + p.drawTextRight( + st::contactsPadding.right() + st::contactsCheckPosition.x(), + st::contactsPadding.top() + + (st::contactsPhotoSize - st::normalFont->height) / 2, + width(), + tr::lng_channels_too_much_public_revoke(tr::now), + _revokeWidth); p.setPen(st::contactsStatusFg); p.setTextPalette(st::revokePublicLinkStatusPalette); - row.status.drawLeftElided(p, namex, st::contactsPadding.top() + st::contactsStatusTop, namew, width()); + row.status.drawLeftElided( + p, + namex, + st::contactsPadding.top() + st::contactsStatusTop, + namew, + width()); p.restoreTextPalette(); } diff --git a/Telegram/SourceFiles/boxes/add_contact_box.h b/Telegram/SourceFiles/boxes/add_contact_box.h index c58133504..0bad0ebcc 100644 --- a/Telegram/SourceFiles/boxes/add_contact_box.h +++ b/Telegram/SourceFiles/boxes/add_contact_box.h @@ -8,10 +8,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "boxes/abstract_box.h" +#include "base/timer.h" #include "mtproto/sender.h" -#include "styles/style_widgets.h" - -#include class ConfirmBox; class PeerListBox; @@ -118,7 +116,10 @@ protected: private: void createChannel(const QString &title, const QString &description); - void createGroup(not_null selectUsersBox, const QString &title, const std::vector> &users); + void createGroup( + not_null selectUsersBox, + const QString &title, + const std::vector> &users); void submitName(); void submit(); void checkInviteLink(); @@ -176,12 +177,10 @@ private: void check(); void save(); - void updateDone(const MTPBool &result); - void updateFail(const MTP::Error &error); + void updateFail(const QString &error); - void checkDone(const MTPBool &result); - void checkFail(const MTP::Error &error); - void firstCheckFail(const MTP::Error &error); + void checkFail(const QString &error); + void firstCheckFail(const QString &error); void updateMaxHeight(); @@ -209,7 +208,7 @@ private: mtpRequestId _checkRequestId = 0; QString _sentUsername, _checkUsername, _errorText, _goodText; - QTimer _checkTimer; + base::Timer _checkTimer; }; @@ -226,8 +225,7 @@ protected: private: void submit(); void save(); - void saveSelfDone(const MTPUser &user); - void saveSelfFail(const MTP::Error &error); + void saveSelfFail(const QString &error); const not_null _user; MTP::Sender _api;