From af040c9e2eab4d6a135d53d88750152b43adb132 Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 20 Dec 2022 22:15:35 +1100 Subject: [PATCH] fix(cpn): Ignore 'Quit' event in main window on MacOS if simulator running (#2874) Prevents app crashing on exit while the simulator is open. --- companion/src/helpers.cpp | 12 ++++++++++++ companion/src/helpers.h | 5 +++++ companion/src/mainwindow.cpp | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/companion/src/helpers.cpp b/companion/src/helpers.cpp index 5289b98af..bac0a2f23 100644 --- a/companion/src/helpers.cpp +++ b/companion/src/helpers.cpp @@ -324,6 +324,12 @@ void Helpers::setBitmappedValue(unsigned int & field, unsigned int value, unsign field = (field & ~fieldmask) | (value << (numbits * index + offset)); } +#ifdef __APPLE__ +// Flag when simulator is running +static bool simulatorRunning = false; +bool isSimulatorRunning() { return simulatorRunning; } +#endif + void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) { QString fwId = SimulatorLoader::findSimulatorByFirmwareName(getCurrentFirmware()->getId()); @@ -347,6 +353,9 @@ void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) dialog->setAttribute(Qt::WA_DeleteOnClose); QObject::connect(dialog, &SimulatorMainWindow::destroyed, [simuData] (void) { +#ifdef __APPLE__ + simulatorRunning = false; +#endif // TODO simuData and Horus tmp directory is deleted on simulator close OR we could use it to get back data from the simulation delete simuData; }); @@ -359,6 +368,9 @@ void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx) dialog->deleteLater(); } else if (dialog->setRadioData(simuData)) { +#ifdef __APPLE__ + simulatorRunning = true; +#endif dialog->show(); } else { diff --git a/companion/src/helpers.h b/companion/src/helpers.h index 5a48abaf6..9719a2119 100644 --- a/companion/src/helpers.h +++ b/companion/src/helpers.h @@ -122,6 +122,11 @@ namespace Helpers void startSimulation(QWidget * parent, RadioData & radioData, int modelIdx); +#ifdef __APPLE__ +// Flag when simulator is running +bool isSimulatorRunning(); +#endif + // Format a pixmap to fit on the current firmware QPixmap makePixMap(const QImage & image); diff --git a/companion/src/mainwindow.cpp b/companion/src/mainwindow.cpp index ef9453672..422d4effc 100644 --- a/companion/src/mainwindow.cpp +++ b/companion/src/mainwindow.cpp @@ -197,6 +197,14 @@ void MainWindow::displayWarnings() void MainWindow::closeEvent(QCloseEvent *event) { +#ifdef __APPLE__ + // If simulator is running ignore this Quit event (simulator will still be closed) + // - prevents app crash on exit + if (isSimulatorRunning()) { + event->ignore(); + return; + } +#endif g.mainWinGeo(saveGeometry()); g.mainWinState(saveState()); g.tabbedMdi(actTabbedWindows->isChecked());