mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Replaced UsernameBox with generic box.
This commit is contained in:
parent
4f4d0bef25
commit
acbc248f23
4 changed files with 110 additions and 174 deletions
|
@ -7,28 +7,73 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#include "boxes/username_box.h"
|
#include "boxes/username_box.h"
|
||||||
|
|
||||||
|
#include "base/timer.h"
|
||||||
#include "boxes/peers/edit_peer_common.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_session.h"
|
||||||
#include "data/data_user.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_layers.h"
|
||||||
#include "styles/style_boxes.h"
|
#include "styles/style_boxes.h"
|
||||||
|
#include "styles/style_settings.h"
|
||||||
|
|
||||||
#include <QtGui/QGuiApplication>
|
namespace {
|
||||||
#include <QtGui/QClipboard>
|
|
||||||
|
|
||||||
UsernameBox::UsernameBox(QWidget*, not_null<Main::Session*> session)
|
class UsernameEditor final : public Ui::RpWidget {
|
||||||
|
public:
|
||||||
|
UsernameEditor(not_null<Ui::RpWidget*>, not_null<Main::Session*> 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<Main::Session*> _session;
|
||||||
|
const style::font &_font;
|
||||||
|
const style::margins &_padding;
|
||||||
|
MTP::Sender _api;
|
||||||
|
|
||||||
|
object_ptr<Ui::UsernameInput> _username;
|
||||||
|
|
||||||
|
mtpRequestId _saveRequestId = 0;
|
||||||
|
mtpRequestId _checkRequestId = 0;
|
||||||
|
QString _sentUsername, _checkUsername, _errorText, _goodText;
|
||||||
|
|
||||||
|
base::Timer _checkTimer;
|
||||||
|
|
||||||
|
rpl::event_stream<> _closeRequests;
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
UsernameEditor::UsernameEditor(
|
||||||
|
not_null<Ui::RpWidget*>,
|
||||||
|
not_null<Main::Session*> session)
|
||||||
: _session(session)
|
: _session(session)
|
||||||
, _font(st::normalFont)
|
, _font(st::normalFont)
|
||||||
, _padding(st::usernamePadding)
|
, _padding(st::usernamePadding)
|
||||||
, _textCenterTop((_font->height - _font->height) / 2)
|
|
||||||
, _api(&_session->mtp())
|
, _api(&_session->mtp())
|
||||||
, _username(
|
, _username(
|
||||||
this,
|
this,
|
||||||
|
@ -36,52 +81,30 @@ UsernameBox::UsernameBox(QWidget*, not_null<Main::Session*> session)
|
||||||
rpl::single(qsl("@username")),
|
rpl::single(qsl("@username")),
|
||||||
session->user()->username(),
|
session->user()->username(),
|
||||||
QString())
|
QString())
|
||||||
, _about(
|
|
||||||
this,
|
|
||||||
tr::lng_username_description(Ui::Text::RichLangValue),
|
|
||||||
st::defaultBoxLabel)
|
|
||||||
, _link(this, QString(), st::defaultLinkButton)
|
|
||||||
, _checkTimer([=] { check(); }) {
|
, _checkTimer([=] { check(); }) {
|
||||||
}
|
|
||||||
|
|
||||||
void UsernameBox::prepare() {
|
|
||||||
_goodText = _session->user()->username().isEmpty()
|
_goodText = _session->user()->username().isEmpty()
|
||||||
? QString()
|
? QString()
|
||||||
: tr::lng_username_available(tr::now);
|
: 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::changed, [=] { changed(); });
|
||||||
connect(_username, &Ui::MaskedInputField::submitted, [=] { save(); });
|
connect(_username, &Ui::MaskedInputField::submitted, [=] { save(); });
|
||||||
_link->addClickHandler([=] { linkClick(); });
|
|
||||||
|
|
||||||
_about->resizeToWidth(
|
resize(
|
||||||
st::boxWideWidth - _padding.left() - _padding.right());
|
width(),
|
||||||
_about->heightValue(
|
(_padding.top()
|
||||||
) | rpl::start_with_next([=](int height) {
|
+ _username->height()
|
||||||
setDimensions(
|
+ st::usernameSkip));
|
||||||
st::boxWideWidth,
|
|
||||||
(_padding.top()
|
|
||||||
+ _username->height()
|
|
||||||
+ st::usernameSkip
|
|
||||||
+ height
|
|
||||||
+ 3 * _font->height
|
|
||||||
+ _padding.bottom()));
|
|
||||||
}, lifetime());
|
|
||||||
|
|
||||||
updateLinkText();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::setInnerFocus() {
|
rpl::producer<> UsernameEditor::closeRequests() const {
|
||||||
|
return _closeRequests.events();
|
||||||
|
}
|
||||||
|
|
||||||
|
void UsernameEditor::setInnerFocus() {
|
||||||
_username->setFocusFast();
|
_username->setFocusFast();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::paintEvent(QPaintEvent *e) {
|
void UsernameEditor::paintEvent(QPaintEvent *e) {
|
||||||
BoxContent::paintEvent(e);
|
|
||||||
|
|
||||||
Painter p(this);
|
Painter p(this);
|
||||||
|
|
||||||
const auto textTop = _username->y()
|
const auto textTop = _username->y()
|
||||||
|
@ -112,59 +135,16 @@ void UsernameBox::paintEvent(QPaintEvent *e) {
|
||||||
tr::lng_username_choose(tr::now));
|
tr::lng_username_choose(tr::now));
|
||||||
}
|
}
|
||||||
p.setPen(st::boxTextFg);
|
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) {
|
void UsernameEditor::resizeEvent(QResizeEvent *e) {
|
||||||
BoxContent::resizeEvent(e);
|
|
||||||
|
|
||||||
_username->resize(
|
_username->resize(
|
||||||
width() - _padding.left() - _padding.right(),
|
width() - _padding.left() - _padding.right(),
|
||||||
_username->height());
|
_username->height());
|
||||||
_username->moveToLeft(_padding.left(), _padding.top());
|
_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) {
|
if (_saveRequestId) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -175,14 +155,14 @@ void UsernameBox::save() {
|
||||||
)).done([=](const MTPUser &result) {
|
)).done([=](const MTPUser &result) {
|
||||||
_saveRequestId = 0;
|
_saveRequestId = 0;
|
||||||
_session->data().processUser(result);
|
_session->data().processUser(result);
|
||||||
closeBox();
|
_closeRequests.fire({});
|
||||||
}).fail([=](const MTP::Error &error) {
|
}).fail([=](const MTP::Error &error) {
|
||||||
_saveRequestId = 0;
|
_saveRequestId = 0;
|
||||||
updateFail(error.type());
|
updateFail(error.type());
|
||||||
}).send();
|
}).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::check() {
|
void UsernameEditor::check() {
|
||||||
_api.request(base::take(_checkRequestId)).cancel();
|
_api.request(base::take(_checkRequestId)).cancel();
|
||||||
|
|
||||||
const auto name = getName();
|
const auto name = getName();
|
||||||
|
@ -210,8 +190,7 @@ void UsernameBox::check() {
|
||||||
}).send();
|
}).send();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::changed() {
|
void UsernameEditor::changed() {
|
||||||
updateLinkText();
|
|
||||||
const auto name = getName();
|
const auto name = getName();
|
||||||
if (name.isEmpty()) {
|
if (name.isEmpty()) {
|
||||||
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
|
if (!_errorText.isEmpty() || !_goodText.isEmpty()) {
|
||||||
|
@ -252,15 +231,7 @@ void UsernameBox::changed() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::linkClick() {
|
void UsernameEditor::updateFail(const QString &error) {
|
||||||
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) {
|
|
||||||
const auto self = _session->user();
|
const auto self = _session->user();
|
||||||
if ((error == qstr("USERNAME_NOT_MODIFIED"))
|
if ((error == qstr("USERNAME_NOT_MODIFIED"))
|
||||||
|| (_sentUsername == self->username())) {
|
|| (_sentUsername == self->username())) {
|
||||||
|
@ -269,7 +240,7 @@ void UsernameBox::updateFail(const QString &error) {
|
||||||
TextUtilities::SingleLine(self->lastName),
|
TextUtilities::SingleLine(self->lastName),
|
||||||
TextUtilities::SingleLine(self->nameOrPhone),
|
TextUtilities::SingleLine(self->nameOrPhone),
|
||||||
TextUtilities::SingleLine(_sentUsername));
|
TextUtilities::SingleLine(_sentUsername));
|
||||||
closeBox();
|
_closeRequests.fire({});
|
||||||
} else if (error == qstr("USERNAME_INVALID")) {
|
} else if (error == qstr("USERNAME_INVALID")) {
|
||||||
_username->setFocus();
|
_username->setFocus();
|
||||||
_username->showError();
|
_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")) {
|
if (error == qstr("USERNAME_INVALID")) {
|
||||||
_errorText = tr::lng_username_invalid(tr::now);
|
_errorText = tr::lng_username_invalid(tr::now);
|
||||||
update();
|
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();
|
return _username->text().replace('@', QString()).trimmed();
|
||||||
}
|
}
|
||||||
|
|
||||||
void UsernameBox::updateLinkText() {
|
} // namespace
|
||||||
const auto uname = getName();
|
|
||||||
_link->setText(_font->elided(
|
void UsernamesBox(
|
||||||
_session->createInternalLinkFull(uname),
|
not_null<Ui::GenericBox*> box,
|
||||||
st::boxWideWidth - _padding.left() - _padding.right()));
|
not_null<Main::Session*> session) {
|
||||||
if (uname.isEmpty()) {
|
box->setTitle(tr::lng_username_title());
|
||||||
if (!_link->isHidden()) {
|
|
||||||
_link->hide();
|
const auto container = box->verticalLayout();
|
||||||
update();
|
|
||||||
}
|
const auto editor = box->addRow(
|
||||||
} else {
|
object_ptr<UsernameEditor>(box, session),
|
||||||
if (_link->isHidden()) {
|
{});
|
||||||
_link->show();
|
editor->closeRequests(
|
||||||
update();
|
) | rpl::start_with_next([=] {
|
||||||
}
|
box->closeBox();
|
||||||
}
|
}, editor->lifetime());
|
||||||
|
|
||||||
|
container->add(object_ptr<Ui::DividerLabel>(
|
||||||
|
container,
|
||||||
|
object_ptr<Ui::FlatLabel>(
|
||||||
|
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(); });
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,60 +7,14 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "boxes/abstract_box.h"
|
|
||||||
#include "base/timer.h"
|
|
||||||
#include "mtproto/sender.h"
|
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class UsernameInput;
|
class GenericBox;
|
||||||
class LinkButton;
|
|
||||||
class FlatLabel;
|
|
||||||
} // namespace Ui
|
} // namespace Ui
|
||||||
|
|
||||||
namespace Main {
|
namespace Main {
|
||||||
class Session;
|
class Session;
|
||||||
} // namespace Main
|
} // namespace Main
|
||||||
|
|
||||||
class UsernameBox : public Ui::BoxContent {
|
void UsernamesBox(
|
||||||
public:
|
not_null<Ui::GenericBox*> box,
|
||||||
UsernameBox(QWidget*, not_null<Main::Session*> session);
|
not_null<Main::Session*> 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<Main::Session*> _session;
|
|
||||||
const style::font &_font;
|
|
||||||
const style::margins &_padding;
|
|
||||||
const int _textCenterTop;
|
|
||||||
MTP::Sender _api;
|
|
||||||
|
|
||||||
object_ptr<Ui::UsernameInput> _username;
|
|
||||||
object_ptr<Ui::FlatLabel> _about;
|
|
||||||
object_ptr<Ui::LinkButton> _link;
|
|
||||||
|
|
||||||
mtpRequestId _saveRequestId = 0;
|
|
||||||
mtpRequestId _checkRequestId = 0;
|
|
||||||
QString _sentUsername, _checkUsername, _errorText, _goodText;
|
|
||||||
|
|
||||||
base::Timer _checkTimer;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
|
@ -450,7 +450,7 @@ void SetupRows(
|
||||||
std::move(label),
|
std::move(label),
|
||||||
std::move(value),
|
std::move(value),
|
||||||
tr::lng_context_copy_mention(tr::now),
|
tr::lng_context_copy_mention(tr::now),
|
||||||
[=] { controller->show(Box<UsernameBox>(session)); },
|
[=] { controller->show(Box(UsernamesBox, session)); },
|
||||||
{ &st::settingsIconMention, kIconLightOrange });
|
{ &st::settingsIconMention, kIconLightOrange });
|
||||||
|
|
||||||
AddSkip(container);
|
AddSkip(container);
|
||||||
|
|
|
@ -200,7 +200,7 @@ void Cover::initViewers() {
|
||||||
_username->overrideLinkClickHandler([=] {
|
_username->overrideLinkClickHandler([=] {
|
||||||
const auto username = _user->userName();
|
const auto username = _user->userName();
|
||||||
if (username.isEmpty()) {
|
if (username.isEmpty()) {
|
||||||
_controller->show(Box<UsernameBox>(&_user->session()));
|
_controller->show(Box(UsernamesBox, &_user->session()));
|
||||||
} else {
|
} else {
|
||||||
QGuiApplication::clipboard()->setText(
|
QGuiApplication::clipboard()->setText(
|
||||||
_user->session().createInternalLinkFull(username));
|
_user->session().createInternalLinkFull(username));
|
||||||
|
|
Loading…
Add table
Reference in a new issue