mirror of
https://github.com/keepassxreboot/keepassxc.git
synced 2025-09-20 04:44:43 -04:00
Improve inactivity timer
* Fix #11957 * Prevent resetting the timer hundreds of times per second * Improve code flow for inactivity timer in general
This commit is contained in:
parent
8c7cc90363
commit
634a5b34f1
4 changed files with 26 additions and 36 deletions
|
@ -20,28 +20,28 @@
|
|||
#include <QCoreApplication>
|
||||
#include <QTimer>
|
||||
|
||||
namespace
|
||||
{
|
||||
// Minimum timeout is 10 seconds
|
||||
constexpr int MIN_TIMEOUT = 10000;
|
||||
} // namespace
|
||||
|
||||
InactivityTimer::InactivityTimer(QObject* parent)
|
||||
: QObject(parent)
|
||||
, m_timer(new QTimer(this))
|
||||
, m_active(false)
|
||||
{
|
||||
m_timer->setSingleShot(true);
|
||||
m_timer->setSingleShot(false);
|
||||
connect(m_timer, SIGNAL(timeout()), SLOT(timeout()));
|
||||
}
|
||||
|
||||
void InactivityTimer::setInactivityTimeout(int inactivityTimeout)
|
||||
{
|
||||
Q_ASSERT(inactivityTimeout > 0);
|
||||
|
||||
m_timer->setInterval(inactivityTimeout);
|
||||
}
|
||||
|
||||
void InactivityTimer::activate()
|
||||
void InactivityTimer::activate(int inactivityTimeout)
|
||||
{
|
||||
if (!m_active) {
|
||||
qApp->installEventFilter(this);
|
||||
}
|
||||
m_active = true;
|
||||
m_resetBlocked = false;
|
||||
m_timer->setInterval(qMax(MIN_TIMEOUT, inactivityTimeout));
|
||||
m_timer->start();
|
||||
}
|
||||
|
||||
|
@ -54,12 +54,15 @@ void InactivityTimer::deactivate()
|
|||
|
||||
bool InactivityTimer::eventFilter(QObject* watched, QEvent* event)
|
||||
{
|
||||
const QEvent::Type type = event->type();
|
||||
const auto type = event->type();
|
||||
// clang-format off
|
||||
if ((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease)
|
||||
|| (type >= QEvent::HoverEnter && type <= QEvent::HoverMove)
|
||||
|| (type == QEvent::Wheel)) {
|
||||
if (!m_resetBlocked &&
|
||||
((type >= QEvent::MouseButtonPress && type <= QEvent::KeyRelease) ||
|
||||
(type >= QEvent::HoverEnter && type <= QEvent::HoverMove) ||
|
||||
type == QEvent::Wheel)) {
|
||||
m_timer->start();
|
||||
m_resetBlocked = true;
|
||||
QTimer::singleShot(500, this, [this]() { m_resetBlocked = false; });
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
|
@ -73,7 +76,7 @@ void InactivityTimer::timeout()
|
|||
return;
|
||||
}
|
||||
|
||||
if (m_active && !m_timer->isActive()) {
|
||||
if (m_active) {
|
||||
emit inactivityDetected();
|
||||
}
|
||||
|
||||
|
|
|
@ -29,8 +29,7 @@ class InactivityTimer : public QObject
|
|||
|
||||
public:
|
||||
explicit InactivityTimer(QObject* parent = nullptr);
|
||||
void setInactivityTimeout(int inactivityTimeout);
|
||||
void activate();
|
||||
void activate(int inactivityTimeout);
|
||||
void deactivate();
|
||||
|
||||
signals:
|
||||
|
@ -44,7 +43,8 @@ private slots:
|
|||
|
||||
private:
|
||||
QTimer* m_timer;
|
||||
bool m_active;
|
||||
bool m_active = false;
|
||||
bool m_resetBlocked = false;
|
||||
QMutex m_emitMutx;
|
||||
};
|
||||
|
||||
|
|
|
@ -273,7 +273,7 @@ MainWindow::MainWindow()
|
|||
m_ui->actionAllowScreenCapture->setVisible(osUtils->canPreventScreenCapture());
|
||||
|
||||
m_inactivityTimer = new InactivityTimer(this);
|
||||
connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockDatabasesAfterInactivity()));
|
||||
connect(m_inactivityTimer, SIGNAL(inactivityDetected()), this, SLOT(lockAllDatabases()));
|
||||
applySettingsChanges();
|
||||
|
||||
// Qt 5.10 introduced a new "feature" to hide shortcuts in context menus
|
||||
|
@ -1656,14 +1656,9 @@ void MainWindow::showGroupContextMenu(const QPoint& globalPos)
|
|||
|
||||
void MainWindow::applySettingsChanges()
|
||||
{
|
||||
int timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000;
|
||||
if (timeout <= 0) {
|
||||
timeout = 60;
|
||||
}
|
||||
|
||||
m_inactivityTimer->setInactivityTimeout(timeout);
|
||||
if (config()->get(Config::Security_LockDatabaseIdle).toBool()) {
|
||||
m_inactivityTimer->activate();
|
||||
auto timeout = config()->get(Config::Security_LockDatabaseIdleSeconds).toInt() * 1000;
|
||||
m_inactivityTimer->activate(timeout);
|
||||
} else {
|
||||
m_inactivityTimer->deactivate();
|
||||
}
|
||||
|
@ -1833,13 +1828,6 @@ void MainWindow::closeModalWindow()
|
|||
}
|
||||
}
|
||||
|
||||
void MainWindow::lockDatabasesAfterInactivity()
|
||||
{
|
||||
if (!m_ui->tabWidget->lockDatabases()) {
|
||||
m_inactivityTimer->activate();
|
||||
}
|
||||
}
|
||||
|
||||
bool MainWindow::isTrayIconEnabled() const
|
||||
{
|
||||
return m_trayIcon && m_trayIcon->isVisible();
|
||||
|
@ -1894,7 +1882,7 @@ void MainWindow::bringToFront()
|
|||
void MainWindow::handleScreenLock()
|
||||
{
|
||||
if (config()->get(Config::Security_LockDatabaseScreenLock).toBool()) {
|
||||
lockDatabasesAfterInactivity();
|
||||
lockAllDatabases();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1944,7 +1932,7 @@ void MainWindow::closeAllDatabases()
|
|||
|
||||
void MainWindow::lockAllDatabases()
|
||||
{
|
||||
lockDatabasesAfterInactivity();
|
||||
m_ui->tabWidget->lockDatabases();
|
||||
}
|
||||
|
||||
void MainWindow::displayDesktopNotification(const QString& msg, QString title, int msTimeoutHint)
|
||||
|
|
|
@ -140,7 +140,6 @@ private slots:
|
|||
void applySettingsChanges();
|
||||
void trayIconTriggered(QSystemTrayIcon::ActivationReason reason);
|
||||
void processTrayIconTrigger();
|
||||
void lockDatabasesAfterInactivity();
|
||||
void handleScreenLock();
|
||||
void showErrorMessage(const QString& message);
|
||||
void selectNextDatabaseTab();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue