Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide the menubar when menus lose focus (if toggled off) #11355

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(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_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();
});
}

/**
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