edit_factcheck_box.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "ui/boxes/edit_factcheck_box.h"
  8. #include "lang/lang_keys.h"
  9. #include "ui/widgets/fields/input_field.h"
  10. #include "styles/style_chat.h"
  11. #include "styles/style_layers.h"
  12. void EditFactcheckBox(
  13. not_null<Ui::GenericBox*> box,
  14. TextWithEntities current,
  15. int limit,
  16. Fn<void(TextWithEntities)> save,
  17. Fn<void(not_null<Ui::InputField*>)> initField) {
  18. box->setTitle(tr::lng_factcheck_title());
  19. const auto field = box->addRow(object_ptr<Ui::InputField>(
  20. box,
  21. st::factcheckField,
  22. Ui::InputField::Mode::NoNewlines,
  23. tr::lng_factcheck_placeholder(),
  24. TextWithTags{
  25. current.text,
  26. TextUtilities::ConvertEntitiesToTextTags(current.entities)
  27. }));
  28. AddLengthLimitLabel(field, limit);
  29. initField(field);
  30. enum class State {
  31. Initial,
  32. Changed,
  33. Removed,
  34. };
  35. const auto state = box->lifetime().make_state<rpl::variable<State>>(
  36. State::Initial);
  37. field->changes() | rpl::start_with_next([=] {
  38. const auto now = field->getLastText().trimmed();
  39. *state = !now.isEmpty()
  40. ? State::Changed
  41. : current.empty()
  42. ? State::Initial
  43. : State::Removed;
  44. }, field->lifetime());
  45. state->value() | rpl::start_with_next([=](State state) {
  46. box->clearButtons();
  47. if (state == State::Removed) {
  48. box->addButton(tr::lng_box_remove(), [=] {
  49. box->closeBox();
  50. save({});
  51. }, st::attentionBoxButton);
  52. } else if (state == State::Initial) {
  53. box->addButton(tr::lng_settings_save(), [=] {
  54. if (current.empty()) {
  55. field->showError();
  56. } else {
  57. box->closeBox();
  58. }
  59. });
  60. } else {
  61. box->addButton(tr::lng_settings_save(), [=] {
  62. auto result = field->getTextWithAppliedMarkdown();
  63. if (result.text.size() > limit) {
  64. field->showError();
  65. return;
  66. }
  67. box->closeBox();
  68. save({
  69. result.text,
  70. TextUtilities::ConvertTextTagsToEntities(result.tags)
  71. });
  72. });
  73. }
  74. box->addButton(tr::lng_cancel(), [=] {
  75. box->closeBox();
  76. });
  77. }, box->lifetime());
  78. box->setFocusCallback([=] {
  79. field->setFocusFast();
  80. });
  81. }