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
This commit is contained in:
Jonathan White 2024-10-12 16:17:12 -04:00
parent 34fe413dad
commit 5d24495704
2 changed files with 40 additions and 20 deletions

View File

@ -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<QMouseEvent*>(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<QKeyEvent*>(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<QKeyEvent*>(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

View File

@ -206,7 +206,6 @@ private:
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