Skip to content

Commit

Permalink
Respect system dark theme
Browse files Browse the repository at this point in the history
  • Loading branch information
IgKh committed Jun 15, 2024
1 parent 5e169f9 commit ae63c95
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 16 deletions.
13 changes: 9 additions & 4 deletions src/katvan_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,8 +210,13 @@ void Editor::checkForModelines()

bool Editor::event(QEvent* event)
{
if (event->type() == QEvent::PaletteChange)
{
d_highlighter->setupFormats();
forceRehighlighting();
}
#ifdef Q_OS_LINUX
if (event->type() == QEvent::ShortcutOverride) {
else if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier)) {
if (keyEvent->nativeScanCode() == 50) {
Expand Down Expand Up @@ -538,7 +543,7 @@ int Editor::lineNumberGutterWidth()
void Editor::updateLineNumberGutterWidth()
{
int gutterWidth = lineNumberGutterWidth();
setViewportMargins(gutterWidth, 0, gutterWidth, 0);
setViewportMargins(gutterWidth + 1, 0, gutterWidth + 1, 0);
}

void Editor::updateLineNumberGutters()
Expand Down Expand Up @@ -577,8 +582,8 @@ QTextBlock Editor::getFirstVisibleBlock()

void Editor::lineNumberGutterPaintEvent(QWidget* gutter, QPaintEvent* event)
{
QColor bgColor(Qt::lightGray);
QColor fgColor(120, 120, 120);
QColor bgColor = palette().color(QPalette::Active, QPalette::Window);
QColor fgColor = palette().color(QPalette::Active, QPalette::WindowText);

QPainter painter(gutter);
painter.fillRect(event->rect(), bgColor);
Expand Down
39 changes: 29 additions & 10 deletions src/katvan_highlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
#include "katvan_highlighter.h"
#include "katvan_spellchecker.h"

#include <QGuiApplication>
#include <QHash>
#include <QPalette>
#include <QStyleHints>
#include <QTextDocument>

namespace katvan {
Expand All @@ -44,27 +47,43 @@ Highlighter::Highlighter(QTextDocument* document, SpellChecker* spellChecker)
setupFormats();
}

static bool isAppInDarkMode()
{
if (qGuiApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
return true;
}

// If the Qt platform integration doesn't support signaling a color scheme,
// sniff it from the global palette.
QPalette palette = qGuiApp->palette();
return palette.color(QPalette::WindowText).lightness() > palette.color(QPalette::Window).lightness();
}

void Highlighter::setupFormats()
{
bool isDark = isAppInDarkMode();

d_formats.clear();

QTextCharFormat commentFormat;
commentFormat.setForeground(QColor(0x8a, 0x8a, 0x8a));
commentFormat.setForeground(isDark ? QColor(0xb0, 0xb3, 0xc2) : QColor(0x8a, 0x8a, 0x8a));
d_formats.insert(parsing::HiglightingMarker::Kind::COMMENT, commentFormat);

QTextCharFormat stringLiteralFormat;
stringLiteralFormat.setForeground(QColor(0x29, 0x8e, 0x0d));
stringLiteralFormat.setForeground(isDark ? QColor(0x80, 0xf4, 0xb6) : QColor(0x29, 0x8e, 0x0d));
d_formats.insert(parsing::HiglightingMarker::Kind::STRING_LITERAL, stringLiteralFormat);

QTextCharFormat numberLiteralFormat;
numberLiteralFormat.setForeground(QColor(0xb6, 0x01, 0x57));
numberLiteralFormat.setForeground(isDark ? QColor(0xff, 0x7d, 0x79) : QColor(0xb6, 0x01, 0x57));
d_formats.insert(parsing::HiglightingMarker::Kind::NUMBER_LITERAL, numberLiteralFormat);

QTextCharFormat escapeFormat;
escapeFormat.setForeground(QColor(0x1d, 0x6c, 0x76));
escapeFormat.setForeground(isDark ? QColor(0xd9, 0xf8, 0xf4) : QColor(0x1d, 0x6c, 0x76));
d_formats.insert(parsing::HiglightingMarker::Kind::ESCAPE, escapeFormat);
d_formats.insert(parsing::HiglightingMarker::Kind::MATH_OPERATOR, escapeFormat);

QTextCharFormat mathDelimiterFormat;
mathDelimiterFormat.setForeground(QColor(0x29, 0x8e, 0x0d));
mathDelimiterFormat.setForeground(isDark ? QColor(0x80, 0xf4, 0xb6) : QColor(0x29, 0x8e, 0x0d));
d_formats.insert(parsing::HiglightingMarker::Kind::MATH_DELIMITER, mathDelimiterFormat);

QTextCharFormat headingFormat;
Expand All @@ -89,28 +108,28 @@ void Highlighter::setupFormats()
d_formats.insert(parsing::HiglightingMarker::Kind::RAW, rawFormat);

QTextCharFormat labelAndReferenceFormat;
labelAndReferenceFormat.setForeground(QColor(0x1d, 0x6c, 0x76));
labelAndReferenceFormat.setForeground(isDark ? QColor(0xd9, 0xf8, 0xf4) : QColor(0x1d, 0x6c, 0x76));
d_formats.insert(parsing::HiglightingMarker::Kind::LABEL, labelAndReferenceFormat);
d_formats.insert(parsing::HiglightingMarker::Kind::REFERENCE, labelAndReferenceFormat);

QTextCharFormat listEntryFormat;
listEntryFormat.setForeground(QColor(0x8b, 0x41, 0xb1));
listEntryFormat.setForeground(isDark ? QColor(0xca, 0xa8, 0xff) : QColor(0x8b, 0x41, 0xb1));
d_formats.insert(parsing::HiglightingMarker::Kind::LIST_ENTRY, listEntryFormat);

QTextCharFormat termFormat;
termFormat.setFontWeight(QFont::ExtraBold);
d_formats.insert(parsing::HiglightingMarker::Kind::TERM, termFormat);

QTextCharFormat variableNameFormat;
variableNameFormat.setForeground(QColor(0x8b, 0x41, 0xb1));
variableNameFormat.setForeground(isDark ? QColor(0xca, 0xa8, 0xff) : QColor(0x8b, 0x41, 0xb1));
d_formats.insert(parsing::HiglightingMarker::Kind::VARIABLE_NAME, variableNameFormat);

QTextCharFormat functionNameFormat;
functionNameFormat.setForeground(QColor(0x4b, 0x69, 0xc6));
functionNameFormat.setForeground(isDark ? QColor(0x7c, 0xd5, 0xff) : QColor(0x4b, 0x69, 0xc6));
d_formats.insert(parsing::HiglightingMarker::Kind::FUNCTION_NAME, functionNameFormat);

QTextCharFormat keywordFormat;
keywordFormat.setForeground(QColor(0xd7, 0x3a, 0x49));
keywordFormat.setForeground(isDark ? QColor(0xff, 0xa4, 0x9d) : QColor(0xd7, 0x3a, 0x49));
d_formats.insert(parsing::HiglightingMarker::Kind::KEYWORD, keywordFormat);

d_misspelledWordFormat.setFontUnderline(true);
Expand Down
4 changes: 2 additions & 2 deletions src/katvan_highlighter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ class Highlighter : public QSyntaxHighlighter
public:
Highlighter(QTextDocument* document, SpellChecker* spellChecker);

void setupFormats();

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

private:
void setupFormats();

void doSyntaxHighlighting(
parsing::HighlightingListener& listener,
QList<QTextCharFormat>& charFormats);
Expand Down

0 comments on commit ae63c95

Please sign in to comment.