mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-05 06:33:57 +02:00
Added ability to set custom auto-lock timer.
This commit is contained in:
parent
b55383efe7
commit
b3622b413e
5 changed files with 136 additions and 27 deletions
|
@ -577,6 +577,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
"lng_passcode_autolock_minutes#other" = "{count} minutes";
|
"lng_passcode_autolock_minutes#other" = "{count} minutes";
|
||||||
"lng_passcode_autolock_hours#one" = "{count} hour";
|
"lng_passcode_autolock_hours#one" = "{count} hour";
|
||||||
"lng_passcode_autolock_hours#other" = "{count} hours";
|
"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_old" = "Enter current passcode";
|
||||||
"lng_passcode_enter_first" = "Enter a passcode";
|
"lng_passcode_enter_first" = "Enter a passcode";
|
||||||
"lng_passcode_enter_new" = "Enter new passcode";
|
"lng_passcode_enter_new" = "Enter new passcode";
|
||||||
|
|
|
@ -7,41 +7,138 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
*/
|
*/
|
||||||
#include "boxes/auto_lock_box.h"
|
#include "boxes/auto_lock_box.h"
|
||||||
|
|
||||||
#include "lang/lang_keys.h"
|
|
||||||
#include "storage/localstorage.h"
|
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
#include "mainwindow.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "ui/widgets/checkbox.h"
|
#include "ui/widgets/checkbox.h"
|
||||||
|
#include "ui/widgets/time_input.h"
|
||||||
#include "styles/style_layers.h"
|
#include "styles/style_layers.h"
|
||||||
#include "styles/style_boxes.h"
|
#include "styles/style_boxes.h"
|
||||||
|
|
||||||
AutoLockBox::AutoLockBox(QWidget*, not_null<Main::Session*> session)
|
namespace {
|
||||||
: _session(session) {
|
|
||||||
|
constexpr auto kCustom = std::numeric_limits<int>::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() {
|
void AutoLockBox::prepare() {
|
||||||
setTitle(tr::lng_passcode_autolock());
|
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<Ui::RadiobuttonGroup>(
|
const auto group = std::make_shared<Ui::RadiobuttonGroup>(
|
||||||
Core::App().settings().autoLock());
|
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 y = st::boxOptionListPadding.top() + st::autolockButton.margin.top();
|
||||||
auto count = int(options.size());
|
const auto count = int(kOptions.size());
|
||||||
_options.reserve(count);
|
_options.reserve(count);
|
||||||
for (auto seconds : options) {
|
for (const auto seconds : kOptions) {
|
||||||
_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);
|
const auto text = [&] {
|
||||||
_options.back()->moveToLeft(st::boxPadding.left() + st::boxOptionListPadding.left(), y);
|
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;
|
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<Ui::TimeInput>(
|
||||||
|
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) {
|
void AutoLockBox::durationChanged(int seconds) {
|
||||||
|
if (Core::App().settings().autoLock() == seconds) {
|
||||||
|
closeBox();
|
||||||
|
return;
|
||||||
|
}
|
||||||
Core::App().settings().setAutoLock(seconds);
|
Core::App().settings().setAutoLock(seconds);
|
||||||
Core::App().saveSettingsDelayed();
|
Core::App().saveSettingsDelayed();
|
||||||
|
|
||||||
|
|
|
@ -9,17 +9,13 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
|
|
||||||
#include "boxes/abstract_box.h"
|
#include "boxes/abstract_box.h"
|
||||||
|
|
||||||
namespace Main {
|
|
||||||
class Session;
|
|
||||||
} // namespace Main
|
|
||||||
|
|
||||||
namespace Ui {
|
namespace Ui {
|
||||||
class Radiobutton;
|
class Radiobutton;
|
||||||
} // namespace Ui
|
} // namespace Ui
|
||||||
|
|
||||||
class AutoLockBox : public Ui::BoxContent {
|
class AutoLockBox : public Ui::BoxContent {
|
||||||
public:
|
public:
|
||||||
AutoLockBox(QWidget*, not_null<Main::Session*> session);
|
AutoLockBox(QWidget*);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void prepare() override;
|
void prepare() override;
|
||||||
|
@ -27,8 +23,6 @@ protected:
|
||||||
private:
|
private:
|
||||||
void durationChanged(int seconds);
|
void durationChanged(int seconds);
|
||||||
|
|
||||||
const not_null<Main::Session*> _session;
|
|
||||||
|
|
||||||
std::vector<object_ptr<Ui::Radiobutton>> _options;
|
std::vector<object_ptr<Ui::Radiobutton>> _options;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -965,3 +965,11 @@ scheduleTimeSeparatorPadding: margins(2px, 0px, 2px, 0px);
|
||||||
boxAttentionDividerLabel: FlatLabel(boxDividerLabel) {
|
boxAttentionDividerLabel: FlatLabel(boxDividerLabel) {
|
||||||
textFg: boxTextFgError;
|
textFg: boxTextFgError;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
autolockDateField: InputField(scheduleDateField) {
|
||||||
|
heightMin: 22px;
|
||||||
|
}
|
||||||
|
autolockTimeField: InputField(scheduleTimeField) {
|
||||||
|
heightMin: 20px;
|
||||||
|
}
|
||||||
|
autolockTimeWidth: 52px;
|
||||||
|
|
|
@ -292,15 +292,25 @@ void SetupLocalPasscode(
|
||||||
rpl::empty_value()
|
rpl::empty_value()
|
||||||
) | rpl::map([] {
|
) | rpl::map([] {
|
||||||
const auto autolock = Core::App().settings().autoLock();
|
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::lng_passcode_autolock_minutes(
|
||||||
tr::now,
|
tr::now,
|
||||||
lt_count,
|
lt_count,
|
||||||
autolock / 60)
|
minutes)
|
||||||
: tr::lng_passcode_autolock_hours(
|
: tr::lng_passcode_autolock_hours(
|
||||||
tr::now,
|
tr::now,
|
||||||
lt_count,
|
lt_count,
|
||||||
autolock / 3600);
|
hours);
|
||||||
});
|
});
|
||||||
|
|
||||||
AddButtonWithLabel(
|
AddButtonWithLabel(
|
||||||
|
@ -309,8 +319,7 @@ void SetupLocalPasscode(
|
||||||
std::move(value),
|
std::move(value),
|
||||||
st::settingsButton
|
st::settingsButton
|
||||||
)->addClickHandler([=] {
|
)->addClickHandler([=] {
|
||||||
const auto box = controller->show(
|
const auto box = controller->show(Box<AutoLockBox>());
|
||||||
Box<AutoLockBox>(&controller->session()));
|
|
||||||
box->boxClosing(
|
box->boxClosing(
|
||||||
) | rpl::start_to_stream(*autoLockBoxClosing, box->lifetime());
|
) | rpl::start_to_stream(*autoLockBoxClosing, box->lifetime());
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Reference in a new issue