Close the current window instead of the main window from the menu bar

Systems with native menu bars show the same menu bar for all child windows. As a result, you cannot assume that the menu bar's parent (the `MainWindow`) will be the window in focus.
This commit is contained in:
Kenneth Chew 2022-04-16 02:07:29 -04:00
parent 1049507b3f
commit e59d3a339f
No known key found for this signature in database
GPG Key ID: F17D3E14A07739DA
3 changed files with 9 additions and 2 deletions

View File

@ -80,6 +80,7 @@
#include <QStringList> #include <QStringList>
#include <QDebug> #include <QDebug>
#include <QStyleFactory> #include <QStyleFactory>
#include <QWindow>
#include "InstanceList.h" #include "InstanceList.h"
@ -1267,6 +1268,12 @@ bool Application::kill(InstancePtr instance)
return true; return true;
} }
void Application::closeCurrentWindow()
{
if (focusWindow())
focusWindow()->close();
}
void Application::addRunningInstance() void Application::addRunningInstance()
{ {
m_runningInstances ++; m_runningInstances ++;

View File

@ -189,6 +189,7 @@ public slots:
MinecraftAccountPtr accountToUse = nullptr MinecraftAccountPtr accountToUse = nullptr
); );
bool kill(InstancePtr instance); bool kill(InstancePtr instance);
void closeCurrentWindow();
private slots: private slots:
void on_windowClose(); void on_windowClose();

View File

@ -615,8 +615,7 @@ public:
closeAct = new QAction(tr("Close &Window"), MainWindow); closeAct = new QAction(tr("Close &Window"), MainWindow);
closeAct->setShortcut(QKeySequence::Close); closeAct->setShortcut(QKeySequence::Close);
closeAct->setStatusTip(tr("Close the current window")); closeAct->setStatusTip(tr("Close the current window"));
// FIXME: currently this always closes the main window, even if it is not currently the window in focus connect(closeAct, &QAction::triggered, APPLICATION, &Application::closeCurrentWindow);
connect(closeAct, &QAction::triggered, MainWindow, &MainWindow::close);
undoAct = new QAction(tr("&Undo"), MainWindow); undoAct = new QAction(tr("&Undo"), MainWindow);
undoAct->setShortcuts(QKeySequence::Undo); undoAct->setShortcuts(QKeySequence::Undo);