tdesktop/Telegram/SourceFiles/intro/intro_widget.cpp

845 lines
22 KiB
C++
Raw Normal View History

/*
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
*/
2019-11-27 09:45:23 +00:00
#include "intro/intro_widget.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_start.h"
#include "intro/intro_phone.h"
#include "intro/intro_qr.h"
2019-11-27 09:45:23 +00:00
#include "intro/intro_code.h"
#include "intro/intro_signup.h"
#include "intro/intro_password_check.h"
2019-11-26 11:10:44 +00:00
#include "lang/lang_keys.h"
2020-09-30 09:11:44 +00:00
#include "lang/lang_instance.h"
2019-11-26 11:10:44 +00:00
#include "lang/lang_cloud_manager.h"
#include "storage/localstorage.h"
2019-07-24 08:46:23 +00:00
#include "main/main_account.h"
#include "main/main_domain.h"
#include "main/main_session.h"
#include "mainwindow.h"
#include "history/history.h"
#include "history/history_item.h"
#include "data/data_user.h"
#include "countries/countries_instance.h"
2021-10-18 21:36:55 +00:00
#include "ui/boxes/confirm_box.h"
2021-07-26 06:32:16 +00:00
#include "ui/text/format_values.h" // Ui::FormatPhone
#include "ui/text/text_utilities.h"
#include "ui/widgets/buttons.h"
2016-11-24 19:28:23 +00:00
#include "ui/widgets/labels.h"
#include "ui/wrap/fade_wrap.h"
#include "core/update_checker.h"
#include "core/application.h"
#include "mtproto/mtproto_dc_options.h"
2016-11-24 19:28:23 +00:00
#include "window/window_slide_animation.h"
2018-05-07 17:44:33 +00:00
#include "window/window_connecting_widget.h"
#include "window/window_controller.h"
#include "window/window_session_controller.h"
#include "window/section_widget.h"
2019-09-17 16:13:12 +00:00
#include "base/platform/base_platform_info.h"
#include "api/api_text_entities.h"
2019-09-18 11:19:05 +00:00
#include "styles/style_layers.h"
2019-02-07 16:36:30 +00:00
#include "styles/style_intro.h"
2022-01-24 16:33:30 +00:00
#include "base/qt/qt_common_adapters.h"
2016-11-24 19:28:23 +00:00
namespace Intro {
namespace {
2019-11-26 11:10:44 +00:00
using namespace ::Intro::details;
2018-10-07 09:02:10 +00:00
[[nodiscard]] QString ComputeNewAccountCountry() {
if (const auto parent
= Core::App().domain().maybeLastOrSomeAuthedAccount()) {
if (const auto session = parent->maybeSession()) {
2021-08-26 15:15:49 +00:00
const auto iso = Countries::Instance().countryISO2ByPhone(
session->user()->phone());
if (!iso.isEmpty()) {
return iso;
}
}
}
return Platform::SystemCountry();
}
} // namespace
Widget::Widget(
QWidget *parent,
not_null<Window::Controller*> controller,
not_null<Main::Account*> account,
EnterPoint point)
2019-07-24 08:46:23 +00:00
: RpWidget(parent)
, _account(account)
, _data(details::Data{ .controller = controller })
, _back(this, object_ptr<Ui::IconButton>(this, st::introBackButton))
, _settings(
this,
object_ptr<Ui::RoundButton>(
this,
tr::lng_menu_settings(),
st::defaultBoxButton))
2019-11-26 11:39:54 +00:00
, _next(
this,
2019-11-27 08:02:56 +00:00
object_ptr<Ui::RoundButton>(this, nullptr, st::introNextButton))
, _connecting(std::make_unique<Window::ConnectionState>(
2019-11-26 11:39:54 +00:00
this,
2020-06-10 14:52:44 +00:00
account,
2019-11-27 08:02:56 +00:00
rpl::single(true))) {
Core::App().setDefaultFloatPlayerDelegate(floatPlayerDelegate());
getData()->country = ComputeNewAccountCountry();
_account->mtpValue(
) | rpl::start_with_next([=](not_null<MTP::Instance*> instance) {
_api.emplace(instance);
crl::on_main(this, [=] { createLanguageLink(); });
}, lifetime());
switch (point) {
case EnterPoint::Start:
getNearestDC();
appendStep(new StartWidget(this, _account, getData()));
break;
case EnterPoint::Phone:
appendStep(new PhoneWidget(this, _account, getData()));
break;
case EnterPoint::Qr:
appendStep(new QrWidget(this, _account, getData()));
break;
default: Unexpected("Enter point in Intro::Widget::Widget.");
}
2019-11-27 08:02:56 +00:00
fixOrder();
Lang::CurrentCloudManager().firstLanguageSuggestion(
) | rpl::start_with_next([=] {
2019-11-27 08:02:56 +00:00
createLanguageLink();
}, lifetime());
2019-11-27 08:02:56 +00:00
_account->mtpUpdates(
) | rpl::start_with_next([=](const MTPUpdates &updates) {
handleUpdates(updates);
}, lifetime());
_back->entity()->setClickedCallback([=] { backRequested(); });
_back->hide(anim::type::instant);
2019-11-27 08:02:56 +00:00
_next->entity()->setClickedCallback([=] { getStep()->submit(); });
2016-11-24 19:28:23 +00:00
2019-11-27 08:02:56 +00:00
if (_changeLanguage) {
_changeLanguage->finishAnimating();
}
2020-09-30 09:11:44 +00:00
Lang::Updated(
) | rpl::start_with_next([=] {
refreshLang();
}, lifetime());
show();
2016-11-24 19:28:23 +00:00
showControls();
getStep()->showFast();
setInnerFocus();
cSetPasswordRecovered(false);
if (!Core::UpdaterDisabled()) {
Core::UpdateChecker checker;
checker.start();
2019-11-26 11:10:44 +00:00
rpl::merge(
rpl::single(rpl::empty),
2019-11-26 11:10:44 +00:00
checker.isLatest(),
checker.failed(),
checker.ready()
) | rpl::start_with_next([=] {
checkUpdateStatus();
}, lifetime());
}
}
rpl::producer<> Widget::showSettingsRequested() const {
return _settings->entity()->clicks() | rpl::to_empty;
}
not_null<Media::Player::FloatDelegate*> Widget::floatPlayerDelegate() {
return static_cast<Media::Player::FloatDelegate*>(this);
}
auto Widget::floatPlayerSectionDelegate()
-> not_null<Media::Player::FloatSectionDelegate*> {
return static_cast<Media::Player::FloatSectionDelegate*>(this);
}
not_null<Ui::RpWidget*> Widget::floatPlayerWidget() {
return this;
}
auto Widget::floatPlayerGetSection(Window::Column column)
-> not_null<Media::Player::FloatSectionDelegate*> {
return this;
}
void Widget::floatPlayerEnumerateSections(Fn<void(
not_null<Media::Player::FloatSectionDelegate*> widget,
Window::Column widgetColumn)> callback) {
callback(this, Window::Column::Second);
}
bool Widget::floatPlayerIsVisible(not_null<HistoryItem*> item) {
return false;
}
void Widget::floatPlayerDoubleClickEvent(not_null<const HistoryItem*> item) {
getData()->controller->invokeForSessionController(
&item->history()->peer->session().account(),
item->history()->peer,
[&](not_null<Window::SessionController*> controller) {
controller->showPeerHistoryAtItem(item);
});
}
QRect Widget::floatPlayerAvailableRect() {
return mapToGlobal(rect());
}
bool Widget::floatPlayerHandleWheelEvent(QEvent *e) {
return false;
}
void Widget::refreshLang() {
_changeLanguage.destroy();
createLanguageLink();
InvokeQueued(this, [this] { updateControlsGeometry(); });
}
void Widget::handleUpdates(const MTPUpdates &updates) {
updates.match([&](const MTPDupdateShort &data) {
handleUpdate(data.vupdate());
}, [&](const MTPDupdates &data) {
for (const auto &update : data.vupdates().v) {
handleUpdate(update);
}
}, [&](const MTPDupdatesCombined &data) {
for (const auto &update : data.vupdates().v) {
handleUpdate(update);
}
}, [](const auto &) {});
}
void Widget::handleUpdate(const MTPUpdate &update) {
update.match([&](const MTPDupdateDcOptions &data) {
_account->mtp().dcOptions().addFromList(data.vdc_options());
}, [&](const MTPDupdateConfig &data) {
_account->mtp().requestConfig();
}, [&](const MTPDupdateServiceNotification &data) {
const auto text = TextWithEntities{
qs(data.vmessage()),
2020-06-08 08:03:45 +00:00
Api::EntitiesFromMTP(nullptr, data.ventities().v)
};
Ui::show(Ui::MakeInformBox(text));
}, [](const auto &) {});
}
2017-05-30 09:31:40 +00:00
void Widget::createLanguageLink() {
if (_changeLanguage
|| Core::App().domain().maybeLastOrSomeAuthedAccount()) {
2019-11-27 08:02:56 +00:00
return;
}
2017-05-30 09:31:40 +00:00
2019-11-27 08:02:56 +00:00
const auto createLink = [=](
const QString &text,
const QString &languageId) {
_changeLanguage.create(
this,
object_ptr<Ui::LinkButton>(this, text));
_changeLanguage->hide(anim::type::instant);
_changeLanguage->entity()->setClickedCallback([=] {
2017-05-30 09:31:40 +00:00
Lang::CurrentCloudManager().switchToLanguage(languageId);
});
2018-05-30 15:08:12 +00:00
_changeLanguage->toggle(
2019-11-26 14:27:09 +00:00
!_resetAccount && !_terms && _nextShown,
2018-05-30 15:08:12 +00:00
anim::type::normal);
updateControlsGeometry();
2017-05-30 09:31:40 +00:00
};
2020-09-30 09:11:44 +00:00
const auto currentId = Lang::LanguageIdOrDefault(Lang::Id());
const auto defaultId = Lang::DefaultLanguageId();
const auto suggested = Lang::CurrentCloudManager().suggestedLanguage();
if (currentId != defaultId) {
2019-06-19 15:42:16 +00:00
createLink(
Lang::GetOriginalValue(tr::lng_switch_to_this.base),
defaultId);
2019-11-27 08:02:56 +00:00
} else if (!suggested.isEmpty() && suggested != currentId && _api) {
_api->request(MTPlangpack_GetStrings(
2018-08-20 11:31:40 +00:00
MTP_string(Lang::CloudLangPackName()),
MTP_string(suggested),
MTP_vector<MTPstring>(1, MTP_string("lng_switch_to_this"))
)).done([=](const MTPVector<MTPLangPackString> &result) {
2019-06-19 15:42:16 +00:00
const auto strings = Lang::Instance::ParseStrings(result);
const auto i = strings.find(tr::lng_switch_to_this.base);
if (i != strings.end()) {
createLink(i->second, suggested);
2017-05-30 09:31:40 +00:00
}
}).send();
}
}
2019-11-26 11:10:44 +00:00
void Widget::checkUpdateStatus() {
Expects(!Core::UpdaterDisabled());
if (Core::UpdateChecker().state() == Core::UpdateChecker::State::Ready) {
if (_update) return;
_update.create(
this,
object_ptr<Ui::RoundButton>(
this,
tr::lng_menu_update(),
st::defaultBoxButton));
if (!_a_show.animating()) {
_update->setVisible(true);
}
const auto stepHasCover = getStep()->hasCover();
_update->toggle(!stepHasCover, anim::type::instant);
2016-11-24 19:28:23 +00:00
_update->entity()->setClickedCallback([] {
Core::checkReadyUpdate();
Core::Restart();
});
} else {
if (!_update) return;
_update.destroy();
}
updateControlsGeometry();
}
2016-11-24 19:28:23 +00:00
void Widget::setInnerFocus() {
if (getStep()->animating()) {
setFocus();
} else {
getStep()->setInnerFocus();
}
}
void Widget::historyMove(StackAction action, Animate animate) {
2019-11-26 11:10:44 +00:00
Expects(_stepHistory.size() > 1);
2019-11-26 11:10:44 +00:00
if (getStep()->animating()) {
return;
}
auto wasStep = getStep((action == StackAction::Back) ? 0 : 1);
if (action == StackAction::Back) {
_stepHistory.pop_back();
2016-11-24 19:28:23 +00:00
wasStep->cancelled();
} else if (action == StackAction::Replace) {
2019-11-26 11:10:44 +00:00
_stepHistory.erase(_stepHistory.end() - 2);
2016-11-24 19:28:23 +00:00
}
2018-06-01 07:00:18 +00:00
if (_resetAccount) {
hideAndDestroy(std::exchange(_resetAccount, { nullptr }));
}
if (_terms) {
hideAndDestroy(std::exchange(_terms, { nullptr }));
}
getStep()->finishInit();
2016-11-24 19:28:23 +00:00
getStep()->prepareShowAnimated(wasStep);
if (wasStep->hasCover() != getStep()->hasCover()) {
2019-11-26 11:10:44 +00:00
_nextTopFrom = wasStep->contentTop() + st::introNextTop;
2016-11-24 19:28:23 +00:00
_controlsTopFrom = wasStep->hasCover() ? st::introCoverHeight : 0;
_coverShownAnimation.start([this] { updateControlsGeometry(); }, 0., 1., st::introCoverDuration, wasStep->hasCover() ? anim::linear : anim::easeOutCirc);
}
2015-04-04 20:01:34 +00:00
_stepLifetime.destroy();
if (action == StackAction::Forward || action == StackAction::Replace) {
2016-11-24 19:28:23 +00:00
wasStep->finished();
}
if (action == StackAction::Back || action == StackAction::Replace) {
2016-11-24 19:28:23 +00:00
delete base::take(wasStep);
}
_back->toggle(getStep()->hasBack(), anim::type::normal);
auto stepHasCover = getStep()->hasCover();
_settings->toggle(!stepHasCover, anim::type::normal);
2018-05-30 15:08:12 +00:00
if (_update) {
_update->toggle(!stepHasCover, anim::type::normal);
}
2019-11-26 11:10:44 +00:00
setupNextButton();
2018-06-01 07:00:18 +00:00
if (_resetAccount) _resetAccount->show(anim::type::normal);
if (_terms) _terms->show(anim::type::normal);
getStep()->showAnimated(animate);
2016-11-24 19:28:23 +00:00
fixOrder();
}
2018-05-30 15:08:12 +00:00
void Widget::hideAndDestroy(object_ptr<Ui::FadeWrap<Ui::RpWidget>> widget) {
2019-09-13 12:22:54 +00:00
const auto weak = Ui::MakeWeak(widget.data());
2018-05-30 15:08:12 +00:00
widget->hide(anim::type::normal);
widget->shownValue(
) | rpl::start_with_next([=](bool shown) {
if (!shown && weak) {
weak->deleteLater();
}
}, widget->lifetime());
}
2016-11-24 19:28:23 +00:00
void Widget::fixOrder() {
_next->raise();
if (_update) _update->raise();
2019-11-26 14:27:09 +00:00
if (_changeLanguage) _changeLanguage->raise();
2016-11-24 19:28:23 +00:00
_settings->raise();
_back->raise();
floatPlayerRaiseAll();
2018-05-07 17:44:33 +00:00
_connecting->raise();
2016-11-24 19:28:23 +00:00
}
void Widget::moveToStep(Step *step, StackAction action, Animate animate) {
2016-11-24 19:28:23 +00:00
appendStep(step);
_back->raise();
_settings->raise();
if (_update) {
_update->raise();
}
2018-05-07 17:44:33 +00:00
_connecting->raise();
historyMove(action, animate);
}
2016-11-24 19:28:23 +00:00
void Widget::appendStep(Step *step) {
_stepHistory.push_back(step);
2019-11-26 11:10:44 +00:00
step->setGeometry(rect());
step->setGoCallback([=](Step *step, StackAction action, Animate animate) {
if (action == StackAction::Back) {
backRequested();
2016-11-24 19:28:23 +00:00
} else {
moveToStep(step, action, animate);
2016-11-24 19:28:23 +00:00
}
});
2018-05-30 15:08:12 +00:00
step->setShowResetCallback([=] {
2016-11-24 19:28:23 +00:00
showResetButton();
});
step->setShowTermsCallback([=] {
2018-06-01 07:00:18 +00:00
showTerms();
2018-05-30 15:08:12 +00:00
});
step->setCancelNearestDcCallback([=] {
if (_api) {
_api->request(base::take(_nearestDcRequestId)).cancel();
}
});
step->setAcceptTermsCallback([=](Fn<void()> callback) {
2018-06-01 07:00:18 +00:00
acceptTerms(callback);
2018-05-30 15:08:12 +00:00
});
2016-11-24 19:28:23 +00:00
}
void Widget::showResetButton() {
if (!_resetAccount) {
auto entity = object_ptr<Ui::RoundButton>(
this,
tr::lng_signin_reset_account(),
st::introResetButton);
2018-05-30 15:08:12 +00:00
_resetAccount.create(this, std::move(entity));
_resetAccount->hide(anim::type::instant);
2016-11-24 19:28:23 +00:00
_resetAccount->entity()->setClickedCallback([this] { resetAccount(); });
updateControlsGeometry();
2016-04-14 19:24:42 +00:00
}
_resetAccount->show(anim::type::normal);
2018-05-30 15:08:12 +00:00
if (_changeLanguage) {
_changeLanguage->hide(anim::type::normal);
}
}
2018-06-01 07:00:18 +00:00
void Widget::showTerms() {
if (getData()->termsLock.text.text.isEmpty()) {
2018-06-01 07:00:18 +00:00
_terms.destroy();
} else if (!_terms) {
auto entity = object_ptr<Ui::FlatLabel>(
this,
tr::lng_terms_signup(
2018-06-01 07:00:18 +00:00
lt_link,
tr::lng_terms_signup_link() | Ui::Text::ToLink(),
Ui::Text::WithEntities),
2018-06-01 07:00:18 +00:00
st::introTermsLabel);
_terms.create(this, std::move(entity));
_terms->entity()->setClickHandlerFilter([=](
const ClickHandlerPtr &handler,
Qt::MouseButton button) {
if (button == Qt::LeftButton) {
2018-06-01 07:00:18 +00:00
showTerms(nullptr);
}
return false;
});
2018-06-01 07:00:18 +00:00
updateControlsGeometry();
_terms->hide(anim::type::instant);
2018-05-30 15:08:12 +00:00
}
if (_changeLanguage) {
_changeLanguage->toggle(
2019-11-26 14:27:09 +00:00
!_terms && !_resetAccount && _nextShown,
2018-05-30 15:08:12 +00:00
anim::type::normal);
}
}
void Widget::acceptTerms(Fn<void()> callback) {
2018-05-30 15:08:12 +00:00
showTerms(callback);
2016-11-24 19:28:23 +00:00
}
void Widget::resetAccount() {
2019-11-27 08:02:56 +00:00
if (_resetRequest || !_api) {
return;
}
2016-11-24 19:28:23 +00:00
2021-10-18 22:28:08 +00:00
const auto callback = crl::guard(this, [this] {
2019-11-27 08:02:56 +00:00
if (_resetRequest) {
return;
}
_resetRequest = _api->request(MTPaccount_DeleteAccount(
2022-07-01 09:04:21 +00:00
MTP_flags(0),
MTP_string("Forgot password"),
MTPInputCheckPasswordSRP()
)).done([=] {
2017-05-30 09:31:40 +00:00
_resetRequest = 0;
Ui::hideLayer();
if (getData()->phone.isEmpty()) {
moveToStep(
new QrWidget(this, _account, getData()),
StackAction::Replace,
Animate::Back);
} else {
moveToStep(
new SignupWidget(this, _account, getData()),
StackAction::Replace,
Animate::Forward);
}
2021-03-12 12:48:00 +00:00
}).fail([=](const MTP::Error &error) {
2017-05-30 09:31:40 +00:00
_resetRequest = 0;
2019-07-24 08:46:23 +00:00
const auto &type = error.type();
2017-05-30 09:31:40 +00:00
if (type.startsWith(qstr("2FA_CONFIRM_WAIT_"))) {
const auto seconds = base::StringViewMid(
type,
2021-10-18 22:28:08 +00:00
qstr("2FA_CONFIRM_WAIT_").size()).toInt();
const auto days = (seconds + 59) / 86400;
const auto hours = ((seconds + 59) % 86400) / 3600;
const auto minutes = ((seconds + 59) % 3600) / 60;
2022-03-31 16:45:11 +00:00
auto when = tr::lng_minutes(tr::now, lt_count, minutes);
2017-05-30 09:31:40 +00:00
if (days > 0) {
2022-03-31 16:45:11 +00:00
const auto daysCount = tr::lng_days(
tr::now,
lt_count,
days);
2022-03-31 16:45:11 +00:00
const auto hoursCount = tr::lng_hours(
tr::now,
lt_count,
hours);
when = tr::lng_signin_reset_in_days(
tr::now,
lt_days_count,
daysCount,
lt_hours_count,
hoursCount,
lt_minutes_count,
when);
2017-05-30 09:31:40 +00:00
} else if (hours > 0) {
2022-03-31 16:45:11 +00:00
const auto hoursCount = tr::lng_hours(
tr::now,
lt_count,
hours);
when = tr::lng_signin_reset_in_hours(
tr::now,
lt_hours_count,
hoursCount,
lt_minutes_count,
when);
2017-05-30 09:31:40 +00:00
}
Ui::show(Ui::MakeInformBox(tr::lng_signin_reset_wait(
tr::now,
lt_phone_number,
2021-07-26 06:32:16 +00:00
Ui::FormatPhone(getData()->phone),
lt_when,
when)));
2017-05-30 09:31:40 +00:00
} else if (type == qstr("2FA_RECENT_CONFIRM")) {
Ui::show(Ui::MakeInformBox(
tr::lng_signin_reset_cancelled()));
2017-05-30 09:31:40 +00:00
} else {
Ui::hideLayer();
getStep()->showError(rpl::single(Lang::Hard::ServerError()));
2017-05-30 09:31:40 +00:00
}
}).send();
2021-10-18 22:28:08 +00:00
});
Ui::show(Ui::MakeConfirmBox({
.text = tr::lng_signin_sure_reset(),
.confirmed = callback,
.confirmText = tr::lng_signin_reset(),
.confirmStyle = &st::attentionBoxButton,
}));
}
2017-05-30 09:31:40 +00:00
void Widget::getNearestDC() {
2019-11-27 08:02:56 +00:00
if (!_api) {
return;
}
_nearestDcRequestId = _api->request(MTPhelp_GetNearestDc(
2019-11-27 08:02:56 +00:00
)).done([=](const MTPNearestDc &result) {
_nearestDcRequestId = 0;
2019-11-27 08:02:56 +00:00
const auto &nearest = result.c_nearestDc();
2018-05-30 15:08:12 +00:00
DEBUG_LOG(("Got nearest dc, country: %1, nearest: %2, this: %3"
2019-07-05 13:38:38 +00:00
).arg(qs(nearest.vcountry())
).arg(nearest.vnearest_dc().v
).arg(nearest.vthis_dc().v));
2019-07-24 08:46:23 +00:00
_account->suggestMainDcId(nearest.vnearest_dc().v);
const auto nearestCountry = qs(nearest.vcountry());
2017-05-30 09:31:40 +00:00
if (getData()->country != nearestCountry) {
getData()->country = nearestCountry;
getData()->updated.fire({});
2016-11-24 19:28:23 +00:00
}
2017-05-30 09:31:40 +00:00
}).send();
2016-11-24 19:28:23 +00:00
}
void Widget::showTerms(Fn<void()> callback) {
if (getData()->termsLock.text.text.isEmpty()) {
2018-05-30 15:08:12 +00:00
return;
}
2019-09-13 12:22:54 +00:00
const auto weak = Ui::MakeWeak(this);
const auto box = Ui::show(callback
? Box<Window::TermsBox>(
getData()->termsLock,
tr::lng_terms_agree(),
tr::lng_terms_decline())
: Box<Window::TermsBox>(
getData()->termsLock.text,
tr::lng_box_ok(),
nullptr));
box->setCloseByEscape(false);
box->setCloseByOutsideClick(false);
2018-06-01 07:00:18 +00:00
box->agreeClicks(
) | rpl::start_with_next([=] {
if (callback) {
callback();
}
if (box) {
box->closeBox();
}
}, box->lifetime());
box->cancelClicks(
) | rpl::start_with_next([=] {
const auto box = Ui::show(Box<Window::TermsBox>(
2019-06-19 15:09:03 +00:00
TextWithEntities{ tr::lng_terms_signup_sorry(tr::now) },
tr::lng_intro_finish(),
tr::lng_terms_decline()));
2018-05-30 15:08:12 +00:00
box->agreeClicks(
) | rpl::start_with_next([=] {
2018-06-01 07:00:18 +00:00
if (weak) {
showTerms(callback);
2018-05-30 15:08:12 +00:00
}
2018-06-01 07:00:18 +00:00
}, box->lifetime());
box->cancelClicks(
) | rpl::start_with_next([=] {
2018-05-30 15:08:12 +00:00
if (box) {
box->closeBox();
}
}, box->lifetime());
2018-06-01 07:00:18 +00:00
}, box->lifetime());
2018-05-30 15:08:12 +00:00
}
2016-11-24 19:28:23 +00:00
void Widget::showControls() {
getStep()->show();
2019-11-26 11:10:44 +00:00
setupNextButton();
2020-06-25 15:01:10 +00:00
_next->toggle(_nextShown, anim::type::instant);
2019-11-26 11:10:44 +00:00
_nextShownAnimation.stop();
2018-05-07 17:44:33 +00:00
_connecting->setForceHidden(false);
auto hasCover = getStep()->hasCover();
_settings->toggle(!hasCover, anim::type::instant);
2018-05-30 15:08:12 +00:00
if (_update) {
_update->toggle(!hasCover, anim::type::instant);
}
if (_changeLanguage) {
_changeLanguage->toggle(
2019-11-26 14:27:09 +00:00
!_resetAccount && !_terms && _nextShown,
2018-05-30 15:08:12 +00:00
anim::type::instant);
}
2018-06-01 07:00:18 +00:00
if (_terms) {
_terms->show(anim::type::instant);
}
_back->toggle(getStep()->hasBack(), anim::type::instant);
2016-11-24 19:28:23 +00:00
}
2019-11-26 11:10:44 +00:00
void Widget::setupNextButton() {
2019-11-26 11:39:54 +00:00
_next->entity()->setText(getStep()->nextButtonText(
2019-11-26 11:10:44 +00:00
) | rpl::filter([](const QString &text) {
return !text.isEmpty();
}));
getStep()->nextButtonText(
2019-11-26 11:10:44 +00:00
) | rpl::map([](const QString &text) {
return !text.isEmpty();
}) | rpl::filter([=](bool visible) {
2019-11-26 14:27:09 +00:00
return visible != _nextShown;
}) | rpl::start_with_next([=](bool visible) {
2019-11-26 11:39:54 +00:00
_next->toggle(visible, anim::type::normal);
2019-11-26 11:10:44 +00:00
_nextShown = visible;
2019-11-26 14:27:09 +00:00
if (_changeLanguage) {
_changeLanguage->toggle(
!_resetAccount && !_terms && _nextShown,
anim::type::normal);
}
2019-11-26 11:10:44 +00:00
_nextShownAnimation.start(
[=] { updateControlsGeometry(); },
_nextShown ? 0. : 1.,
_nextShown ? 1. : 0.,
st::slideDuration);
}, _stepLifetime);
2019-11-26 11:10:44 +00:00
}
2016-11-24 19:28:23 +00:00
void Widget::hideControls() {
getStep()->hide();
2019-11-26 11:39:54 +00:00
_next->hide(anim::type::instant);
2018-05-07 17:44:33 +00:00
_connecting->setForceHidden(true);
_settings->hide(anim::type::instant);
if (_update) _update->hide(anim::type::instant);
if (_changeLanguage) _changeLanguage->hide(anim::type::instant);
2018-06-01 07:00:18 +00:00
if (_terms) _terms->hide(anim::type::instant);
_back->hide(anim::type::instant);
2016-11-24 19:28:23 +00:00
}
void Widget::showAnimated(const QPixmap &bgAnimCache, bool back) {
_showBack = back;
2016-11-24 19:28:23 +00:00
(_showBack ? _cacheOver : _cacheUnder) = bgAnimCache;
2016-11-24 19:28:23 +00:00
2019-04-02 09:13:30 +00:00
_a_show.stop();
2016-11-24 19:28:23 +00:00
showControls();
floatPlayerHideAll();
(_showBack ? _cacheUnder : _cacheOver) = Ui::GrabWidget(this);
2016-11-24 19:28:23 +00:00
hideControls();
floatPlayerShowVisible();
2015-10-17 14:52:26 +00:00
2019-11-26 11:10:44 +00:00
_a_show.start(
[=] { animationCallback(); },
0.,
1.,
st::slideDuration,
Window::SlideAnimation::transition());
2015-10-17 14:52:26 +00:00
show();
}
void Widget::animationCallback() {
update();
if (!_a_show.animating()) {
2015-10-17 14:52:26 +00:00
_cacheUnder = _cacheOver = QPixmap();
2016-11-24 19:28:23 +00:00
showControls();
getStep()->activate();
2015-10-17 14:52:26 +00:00
}
}
2016-11-24 19:28:23 +00:00
void Widget::paintEvent(QPaintEvent *e) {
bool trivial = (rect() == e->rect());
setMouseTracking(true);
QPainter p(this);
if (!trivial) {
p.setClipRect(e->rect());
}
2016-10-31 12:29:26 +00:00
p.fillRect(e->rect(), st::windowBg);
2019-04-02 09:13:30 +00:00
auto progress = _a_show.value(1.);
2015-10-17 14:52:26 +00:00
if (_a_show.animating()) {
auto coordUnder = _showBack ? anim::interpolate(-st::slideShift, 0, progress) : anim::interpolate(0, -st::slideShift, progress);
auto coordOver = _showBack ? anim::interpolate(0, width(), progress) : anim::interpolate(width(), 0, progress);
auto shadow = _showBack ? (1. - progress) : progress;
if (coordOver > 0) {
p.drawPixmap(QRect(0, 0, coordOver, height()), _cacheUnder, QRect(-coordUnder * cRetinaFactor(), 0, coordOver * cRetinaFactor(), height() * cRetinaFactor()));
p.setOpacity(shadow);
p.fillRect(0, 0, coordOver, height(), st::slideFadeOutBg);
2015-10-17 14:52:26 +00:00
p.setOpacity(1);
}
p.drawPixmap(coordOver, 0, _cacheOver);
p.setOpacity(shadow);
st::slideShadow.fill(p, QRect(coordOver - st::slideShadow.width(), 0, st::slideShadow.width(), height()));
}
}
2016-11-24 19:28:23 +00:00
void Widget::resizeEvent(QResizeEvent *e) {
2020-07-02 06:42:09 +00:00
if (_stepHistory.empty()) {
return;
}
2019-11-26 11:10:44 +00:00
for (const auto step : _stepHistory) {
step->setGeometry(rect());
2016-11-24 19:28:23 +00:00
}
updateControlsGeometry();
floatPlayerAreaUpdated();
}
2016-11-24 19:28:23 +00:00
void Widget::updateControlsGeometry() {
2019-04-02 09:13:30 +00:00
auto shown = _coverShownAnimation.value(1.);
2016-11-24 19:28:23 +00:00
auto controlsTopTo = getStep()->hasCover() ? st::introCoverHeight : 0;
auto controlsTop = anim::interpolate(_controlsTopFrom, controlsTopTo, shown);
_settings->moveToRight(st::introSettingsSkip, controlsTop + st::introSettingsSkip);
if (_update) {
_update->moveToRight(st::introSettingsSkip + _settings->width() + st::introSettingsSkip, _settings->y());
}
_back->moveToLeft(0, controlsTop);
2019-11-26 11:10:44 +00:00
auto nextTopTo = getStep()->contentTop() + st::introNextTop;
2016-11-24 19:28:23 +00:00
auto nextTop = anim::interpolate(_nextTopFrom, nextTopTo, shown);
2019-11-26 11:10:44 +00:00
const auto shownAmount = _nextShownAnimation.value(_nextShown ? 1. : 0.);
2019-11-26 11:39:54 +00:00
const auto realNextTop = anim::interpolate(
nextTop + st::introNextSlide,
nextTop,
shownAmount);
2019-11-26 11:10:44 +00:00
_next->moveToLeft((width() - _next->width()) / 2, realNextTop);
2019-11-29 13:42:42 +00:00
getStep()->setShowAnimationClipping(shownAmount > 0
? QRect(0, 0, width(), realNextTop)
: QRect());
2016-11-24 19:28:23 +00:00
if (_changeLanguage) {
_changeLanguage->moveToLeft((width() - _changeLanguage->width()) / 2, _next->y() + _next->height() + _changeLanguage->height());
}
if (_resetAccount) {
_resetAccount->moveToLeft((width() - _resetAccount->width()) / 2, height() - st::introResetBottom - _resetAccount->height());
}
2018-05-30 15:08:12 +00:00
if (_terms) {
_terms->moveToLeft((width() - _terms->width()) / 2, height() - st::introTermsBottom - _terms->height());
}
2016-11-24 19:28:23 +00:00
}
void Widget::keyPressEvent(QKeyEvent *e) {
if (_a_show.animating() || getStep()->animating()) return;
if (e->key() == Qt::Key_Escape || e->key() == Qt::Key_Back) {
2016-11-24 19:28:23 +00:00
if (getStep()->hasBack()) {
backRequested();
2016-11-24 19:28:23 +00:00
}
} else if (e->key() == Qt::Key_Enter
|| e->key() == Qt::Key_Return
|| e->key() == Qt::Key_Space) {
2016-11-24 19:28:23 +00:00
getStep()->submit();
}
}
void Widget::backRequested() {
if (_stepHistory.size() > 1) {
historyMove(StackAction::Back, Animate::Back);
} else if (const auto parent
= Core::App().domain().maybeLastOrSomeAuthedAccount()) {
Core::App().domain().activate(parent);
} else {
moveToStep(
new StartWidget(this, _account, getData()),
StackAction::Replace,
Animate::Back);
}
}
2016-11-24 19:28:23 +00:00
Widget::~Widget() {
for (auto step : base::take(_stepHistory)) {
delete step;
}
}
2016-11-24 19:28:23 +00:00
} // namespace Intro