From ab82358dcb327a1a8ee19e102ce79c260e4f1241 Mon Sep 17 00:00:00 2001 From: Kenneth Chew Date: Fri, 8 Apr 2022 16:21:52 -0400 Subject: [PATCH] Show and hide the menu bar with the 'alt' key Only applicable for systems without a native menu bar (i.e. almost anything that is not macOS or Ubuntu Unity). On these systems, the menu bar appears on top of the window, which does not look good next to the tool bar already up there. When the menu bar is hidden, the keyboard shortcuts set by the menu bar are disabled. They should always work, so this also adds a workaround for that. --- launcher/ui/MainWindow.cpp | 59 ++++++++++++++++++++++++++++++++++++++ launcher/ui/MainWindow.h | 4 +++ 2 files changed, 63 insertions(+) diff --git a/launcher/ui/MainWindow.cpp b/launcher/ui/MainWindow.cpp index 45c19ca4..b98cd8b7 100644 --- a/launcher/ui/MainWindow.cpp +++ b/launcher/ui/MainWindow.cpp @@ -527,6 +527,7 @@ public: MainWindow->setMenuBar(menuBar); } + // If a keyboard shortcut is changed here, it must be changed below in keyPressEvent as well void createMenuActions(MainWindow *MainWindow) { newAct = new QAction(tr("&New Instance..."), MainWindow); @@ -1096,6 +1097,64 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow retranslateUi(); } +// macOS always has a native menu bar, so these fixes are not applicable +// Other systems may or may not have a native menu bar (most do not - it seems like only Ubuntu Unity does) +#ifdef Q_OS_MAC +void MainWindow::keyReleaseEvent(QKeyEvent *event) +{ + if(event->key()==Qt::Key_Alt) + ui->menuBar->setVisible(!ui->menuBar->isVisible()); +} + +// FIXME: This is a hack because keyboard shortcuts do nothing while menu bar is hidden on systems without native menu bar +// If a keyboard shortcut is changed above in `createMenuActions`, it must be changed here as well +void MainWindow::keyPressEvent(QKeyEvent *event) +{ + if(ui->menuBar->isVisible() || ui->menuBar->isNativeMenuBar()) + return; // let the menu bar handle the keyboard shortcuts + + if(event->modifiers().testFlag(Qt::ControlModifier)) + { + switch(event->key()) + { + case Qt::Key_N: + on_actionAddInstance_triggered(); + return; + case Qt::Key_O: + if(event->modifiers().testFlag(Qt::ShiftModifier)) + on_actionLaunchInstanceOffline_triggered(); + else + on_actionLaunchInstance_triggered(); + return; + case Qt::Key_I: + on_actionEditInstance_triggered(); + return; + case Qt::Key_G: + on_actionChangeInstGroup_triggered(); + return; + case Qt::Key_M: + on_actionViewSelectedMCFolder_triggered(); + return; + case Qt::Key_E: + on_actionExportInstance_triggered(); + return; + case Qt::Key_Delete: + on_actionDeleteInstance_triggered(); + return; + case Qt::Key_D: + on_actionCopyInstance_triggered(); + return; + case Qt::Key_W: + close(); + return; + // Text editing shortcuts are handled by the OS, so they do not need to be implemented here again + default: + return; + } + } +} +#endif + void MainWindow::retranslateUi() { auto accounts = APPLICATION->accounts(); diff --git a/launcher/ui/MainWindow.h b/launcher/ui/MainWindow.h index bd1a596c..4ce4b7db 100644 --- a/launcher/ui/MainWindow.h +++ b/launcher/ui/MainWindow.h @@ -186,6 +186,10 @@ private slots: void globalSettingsClosed(); + void keyReleaseEvent(QKeyEvent *event) override; + + void keyPressEvent(QKeyEvent *event) override; + private: void retranslateUi();