From 5d24495704b5d2e4af2f9c8d982a9aaa5fe4ea6a Mon Sep 17 00:00:00 2001 From: Jonathan White Date: Sat, 12 Oct 2024 16:17:12 -0400 Subject: [PATCH] Hide the menubar when menus lose focus (if toggled off) * Fixes #10768 * Also fix menubar toggling not working if Qt version is less than 5.15 --- src/gui/MainWindow.cpp | 54 ++++++++++++++++++++++++++++-------------- src/gui/MainWindow.h | 6 +++-- 2 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/gui/MainWindow.cpp b/src/gui/MainWindow.cpp index 1d9c9ba32f..6d0c510e0d 100644 --- a/src/gui/MainWindow.cpp +++ b/src/gui/MainWindow.cpp @@ -553,14 +553,12 @@ MainWindow::MainWindow() connect(osUtils, &OSUtilsBase::statusbarThemeChanged, this, &MainWindow::updateTrayIcon); -#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) - // Install event filter for empty-area drag + // Install event filter for empty-area drag and menubar toggle auto* eventFilter = new MainWindowEventFilter(this); m_ui->menubar->installEventFilter(eventFilter); m_ui->toolBar->installEventFilter(eventFilter); m_ui->tabWidget->tabBar()->installEventFilter(eventFilter); installEventFilter(eventFilter); -#endif #ifdef Q_OS_MACOS setUnifiedTitleAndToolBarOnMac(true); @@ -2119,11 +2117,29 @@ void MainWindow::initActionCollection() QTimer::singleShot(1, ac, &ActionCollection::restoreShortcuts); } -#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) - MainWindowEventFilter::MainWindowEventFilter(QObject* parent) : QObject(parent) { + m_altCoolDown.setInterval(250); + m_altCoolDown.setSingleShot(true); + + m_menubarTimer.setInterval(250); + m_menubarTimer.setSingleShot(false); + connect(&m_menubarTimer, &QTimer::timeout, this, [this] { + auto mainwindow = getMainWindow(); + if (mainwindow && mainwindow->m_ui->menubar->isVisible() && config()->get(Config::GUI_HideMenubar).toBool()) { + // If the menu bar is visible with no active menu, hide it + if (!mainwindow->m_ui->menubar->activeAction()) { + mainwindow->m_ui->menubar->setVisible(false); + m_altCoolDown.start(); + m_menubarTimer.stop(); + } + // Conditions to hide the menubar or stop the timer have not been met + return; + } + // We no longer need the timer + m_menubarTimer.stop(); + }); } /** @@ -2139,6 +2155,8 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event) auto eventType = event->type(); if (eventType == QEvent::MouseButtonPress) { +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) + // startSystemMove was introduced in Qt 5.15 auto mouseEvent = dynamic_cast(event); if (watched == mainWindow->m_ui->menubar) { if (!mainWindow->m_ui->menubar->actionAt(mouseEvent->pos())) { @@ -2156,22 +2174,22 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event) return true; } } - } else if (eventType == QEvent::KeyRelease) { - if (watched == mainWindow) { - auto keyEvent = dynamic_cast(event); - if (keyEvent->key() == Qt::Key_Alt && !keyEvent->modifiers() - && config()->get(Config::GUI_HideMenubar).toBool()) { - auto menubar = mainWindow->m_ui->menubar; - menubar->setVisible(!menubar->isVisible()); - if (menubar->isVisible()) { - menubar->setActiveAction(mainWindow->m_ui->menuFile->menuAction()); - } - return false; +#endif + } else if (eventType == QEvent::KeyRelease && watched == mainWindow) { + auto keyEvent = dynamic_cast(event); + if (keyEvent->key() == Qt::Key_Alt && !keyEvent->modifiers() && config()->get(Config::GUI_HideMenubar).toBool() + && !m_altCoolDown.isActive()) { + auto menubar = mainWindow->m_ui->menubar; + menubar->setVisible(!menubar->isVisible()); + if (menubar->isVisible()) { + menubar->setActiveAction(mainWindow->m_ui->menuFile->menuAction()); + m_menubarTimer.start(); + } else { + m_menubarTimer.stop(); } + return true; } } return QObject::eventFilter(watched, event); } - -#endif diff --git a/src/gui/MainWindow.h b/src/gui/MainWindow.h index 578d6a91f8..51a9f79424 100644 --- a/src/gui/MainWindow.h +++ b/src/gui/MainWindow.h @@ -206,7 +206,6 @@ private slots: friend class MainWindowEventFilter; }; -#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) class MainWindowEventFilter : public QObject { Q_OBJECT @@ -214,8 +213,11 @@ class MainWindowEventFilter : public QObject public: explicit MainWindowEventFilter(QObject* parent); bool eventFilter(QObject* watched, QEvent* event) override; + +private: + QTimer m_menubarTimer; + QTimer m_altCoolDown; }; -#endif /** * Return instance of MainWindow created on app load