Skip to content

Commit

Permalink
fix(cpn): Ignore 'Quit' event in main window on MacOS if simulator ru…
Browse files Browse the repository at this point in the history
…nning (#2874)

Prevents app crashing on exit while the simulator is open.
  • Loading branch information
philmoz authored Dec 20, 2022
1 parent 4841f06 commit af040c9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
12 changes: 12 additions & 0 deletions companion/src/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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;
});
Expand All @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions companion/src/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
8 changes: 8 additions & 0 deletions companion/src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit af040c9

Please sign in to comment.