2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-31 22:46:10 +00:00

Intro redesign done.

This commit is contained in:
John Preston
2016-11-24 22:28:23 +03:00
parent 6e0394dd42
commit 3da0533339
71 changed files with 2150 additions and 1776 deletions

View File

@@ -26,21 +26,28 @@ Copyright (c) 2014-2016 John Preston, https://desktop.telegram.org
#include "intro/introsignup.h"
#include "intro/intropwdcheck.h"
#include "ui/widgets/buttons.h"
#include "ui/widgets/labels.h"
#include "styles/style_intro.h"
CodeInput::CodeInput(QWidget *parent, const style::FlatInput &st, const QString &ph) : Ui::FlatInput(parent, st, ph) {
namespace Intro {
CodeInput::CodeInput(QWidget *parent, const style::InputField &st, const QString &ph) : Ui::MaskedInputField(parent, st, ph) {
}
void CodeInput::correctValue(const QString &was, QString &now) {
void CodeInput::setDigitsCountMax(int digitsCount) {
_digitsCountMax = digitsCount;
}
void CodeInput::correctValue(const QString &was, int wasCursor, QString &now, int &nowCursor) {
QString newText;
int oldPos(cursorPosition()), newPos(-1), oldLen(now.length()), digitCount = 0;
int oldPos(nowCursor), newPos(-1), oldLen(now.length()), digitCount = 0;
for (int i = 0; i < oldLen; ++i) {
if (now[i].isDigit()) {
++digitCount;
}
}
if (digitCount > 5) digitCount = 5;
bool strict = (digitCount == 5);
accumulate_min(digitCount, _digitsCountMax);
auto strict = (digitCount == _digitsCountMax);
newText.reserve(oldLen);
for (int i = 0; i < oldLen; ++i) {
@@ -58,177 +65,131 @@ void CodeInput::correctValue(const QString &was, QString &now) {
newPos = newText.length();
}
}
if (newPos < 0) {
newPos = newText.length();
if (newPos < 0 || newPos > newText.size()) {
newPos = newText.size();
}
if (newText != now) {
now = newText;
setText(now);
updatePlaceholder();
if (newPos != oldPos) {
setCursorPosition(newPos);
}
}
if (newPos != nowCursor) {
nowCursor = newPos;
setCursorPosition(nowCursor);
}
if (strict) emit codeEntered();
}
IntroCode::IntroCode(IntroWidget *parent) : IntroStep(parent)
, a_errorAlpha(0)
, _a_error(animation(this, &IntroCode::step_error))
, _next(this, lang(lng_intro_next), st::introNextButton)
, _desc(st::introTextSize.width())
CodeWidget::CodeWidget(QWidget *parent, Widget::Data *data) : Step(parent, data)
, _noTelegramCode(this, lang(lng_code_no_telegram), st::introLink)
, _noTelegramCodeRequestId(0)
, _code(this, st::introCode, lang(lng_code_ph))
, _callTimer(this)
, _callStatus(intro()->getCallStatus())
, _callStatus(getData()->callStatus)
, _callTimeout(getData()->callTimeout)
, _callLabel(this, st::introDescription, st::introDescriptionTextStyle)
, _checkRequest(this) {
setGeometry(parent->innerRect());
connect(_next, SIGNAL(clicked()), this, SLOT(onSubmitCode()));
connect(_code, SIGNAL(changed()), this, SLOT(onInputChange()));
connect(_callTimer, SIGNAL(timeout()), this, SLOT(onSendCall()));
connect(_checkRequest, SIGNAL(timeout()), this, SLOT(onCheckRequest()));
connect(_noTelegramCode, SIGNAL(clicked()), this, SLOT(onNoTelegramCode()));
updateDescText();
_code->setDigitsCountMax(getData()->codeLength);
setErrorBelowLink(true);
if (!intro()->codeByTelegram()) {
if (_callStatus.type == IntroWidget::CallWaiting) {
_callTimer->start(1000);
}
}
setTitleText(App::formatPhone(getData()->phone));
updateDescText();
}
void IntroCode::updateDescText() {
_desc.setRichText(st::introFont, lang(intro()->codeByTelegram() ? lng_code_telegram : lng_code_desc));
if (intro()->codeByTelegram()) {
void CodeWidget::updateDescText() {
setDescriptionText(lang(getData()->codeByTelegram ? lng_code_telegram : lng_code_desc));
if (getData()->codeByTelegram) {
_noTelegramCode->show();
_callTimer->stop();
} else {
_noTelegramCode->hide();
_callStatus = intro()->getCallStatus();
if (_callStatus.type == IntroWidget::CallWaiting && !_callTimer->isActive()) {
_callStatus = getData()->callStatus;
_callTimeout = getData()->callTimeout;
if (_callStatus == Widget::Data::CallStatus::Waiting && !_callTimer->isActive()) {
_callTimer->start(1000);
}
}
update();
updateCallText();
}
void IntroCode::paintEvent(QPaintEvent *e) {
bool trivial = (rect() == e->rect());
QPainter p(this);
if (!trivial) {
p.setClipRect(e->rect());
}
bool codeByTelegram = intro()->codeByTelegram();
if (trivial || e->rect().intersects(_textRect)) {
p.setFont(st::introHeaderFont->f);
p.drawText(_textRect, intro()->getPhone(), style::al_top);
p.setFont(st::introFont->f);
_desc.draw(p, _textRect.x(), _textRect.y() + _textRect.height() - 2 * st::introFont->height, _textRect.width(), style::al_top);
}
if (codeByTelegram) {
} else {
QString callText;
switch (_callStatus.type) {
case IntroWidget::CallWaiting: {
if (_callStatus.timeout >= 3600) {
callText = lng_code_call(lt_minutes, qsl("%1:%2").arg(_callStatus.timeout / 3600).arg((_callStatus.timeout / 60) % 60, 2, 10, QChar('0')), lt_seconds, qsl("%1").arg(_callStatus.timeout % 60, 2, 10, QChar('0')));
void CodeWidget::updateCallText() {
auto text = ([this]() -> QString {
if (getData()->codeByTelegram) {
return QString();
}
switch (_callStatus) {
case Widget::Data::CallStatus::Waiting: {
if (_callTimeout >= 3600) {
return lng_code_call(lt_minutes, qsl("%1:%2").arg(_callTimeout / 3600).arg((_callTimeout / 60) % 60, 2, 10, QChar('0')), lt_seconds, qsl("%1").arg(_callTimeout % 60, 2, 10, QChar('0')));
} else {
callText = lng_code_call(lt_minutes, QString::number(_callStatus.timeout / 60), lt_seconds, qsl("%1").arg(_callStatus.timeout % 60, 2, 10, QChar('0')));
return lng_code_call(lt_minutes, QString::number(_callTimeout / 60), lt_seconds, qsl("%1").arg(_callTimeout % 60, 2, 10, QChar('0')));
}
} break;
case IntroWidget::CallCalling: {
callText = lang(lng_code_calling);
} break;
case IntroWidget::CallCalled: {
callText = lang(lng_code_called);
} break;
case Widget::Data::CallStatus::Calling: return lang(lng_code_calling);
case Widget::Data::CallStatus::Called: return lang(lng_code_called);
}
if (!callText.isEmpty()) {
p.drawText(QRect(_textRect.left(), _code->y() + _code->height() + st::introCallSkip, st::introTextSize.width(), st::introErrorHeight), callText, style::al_center);
}
}
if (_a_error.animating() || _error.length()) {
p.setOpacity(a_errorAlpha.current());
p.setFont(st::introErrorFont);
p.setPen(st::introErrorFg);
p.drawText(QRect(_textRect.left(), _next->y() + _next->height() + st::introErrorTop, st::introTextSize.width(), st::introErrorHeight), _error, style::al_center);
}
return QString();
})();
_callLabel->setText(text);
_callLabel->setVisible(!text.isEmpty() && !animating());
}
void IntroCode::resizeEvent(QResizeEvent *e) {
if (e->oldSize().width() != width()) {
_next->move((width() - _next->width()) / 2, st::introBtnTop);
_code->move((width() - _code->width()) / 2, st::introTextTop + st::introTextSize.height() + st::introCountry.top);
}
_textRect = QRect((width() - st::introTextSize.width()) / 2, st::introTextTop, st::introTextSize.width(), st::introTextSize.height());
_noTelegramCode->move(_textRect.left() + (st::introTextSize.width() - _noTelegramCode->width()) / 2, _code->y() + _code->height() + st::introCallSkip + (st::introErrorHeight - _noTelegramCode->height()) / 2);
void CodeWidget::resizeEvent(QResizeEvent *e) {
Step::resizeEvent(e);
_code->moveToLeft(contentLeft(), contentTop() + st::introStepFieldTop);
auto linkTop = _code->y() + _code->height() + st::introLinkTop;
_noTelegramCode->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
_callLabel->moveToLeft(contentLeft() + st::buttonRadius, linkTop);
}
void IntroCode::showError(const QString &error) {
if (!error.isEmpty()) _code->notaBene();
if (!_a_error.animating() && error == _error) return;
if (error.length()) {
_error = error;
a_errorAlpha.start(1);
} else {
a_errorAlpha.start(0);
}
_a_error.start();
void CodeWidget::showCodeError(const QString &text) {
if (!text.isEmpty()) _code->showError();
showError(text);
}
void IntroCode::step_error(float64 ms, bool timer) {
float64 dt = ms / st::introErrorDuration;
if (dt >= 1) {
_a_error.stop();
a_errorAlpha.finish();
if (!a_errorAlpha.current()) {
_error.clear();
}
} else {
a_errorAlpha.update(dt, anim::linear);
}
if (timer) update();
}
void IntroCode::activate() {
IntroStep::activate();
void CodeWidget::setInnerFocus() {
_code->setFocus();
}
void IntroCode::finished() {
IntroStep::finished();
_error.clear();
a_errorAlpha = anim::fvalue(0);
_sentCode.clear();
_code->setDisabled(false);
_callTimer->stop();
_code->setText(QString());
rpcClear();
}
void IntroCode::cancelled() {
if (_sentRequest) {
MTP::cancel(base::take(_sentRequest));
void CodeWidget::activate() {
Step::activate();
_code->show();
if (getData()->codeByTelegram) {
_noTelegramCode->show();
} else {
_callLabel->show();
}
MTP::send(MTPauth_CancelCode(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash())));
setInnerFocus();
}
void IntroCode::stopCheck() {
void CodeWidget::finished() {
Step::finished();
_checkRequest->stop();
_callTimer->stop();
rpcClear();
cancelled();
_sentCode.clear();
_code->setText(QString());
_code->setDisabled(false);
}
void CodeWidget::cancelled() {
MTP::cancel(base::take(_sentRequest));
MTP::cancel(base::take(_callRequestId));
MTP::send(MTPauth_CancelCode(MTP_string(getData()->phone), MTP_string(getData()->phoneHash)));
}
void CodeWidget::stopCheck() {
_checkRequest->stop();
}
void IntroCode::onCheckRequest() {
void CodeWidget::onCheckRequest() {
int32 status = MTP::state(_sentRequest);
if (status < 0) {
int32 leftms = -status;
@@ -248,26 +209,25 @@ void IntroCode::onCheckRequest() {
}
}
void IntroCode::codeSubmitDone(const MTPauth_Authorization &result) {
void CodeWidget::codeSubmitDone(const MTPauth_Authorization &result) {
stopCheck();
_sentRequest = 0;
_code->setDisabled(false);
const auto &d(result.c_auth_authorization());
auto &d = result.c_auth_authorization();
if (d.vuser.type() != mtpc_user || !d.vuser.c_user().is_self()) { // wtf?
showError(lang(lng_server_error));
showCodeError(lang(lng_server_error));
return;
}
cSetLoggedPhoneNumber(intro()->getPhone());
intro()->finish(d.vuser);
cSetLoggedPhoneNumber(getData()->phone);
finish(d.vuser);
}
bool IntroCode::codeSubmitFail(const RPCError &error) {
bool CodeWidget::codeSubmitFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
stopCheck();
_sentRequest = 0;
showError(lang(lng_flood_error));
_code->setDisabled(false);
_code->setFocus();
showCodeError(lang(lng_flood_error));
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
@@ -277,138 +237,138 @@ bool IntroCode::codeSubmitFail(const RPCError &error) {
_code->setDisabled(false);
const QString &err = error.type();
if (err == qstr("PHONE_NUMBER_INVALID") || err == qstr("PHONE_CODE_EXPIRED")) { // show error
intro()->onBack();
goBack();
return true;
} else if (err == qstr("PHONE_CODE_EMPTY") || err == qstr("PHONE_CODE_INVALID")) {
showError(lang(lng_bad_code));
_code->notaBene();
showCodeError(lang(lng_bad_code));
return true;
} else if (err == qstr("PHONE_NUMBER_UNOCCUPIED")) { // success, need to signUp
intro()->setCode(_sentCode);
intro()->replaceStep(new IntroSignup(intro()));
getData()->code = _sentCode;
goReplace(new Intro::SignupWidget(parentWidget(), getData()));
return true;
} else if (err == qstr("SESSION_PASSWORD_NEEDED")) {
intro()->setCode(_sentCode);
getData()->code = _sentCode;
_code->setDisabled(false);
_checkRequest->start(1000);
_sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&IntroCode::gotPassword), rpcFail(&IntroCode::codeSubmitFail));
_sentRequest = MTP::send(MTPaccount_GetPassword(), rpcDone(&CodeWidget::gotPassword), rpcFail(&CodeWidget::codeSubmitFail));
return true;
}
if (cDebug()) { // internal server error
showError(err + ": " + error.description());
showCodeError(err + ": " + error.description());
} else {
showError(lang(lng_server_error));
showCodeError(lang(lng_server_error));
}
_code->setFocus();
return false;
}
void IntroCode::onInputChange() {
showError(QString());
if (_code->text().length() == 5) onSubmitCode();
void CodeWidget::onInputChange() {
hideError();
if (_code->getLastText().length() == getData()->codeLength) {
submit();
}
}
void IntroCode::onSendCall() {
if (_callStatus.type == IntroWidget::CallWaiting) {
if (--_callStatus.timeout <= 0) {
_callStatus.type = IntroWidget::CallCalling;
void CodeWidget::onSendCall() {
if (_callStatus == Widget::Data::CallStatus::Waiting) {
if (--_callTimeout <= 0) {
_callStatus = Widget::Data::CallStatus::Calling;
_callTimer->stop();
MTP::send(MTPauth_ResendCode(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash())), rpcDone(&IntroCode::callDone));
_callRequestId = MTP::send(MTPauth_ResendCode(MTP_string(getData()->phone), MTP_string(getData()->phoneHash)), rpcDone(&CodeWidget::callDone));
} else {
intro()->setCallStatus(_callStatus);
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
}
}
update();
}
void IntroCode::callDone(const MTPauth_SentCode &v) {
if (_callStatus.type == IntroWidget::CallCalling) {
_callStatus.type = IntroWidget::CallCalled;
intro()->setCallStatus(_callStatus);
update();
updateCallText();
}
}
void IntroCode::gotPassword(const MTPaccount_Password &result) {
void CodeWidget::callDone(const MTPauth_SentCode &v) {
if (v.type() == mtpc_auth_sentCode) {
fillSentCodeData(v.c_auth_sentCode().vtype);
_code->setDigitsCountMax(getData()->codeLength);
}
if (_callStatus == Widget::Data::CallStatus::Calling) {
_callStatus = Widget::Data::CallStatus::Called;
getData()->callStatus = _callStatus;
getData()->callTimeout = _callTimeout;
updateCallText();
}
}
void CodeWidget::gotPassword(const MTPaccount_Password &result) {
stopCheck();
_sentRequest = 0;
_code->setDisabled(false);
switch (result.type()) {
case mtpc_account_noPassword: // should not happen
case mtpc_account_noPassword: { // should not happen
_code->setFocus();
break;
} break;
case mtpc_account_password: {
const auto &d(result.c_account_password());
intro()->setPwdSalt(qba(d.vcurrent_salt));
intro()->setHasRecovery(mtpIsTrue(d.vhas_recovery));
intro()->setPwdHint(qs(d.vhint));
intro()->replaceStep(new IntroPwdCheck(intro()));
auto &d = result.c_account_password();
getData()->pwdSalt = qba(d.vcurrent_salt);
getData()->hasRecovery = mtpIsTrue(d.vhas_recovery);
getData()->pwdHint = qs(d.vhint);
goReplace(new Intro::PwdCheckWidget(parentWidget(), getData()));
} break;
}
}
void IntroCode::onSubmitCode() {
void CodeWidget::submit() {
if (_sentRequest) return;
_code->setDisabled(true);
setFocus();
showError(QString());
hideError();
_checkRequest->start(1000);
_sentCode = _code->text();
intro()->setPwdSalt(QByteArray());
intro()->setHasRecovery(false);
intro()->setPwdHint(QString());
_sentRequest = MTP::send(MTPauth_SignIn(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash()), MTP_string(_sentCode)), rpcDone(&IntroCode::codeSubmitDone), rpcFail(&IntroCode::codeSubmitFail));
_sentCode = _code->getLastText();
getData()->pwdSalt = QByteArray();
getData()->hasRecovery = false;
getData()->pwdHint = QString();
_sentRequest = MTP::send(MTPauth_SignIn(MTP_string(getData()->phone), MTP_string(getData()->phoneHash), MTP_string(_sentCode)), rpcDone(&CodeWidget::codeSubmitDone), rpcFail(&CodeWidget::codeSubmitFail));
}
void IntroCode::onNoTelegramCode() {
void CodeWidget::onNoTelegramCode() {
if (_noTelegramCodeRequestId) return;
_noTelegramCodeRequestId = MTP::send(MTPauth_ResendCode(MTP_string(intro()->getPhone()), MTP_string(intro()->getPhoneHash())), rpcDone(&IntroCode::noTelegramCodeDone), rpcFail(&IntroCode::noTelegramCodeFail));
_noTelegramCodeRequestId = MTP::send(MTPauth_ResendCode(MTP_string(getData()->phone), MTP_string(getData()->phoneHash)), rpcDone(&CodeWidget::noTelegramCodeDone), rpcFail(&CodeWidget::noTelegramCodeFail));
}
void IntroCode::noTelegramCodeDone(const MTPauth_SentCode &result) {
void CodeWidget::noTelegramCodeDone(const MTPauth_SentCode &result) {
if (result.type() != mtpc_auth_sentCode) {
showError(lang(lng_server_error));
showCodeError(lang(lng_server_error));
return;
}
const auto &d(result.c_auth_sentCode());
switch (d.vtype.type()) {
case mtpc_auth_sentCodeTypeApp: intro()->setCodeByTelegram(true);
case mtpc_auth_sentCodeTypeSms:
case mtpc_auth_sentCodeTypeCall: intro()->setCodeByTelegram(false);
case mtpc_auth_sentCodeTypeFlashCall: LOG(("Error: should not be flashcall!")); break;
}
auto &d = result.c_auth_sentCode();
fillSentCodeData(d.vtype);
_code->setDigitsCountMax(getData()->codeLength);
if (d.has_next_type() && d.vnext_type.type() == mtpc_auth_codeTypeCall) {
intro()->setCallStatus({ IntroWidget::CallWaiting, d.has_timeout() ? d.vtimeout.v : 60 });
getData()->callStatus = Widget::Data::CallStatus::Waiting;
getData()->callTimeout = d.has_timeout() ? d.vtimeout.v : 60;
} else {
intro()->setCallStatus({ IntroWidget::CallDisabled, 0 });
getData()->callStatus = Widget::Data::CallStatus::Disabled;
getData()->callTimeout = 0;
}
intro()->setCodeByTelegram(false);
getData()->codeByTelegram = false;
updateDescText();
}
bool IntroCode::noTelegramCodeFail(const RPCError &error) {
bool CodeWidget::noTelegramCodeFail(const RPCError &error) {
if (MTP::isFloodError(error)) {
showError(lang(lng_flood_error));
_code->setFocus();
showCodeError(lang(lng_flood_error));
return true;
}
if (MTP::isDefaultHandledError(error)) return false;
if (cDebug()) { // internal server error
showError(error.type() + ": " + error.description());
showCodeError(error.type() + ": " + error.description());
} else {
showError(lang(lng_server_error));
showCodeError(lang(lng_server_error));
}
_code->setFocus();
return false;
}
void IntroCode::onSubmit() {
onSubmitCode();
}
} // namespace Intro