Skip to content

Commit

Permalink
Hide the menubar when menus lose focus (if toggled off)
Browse files Browse the repository at this point in the history
* Fixes #10768
* Also fix menubar toggling not working if Qt version is less than 5.15
  • Loading branch information
droidmonkey committed Oct 12, 2024
1 parent d57d167 commit 7193d5a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
30 changes: 23 additions & 7 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -2113,11 +2111,25 @@ 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_menubarTimer.setInterval(1000);
m_menubarTimer.setSingleShot(false);
connect(&m_menubarTimer, &QTimer::timeout, this, [this] {
if (!config()->get(Config::GUI_HideMenubar).toBool()) {
// Early out reset if the menubar is toggled on
m_menubarTimer.stop();
return;
}

auto mainwindow = getMainWindow();
if (mainwindow && mainwindow->m_ui->menubar->isVisible() && !mainwindow->m_ui->menubar->activeAction()) {
// The menu bar is visible with no active menu, hide it
mainwindow->m_ui->menubar->setVisible(false);
m_menubarTimer.stop();
}
});
}

/**
Expand All @@ -2133,6 +2145,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())) {
Expand All @@ -2150,6 +2164,7 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
return true;
}
}
#endif
} else if (eventType == QEvent::KeyRelease) {
if (watched == mainWindow) {
auto keyEvent = dynamic_cast<QKeyEvent*>(event);
Expand All @@ -2159,6 +2174,9 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
menubar->setVisible(!menubar->isVisible());
if (menubar->isVisible()) {
menubar->setActiveAction(mainWindow->m_ui->menuFile->menuAction());
m_menubarTimer.start();
} else {
m_menubarTimer.stop();
}
return false;
}
Expand All @@ -2167,5 +2185,3 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)

return QObject::eventFilter(watched, event);
}

#endif
5 changes: 3 additions & 2 deletions src/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,17 @@ private slots:
friend class MainWindowEventFilter;
};

#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
class MainWindowEventFilter : public QObject
{
Q_OBJECT

public:
explicit MainWindowEventFilter(QObject* parent);
bool eventFilter(QObject* watched, QEvent* event) override;

private:
QTimer m_menubarTimer;
};
#endif

/**
* Return instance of MainWindow created on app load
Expand Down

0 comments on commit 7193d5a

Please sign in to comment.