From 99c9e29f115814a4855cf9fd20adf3761ee09e69 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Sun, 1 Aug 2021 10:02:15 +0200
Subject: [PATCH] UI: Small theme selector at compile time

---
 src/UI/ScriptViews/ScriptEditor.cc | 13 +++++++++++--
 src/VivyApplication.cc             | 28 +++++++++++++++++++++-------
 src/VivyApplication.hh             |  7 +++++++
 3 files changed, 39 insertions(+), 9 deletions(-)

diff --git a/src/UI/ScriptViews/ScriptEditor.cc b/src/UI/ScriptViews/ScriptEditor.cc
index f89e5095..36259840 100644
--- a/src/UI/ScriptViews/ScriptEditor.cc
+++ b/src/UI/ScriptViews/ScriptEditor.cc
@@ -29,13 +29,22 @@ ScriptEditor::LineNumberArea::paintEvent(QPaintEvent *event) noexcept
 ScriptEditor::ScriptEditor(QWidget *parent) noexcept
     : QPlainTextEdit(parent)
 {
-    setStyleSheet(QStringLiteral("* { font-family: \"FiraCode\"; font-size: 10pt}"));
+    setStyleSheet(QStringLiteral("* {"
+                                 "  background-color: #232629;"
+                                 "  font-family: \"FiraCode\";"
+                                 "  font-size: 10pt"
+                                 "}"));
     lineNumberArea = new LineNumberArea(this);
     setFrameShape(QFrame::NoFrame);
 
     connect(this, &ScriptEditor::blockCountChanged, this, &ScriptEditor::updateLineNumberAreaWidth);
     connect(this, &ScriptEditor::updateRequest, this, &ScriptEditor::updateLineNumberArea);
 
+    QTextCharFormat textFormat;
+    textFormat.setForeground(QBrush(Qt::white));
+    mergeCurrentCharFormat(textFormat);
+
+    setBackgroundVisible(true);
     updateLineNumberAreaWidth(0);
 }
 
@@ -99,7 +108,7 @@ void
 ScriptEditor::lineNumberAreaPaintEvent(QPaintEvent *event) noexcept
 {
     QPainter painter(lineNumberArea);
-    painter.fillRect(event->rect(), QColor::fromRgb(69, 83, 100));
+    painter.fillRect(event->rect(), QColor::fromRgb(49, 54, 59));
     QTextBlock block  = firstVisibleBlock();
     int blockNumber   = block.blockNumber();
     int top           = qRound(blockBoundingGeometry(block).translated(contentOffset()).top());
diff --git a/src/VivyApplication.cc b/src/VivyApplication.cc
index dfa981ae..86ef22ab 100644
--- a/src/VivyApplication.cc
+++ b/src/VivyApplication.cc
@@ -12,6 +12,25 @@ VivyApplication::VivyApplication(int &argc, char **argv)
 {
 }
 
+void
+VivyApplication::setTheme(Theme theme) noexcept
+{
+    if (theme == Theme::System)
+        return;
+
+    const QString sheet = theme == Theme::Dark ? QStringLiteral(":qdarkstyle/dark/style.qss")
+                                               : QStringLiteral(":qdarkstyle/light/style.qss");
+
+    QFile stylesheet(sheet);
+    if (!stylesheet.exists()) {
+        qFatal("Missing stylesheet");
+    } else {
+        stylesheet.open(QFile::ReadOnly | QFile::Text);
+        QTextStream stylesheetStream(&stylesheet);
+        setStyleSheet(stylesheetStream.readAll());
+    }
+}
+
 int
 VivyApplication::exec() noexcept
 {
@@ -27,13 +46,8 @@ VivyApplication::exec() noexcept
     setAttribute(Qt::AA_DontShowIconsInMenus, false);
     setAttribute(Qt::AA_DontShowShortcutsInContextMenus, false);
     setFont(getApplicationFont(Font::Default));
-    QFile stylesheet(":qdarkstyle/dark/style.qss");
-    if (!stylesheet.exists()) {
-        qFatal("Missing stylesheet");
-    }
-    stylesheet.open(QFile::ReadOnly | QFile::Text);
-    QTextStream stylesheetStream(&stylesheet);
-    setStyleSheet(stylesheetStream.readAll());
+    setTheme(Theme::Dark); // TODO: Set system theme for now (because we
+                           // don't have a central theme provider).
 
     // Cursor blinking
     setCursorFlashTime(0);
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index 9d02e6b8..4d51494c 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -44,6 +44,12 @@ public:
         DefaultBoldItalic,
     };
 
+    enum class Theme {
+        System,
+        Dark,
+        Light,
+    };
+
 private:
     int fontIdMonospace;
     int fontIdMonospaceBold;
@@ -58,6 +64,7 @@ public:
     int exec() noexcept;
 
     QFont getApplicationFont(Font) const noexcept;
+    void setTheme(Theme) noexcept;
 };
 
 }
-- 
GitLab