From b3622b413e86c92ff3976bd9b8453111034a03d0 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Wed, 11 Aug 2021 08:58:11 +0300 Subject: [PATCH] Added ability to set custom auto-lock timer. --- Telegram/Resources/langs/lang.strings | 1 + Telegram/SourceFiles/boxes/auto_lock_box.cpp | 127 +++++++++++++++--- Telegram/SourceFiles/boxes/auto_lock_box.h | 8 +- Telegram/SourceFiles/boxes/boxes.style | 8 ++ .../settings/settings_privacy_security.cpp | 19 ++- 5 files changed, 136 insertions(+), 27 deletions(-) diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 559323395..7a78cd6b4 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -577,6 +577,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "lng_passcode_autolock_minutes#other" = "{count} minutes"; "lng_passcode_autolock_hours#one" = "{count} hour"; "lng_passcode_autolock_hours#other" = "{count} hours"; +"lng_passcode_autolock_hours_minutes" = "{hours_count}h {minutes_count}m"; "lng_passcode_enter_old" = "Enter current passcode"; "lng_passcode_enter_first" = "Enter a passcode"; "lng_passcode_enter_new" = "Enter new passcode"; diff --git a/Telegram/SourceFiles/boxes/auto_lock_box.cpp b/Telegram/SourceFiles/boxes/auto_lock_box.cpp index 77ef64b2f..356ac7f21 100644 --- a/Telegram/SourceFiles/boxes/auto_lock_box.cpp +++ b/Telegram/SourceFiles/boxes/auto_lock_box.cpp @@ -7,41 +7,138 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #include "boxes/auto_lock_box.h" -#include "lang/lang_keys.h" -#include "storage/localstorage.h" #include "core/application.h" -#include "mainwindow.h" +#include "lang/lang_keys.h" #include "ui/widgets/checkbox.h" +#include "ui/widgets/time_input.h" #include "styles/style_layers.h" #include "styles/style_boxes.h" -AutoLockBox::AutoLockBox(QWidget*, not_null session) -: _session(session) { +namespace { + +constexpr auto kCustom = std::numeric_limits::max(); +constexpr auto kOptions = { 60, 300, 3600, 18000, kCustom }; +constexpr auto kDefaultCustom = "10:00"_cs; + +auto TimeString(int seconds) { + const auto hours = seconds / 3600; + const auto minutes = (seconds - hours * 3600) / 60; + return QString("%1:%2").arg(hours).arg(minutes, 2, 10, QLatin1Char('0')); +} + +} // namespace + +AutoLockBox::AutoLockBox(QWidget*) { } void AutoLockBox::prepare() { setTitle(tr::lng_passcode_autolock()); - addButton(tr::lng_box_ok(), [this] { closeBox(); }); + addButton(tr::lng_box_ok(), [=] { closeBox(); }); - auto options = { 60, 300, 3600, 18000 }; + const auto currentTime = Core::App().settings().autoLock(); - auto group = std::make_shared( - Core::App().settings().autoLock()); + const auto group = std::make_shared( + ranges::contains(kOptions, currentTime) ? currentTime : kCustom); + + const auto x = st::boxPadding.left() + st::boxOptionListPadding.left(); auto y = st::boxOptionListPadding.top() + st::autolockButton.margin.top(); - auto count = int(options.size()); + const auto count = int(kOptions.size()); _options.reserve(count); - for (auto seconds : options) { - _options.emplace_back(this, group, seconds, (seconds % 3600) ? tr::lng_passcode_autolock_minutes(tr::now, lt_count, seconds / 60) : tr::lng_passcode_autolock_hours(tr::now, lt_count, seconds / 3600), st::autolockButton); - _options.back()->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), y); + for (const auto seconds : kOptions) { + const auto text = [&] { + if (seconds == kCustom) { + return QString(); + } + const auto minutes = (seconds % 3600); + return (minutes + ? tr::lng_passcode_autolock_minutes + : tr::lng_passcode_autolock_hours)( + tr::now, + lt_count, + minutes ? (seconds / 60) : (seconds / 3600)); + }(); + _options.emplace_back( + this, + group, + seconds, + text, + st::autolockButton); + _options.back()->moveToLeft(x, y); y += _options.back()->heightNoMargins() + st::boxOptionListSkip; } - group->setChangedCallback([this](int value) { durationChanged(value); }); - setDimensions(st::autolockWidth, st::boxOptionListPadding.top() + count * _options.back()->heightNoMargins() + (count - 1) * st::boxOptionListSkip + st::boxOptionListPadding.bottom() + st::boxPadding.bottom()); + const auto timeInput = [&] { + const auto &last = _options.back(); + const auto &st = st::autolockButton; + + const auto textLeft = st.checkPosition.x() + + last->checkRect().width() + + st.textPosition.x(); + const auto textTop = st.margin.top() + st.textPosition.y(); + + const auto timeInput = CreateChild( + this, + (group->value() == kCustom) + ? TimeString(currentTime) + : kDefaultCustom.utf8(), + st::autolockTimeField, + st::autolockDateField, + st::scheduleTimeSeparator, + st::scheduleTimeSeparatorPadding); + timeInput->resizeToWidth(st::autolockTimeWidth); + timeInput->moveToLeft(last->x() + textLeft, last->y() + textTop); + return timeInput; + }(); + + const auto collect = [=] { + const auto timeValue = timeInput->valueCurrent().split(':'); + if (timeValue.size() != 2) { + return 0; + } + return timeValue[0].toInt() * 3600 + timeValue[1].toInt() * 60; + }; + + timeInput->focuses( + ) | rpl::start_with_next([=] { + group->setValue(kCustom); + }, lifetime()); + + group->setChangedCallback([=](int value) { + if (value != kCustom) { + durationChanged(value); + } else { + timeInput->setFocusFast(); + } + }); + + rpl::merge( + boxClosing() | rpl::filter([=] { return group->value() == kCustom; }), + timeInput->submitRequests() + ) | rpl::start_with_next([=] { + if (const auto result = collect()) { + durationChanged(result); + } else { + timeInput->showError(); + } + }, lifetime()); + + const auto timeInputBottom = timeInput->y() + timeInput->height(); + setDimensions( + st::autolockWidth, + st::boxOptionListPadding.top() + + (timeInputBottom - _options.back()->bottomNoMargins()) + + count * _options.back()->heightNoMargins() + + (count - 1) * st::boxOptionListSkip + + st::boxOptionListPadding.bottom() + + st::boxPadding.bottom()); } void AutoLockBox::durationChanged(int seconds) { + if (Core::App().settings().autoLock() == seconds) { + closeBox(); + return; + } Core::App().settings().setAutoLock(seconds); Core::App().saveSettingsDelayed(); diff --git a/Telegram/SourceFiles/boxes/auto_lock_box.h b/Telegram/SourceFiles/boxes/auto_lock_box.h index 13cd573d5..b20cbfa2b 100644 --- a/Telegram/SourceFiles/boxes/auto_lock_box.h +++ b/Telegram/SourceFiles/boxes/auto_lock_box.h @@ -9,17 +9,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "boxes/abstract_box.h" -namespace Main { -class Session; -} // namespace Main - namespace Ui { class Radiobutton; } // namespace Ui class AutoLockBox : public Ui::BoxContent { public: - AutoLockBox(QWidget*, not_null session); + AutoLockBox(QWidget*); protected: void prepare() override; @@ -27,8 +23,6 @@ protected: private: void durationChanged(int seconds); - const not_null _session; - std::vector> _options; }; diff --git a/Telegram/SourceFiles/boxes/boxes.style b/Telegram/SourceFiles/boxes/boxes.style index c963a5cd8..30a8b48e7 100644 --- a/Telegram/SourceFiles/boxes/boxes.style +++ b/Telegram/SourceFiles/boxes/boxes.style @@ -965,3 +965,11 @@ scheduleTimeSeparatorPadding: margins(2px, 0px, 2px, 0px); boxAttentionDividerLabel: FlatLabel(boxDividerLabel) { textFg: boxTextFgError; } + +autolockDateField: InputField(scheduleDateField) { + heightMin: 22px; +} +autolockTimeField: InputField(scheduleTimeField) { + heightMin: 20px; +} +autolockTimeWidth: 52px; diff --git a/Telegram/SourceFiles/settings/settings_privacy_security.cpp b/Telegram/SourceFiles/settings/settings_privacy_security.cpp index 5dbe34c9f..65d0e5df6 100644 --- a/Telegram/SourceFiles/settings/settings_privacy_security.cpp +++ b/Telegram/SourceFiles/settings/settings_privacy_security.cpp @@ -292,15 +292,25 @@ void SetupLocalPasscode( rpl::empty_value() ) | rpl::map([] { const auto autolock = Core::App().settings().autoLock(); - return (autolock % 3600) + const auto hours = autolock / 3600; + const auto minutes = (autolock - (hours * 3600)) / 60; + + return (hours && minutes) + ? tr::lng_passcode_autolock_hours_minutes( + tr::now, + lt_hours_count, + QString::number(hours), + lt_minutes_count, + QString::number(minutes)) + : minutes ? tr::lng_passcode_autolock_minutes( tr::now, lt_count, - autolock / 60) + minutes) : tr::lng_passcode_autolock_hours( tr::now, lt_count, - autolock / 3600); + hours); }); AddButtonWithLabel( @@ -309,8 +319,7 @@ void SetupLocalPasscode( std::move(value), st::settingsButton )->addClickHandler([=] { - const auto box = controller->show( - Box(&controller->session())); + const auto box = controller->show(Box()); box->boxClosing( ) | rpl::start_to_stream(*autoLockBoxClosing, box->lifetime()); });