-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathravenmonacopage.cpp
More file actions
77 lines (64 loc) · 2.71 KB
/
ravenmonacopage.cpp
File metadata and controls
77 lines (64 loc) · 2.71 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
#include "ravenmonacopage.h"
#include <QGuiApplication>
#include <QJsonObject>
#include <QMessageBox>
#include <QStyleHints>
RavenMonacoPage::RavenMonacoPage(QObject *parent) : QWebEnginePage(parent) {
// Update QWebEnginePage theme
// Note: This is required to avoid "white flash"
// when RavenMonaco is loading this page.
QStyleHints *hints = QGuiApplication::styleHints();
bool isLightTheme = hints->colorScheme() == Qt::ColorScheme::Light;
if (!isLightTheme) {
setBackgroundColor(QColor(30, 30, 30));
}
}
RavenMonacoPage::~RavenMonacoPage() {}
void RavenMonacoPage::init() {
// Init monaco when the page load is finished.
connect(this, &QWebEnginePage::loadFinished, this, [this](bool ok) {
if (!ok) {
qCritical() << "Failed to load Monaco editor, check Monaco HTTP server.";
QMessageBox errorMsg(QMessageBox::Critical, "GitRaven", "Failed to load Diff Viewer components.",
QMessageBox::Ok);
errorMsg.exec();
std::exit(-1);
}
// Call init() function
this->runJavaScript("init()", 0, [this](const QVariant &) {
// Update Monaco theme
setTheme(QGuiApplication::styleHints()->colorScheme());
// emit Monaco init is completed
m_initFinished = true;
emit this->signalInitFinished();
});
});
// Load index.html
load(QUrl("http://localhost:9191/index.html"));
}
void RavenMonacoPage::setReadonly(bool readonly) {
runJavaScript(QString("setReadonly({opt})").replace("{opt}", QVariant(readonly).toString()));
}
void RavenMonacoPage::setTheme(Qt::ColorScheme colorScheme) {
QJsonObject obj;
obj["theme"] = colorScheme == Qt::ColorScheme::Light ? "light" : "dark";
QJsonDocument jd(obj);
runJavaScript(QString("setTheme({opt})").replace("{opt}", jd.toJson()));
}
void RavenMonacoPage::updateText(GitManager::GitDiffItem diffItem) {
// Build JSON payload
QJsonObject payloadJ;
payloadJ["oldText"] = diffItem.oldFileContent;
payloadJ["oldPath"] = diffItem.oldFilePath;
payloadJ["newText"] = diffItem.newFileContent;
payloadJ["newPath"] = diffItem.newFilePath;
QJsonDocument payloadJD(payloadJ);
QString payloadJDStr = QString(payloadJD.toJson());
// Send request
runJavaScript(QString("update({opt})").replace("{opt}", payloadJDStr));
}
void RavenMonacoPage::javaScriptConsoleMessage(JavaScriptConsoleMessageLevel level, const QString &message,
int lineNumber, const QString &sourceID) {
qDebug() << "RavenMonacoPage::javaScriptConsoleMessage";
qDebug() << level << message << lineNumber << sourceID;
}