QMenubar option to show/hide itself (#10341)

--------

Co-authored-by: Mikko Saarinki <mikko.saarinki@michaelkingston.fi>
Co-authored-by: Jonathan White <support@dmapps.us>
This commit is contained in:
Jonathan White 2024-04-27 23:27:19 -04:00
parent d8006e0b76
commit 775efc65ed
No known key found for this signature in database
GPG key ID: 440FC65F2E0C6E01
5 changed files with 61 additions and 10 deletions

View file

@ -583,6 +583,7 @@ MainWindow::MainWindow()
m_ui->menubar->installEventFilter(eventFilter);
m_ui->toolBar->installEventFilter(eventFilter);
m_ui->tabWidget->tabBar()->installEventFilter(eventFilter);
installEventFilter(eventFilter);
#endif
#ifdef Q_OS_MACOS
@ -1675,6 +1676,7 @@ void MainWindow::applySettingsChanges()
m_inactivityTimer->deactivate();
}
m_ui->menubar->setHidden(config()->get(Config::GUI_HideMenubar).toBool());
m_ui->toolBar->setHidden(config()->get(Config::GUI_HideToolbar).toBool());
auto movable = config()->get(Config::GUI_MovableToolbar).toBool();
m_ui->toolBar->setMovable(movable);
@ -2037,6 +2039,16 @@ void MainWindow::initViewMenu()
}
});
#ifdef Q_OS_MACOS
m_ui->actionShowMenubar->setVisible(false);
#else
m_ui->actionShowMenubar->setChecked(!config()->get(Config::GUI_HideMenubar).toBool());
connect(m_ui->actionShowMenubar, &QAction::toggled, this, [this](bool checked) {
config()->set(Config::GUI_HideMenubar, !checked);
applySettingsChanges();
});
#endif
m_ui->actionShowToolbar->setChecked(!config()->get(Config::GUI_HideToolbar).toBool());
connect(m_ui->actionShowToolbar, &QAction::toggled, this, [this](bool checked) {
config()->set(Config::GUI_HideToolbar, !checked);
@ -2080,6 +2092,7 @@ MainWindowEventFilter::MainWindowEventFilter(QObject* parent)
/**
* MainWindow event filter to initiate empty-area drag on the toolbar, menubar, and tabbar.
* Also shows menubar with Alt when menubar itself is hidden.
*/
bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
{
@ -2088,10 +2101,11 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
return QObject::eventFilter(watched, event);
}
if (event->type() == QEvent::MouseButtonPress) {
auto eventType = event->type();
if (eventType == QEvent::MouseButtonPress) {
auto mouseEvent = dynamic_cast<QMouseEvent*>(event);
if (watched == mainWindow->m_ui->menubar) {
auto* m = static_cast<QMouseEvent*>(event);
if (!mainWindow->m_ui->menubar->actionAt(m->pos())) {
if (!mainWindow->m_ui->menubar->actionAt(mouseEvent->pos())) {
mainWindow->windowHandle()->startSystemMove();
return false;
}
@ -2101,12 +2115,24 @@ bool MainWindowEventFilter::eventFilter(QObject* watched, QEvent* event)
return false;
}
} else if (watched == mainWindow->m_ui->tabWidget->tabBar()) {
auto* m = static_cast<QMouseEvent*>(event);
if (mainWindow->m_ui->tabWidget->tabBar()->tabAt(m->pos()) == -1) {
if (mainWindow->m_ui->tabWidget->tabBar()->tabAt(mouseEvent->pos()) == -1) {
mainWindow->windowHandle()->startSystemMove();
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;
}
}
}
return QObject::eventFilter(watched, event);