passport_panel_password.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "passport/passport_panel_password.h"
  8. #include "passport/passport_panel_controller.h"
  9. #include "ui/controls/userpic_button.h"
  10. #include "ui/widgets/labels.h"
  11. #include "ui/widgets/buttons.h"
  12. #include "ui/widgets/fields/password_input.h"
  13. #include "ui/wrap/vertical_layout.h"
  14. #include "ui/wrap/padding_wrap.h"
  15. #include "boxes/passcode_box.h"
  16. #include "data/data_user.h"
  17. #include "lang/lang_keys.h"
  18. #include "info/profile/info_profile_icon.h"
  19. #include "styles/style_passport.h"
  20. #include "styles/style_layers.h"
  21. namespace Passport {
  22. PanelAskPassword::PanelAskPassword(
  23. QWidget *parent,
  24. not_null<PanelController*> controller)
  25. : RpWidget(parent)
  26. , _controller(controller)
  27. , _userpic(
  28. this,
  29. _controller->bot(),
  30. st::passportPasswordUserpic)
  31. , _about1(
  32. this,
  33. tr::lng_passport_request1(
  34. tr::now,
  35. lt_bot,
  36. _controller->bot()->name()),
  37. st::passportPasswordLabelBold)
  38. , _about2(
  39. this,
  40. tr::lng_passport_request2(tr::now),
  41. st::passportPasswordLabel)
  42. , _password(
  43. this,
  44. st::defaultInputField,
  45. tr::lng_passport_password_placeholder())
  46. , _submit(this, tr::lng_passport_next(), st::passportPasswordSubmit)
  47. , _forgot(this, tr::lng_signin_recover(tr::now), st::defaultLinkButton) {
  48. connect(_password, &Ui::PasswordInput::submitted, this, [=] {
  49. submit();
  50. });
  51. connect(_password, &Ui::PasswordInput::changed, this, [=] {
  52. hideError();
  53. });
  54. if (const auto hint = _controller->passwordHint(); !hint.isEmpty()) {
  55. _hint.create(
  56. this,
  57. hint,
  58. st::passportPasswordHintLabel);
  59. }
  60. _controller->passwordError(
  61. ) | rpl::start_with_next([=](const QString &error) {
  62. showError(error);
  63. }, lifetime());
  64. _forgot->addClickHandler([=] {
  65. recover();
  66. });
  67. _password->setFocusFast();
  68. _userpic->setAttribute(Qt::WA_TransparentForMouseEvents);
  69. _submit->addClickHandler([=] {
  70. submit();
  71. });
  72. }
  73. void PanelAskPassword::showError(const QString &error) {
  74. _password->showError();
  75. _error.create(
  76. this,
  77. error,
  78. st::passportErrorLabel);
  79. _error->show();
  80. updateControlsGeometry();
  81. }
  82. void PanelAskPassword::hideError() {
  83. _error.destroy();
  84. }
  85. void PanelAskPassword::submit() {
  86. _controller->submitPassword(_password->getLastText().toUtf8());
  87. }
  88. void PanelAskPassword::recover() {
  89. _controller->recoverPassword();
  90. }
  91. void PanelAskPassword::resizeEvent(QResizeEvent *e) {
  92. updateControlsGeometry();
  93. }
  94. void PanelAskPassword::focusInEvent(QFocusEvent *e) {
  95. crl::on_main(this, [=] {
  96. _password->setFocusFast();
  97. });
  98. }
  99. void PanelAskPassword::updateControlsGeometry() {
  100. const auto padding = st::passportPasswordPadding;
  101. const auto availableWidth = width()
  102. - st::boxPadding.left()
  103. - st::boxPadding.right();
  104. auto top = st::passportPasswordFieldBottom;
  105. top -= _password->height();
  106. _password->resize(
  107. st::passportPasswordSubmit.width,
  108. _password->height());
  109. _password->moveToLeft((width() - _password->width()) / 2, top);
  110. top -= st::passportPasswordFieldSkip + _about2->height();
  111. _about2->resizeToWidth(availableWidth);
  112. _about2->moveToLeft(padding.left(), top);
  113. top -= _about1->height();
  114. _about1->resizeToWidth(availableWidth);
  115. _about1->moveToLeft(padding.left(), top);
  116. top -= st::passportPasswordUserpicSkip + _userpic->height();
  117. _userpic->moveToLeft((width() - _userpic->width()) / 2, top);
  118. top = st::passportPasswordFieldBottom;
  119. if (_hint) {
  120. top += st::passportPasswordHintSkip;
  121. _hint->resizeToWidth(availableWidth);
  122. _hint->moveToLeft(padding.left(), top);
  123. top += _hint->height();
  124. }
  125. if (_error) {
  126. top += st::passportPasswordHintSkip;
  127. _error->resizeToWidth(availableWidth);
  128. _error->moveToLeft(padding.left(), top);
  129. top += _error->height();
  130. }
  131. top = height() - st::passportPasswordSubmitBottom - _submit->height();
  132. _submit->moveToLeft((width() - _submit->width()) / 2, top);
  133. top = height() - st::passportPasswordForgotBottom - _forgot->height();
  134. _forgot->moveToLeft((width() - _forgot->width()) / 2, top);
  135. }
  136. PanelNoPassword::PanelNoPassword(
  137. QWidget *parent,
  138. not_null<PanelController*> controller)
  139. : RpWidget(parent)
  140. , _controller(controller)
  141. , _inner(Ui::CreateChild<Ui::VerticalLayout>(this)) {
  142. setupContent();
  143. }
  144. void PanelNoPassword::setupContent() {
  145. widthValue(
  146. ) | rpl::start_with_next([=](int newWidth) {
  147. _inner->resizeToWidth(newWidth);
  148. }, _inner->lifetime());
  149. _inner->add(
  150. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  151. _inner,
  152. object_ptr<Ui::FlatLabel>(
  153. _inner,
  154. tr::lng_passport_request1(
  155. tr::now,
  156. lt_bot,
  157. _controller->bot()->name()),
  158. st::passportPasswordLabelBold)),
  159. st::passportPasswordAbout1Padding)->entity();
  160. _inner->add(
  161. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  162. _inner,
  163. object_ptr<Ui::FlatLabel>(
  164. _inner,
  165. tr::lng_passport_request2(tr::now),
  166. st::passportPasswordLabel)),
  167. st::passportPasswordAbout2Padding)->entity();
  168. const auto iconWrap = _inner->add(
  169. object_ptr<Ui::CenterWrap<Ui::FixedHeightWidget>>(
  170. _inner,
  171. object_ptr<Ui::FixedHeightWidget>(
  172. _inner,
  173. st::passportPasswordIconHeight)));
  174. iconWrap->entity()->resizeToWidth(st::passportPasswordIcon.width());
  175. Ui::CreateChild<Info::Profile::FloatingIcon>(
  176. iconWrap->entity(),
  177. st::passportPasswordIcon,
  178. QPoint(0, 0));
  179. _inner->add(
  180. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  181. _inner,
  182. object_ptr<Ui::FlatLabel>(
  183. _inner,
  184. tr::lng_passport_create_password(tr::now),
  185. st::passportPasswordSetupLabel)),
  186. st::passportFormAbout2Padding)->entity();
  187. refreshBottom();
  188. }
  189. void PanelNoPassword::refreshBottom() {
  190. const auto pattern = _controller->unconfirmedEmailPattern();
  191. _about.reset(_inner->add(
  192. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  193. _inner,
  194. object_ptr<Ui::FlatLabel>(
  195. _inner,
  196. (pattern.isEmpty()
  197. ? tr::lng_passport_about_password(tr::now)
  198. : tr::lng_passport_code_sent(tr::now, lt_email, pattern)),
  199. st::passportPasswordSetupLabel)),
  200. st::passportFormAbout2Padding)->entity());
  201. if (pattern.isEmpty()) {
  202. const auto button = _inner->add(
  203. object_ptr<Ui::CenterWrap<Ui::RoundButton>>(
  204. _inner,
  205. object_ptr<Ui::RoundButton>(
  206. _inner,
  207. tr::lng_passport_password_create(),
  208. st::defaultBoxButton)));
  209. button->entity()->addClickHandler([=] {
  210. _controller->setupPassword();
  211. });
  212. } else {
  213. const auto container = _inner->add(
  214. object_ptr<Ui::FixedHeightWidget>(
  215. _inner,
  216. st::defaultBoxButton.height));
  217. const auto cancel = Ui::CreateChild<Ui::RoundButton>(
  218. container,
  219. tr::lng_cancel(),
  220. st::defaultBoxButton);
  221. cancel->setTextTransform(
  222. Ui::RoundButton::TextTransform::NoTransform);
  223. cancel->addClickHandler([=] {
  224. _controller->cancelPasswordSubmit();
  225. });
  226. const auto validate = Ui::CreateChild<Ui::RoundButton>(
  227. container,
  228. tr::lng_passport_email_validate(),
  229. st::defaultBoxButton);
  230. validate->setTextTransform(
  231. Ui::RoundButton::TextTransform::NoTransform);
  232. validate->addClickHandler([=] {
  233. _controller->validateRecoveryEmail();
  234. });
  235. container->widthValue(
  236. ) | rpl::start_with_next([=](int width) {
  237. const auto both = cancel->width()
  238. + validate->width()
  239. + st::boxLittleSkip;
  240. cancel->moveToLeft((width - both) / 2, 0, width);
  241. validate->moveToRight((width - both) / 2, 0, width);
  242. }, container->lifetime());
  243. }
  244. }
  245. } // namespace Passport