auto_lock_box.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "boxes/auto_lock_box.h"
  8. #include "core/application.h"
  9. #include "core/core_settings.h"
  10. #include "lang/lang_keys.h"
  11. #include "ui/widgets/checkbox.h"
  12. #include "ui/widgets/time_input.h"
  13. #include "ui/ui_utility.h"
  14. #include "styles/style_layers.h"
  15. #include "styles/style_boxes.h"
  16. namespace {
  17. constexpr auto kCustom = std::numeric_limits<int>::max();
  18. constexpr auto kOptions = { 60, 300, 3600, 18000, kCustom };
  19. constexpr auto kDefaultCustom = "10:00"_cs;
  20. auto TimeString(int seconds) {
  21. const auto hours = seconds / 3600;
  22. const auto minutes = (seconds - hours * 3600) / 60;
  23. return QString("%1:%2").arg(hours).arg(minutes, 2, 10, QLatin1Char('0'));
  24. }
  25. } // namespace
  26. AutoLockBox::AutoLockBox(QWidget*) {
  27. }
  28. void AutoLockBox::prepare() {
  29. setTitle(tr::lng_passcode_autolock());
  30. addButton(tr::lng_box_ok(), [=] { closeBox(); });
  31. const auto currentTime = Core::App().settings().autoLock();
  32. const auto group = std::make_shared<Ui::RadiobuttonGroup>(
  33. ranges::contains(kOptions, currentTime) ? currentTime : kCustom);
  34. const auto x = st::boxPadding.left() + st::boxOptionListPadding.left();
  35. auto y = st::boxOptionListPadding.top() + st::autolockButton.margin.top();
  36. const auto count = int(kOptions.size());
  37. _options.reserve(count);
  38. for (const auto seconds : kOptions) {
  39. const auto text = [&] {
  40. if (seconds == kCustom) {
  41. return QString();
  42. }
  43. const auto minutes = (seconds % 3600);
  44. return (minutes
  45. ? tr::lng_minutes
  46. : tr::lng_hours)(
  47. tr::now,
  48. lt_count,
  49. minutes ? (seconds / 60) : (seconds / 3600));
  50. }();
  51. _options.emplace_back(
  52. this,
  53. group,
  54. seconds,
  55. text,
  56. st::autolockButton);
  57. _options.back()->moveToLeft(x, y);
  58. y += _options.back()->heightNoMargins() + st::boxOptionListSkip;
  59. }
  60. const auto timeInput = [&] {
  61. const auto &last = _options.back();
  62. const auto &st = st::autolockButton;
  63. const auto textLeft = st.checkPosition.x()
  64. + last->checkRect().width()
  65. + st.textPosition.x();
  66. const auto textTop = st.margin.top() + st.textPosition.y();
  67. const auto timeInput = Ui::CreateChild<Ui::TimeInput>(
  68. this,
  69. (group->current() == kCustom
  70. ? TimeString(currentTime)
  71. : kDefaultCustom.utf8()),
  72. st::autolockTimeField,
  73. st::autolockDateField,
  74. st::scheduleTimeSeparator,
  75. st::scheduleTimeSeparatorPadding);
  76. timeInput->resizeToWidth(st::autolockTimeWidth);
  77. timeInput->moveToLeft(last->x() + textLeft, last->y() + textTop);
  78. return timeInput;
  79. }();
  80. const auto collect = [=] {
  81. const auto timeValue = timeInput->valueCurrent().split(':');
  82. if (timeValue.size() != 2) {
  83. return 0;
  84. }
  85. return timeValue[0].toInt() * 3600 + timeValue[1].toInt() * 60;
  86. };
  87. timeInput->focuses(
  88. ) | rpl::start_with_next([=] {
  89. group->setValue(kCustom);
  90. }, lifetime());
  91. group->setChangedCallback([=](int value) {
  92. if (value != kCustom) {
  93. durationChanged(value);
  94. } else {
  95. timeInput->setFocusFast();
  96. }
  97. });
  98. rpl::merge(
  99. boxClosing() | rpl::filter(
  100. [=] { return group->current() == kCustom; }
  101. ),
  102. timeInput->submitRequests()
  103. ) | rpl::start_with_next([=] {
  104. if (const auto result = collect()) {
  105. durationChanged(result);
  106. } else {
  107. timeInput->showError();
  108. }
  109. }, lifetime());
  110. const auto timeInputBottom = timeInput->y() + timeInput->height();
  111. setDimensions(
  112. st::autolockWidth,
  113. st::boxOptionListPadding.top()
  114. + (timeInputBottom - _options.back()->bottomNoMargins())
  115. + count * _options.back()->heightNoMargins()
  116. + (count - 1) * st::boxOptionListSkip
  117. + st::boxOptionListPadding.bottom()
  118. + st::boxPadding.bottom());
  119. }
  120. void AutoLockBox::durationChanged(int seconds) {
  121. if (Core::App().settings().autoLock() == seconds) {
  122. closeBox();
  123. return;
  124. }
  125. Core::App().settings().setAutoLock(seconds);
  126. Core::App().saveSettingsDelayed();
  127. Core::App().checkAutoLock(crl::now());
  128. closeBox();
  129. }