Skip to content

Commit 634a5b3

Browse files
committed
Improve inactivity timer
* Fix #11957 * Prevent resetting the timer hundreds of times per second * Improve code flow for inactivity timer in general
1 parent 8c7cc90 commit 634a5b3

File tree

4 files changed

+26
-36
lines changed

4 files changed

+26
-36
lines changed

src/core/InactivityTimer.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,28 @@
2020
#include <QCoreApplication>
2121
#include <QTimer>
2222

23+
namespace
24+
{
25+
// Minimum timeout is 10 seconds
26+
constexpr int MIN_TIMEOUT = 10000;
27+
} // namespace
28+
2329
InactivityTimer::InactivityTimer(QObject* parent)
2430
: QObject(parent)
2531
, m_timer(new QTimer(this))
26-
, m_active(false)
2732
{
28-
m_timer->setSingleShot(true);
33+
m_timer->setSingleShot(false);
2934
connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
3035
}
3136

32-
void InactivityTimer::setInactivityTimeout(int inactivityTimeout)
33-
{
34-
Q_ASSERT(inactivityTimeout > 0);
35-
36-
m_timer->setInterval(inactivityTimeout);
37-
}
38-
39-
void InactivityTimer::activate()
37+
void InactivityTimer::activate(int inactivityTimeout)
4038
{
4139
if (!m_active) {
4240
qApp->installEventFilter(this);
4341
}
4442
m_active = true;
43+
m_resetBlocked = false;
44+
m_timer->setInterval(qMax(MIN_TIMEOUT, inactivityTimeout));
4545
m_timer->start();
4646
}
4747

@@ -54,12 +54,15 @@ void InactivityTimer::deactivate()
5454

5555
bool InactivityTimer::eventFilter(QObject* watched, QEvent* event)
5656
{
57-
const QEvent::Type type = event->type();
57+
const auto type = event->type();
5858
// clang-format off
59-
if ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease)
60-
|| (type >= QEvent::HoverEnter && type <= QEvent::HoverMove)
61-
|| (type == QEvent::Wheel)) {
59+
if (!m_resetBlocked &&
60+
((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease) ||
61+
(type >= QEvent::HoverEnter && type <= QEvent::HoverMove) ||
62+
type == QEvent::Wheel)) {
6263
m_timer->start();
64+
m_resetBlocked = true;
65+
QTimer::singleShot(500, this, [this]() { m_resetBlocked = false; });
6366
}
6467
// clang-format on
6568

@@ -73,7 +76,7 @@ void InactivityTimer::timeout()
7376
return;
7477
}
7578

76-
if (m_active && !m_timer->isActive()) {
79+
if (m_active) {
7780
emit inactivityDetected();
7881
}
7982

src/core/InactivityTimer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class InactivityTimer : public QObject
2929

3030
public:
3131
explicit InactivityTimer(QObject* parent = nullptr);
32-
void setInactivityTimeout(int inactivityTimeout);
33-
void activate();
32+
void activate(int inactivityTimeout);
3433
void deactivate();
3534

3635
signals:
@@ -44,7 +43,8 @@ private slots:
4443

4544
private:
4645
QTimer* m_timer;
47-
bool m_active;
46+
bool m_active = false;
47+
bool m_resetBlocked = false;
4848
QMutex m_emitMutx;
4949
};
5050

src/gui/MainWindow.cpp

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ MainWindow::MainWindow()
273273
m_ui->actionAllowScreenCapture->setVisible(osUtils->canPreventScreenCapture());
274274

275275
m_inactivityTimer = new InactivityTimer(this);
276-
connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockDatabasesAfterInactivity()));
276+
connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockAllDatabases()));
277277
applySettingsChanges();
278278

279279
// Qt 5.10 introduced a new "feature" to hide shortcuts in context menus
@@ -1656,14 +1656,9 @@ void MainWindow::showGroupContextMenu(const QPoint& globalPos)
16561656

16571657
void MainWindow::applySettingsChanges()
16581658
{
1659-
int timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000;
1660-
if (timeout <= 0) {
1661-
timeout = 60;
1662-
}
1663-
1664-
m_inactivityTimer->setInactivityTimeout(timeout);
16651659
if (config()->get(Config::Security_LockDatabaseIdle).toBool()) {
1666-
m_inactivityTimer->activate();
1660+
auto timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000;
1661+
m_inactivityTimer->activate(timeout);
16671662
} else {
16681663
m_inactivityTimer->deactivate();
16691664
}
@@ -1833,13 +1828,6 @@ void MainWindow::closeModalWindow()
18331828
}
18341829
}
18351830

1836-
void MainWindow::lockDatabasesAfterInactivity()
1837-
{
1838-
if (!m_ui->tabWidget->lockDatabases()) {
1839-
m_inactivityTimer->activate();
1840-
}
1841-
}
1842-
18431831
bool MainWindow::isTrayIconEnabled() const
18441832
{
18451833
return m_trayIcon && m_trayIcon->isVisible();
@@ -1894,7 +1882,7 @@ void MainWindow::bringToFront()
18941882
void MainWindow::handleScreenLock()
18951883
{
18961884
if (config()->get(Config::Security_LockDatabaseScreenLock).toBool()) {
1897-
lockDatabasesAfterInactivity();
1885+
lockAllDatabases();
18981886
}
18991887
}
19001888

@@ -1944,7 +1932,7 @@ void MainWindow::closeAllDatabases()
19441932

19451933
void MainWindow::lockAllDatabases()
19461934
{
1947-
lockDatabasesAfterInactivity();
1935+
m_ui->tabWidget->lockDatabases();
19481936
}
19491937

19501938
void MainWindow::displayDesktopNotification(const QString& msg, QString title, int msTimeoutHint)

src/gui/MainWindow.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ private slots:
140140
void applySettingsChanges();
141141
void trayIconTriggered(QSystemTrayIcon::ActivationReason reason);
142142
void processTrayIconTrigger();
143-
void lockDatabasesAfterInactivity();
144143
void handleScreenLock();
145144
void showErrorMessage(const QString& message);
146145
void selectNextDatabaseTab();

0 commit comments

Comments
 (0)