实现深色模式支持和配置文件路径更新

- 为代码编辑器和主窗口实现深色模式样式,以增强黑暗环境下的用户体验。
- 更新配置文件的读取路径。
This commit is contained in:
moyanj 2024-09-09 16:10:46 +08:00
parent 50555786fd
commit 11e843bff3
5 changed files with 30 additions and 15 deletions

10
app/utils.py Normal file
View File

@ -0,0 +1,10 @@
import app.env as env
from qfluentwidgets import setTheme, Theme
def switchTheme(theme):
if theme == "dark":
setTheme(Theme.DARK)
env.theme = "dark"
elif theme == "light":
setTheme(Theme.LIGHT)
else:
setTheme(Theme.AUTO)

View File

@ -1,3 +0,0 @@
{
"1":"13"
}

View File

@ -1,17 +1,25 @@
from PyQt5.QtWidgets import QApplication, QTextEdit
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QFontMetrics
from PyQt5.QtGui import QSyntaxHighlighter, QTextCharFormat, QFont, QFontMetrics, QColor
from PyQt5.QtCore import QRegularExpression, Qt
import app.env as env
class CodeEditor(QTextEdit):
def __init__(self, text:str=""):
super().__init__()
self.setFont(QFont("Courier New", 12))
fontMetrics = QFontMetrics(self.font())
tabWidth = 4 * fontMetrics.width(' ') # Calculate the width of a tab
tabWidth = 4 * fontMetrics.width(' ') # 计算tab的宽度
self.setTabStopWidth(tabWidth)
self.setPlainText(text)
self.highlighter = CodeHighlighter(self.document())
self.highlighter.rehighlight()
if env.config.theme== "dark":
# 设置深色模式的样式
self.setStyleSheet("""
background-color: #2b2b2b; /* 背景颜色 */
color: #ffffff; /* 文本颜色 */
selection-background-color: #4e9a06; /* 选中背景颜色 */
selection-color: #ffffff; /* 选中文本颜色 */
""")
def paste(self):
super().paste()
@ -24,21 +32,21 @@ class CodeHighlighter(QSyntaxHighlighter):
# 定义 JSON 高亮规则
self.keyFormat = QTextCharFormat()
self.keyFormat.setForeground(Qt.GlobalColor.darkBlue)
self.keyFormat.setForeground(QColor("#0000ff")) # 蓝色关键字
self.keyFormat.setFontWeight(QFont.Bold)
self.valueFormat = QTextCharFormat()
self.valueFormat.setForeground(Qt.GlobalColor.darkGreen)
self.valueFormat.setForeground(QColor("#008000")) # 绿色字符串值
self.boolFormat = QTextCharFormat()
self.boolFormat.setForeground(Qt.GlobalColor.red)
self.boolFormat.setForeground(QColor("#ff0000")) # 红色布尔值
self.numberFormat = QTextCharFormat()
self.numberFormat.setForeground(Qt.GlobalColor.darkCyan)
self.numberFormat.setForeground(QColor("#008080")) # 青色数字
# 创建蓝色格式
blueFormat = QTextCharFormat()
blueFormat.setForeground(Qt.GlobalColor.blue)
blueFormat.setForeground(QColor("#0000ff"))
self.rules = [
(QRegularExpression(r'\b(true|false|null)\b'), self.boolFormat),

View File

@ -3,6 +3,8 @@ from PyQt5.QtCore import Qt
from qfluentwidgets import FluentIcon as FIF
from qfluentwidgets import CommandBar, Action, InfoBar, InfoBarPosition, SubtitleLabel
from .json_edit import CodeEditor
import app.env as env
import os
class SettingWidget(QWidget):
def __init__(self):
@ -23,7 +25,7 @@ class SettingWidget(QWidget):
self.base.addLayout(bar_layout)
# 加载 JSON 配置
with open("config.json", "r") as config_file:
with open(os.path.join(env.dirs.user_config_path, "config.json"), "r") as config_file:
config_data = config_file.read()
self.c1 = CodeEditor(config_data)
# 将代码编辑器添加到垂直布局中

View File

@ -18,10 +18,8 @@ class MainWindow(FluentWindow):
if env.config.theme == "dark":
setTheme(Theme.DARK)
elif env.config.theme == "light":
setTheme(Theme.LIGHT)
else:
setTheme(Theme.AUTO)
setTheme(Theme.LIGHT)
# 定义id和对象映射
self.id2obj = {