window_lock_widgets.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "window/window_lock_widgets.h"
  8. #include "base/platform/base_platform_info.h"
  9. #include "base/call_delayed.h"
  10. #include "base/system_unlock.h"
  11. #include "lang/lang_keys.h"
  12. #include "storage/storage_domain.h"
  13. #include "mainwindow.h"
  14. #include "core/application.h"
  15. #include "api/api_text_entities.h"
  16. #include "ui/text/text.h"
  17. #include "ui/widgets/buttons.h"
  18. #include "ui/widgets/checkbox.h"
  19. #include "ui/widgets/fields/password_input.h"
  20. #include "ui/widgets/labels.h"
  21. #include "ui/wrap/vertical_layout.h"
  22. #include "ui/toast/toast.h"
  23. #include "ui/ui_utility.h"
  24. #include "window/window_controller.h"
  25. #include "window/window_slide_animation.h"
  26. #include "window/window_session_controller.h"
  27. #include "main/main_domain.h"
  28. #include "styles/style_layers.h"
  29. #include "styles/style_boxes.h"
  30. namespace Window {
  31. namespace {
  32. constexpr auto kSystemUnlockDelay = crl::time(1000);
  33. } // namespace
  34. LockWidget::LockWidget(QWidget *parent, not_null<Controller*> window)
  35. : RpWidget(parent)
  36. , _window(window) {
  37. show();
  38. }
  39. LockWidget::~LockWidget() = default;
  40. not_null<Controller*> LockWidget::window() const {
  41. return _window;
  42. }
  43. void LockWidget::setInnerFocus() {
  44. setFocus();
  45. }
  46. void LockWidget::showAnimated(QPixmap oldContentCache) {
  47. _showAnimation = nullptr;
  48. showChildren();
  49. setInnerFocus();
  50. auto newContentCache = Ui::GrabWidget(this);
  51. hideChildren();
  52. _showAnimation = std::make_unique<Window::SlideAnimation>();
  53. _showAnimation->setRepaintCallback([=] { update(); });
  54. _showAnimation->setFinishedCallback([=] { showFinished(); });
  55. _showAnimation->setPixmaps(oldContentCache, newContentCache);
  56. _showAnimation->start();
  57. show();
  58. }
  59. void LockWidget::showFinished() {
  60. showChildren();
  61. _window->widget()->setInnerFocus();
  62. _showAnimation = nullptr;
  63. if (const auto controller = _window->sessionController()) {
  64. controller->clearSectionStack();
  65. }
  66. }
  67. void LockWidget::paintEvent(QPaintEvent *e) {
  68. auto p = QPainter(this);
  69. if (_showAnimation) {
  70. _showAnimation->paintContents(p);
  71. return;
  72. }
  73. paintContent(p);
  74. }
  75. void LockWidget::paintContent(QPainter &p) {
  76. p.fillRect(rect(), st::windowBg);
  77. }
  78. PasscodeLockWidget::PasscodeLockWidget(
  79. QWidget *parent,
  80. not_null<Controller*> window)
  81. : LockWidget(parent, window)
  82. , _passcode(this, st::passcodeInput, tr::lng_passcode_ph())
  83. , _submit(this, tr::lng_passcode_submit(), st::passcodeSubmit)
  84. , _logout(this, tr::lng_passcode_logout(tr::now)) {
  85. connect(_passcode, &Ui::MaskedInputField::changed, [=] { changed(); });
  86. connect(_passcode, &Ui::MaskedInputField::submitted, [=] { submit(); });
  87. _submit->setTextTransform(Ui::RoundButton::TextTransform::NoTransform);
  88. _submit->setClickedCallback([=] { submit(); });
  89. _logout->setClickedCallback([=] {
  90. window->showLogoutConfirmation();
  91. });
  92. using namespace rpl::mappers;
  93. if (Core::App().settings().systemUnlockEnabled()) {
  94. _systemUnlockAvailable = base::SystemUnlockStatus(
  95. true
  96. ) | rpl::map([](base::SystemUnlockAvailability status) {
  97. return status.withBiometrics
  98. ? SystemUnlockType::Biometrics
  99. : status.withCompanion
  100. ? SystemUnlockType::Companion
  101. : status.available
  102. ? SystemUnlockType::Default
  103. : SystemUnlockType::None;
  104. });
  105. if (Core::App().domain().started()) {
  106. _systemUnlockAllowed = _systemUnlockAvailable.value();
  107. setupSystemUnlock();
  108. } else {
  109. setupSystemUnlockInfo();
  110. }
  111. }
  112. }
  113. void PasscodeLockWidget::setupSystemUnlockInfo() {
  114. const auto macos = [&] {
  115. return _systemUnlockAvailable.value(
  116. ) | rpl::map([](SystemUnlockType type) {
  117. return (type == SystemUnlockType::Biometrics)
  118. ? tr::lng_passcode_touchid()
  119. : (type == SystemUnlockType::Companion)
  120. ? tr::lng_passcode_applewatch()
  121. : tr::lng_passcode_systempwd();
  122. }) | rpl::flatten_latest();
  123. };
  124. auto text = Platform::IsWindows()
  125. ? tr::lng_passcode_winhello()
  126. : macos();
  127. const auto info = Ui::CreateChild<Ui::FlatLabel>(
  128. this,
  129. std::move(text),
  130. st::passcodeSystemUnlockLater);
  131. _logout->geometryValue(
  132. ) | rpl::start_with_next([=](QRect logout) {
  133. info->resizeToWidth(width()
  134. - st::boxRowPadding.left()
  135. - st::boxRowPadding.right());
  136. info->moveToLeft(
  137. st::boxRowPadding.left(),
  138. logout.y() + logout.height() + st::passcodeSystemUnlockSkip);
  139. }, info->lifetime());
  140. info->showOn(_systemUnlockAvailable.value(
  141. ) | rpl::map(rpl::mappers::_1 != SystemUnlockType::None));
  142. }
  143. void PasscodeLockWidget::setupSystemUnlock() {
  144. windowActiveValue() | rpl::skip(1) | rpl::filter([=](bool active) {
  145. return active
  146. && !_systemUnlockSuggested
  147. && !_systemUnlockCooldown.isActive();
  148. }) | rpl::start_with_next([=](bool) {
  149. [[maybe_unused]] auto refresh = base::SystemUnlockStatus();
  150. suggestSystemUnlock();
  151. }, lifetime());
  152. const auto button = Ui::CreateChild<Ui::IconButton>(
  153. _passcode.data(),
  154. st::passcodeSystemUnlock);
  155. if (!Platform::IsWindows()) {
  156. using namespace base;
  157. _systemUnlockAllowed.value(
  158. ) | rpl::start_with_next([=](SystemUnlockType type) {
  159. const auto icon = (type == SystemUnlockType::Biometrics)
  160. ? &st::passcodeSystemTouchID
  161. : (type == SystemUnlockType::Companion)
  162. ? &st::passcodeSystemAppleWatch
  163. : &st::passcodeSystemSystemPwd;
  164. button->setIconOverride(icon, icon);
  165. }, button->lifetime());
  166. }
  167. button->showOn(_systemUnlockAllowed.value(
  168. ) | rpl::map(rpl::mappers::_1 != SystemUnlockType::None));
  169. _passcode->sizeValue() | rpl::start_with_next([=](QSize size) {
  170. button->moveToRight(0, size.height() - button->height());
  171. }, button->lifetime());
  172. button->setClickedCallback([=] {
  173. const auto delay = st::passcodeSystemUnlock.ripple.hideDuration;
  174. base::call_delayed(delay, this, [=] {
  175. suggestSystemUnlock();
  176. });
  177. });
  178. }
  179. void PasscodeLockWidget::suggestSystemUnlock() {
  180. InvokeQueued(this, [=] {
  181. if (_systemUnlockSuggested) {
  182. return;
  183. }
  184. _systemUnlockCooldown.cancel();
  185. using namespace base;
  186. _systemUnlockAllowed.value(
  187. ) | rpl::filter(
  188. rpl::mappers::_1 != SystemUnlockType::None
  189. ) | rpl::take(1) | rpl::start_with_next([=] {
  190. const auto weak = Ui::MakeWeak(this);
  191. const auto done = [weak](SystemUnlockResult result) {
  192. crl::on_main([=] {
  193. if (const auto strong = weak.data()) {
  194. strong->systemUnlockDone(result);
  195. }
  196. });
  197. };
  198. SuggestSystemUnlock(
  199. this,
  200. (::Platform::IsWindows()
  201. ? tr::lng_passcode_winhello_unlock(tr::now)
  202. : tr::lng_passcode_touchid_unlock(tr::now)),
  203. done);
  204. }, _systemUnlockSuggested);
  205. });
  206. }
  207. void PasscodeLockWidget::systemUnlockDone(base::SystemUnlockResult result) {
  208. if (result == base::SystemUnlockResult::Success) {
  209. Core::App().unlockPasscode();
  210. return;
  211. }
  212. _systemUnlockCooldown.callOnce(kSystemUnlockDelay);
  213. _systemUnlockSuggested.destroy();
  214. if (result == base::SystemUnlockResult::FloodError) {
  215. _error = tr::lng_flood_error(tr::now);
  216. _passcode->setFocusFast();
  217. update();
  218. }
  219. }
  220. void PasscodeLockWidget::paintContent(QPainter &p) {
  221. LockWidget::paintContent(p);
  222. p.setFont(st::passcodeHeaderFont);
  223. p.setPen(st::windowFg);
  224. p.drawText(QRect(0, _passcode->y() - st::passcodeHeaderHeight, width(), st::passcodeHeaderHeight), tr::lng_passcode_enter(tr::now), style::al_center);
  225. if (!_error.isEmpty()) {
  226. p.setFont(st::boxTextFont);
  227. p.setPen(st::boxTextFgError);
  228. p.drawText(QRect(0, _passcode->y() + _passcode->height(), width(), st::passcodeSubmitSkip), _error, style::al_center);
  229. }
  230. }
  231. void PasscodeLockWidget::submit() {
  232. if (_passcode->text().isEmpty()) {
  233. _passcode->showError();
  234. return;
  235. }
  236. if (!passcodeCanTry()) {
  237. _error = tr::lng_flood_error(tr::now);
  238. _passcode->showError();
  239. update();
  240. return;
  241. }
  242. const auto passcode = _passcode->text().toUtf8();
  243. auto &domain = Core::App().domain();
  244. const auto correct = domain.started()
  245. ? domain.local().checkPasscode(passcode)
  246. : (domain.start(passcode) == Storage::StartResult::Success);
  247. if (!correct) {
  248. cSetPasscodeBadTries(cPasscodeBadTries() + 1);
  249. cSetPasscodeLastTry(crl::now());
  250. error();
  251. return;
  252. }
  253. Core::App().unlockPasscode(); // Destroys this widget.
  254. }
  255. void PasscodeLockWidget::error() {
  256. _error = tr::lng_passcode_wrong(tr::now);
  257. _passcode->selectAll();
  258. _passcode->showError();
  259. update();
  260. }
  261. void PasscodeLockWidget::changed() {
  262. if (!_error.isEmpty()) {
  263. _error = QString();
  264. update();
  265. }
  266. }
  267. void PasscodeLockWidget::resizeEvent(QResizeEvent *e) {
  268. _passcode->move((width() - _passcode->width()) / 2, (height() / 3));
  269. _submit->move(_passcode->x(), _passcode->y() + _passcode->height() + st::passcodeSubmitSkip);
  270. _logout->move(_passcode->x() + (_passcode->width() - _logout->width()) / 2, _submit->y() + _submit->height() + st::linkFont->ascent);
  271. }
  272. void PasscodeLockWidget::setInnerFocus() {
  273. LockWidget::setInnerFocus();
  274. _passcode->setFocusFast();
  275. }
  276. TermsLock TermsLock::FromMTP(
  277. Main::Session *session,
  278. const MTPDhelp_termsOfService &data) {
  279. const auto minAge = data.vmin_age_confirm();
  280. return {
  281. bytes::make_vector(data.vid().c_dataJSON().vdata().v),
  282. TextWithEntities {
  283. qs(data.vtext()),
  284. Api::EntitiesFromMTP(session, data.ventities().v) },
  285. (minAge ? std::make_optional(minAge->v) : std::nullopt),
  286. data.is_popup()
  287. };
  288. }
  289. TermsBox::TermsBox(
  290. QWidget*,
  291. const TermsLock &data,
  292. rpl::producer<QString> agree,
  293. rpl::producer<QString> cancel)
  294. : _data(data)
  295. , _agree(std::move(agree))
  296. , _cancel(std::move(cancel)) {
  297. }
  298. TermsBox::TermsBox(
  299. QWidget*,
  300. const TextWithEntities &text,
  301. rpl::producer<QString> agree,
  302. rpl::producer<QString> cancel,
  303. bool attentionAgree)
  304. : _data{ {}, text, std::nullopt, false }
  305. , _agree(std::move(agree))
  306. , _cancel(std::move(cancel))
  307. , _attentionAgree(attentionAgree) {
  308. }
  309. rpl::producer<> TermsBox::agreeClicks() const {
  310. return _agreeClicks.events();
  311. }
  312. rpl::producer<> TermsBox::cancelClicks() const {
  313. return _cancelClicks.events();
  314. }
  315. void TermsBox::prepare() {
  316. setTitle(tr::lng_terms_header());
  317. auto check = std::make_unique<Ui::CheckView>(st::defaultCheck, false);
  318. const auto ageCheck = check.get();
  319. const auto age = _data.minAge
  320. ? Ui::CreateChild<Ui::PaddingWrap<Ui::Checkbox>>(
  321. this,
  322. object_ptr<Ui::Checkbox>(
  323. this,
  324. tr::lng_terms_age(tr::now, lt_count, *_data.minAge),
  325. st::defaultCheckbox,
  326. std::move(check)),
  327. st::termsAgePadding)
  328. : nullptr;
  329. if (age) {
  330. age->resizeToNaturalWidth(st::boxWideWidth);
  331. }
  332. const auto content = setInnerWidget(
  333. object_ptr<Ui::PaddingWrap<Ui::FlatLabel>>(
  334. this,
  335. object_ptr<Ui::FlatLabel> (
  336. this,
  337. rpl::single(_data.text),
  338. st::termsContent),
  339. st::termsPadding),
  340. 0,
  341. age ? age->height() : 0);
  342. const auto show = uiShow();
  343. content->entity()->setClickHandlerFilter([=](
  344. const ClickHandlerPtr &handler,
  345. Qt::MouseButton button) {
  346. const auto link = handler
  347. ? handler->copyToClipboardText()
  348. : QString();
  349. if (TextUtilities::RegExpMention().match(link).hasMatch()) {
  350. _lastClickedMention = link;
  351. show->showToast(
  352. tr::lng_terms_agree_to_proceed(tr::now, lt_bot, link));
  353. return false;
  354. }
  355. return true;
  356. });
  357. const auto errorAnimationCallback = [=] {
  358. const auto check = ageCheck;
  359. const auto error = _ageErrorAnimation.value(
  360. _ageErrorShown ? 1. : 0.);
  361. if (error == 0.) {
  362. check->setUntoggledOverride(std::nullopt);
  363. } else {
  364. const auto color = anim::color(
  365. st::defaultCheck.untoggledFg,
  366. st::boxTextFgError,
  367. error);
  368. check->setUntoggledOverride(color);
  369. }
  370. };
  371. const auto toggleAgeError = [=](bool shown) {
  372. if (_ageErrorShown != shown) {
  373. _ageErrorShown = shown;
  374. _ageErrorAnimation.start(
  375. [=] { errorAnimationCallback(); },
  376. _ageErrorShown ? 0. : 1.,
  377. _ageErrorShown ? 1. : 0.,
  378. st::defaultCheck.duration);
  379. }
  380. };
  381. const auto &agreeStyle = _attentionAgree
  382. ? st::attentionBoxButton
  383. : st::defaultBoxButton;
  384. addButton(std::move(_agree), [=] {}, agreeStyle)->clicks(
  385. ) | rpl::filter([=] {
  386. if (age && !age->entity()->checked()) {
  387. toggleAgeError(true);
  388. return false;
  389. }
  390. return true;
  391. }) | rpl::to_empty | rpl::start_to_stream(_agreeClicks, lifetime());
  392. if (_cancel) {
  393. addButton(std::move(_cancel), [] {})->clicks(
  394. ) | rpl::to_empty | rpl::start_to_stream(_cancelClicks, lifetime());
  395. }
  396. if (age) {
  397. age->entity()->checkedChanges(
  398. ) | rpl::start_with_next([=] {
  399. toggleAgeError(false);
  400. }, age->lifetime());
  401. heightValue(
  402. ) | rpl::start_with_next([=](int height) {
  403. age->moveToLeft(0, height - age->height());
  404. }, age->lifetime());
  405. }
  406. content->resizeToWidth(st::boxWideWidth);
  407. using namespace rpl::mappers;
  408. rpl::combine(
  409. content->heightValue(),
  410. age ? age->heightValue() : rpl::single(0),
  411. _1 + _2
  412. ) | rpl::start_with_next([=](int height) {
  413. setDimensions(st::boxWideWidth, height);
  414. }, content->lifetime());
  415. }
  416. void TermsBox::keyPressEvent(QKeyEvent *e) {
  417. if (e->key() == Qt::Key_Enter || e->key() == Qt::Key_Return) {
  418. _agreeClicks.fire({});
  419. } else {
  420. BoxContent::keyPressEvent(e);
  421. }
  422. }
  423. QString TermsBox::lastClickedMention() const {
  424. return _lastClickedMention;
  425. }
  426. } // namespace Window