passport_panel_form.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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_form.h"
  8. #include "passport/passport_panel_controller.h"
  9. #include "passport/ui/passport_form_row.h"
  10. #include "lang/lang_keys.h"
  11. #include "boxes/abstract_box.h"
  12. #include "core/click_handler_types.h"
  13. #include "data/data_user.h"
  14. #include "ui/controls/userpic_button.h"
  15. #include "ui/effects/animations.h"
  16. #include "ui/effects/scroll_content_shadow.h"
  17. #include "ui/widgets/buttons.h"
  18. #include "ui/widgets/scroll_area.h"
  19. #include "ui/widgets/labels.h"
  20. #include "ui/widgets/box_content_divider.h"
  21. #include "ui/wrap/vertical_layout.h"
  22. #include "ui/wrap/padding_wrap.h"
  23. #include "ui/text/text_utilities.h"
  24. #include "ui/text/text_options.h"
  25. #include "ui/ui_utility.h"
  26. #include "styles/style_passport.h"
  27. #include "styles/style_layers.h"
  28. #include "styles/style_boxes.h"
  29. namespace Passport {
  30. PanelForm::PanelForm(
  31. QWidget *parent,
  32. not_null<PanelController*> controller)
  33. : RpWidget(parent)
  34. , _controller(controller)
  35. , _scroll(this, st::passportPanelScroll)
  36. , _submit(
  37. this,
  38. tr::lng_passport_authorize(),
  39. st::passportPanelAuthorize) {
  40. setupControls();
  41. }
  42. void PanelForm::setupControls() {
  43. const auto inner = setupContent();
  44. _submit->addClickHandler([=] {
  45. _controller->submitForm();
  46. });
  47. SetupShadowsToScrollContent(this, _scroll, inner->heightValue());
  48. }
  49. not_null<Ui::RpWidget*> PanelForm::setupContent() {
  50. const auto bot = _controller->bot();
  51. const auto inner = _scroll->setOwnedWidget(
  52. object_ptr<Ui::VerticalLayout>(this));
  53. _scroll->widthValue(
  54. ) | rpl::start_with_next([=](int width) {
  55. inner->resizeToWidth(width);
  56. }, inner->lifetime());
  57. const auto userpicWrap = inner->add(
  58. object_ptr<Ui::FixedHeightWidget>(
  59. inner,
  60. st::passportFormUserpic.size.height()),
  61. st::passportFormUserpicPadding);
  62. _userpic = Ui::AttachParentChild(
  63. userpicWrap,
  64. object_ptr<Ui::UserpicButton>(
  65. userpicWrap,
  66. bot,
  67. st::passportFormUserpic));
  68. userpicWrap->widthValue(
  69. ) | rpl::start_with_next([=](int width) {
  70. _userpic->move((width - _userpic->width()) / 2, _userpic->y());
  71. }, _userpic->lifetime());
  72. _about1 = inner->add(
  73. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  74. inner,
  75. object_ptr<Ui::FlatLabel>(
  76. inner,
  77. tr::lng_passport_request1(tr::now, lt_bot, bot->name()),
  78. st::passportPasswordLabelBold)),
  79. st::passportFormAbout1Padding)->entity();
  80. _about2 = inner->add(
  81. object_ptr<Ui::CenterWrap<Ui::FlatLabel>>(
  82. inner,
  83. object_ptr<Ui::FlatLabel>(
  84. inner,
  85. tr::lng_passport_request2(tr::now),
  86. st::passportPasswordLabel)),
  87. st::passportFormAbout2Padding)->entity();
  88. inner->add(object_ptr<Ui::BoxContentDivider>(
  89. inner,
  90. st::passportFormDividerHeight));
  91. inner->add(
  92. object_ptr<Ui::FlatLabel>(
  93. inner,
  94. tr::lng_passport_header(tr::now),
  95. st::passportFormHeader),
  96. st::passportFormHeaderPadding);
  97. auto index = 0;
  98. _controller->fillRows([&](
  99. QString title,
  100. QString description,
  101. bool ready,
  102. bool error) {
  103. _rows.push_back(inner->add(object_ptr<Row>(this)));
  104. _rows.back()->addClickHandler([=] {
  105. _controller->editScope(index);
  106. });
  107. _rows.back()->updateContent(
  108. title,
  109. description,
  110. ready,
  111. error,
  112. anim::type::instant);
  113. ++index;
  114. });
  115. _controller->refillRows(
  116. ) | rpl::start_with_next([=] {
  117. auto index = 0;
  118. _controller->fillRows([&](
  119. QString title,
  120. QString description,
  121. bool ready,
  122. bool error) {
  123. Expects(index < _rows.size());
  124. _rows[index++]->updateContent(
  125. title,
  126. description,
  127. ready,
  128. error,
  129. anim::type::normal);
  130. });
  131. }, lifetime());
  132. const auto policyUrl = _controller->privacyPolicyUrl();
  133. auto policyLink = tr::lng_passport_policy(
  134. lt_bot,
  135. rpl::single(bot->name())
  136. ) | Ui::Text::ToLink(
  137. policyUrl
  138. ) | rpl::map([=](TextWithEntities &&text) {
  139. return Ui::Text::Wrapped(std::move(text), EntityType::Bold);
  140. });
  141. auto text = policyUrl.isEmpty()
  142. ? tr::lng_passport_allow(
  143. lt_bot,
  144. rpl::single('@' + bot->username())
  145. ) | Ui::Text::ToWithEntities()
  146. : tr::lng_passport_accept_allow(
  147. lt_policy,
  148. std::move(policyLink),
  149. lt_bot,
  150. rpl::single('@' + bot->username()) | Ui::Text::ToWithEntities(),
  151. Ui::Text::WithEntities);
  152. const auto policy = inner->add(
  153. object_ptr<Ui::FlatLabel>(
  154. inner,
  155. std::move(text),
  156. st::passportFormPolicy),
  157. st::passportFormPolicyPadding);
  158. policy->setLinksTrusted();
  159. return inner;
  160. }
  161. void PanelForm::resizeEvent(QResizeEvent *e) {
  162. updateControlsGeometry();
  163. }
  164. void PanelForm::updateControlsGeometry() {
  165. const auto submitTop = height() - _submit->height();
  166. _scroll->setGeometry(0, 0, width(), submitTop);
  167. _submit->setFullWidth(width());
  168. _submit->moveToLeft(0, submitTop);
  169. _scroll->updateBars();
  170. }
  171. } // namespace Passport