-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTerminalPlugin.cpp
More file actions
279 lines (253 loc) · 12.7 KB
/
TerminalPlugin.cpp
File metadata and controls
279 lines (253 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* \file TerminalPlugin.cpp
* \brief Implementation of the Terminal plugin
* \author Diego Iastrubni (diegoiast@gmail.com)
* License MIT
*/
#include <KodoTerm/KodoTerm.hpp>
#include <QCheckBox>
#include <QDockWidget>
#include <QFontDatabase>
#include <QKeySequence>
#include <QPushButton>
#include <QSettings>
#include <fontwidget.hpp>
#include <qmdidialogevents.hpp>
#include "plugins/Terminal/TerminalPlugin.hpp"
static QString systemCurrentShell() {
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
#if defined(Q_OS_WIN)
// Windows shell (usually cmd.exe or powershell)
if (env.contains("ComSpec")) {
return env.value("ComSpec");
}
return QString();
#else
// Unix / Linux / macOS shell (bash, zsh, fish, etc.)
if (env.contains("SHELL")) {
return env.value("SHELL");
}
return QString();
#endif
}
TerminalPlugin::TerminalPlugin() {
name = tr("Terminal support");
author = tr("Diego Iastrubni <diegoiast@gmail.com>");
iVersion = 0;
sVersion = "0.0.1";
autoEnabled = true;
alwaysEnabled = false;
consoleConfig.theme = TerminalTheme::defaultTheme();
toggleTerminal = new QAction(tr("Toggle terminal"), this);
toggleTerminal->setShortcut(QKeySequence("Ctrl+T"));
connect(toggleTerminal, &QAction::triggered, this, [this]() {
if (terminalDock->isVisible()) {
terminalDock->hide();
} else {
terminalDock->show();
terminalDock->raise();
terminalDock->widget()->setFocus();
console->setFocus();
}
});
menus[tr("&File")]->addAction(toggleTerminal);
auto monospacedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont);
monospacedFont.setFixedPitch(true);
tempConfig.theme = consoleConfig.theme;
config.pluginName = tr("Terminal");
config.configItems.push_back(qmdiConfigItem::Builder()
.setKey(Config::PromptPreviewKey)
.setType(qmdiConfigItem::Label)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Display font"))
.setKey(Config::TerminalFontKey)
.setType(qmdiConfigItem::Font)
.setDefaultValue(monospacedFont)
.setValue(monospacedFont)
.build());
// -> start hack
// Instead of registering a full normal widget, we create it here in this plugin,
// by using a list of configs:
// 1. A button that will open a popup - to choose the theme. This is the main
// button seen on screen.
// 2. A user defined config, to store the actual file chosen. No visible.
// 0. As buttons don't have label - a special label is used.
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Theme"))
.setDescription(tr("Which theme to use for the terminal"))
.setType(qmdiConfigItem::Label)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setKey(Config::ThemeFileKey)
.setType(qmdiConfigItem::String)
.setUserEditable(false)
.build());
config.configItems.push_back(
qmdiConfigItem::Builder()
.setDisplayName(tr("Choose theme"))
.setDescription(tr("Click to choose form internal themes available"))
.setKey(Config::ThemeFileChooseKey)
.setType(qmdiConfigItem::Button)
.build());
// -> end hack
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Enable anti aliased painting"))
.setKey(Config::AntiAliasKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Triple click selects whole line"))
.setKey(Config::TrippleClickClickKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Copy text on selection"))
.setKey(Config::CopyOnSelectKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Paste on middle mouse click"))
.setKey(Config::PasteOnMiddleClickKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Make sound on terminal bells"))
.setKey(Config::AudioBellKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
config.configItems.push_back(qmdiConfigItem::Builder()
.setDisplayName(tr("Visual bell"))
.setKey(Config::VisualBellKey)
.setType(qmdiConfigItem::Bool)
.setDefaultValue(true)
.build());
connect(&qmdiDialogEvents::instance(), &qmdiDialogEvents::widgetCreated, this,
[this, monospacedFont](auto dialog, auto const &item, auto label, auto widget) {
if (item.key == Config::PromptPreviewKey) {
// hack?
// We should in theory have a referenced optional. Which is not available in
// C++. In theory, we could use this use this widget and de-reference it.
// However - the places where we use it, are callbacks from the dialog.
// Meaning, that we first must pass through this place.
promptPreviewLabel = label;
promptPreviewLabel->setAutoFillBackground(true);
promptPreviewLabel->setFrameStyle(QFrame::Panel);
{
QFont f;
f.fromString(getConfig().getTerminalFont());
f.setStyleStrategy(getConfig().getAntiAlias() ? QFont::PreferAntialias
: QFont::NoAntialias);
promptPreviewLabel->setFont(f);
}
updateTerminalPreview();
}
if (item.key == Config::TerminalFontKey) {
auto f = qobject_cast<FontWidget *>(widget);
connect(f, &FontWidget::fontUpdated, f, [this, f]() {
QFont font = f->font();
font.setStyleStrategy(getConfig().getAntiAlias() ? QFont::PreferAntialias
: QFont::NoAntialias);
promptPreviewLabel->setFont(font);
updateTerminalPreview();
tempConfig.fontString = font.toString();
});
}
if (item.key == Config::ThemeFileChooseKey) {
auto themeMenu = new QMenu(widget);
auto button = qobject_cast<QPushButton *>(widget);
auto themeCallback = [this](const TerminalTheme::ThemeInfo &info) {
qDebug() << "ThemeFileChooseKey - Theme is " << info.path;
this->tempConfig.theme = TerminalTheme::loadTheme(info.path);
this->tempConfig.themeFile = info.path;
updateTerminalPreview();
};
KodoTerm::populateThemeMenu(themeMenu, tr("Konsole"),
TerminalTheme::ThemeFormat::Konsole, themeCallback);
KodoTerm::populateThemeMenu(themeMenu, tr("Windows Terminal"),
TerminalTheme::ThemeFormat::WindowsTerminal,
themeCallback);
KodoTerm::populateThemeMenu(themeMenu, tr("iTerm"),
TerminalTheme::ThemeFormat::ITerm, themeCallback);
button->setMenu(themeMenu);
}
if (item.key == Config::AntiAliasKey) {
auto checkbox = qobject_cast<QCheckBox *>(widget);
if (checkbox) {
connect(checkbox, &QCheckBox::toggled, this, [this](bool checked) {
if (promptPreviewLabel) {
auto f = promptPreviewLabel->font();
f.setStyleStrategy(checked ? QFont::PreferAntialias
: QFont::NoAntialias);
promptPreviewLabel->setFont(f);
promptPreviewLabel->update();
}
});
}
}
Q_UNUSED(dialog);
});
}
TerminalPlugin::~TerminalPlugin() {
// TODO
}
// IPlugin interface
void TerminalPlugin::on_client_merged(qmdiHost *host) {
auto manager = dynamic_cast<PluginManager *>(host);
console = new KodoTerm(manager);
console->setProgram(systemCurrentShell());
console->start();
terminalDock = manager->createNewPanel(Panels::South, "terminalPanel", tr("Terminal"), console);
}
void TerminalPlugin::on_client_unmerged(qmdiHost *) { delete terminalDock; }
void TerminalPlugin::loadConfig(QSettings &settings) {
IPlugin::loadConfig(settings);
configurationHasBeenModified();
}
void TerminalPlugin::configurationHasBeenModified() {
// TODO: register a normal widget for editing theme files, instead
// of this ugly workaround.
// Why are we modifying the config here? it should have been done by the config
// dialog?
// Not on this case. We set the button for the config, instead of registering
// a widget. This means that this data is handled by the dialog itself.
getConfig().setThemeFile(tempConfig.themeFile);
if (!tempConfig.fontString.isEmpty()) {
getConfig().setTerminalFont(tempConfig.fontString);
}
consoleConfig.setDefaults();
consoleConfig.font.fromString(getConfig().getTerminalFont());
consoleConfig.tripleClickSelectsLine = getConfig().getTrippleClickClick();
consoleConfig.copyOnSelect = getConfig().getCopyOnSelect();
consoleConfig.pasteOnMiddleClick = getConfig().getPasteOnMiddleClick();
consoleConfig.textAntialiasing = getConfig().getAntiAlias();
consoleConfig.visualBell = getConfig().getVisualBell();
consoleConfig.theme = TerminalTheme::loadTheme(getConfig().getThemeFile());
consoleConfig.customBoxDrawing = true;
console->setConfig(consoleConfig);
}
void TerminalPlugin::updateTerminalPreview() {
#ifdef Q_OS_WIN
auto static pseudoPrompt = QString("C:\\> ver<br>Microsoft Windows [Version 10.0.19045.4170]");
#else
auto static pseudoPrompt =
QString("user@localhost:~$ uptime<br>12:34:56 up 10 days, 1:23, 2<br>users, "
" load average: 0.05, 0.01, 0.00");
#endif
auto pal = promptPreviewLabel->palette();
pal.setColor(QPalette::Window, this->tempConfig.theme.background);
pal.setColor(QPalette::WindowText, this->tempConfig.theme.foreground);
auto colorsSpan = QString("<br><br>");
for (auto i = 0; i < 16; i++) {
auto c1 = this->tempConfig.theme.palette[i].name();
auto c3 = QString("<span style='background-color: %1; color: %1'>-x-</span>").arg(c1);
colorsSpan += c3;
}
promptPreviewLabel->setPalette(pal);
promptPreviewLabel->setText(pseudoPrompt + colorsSpan);
}