Terminal: Make ESC key behavior configurable

Change-Id: I806870c5dd7edbcd3ac2642849cca82f1939ce01
Reviewed-by: Cristian Adam <cristian.adam@qt.io>
This commit is contained in:
Marcus Tillmanns
2023-03-24 22:02:23 +01:00
parent 83759e8083
commit af5f702f54
6 changed files with 69 additions and 16 deletions

View File

@@ -5,11 +5,13 @@
#include "shellmodel.h" #include "shellmodel.h"
#include "terminalcommands.h" #include "terminalcommands.h"
#include "terminalsettings.h"
#include "terminaltr.h" #include "terminaltr.h"
#include "terminalwidget.h" #include "terminalwidget.h"
#include <coreplugin/actionmanager/actionmanager.h> #include <coreplugin/actionmanager/actionmanager.h>
#include <coreplugin/icontext.h> #include <coreplugin/icontext.h>
#include <coreplugin/icore.h>
#include <projectexplorer/project.h> #include <projectexplorer/project.h>
#include <projectexplorer/projectmanager.h> #include <projectexplorer/projectmanager.h>
@@ -127,6 +129,30 @@ TerminalPane::TerminalPane(QObject *parent)
connect(m_openSettingsButton, &QToolButton::clicked, m_openSettingsButton, []() { connect(m_openSettingsButton, &QToolButton::clicked, m_openSettingsButton, []() {
TerminalCommands::openSettingsAction()->trigger(); TerminalCommands::openSettingsAction()->trigger();
}); });
auto updateEscButton = [this] {
m_escSettingButton->setChecked(TerminalSettings::instance().sendEscapeToTerminal.value());
if (TerminalSettings::instance().sendEscapeToTerminal.value()) {
m_escSettingButton->setText("");
m_escSettingButton->setToolTip(Tr::tr("Sending ESC to terminal instead of Qt Creator"));
} else {
m_escSettingButton->setText("⇧+⎋");
m_escSettingButton->setToolTip(Tr::tr("Press ⇧+⎋ to send ESC to terminal"));
}
};
m_escSettingButton = new QToolButton();
m_escSettingButton->setCheckable(true);
updateEscButton();
connect(m_escSettingButton, &QToolButton::toggled, this, [this] {
TerminalSettings::instance().sendEscapeToTerminal.setValue(m_escSettingButton->isChecked());
TerminalSettings::instance().apply();
TerminalSettings::instance().writeSettings(Core::ICore::settings());
});
connect(&TerminalSettings::instance(), &TerminalSettings::applied, this, updateEscButton);
} }
TerminalPane::~TerminalPane() TerminalPane::~TerminalPane()
@@ -252,15 +278,15 @@ void TerminalPane::setupTerminalWidget(TerminalWidget *terminal)
m_tabWidget->setTabText(index, exe + " - " + cwd.fileName()); m_tabWidget->setTabText(index, exe + " - " + cwd.fileName());
}; };
connect(terminal, &TerminalWidget::started, [setTabText, terminal](qint64 /*pid*/) { connect(terminal, &TerminalWidget::started, this, [setTabText, terminal](qint64 /*pid*/) {
setTabText(terminal); setTabText(terminal);
}); });
connect(terminal, &TerminalWidget::cwdChanged, [setTabText, terminal]() { connect(terminal, &TerminalWidget::cwdChanged, this, [setTabText, terminal]() {
setTabText(terminal); setTabText(terminal);
}); });
connect(terminal, &TerminalWidget::commandChanged, [setTabText, terminal]() { connect(terminal, &TerminalWidget::commandChanged, this, [setTabText, terminal]() {
setTabText(terminal); setTabText(terminal);
}); });
@@ -274,7 +300,7 @@ QList<QWidget *> TerminalPane::toolBarWidgets() const
widgets.prepend(m_newTerminalButton); widgets.prepend(m_newTerminalButton);
widgets.prepend(m_closeTerminalButton); widgets.prepend(m_closeTerminalButton);
return widgets << m_openSettingsButton; return widgets << m_openSettingsButton << m_escSettingButton;
} }
QString TerminalPane::displayName() const QString TerminalPane::displayName() const

View File

@@ -53,6 +53,7 @@ private:
QToolButton *m_newTerminalButton{nullptr}; QToolButton *m_newTerminalButton{nullptr};
QToolButton *m_closeTerminalButton{nullptr}; QToolButton *m_closeTerminalButton{nullptr};
QToolButton *m_openSettingsButton{nullptr}; QToolButton *m_openSettingsButton{nullptr};
QToolButton *m_escSettingButton{nullptr};
bool m_widgetInitialized{false}; bool m_widgetInitialized{false};
}; };

View File

@@ -97,6 +97,20 @@ TerminalSettings::TerminalSettings()
shell.setToolTip(Tr::tr("The shell executable to be started as terminal")); shell.setToolTip(Tr::tr("The shell executable to be started as terminal"));
shell.setDefaultValue(defaultShell()); shell.setDefaultValue(defaultShell());
sendEscapeToTerminal.setSettingsKey("SendEscapeToTerminal");
sendEscapeToTerminal.setLabelText(Tr::tr("Send escape key to terminal"));
sendEscapeToTerminal.setToolTip(
Tr::tr("If enabled, pressing the escape key will send it to the terminal "
"instead of closing the terminal."));
sendEscapeToTerminal.setDefaultValue(false);
registerAspect(&font);
registerAspect(&fontSize);
registerAspect(&shell);
registerAspect(&allowBlinkingCursor);
registerAspect(&enableTerminal);
registerAspect(&sendEscapeToTerminal);
setupColor(this, setupColor(this,
foregroundColor, foregroundColor,
"Foreground", "Foreground",
@@ -138,12 +152,6 @@ TerminalSettings::TerminalSettings()
setupColor(this, colors[7], "7", Utils::creatorTheme()->color(Theme::TerminalAnsi7)); setupColor(this, colors[7], "7", Utils::creatorTheme()->color(Theme::TerminalAnsi7));
setupColor(this, colors[15], "15", Utils::creatorTheme()->color(Theme::TerminalAnsi15)); setupColor(this, colors[15], "15", Utils::creatorTheme()->color(Theme::TerminalAnsi15));
registerAspect(&font);
registerAspect(&fontSize);
registerAspect(&shell);
registerAspect(&allowBlinkingCursor);
registerAspect(&enableTerminal);
} }
} // namespace Terminal } // namespace Terminal

View File

@@ -27,6 +27,8 @@ public:
Utils::ColorAspect colors[16]; Utils::ColorAspect colors[16];
Utils::BoolAspect allowBlinkingCursor; Utils::BoolAspect allowBlinkingCursor;
Utils::BoolAspect sendEscapeToTerminal;
}; };
} // namespace Terminal } // namespace Terminal

View File

@@ -376,8 +376,12 @@ QWidget *TerminalSettingsPage::widget()
// clang-format off // clang-format off
Column { Column {
Row { Group {
settings.enableTerminal, st, title(Tr::tr("General")),
Row {
settings.enableTerminal, st,
settings.sendEscapeToTerminal, st
},
}, },
Group { Group {
title(Tr::tr("Font")), title(Tr::tr("Font")),

View File

@@ -808,6 +808,7 @@ bool TerminalWidget::paintFindMatches(QPainter &p,
} }
break; break;
} }
if (it == m_search->hits().constEnd()) if (it == m_search->hits().constEnd())
return false; return false;
@@ -821,13 +822,11 @@ bool TerminalWidget::paintSelection(QPainter &p, const QRectF &cellRect, const Q
bool isInSelection = false; bool isInSelection = false;
const int pos = m_surface->gridToPos(gridPos); const int pos = m_surface->gridToPos(gridPos);
if (m_selection) { if (m_selection)
isInSelection = pos >= m_selection->start && pos < m_selection->end; isInSelection = pos >= m_selection->start && pos < m_selection->end;
}
if (isInSelection) { if (isInSelection)
p.fillRect(cellRect, m_currentColors[ColorIndex::Selection]); p.fillRect(cellRect, m_currentColors[ColorIndex::Selection]);
}
return isInSelection; return isInSelection;
} }
@@ -1049,6 +1048,19 @@ void TerminalWidget::keyPressEvent(QKeyEvent *event)
} }
if (event->key() == Qt::Key_Escape) { if (event->key() == Qt::Key_Escape) {
bool sendToTerminal = TerminalSettings::instance().sendEscapeToTerminal.value();
bool send = false;
if (sendToTerminal && event->modifiers() == Qt::NoModifier)
send = true;
else if (!sendToTerminal && event->modifiers() == Qt::ShiftModifier)
send = true;
if (send) {
event->setModifiers(Qt::NoModifier);
m_surface->sendKey(event);
return;
}
if (m_selection) if (m_selection)
TerminalCommands::widgetActions().clearSelection.trigger(); TerminalCommands::widgetActions().clearSelection.trigger();
else { else {