Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark mode improvements #473

Merged
merged 4 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions gui/qt/basiccodeviewerwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ BasicEditor::BasicEditor(QWidget *parent) : QPlainTextEdit(parent)
updateLineNumberAreaWidth(0);
}

void BasicEditor::updateDarkMode()
{
if (highlighter != nullptr) {
delete highlighter;
highlighter = new BasicHighlighter(document());
}
}

void BasicEditor::toggleHighlight()
{
if (highlighter == nullptr) {
Expand Down
1 change: 1 addition & 0 deletions gui/qt/basiccodeviewerwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class BasicEditor : public QPlainTextEdit
public:
BasicEditor(QWidget *parent = nullptr);

void updateDarkMode();
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
void toggleHighlight();
Expand Down
99 changes: 94 additions & 5 deletions gui/qt/datawidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,23 @@

DataWidget::DataWidget(QWidget *parent) : QPlainTextEdit{parent} {
moveable = false;
currentLineColor = isRunningInDarkMode() ? QColor(Qt::black) : QColor(Qt::yellow).lighter(160);
highlighter = new AsmHighlighter(document());
QFont font = this->font();
font.setStyleHint(QFont::TypeWriter);
setFont(font);
setContextMenuPolicy(Qt::CustomContextMenu);
}

void DataWidget::updateDarkMode() {
bool darkMode = isRunningInDarkMode();
for (QTextEdit::ExtraSelection &selection : highlights) {
selection.format.setBackground(selection.format.colorProperty(QTextFormat::UserProperty + darkMode));
}
updateAllHighlights();
delete highlighter;
highlighter = new AsmHighlighter(document());
}

void DataWidget::clearAllHighlights() {
disconnect(this, &DataWidget::cursorPositionChanged, this, &DataWidget::highlightCurrentLine);
while (!highlights.isEmpty()) {
Expand All @@ -18,6 +31,8 @@ void DataWidget::clearAllHighlights() {

highlights.clear();
updateAllHighlights();
delete highlighter;
highlighter = new AsmHighlighter(document());
}

void DataWidget::updateAllHighlights() {
Expand Down Expand Up @@ -50,15 +65,17 @@ bool DataWidget::labelCheck() {
void DataWidget::cursorState(bool state) {
moveable = state;
if (moveable) {
addHighlight(currentLineColor);
addHighlight(QColor(Qt::yellow).lighter(160), Qt::black);
}
}

void DataWidget::addHighlight(const QColor &color) {
void DataWidget::addHighlight(const QColor &lightModeColor, const QColor &darkModeColor) {
QTextEdit::ExtraSelection selection;

selection.format.setBackground(color);
selection.format.setBackground(isRunningInDarkMode() ? darkModeColor : lightModeColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.format.setProperty(QTextFormat::UserProperty, lightModeColor);
selection.format.setProperty(QTextFormat::UserProperty + 1, darkModeColor);
selection.cursor = textCursor();
selection.cursor.movePosition(QTextCursor::StartOfLine);

Expand All @@ -70,7 +87,7 @@ void DataWidget::highlightCurrentLine() {
if (!highlights.isEmpty()) {
highlights.removeLast();
}
addHighlight(currentLineColor);
addHighlight(QColor(Qt::yellow).lighter(160), Qt::black);
setExtraSelections(highlights);
if (QApplication::keyboardModifiers().testFlag(Qt::ControlModifier)) {
bool ok = true;
Expand Down Expand Up @@ -109,3 +126,75 @@ void DataWidget::highlightCurrentLine() {
}
}
}

AsmHighlighter::AsmHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) {
HighlightingRule rule;
bool darkMode = isRunningInDarkMode();

addressFormat.setForeground(QColor(darkMode ? "#888" : "#444"));
addressFormat.setFontWeight(QFont::Bold);

watchRFormat.setForeground(QColor("#008000"));
watchRFormat.setFontWeight(QFont::Bold);
watchWFormat.setForeground(QColor("#808000"));
watchWFormat.setFontWeight(QFont::Bold);
breakPFormat.setForeground(QColor("#800000"));
breakPFormat.setFontWeight(QFont::Bold);

mnemonicFormat.setForeground(QColor(darkMode ? "darkorange" : "darkblue"));

symbolFormat.setFontWeight(disasm.bold_sym ? QFont::DemiBold : QFont::Normal);
rule.pattern = QRegularExpression("\\b\\w+\\b");
rule.format = symbolFormat;
highlightingRules.append(rule);

decimalFormat.setForeground(QColor(darkMode ? "lime" : "green"));
rule.pattern = QRegularExpression("\\b\\d+\\b");
rule.format = decimalFormat;
highlightingRules.append(rule);

registerFormat.setForeground(QColor(darkMode ? "magenta" : "purple"));
rule.pattern = QRegularExpression("\\b([abcdehlirmpz]|af|bc|de|hl|sp|i[xy][hl]?|mb|n[cz]|p[eo])\\b", QRegularExpression::CaseInsensitiveOption);
rule.format = registerFormat;
highlightingRules.append(rule);

hexFormat.setForeground(QColor(darkMode ? "lime" : "green"));
rule.pattern = QRegularExpression("\\$[0-9a-fA-F]+\\b");
rule.format = hexFormat;
highlightingRules.append(rule);

parenFormat.setForeground(QColor(darkMode ? "lightblue" : "navy"));
rule.pattern = QRegularExpression("[()]");
rule.format = parenFormat;
highlightingRules.append(rule);

labelPattern = QRegularExpression(QStringLiteral("^(%1)\\s+(\\S+):")
.arg(disasm.addr ? QStringLiteral("[0-9a-fA-F]+") : QString()));
instructionPattern = QRegularExpression(QStringLiteral("^(%1) ([ R])([ W])([ X])\\s+(%2)\\s+(\\S+)")
.arg(disasm.addr ? QStringLiteral("[0-9a-fA-F]+") : QString(),
disasm.bytes ? QStringLiteral("[0-9a-fA-F]+") : QStringLiteral(" ")));
}

void AsmHighlighter::highlightBlock(const QString &text) {
QRegularExpressionMatch match;
if ((match = labelPattern.match(text)).hasMatch()) {
setFormat(match.capturedStart(1), match.capturedLength(1), addressFormat);
setFormat(match.capturedStart(2), match.capturedLength(2), symbolFormat);
} else if ((match = instructionPattern.match(text)).hasMatch()) {
setFormat(match.capturedStart(1), match.capturedLength(1), addressFormat);
setFormat(match.capturedStart(2), match.capturedLength(2), watchRFormat);
setFormat(match.capturedStart(3), match.capturedLength(3), watchWFormat);
setFormat(match.capturedStart(4), match.capturedLength(4), breakPFormat);
setFormat(match.capturedStart(5), match.capturedLength(5), bytesFormat);
setFormat(match.capturedStart(6), match.capturedLength(6), mnemonicFormat);
foreach(const HighlightingRule &rule, highlightingRules) {
QRegularExpressionMatchIterator iter = rule.pattern.globalMatch(text, match.capturedEnd());
while (iter.hasNext()) {
const auto& innerMatch = iter.next();
if (innerMatch.hasMatch()) {
setFormat(innerMatch.capturedStart(), innerMatch.capturedLength(), rule.format);
}
}
}
}
}
42 changes: 40 additions & 2 deletions gui/qt/datawidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define CODEEDITOR_H

#include <QtWidgets/QPlainTextEdit>
#include <QtGui/QSyntaxHighlighter>
#include <QtGui/QTextCharFormat>
#include <QtCore/QRegularExpression>
#include <QtCore/QObject>

QT_BEGIN_NAMESPACE
Expand All @@ -11,14 +14,49 @@ QT_BEGIN_NAMESPACE
class QWidget;
QT_END_NAMESPACE

class AsmHighlighter : public QSyntaxHighlighter
{
Q_OBJECT

public:
AsmHighlighter(QTextDocument *parent = nullptr);

protected:
void highlightBlock(const QString &text) Q_DECL_OVERRIDE;

private:
struct HighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
QVector<HighlightingRule> highlightingRules;

QTextCharFormat addressFormat;
QTextCharFormat symbolFormat;
QTextCharFormat watchRFormat;
QTextCharFormat watchWFormat;
QTextCharFormat breakPFormat;
QTextCharFormat bytesFormat;
QTextCharFormat mnemonicFormat;
QTextCharFormat hexFormat;
QTextCharFormat decimalFormat;
QTextCharFormat parenFormat;
QTextCharFormat registerFormat;

QRegularExpression labelPattern;
QRegularExpression instructionPattern;
};

class DataWidget : public QPlainTextEdit {
Q_OBJECT

public:
explicit DataWidget(QWidget *p = Q_NULLPTR);
void updateDarkMode();
void clearAllHighlights();
void updateAllHighlights();
void addHighlight(const QColor& color);
void addHighlight(const QColor &lightModeColor, const QColor &darkModeColor);
void highlightCurrentLine();
void cursorState(bool movable);
bool labelCheck();
Expand All @@ -30,7 +68,7 @@ class DataWidget : public QPlainTextEdit {

private:
bool moveable;
QColor currentLineColor;
AsmHighlighter *highlighter;
QList<QTextEdit::ExtraSelection> highlights;
};

Expand Down
24 changes: 4 additions & 20 deletions gui/qt/debugger/disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ static std::string strW(uint32_t data) {
if (high) {
range = disasm.map.equal_range(data);
for (sit = range.first; sit != range.second; ++sit) {
if (disasm.bold_sym) {
ret += "<b>" + sit->second + "</b>";
} else {
ret += sit->second;
}
ret += sit->second;
ret += '|';
}
if (!ret.empty()) {
Expand All @@ -37,11 +33,7 @@ static std::string strW(uint32_t data) {
if (!disasm.il) {
range = disasm.map.equal_range(cpu.registers.MBASE<<16|data);
for (sit = range.first; sit != range.second; ++sit) {
if (disasm.bold_sym) {
ret += "<b>" + sit->second + "</b>";
} else {
ret += sit->second;
}
ret += sit->second;
ret += '|';
}
if (!ret.empty()) {
Expand All @@ -67,11 +59,7 @@ static std::string strA(uint32_t data) {
if (!ret.empty()) {
ret += '|';
}
if (disasm.bold_sym) {
ret += "<b>" + sit->second + "</b>";
} else {
ret += sit->second;
}
ret += sit->second;
}
}
if (!ret.empty()) {
Expand All @@ -86,11 +74,7 @@ static std::string strA(uint32_t data) {
if (!ret.empty()) {
ret += '|';
}
if (disasm.bold_sym) {
ret += "<b>" + sit->second + "</b>";
} else {
ret += sit->second;
}
ret += sit->second;
}
}
if (!ret.empty()) {
Expand Down
Loading
Loading