Correct issues with hiding and minimizing the MainWindow

The GUI features depend on windowing system used, not just OS.

There is an issue with the WM sometimes producing an event that keepassxc interprets as request to hide the main window just after it is shown. A workaround with immediately firing a timer was implemented. However, there is no guarantee on execution ordering of the timer callback and other application code. Remove the timer and override
show() and hide() on main window to only hide if the window has not been shown recently.

The user can set an option to hide window instead of minimizing when tray icon is enabled. This is not honored in most places where the main windows is minimized. Fix it. This also allows using the tray icon as a workaround for minimization not working under some circumstances in X11.

Signed-off-by: Michal Suchanek <[email protected]>
This commit is contained in:
Michal Suchanek authored and Jonathan White committed 2020-02-27 23:25:44 -05:00
1 parent eb88b8cc0c
commit b9daed2055
7 files changed
+67 -19

No files matched your search

+48 -12
View File
@@ -294,7 +294,7 @@ MainWindow::MainWindow()
connect(m_ui->menuGroups, SIGNAL(aboutToHide()), SLOT(releaseContextFocusLock()));
// Control window state
new QShortcut(Qt::CTRL + Qt::Key_M, this, SLOT(showMinimized()));
new QShortcut(Qt::CTRL + Qt::Key_M, this, SLOT(minimizeOrHide()));
new QShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_M, this, SLOT(hideWindow()));
// Control database tabs
new QShortcut(Qt::CTRL + Qt::Key_Tab, this, SLOT(selectNextDatabaseTab()));
@@ -1072,7 +1072,7 @@ void MainWindow::changeEvent(QEvent* event)
if (isTrayIconEnabled() && m_trayIcon && m_trayIcon->isVisible()
&& config()->get("GUI/MinimizeToTray").toBool()) {
event->ignore();
QTimer::singleShot(0, this, SLOT(hide()));
hide();
}
if (config()->get("security/lockdatabaseminimize").toBool()) {
@@ -1245,7 +1245,7 @@ void MainWindow::applySettingsChanges()
void MainWindow::focusWindowChanged(QWindow* focusWindow)
{
if (focusWindow != windowHandle()) {
m_lastFocusOutTime = Clock::currentSecondsSinceEpoch();
m_lastFocusOutTime = Clock::currentMilliSecondsSinceEpoch();
}
}
@@ -1269,9 +1269,9 @@ void MainWindow::processTrayIconTrigger()
|| m_trayIconTriggerReason == QSystemTrayIcon::MiddleClick) {
// Toggle window if is not in front.
#ifdef Q_OS_WIN
// If on Windows, check if focus switched within the last second because
// If on Windows, check if focus switched within the 500 milliseconds since
// clicking the tray icon removes focus from main window.
if (isHidden() || (Clock::currentSecondsSinceEpoch() - m_lastFocusOutTime) <= 1) {
if (isHidden() || (Clock::currentMilliSecondsSinceEpoch() - m_lastFocusOutTime) <= 500) {
#else
// If on Linux or macOS, check if the window has focus.
if (hasFocus() || isHidden() || windowHandle()->isActive()) {
@@ -1283,16 +1283,43 @@ void MainWindow::processTrayIconTrigger()
}
}
void MainWindow::show()
{
#ifndef Q_OS_WIN
m_lastShowTime = Clock::currentMilliSecondsSinceEpoch();
#endif
QMainWindow::show();
}
bool MainWindow::shouldHide()
{
#ifndef Q_OS_WIN
qint64 current_time = Clock::currentMilliSecondsSinceEpoch();
if (current_time - m_lastShowTime < 50) {
return false;
}
#endif
return true;
}
void MainWindow::hide()
{
if (shouldHide()) {
QMainWindow::hide();
}
}
void MainWindow::hideWindow()
{
saveWindowInformation();
#if !defined(Q_OS_LINUX) && !defined(Q_OS_MACOS)
// On some Linux systems, the window should NOT be minimized and hidden (i.e. not shown), at
// the same time (which would happen if both minimize on startup and minimize to tray are set)
// since otherwise it causes problems on restore as seen on issue #1595. Hiding it is enough.
// TODO: Add an explanation for why this is also not done on Mac (or remove the check)
setWindowState(windowState() | Qt::WindowMinimized);
#endif
if (QGuiApplication::platformName() != "xcb") {
// In X11 the window should NOT be minimized and hidden (i.e. not
// shown) at the same time (which would happen if both minimize on
// startup and minimize to tray are set) since otherwise it causes
// problems on restore as seen on issue #1595. Hiding it is enough.
setWindowState(windowState() | Qt::WindowMinimized);
}
// Only hide if tray icon is active, otherwise window will be gone forever
if (isTrayIconEnabled()) {
hide();
@@ -1305,6 +1332,15 @@ void MainWindow::hideWindow()
}
}
void MainWindow::minimizeOrHide()
{
if (config()->get("GUI/MinimizeToTray").toBool()) {
hideWindow();
} else {
showMinimized();
}
}
void MainWindow::toggleWindow()
{
if (isVisible() && !isMinimized()) {