Skip to content

Commit

Permalink
Merge pull request #301 from netromdk/handle_cmd_q
Browse files Browse the repository at this point in the history
Proper ⌘+Q behavior on Mac
  • Loading branch information
raoulh authored Jun 21, 2018
2 parents 575c80c + 05142e9 commit 467cddd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,5 +234,11 @@ Q_DECLARE_METATYPE(Common::MPHwVersion)
"border: 1px solid #FcFcFc;"\
"}"

enum class CloseBehavior
{
CloseApp,
HideWindow
};

#endif // COMMON_H

43 changes: 43 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(CloseBehavior::CloseApp));
ui->closeBehaviorComboBox->addItem(
tr("Hide Window"), static_cast<int>(CloseBehavior::HideWindow));
ui->closeBehaviorComboBox->setCurrentIndex(
s.value("settings/CloseBehavior",
static_cast<int>(CloseBehavior::CloseApp)).toInt());

connect(ui->closeBehaviorComboBox,
static_cast<void (QComboBox::*)(int)>(&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<CloseBehavior>(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;

Expand Down
33 changes: 33 additions & 0 deletions src/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -2912,6 +2912,39 @@ Hint: keep your mouse positioned over an option to get more details.</string>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="closeBehaviorLayout">
<item>
<widget class="QLabel" name="closeBehaviorLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="MinimumExpanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Behavior of ⌘+Q&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
</widget>
</item>
<item>
<spacer name="closeBehaviorSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QComboBox" name="closeBehaviorComboBox"/>
</item>
</layout>
</item>
<item>
<spacer name="horizontalSpacer_41">
<property name="orientation">
Expand Down

0 comments on commit 467cddd

Please sign in to comment.