From 05142e927cef8b467e3f3c3c6d72623fdb1d84dc Mon Sep 17 00:00:00 2001 From: Morten Kristensen Date: Wed, 20 Jun 2018 20:34:34 +0200 Subject: [PATCH] =?UTF-8?q?Proper=20=E2=8C=98+Q=20behavior=20on=20Mac?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two behaviors can be configured in app settings: 1. Close App -> Correctly closes down app 2. Hide Window -> Hides window and doesn't close the app Since no File->Quit menu action was defined before, ⌘+Q would abruptly terminate the app without calling all deleters as expected. --- src/Common.h | 6 ++++++ src/MainWindow.cpp | 43 +++++++++++++++++++++++++++++++++++++++++++ src/MainWindow.ui | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) diff --git a/src/Common.h b/src/Common.h index 4b9ce7a1..5df5b8f9 100644 --- a/src/Common.h +++ b/src/Common.h @@ -234,5 +234,11 @@ Q_DECLARE_METATYPE(Common::MPHwVersion) "border: 1px solid #FcFcFc;"\ "}" +enum class CloseBehavior +{ + CloseApp, + HideWindow +}; + #endif // COMMON_H diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 33664b8d..b7807f9f 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -275,6 +275,49 @@ MainWindow::MainWindow(WSClient *client, DbMasterController *mc, QWidget *parent ui->comboBoxPasswordOutput->addItem(tr("Enter"), 40); ui->comboBoxPasswordOutput->addItem(tr("Space"), 44); + // Close behavior +#ifdef Q_OS_MAC + ui->closeBehaviorComboBox->addItem( + tr("Close Application"), static_cast(CloseBehavior::CloseApp)); + ui->closeBehaviorComboBox->addItem( + tr("Hide Window"), static_cast(CloseBehavior::HideWindow)); + ui->closeBehaviorComboBox->setCurrentIndex( + s.value("settings/CloseBehavior", + static_cast(CloseBehavior::CloseApp)).toInt()); + + connect(ui->closeBehaviorComboBox, + static_cast(&QComboBox::currentIndexChanged), this, + [this](int index) + { + Q_UNUSED(index); + const auto behavior = ui->closeBehaviorComboBox->currentData().toInt(); + QSettings s; + s.setValue("settings/CloseBehavior", behavior); + }); + + // On macOS Command+Q will terminate the program without calling the proper destructors if not + // the following is defined. + auto *fileMenu = menuBar()->addMenu(tr("&File")); + auto *actionExit = fileMenu->addAction(tr("&Quit")); + connect(actionExit, &QAction::triggered, this, [this] + { + const auto behavior = + static_cast(ui->closeBehaviorComboBox->currentData().toInt()); + switch (behavior) + { + case CloseBehavior::CloseApp: + qApp->quit(); + break; + + case CloseBehavior::HideWindow: + close(); + break; + } + }); +#else + ui->closeBehaviorLabel->setVisible(false); + ui->closeBehaviorComboBox->setVisible(false); +#endif using LF = Common::LockUnlockModeFeatureFlags; diff --git a/src/MainWindow.ui b/src/MainWindow.ui index 8996e9ff..38aaf80f 100644 --- a/src/MainWindow.ui +++ b/src/MainWindow.ui @@ -2912,6 +2912,39 @@ Hint: keep your mouse positioned over an option to get more details. + + + + + + + 0 + 0 + + + + <html><head/><body><p>Behavior of ⌘+Q</p></body></html> + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + +