Skip to content

Commit

Permalink
新增 KDE 主题色获取, 支持 Linux 添加开机自启
Browse files Browse the repository at this point in the history
  • Loading branch information
XiaoYouChR committed Sep 1, 2024
1 parent 316eb66 commit 4d0f903
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 44 deletions.
2 changes: 1 addition & 1 deletion app/common/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Config(QConfig):

YEAR = 2024
AUTHOR = "XiaoYouChR"
VERSION = "3.3.5"
VERSION = "3.3.4"
AUTHOR_URL = "https://space.bilibili.com/437313511"
FEEDBACK_URL = "https://github.com/XiaoYouChR/Ghost-Downloader-3/issues"
# RELEASE_URL = "https://github.com/XiaoYouChR/Ghost-Downloader-3/releases/latest"
Expand Down
2 changes: 1 addition & 1 deletion app/common/methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,4 @@ def openFile(fileResolve):
elif sys.platform == "darwin": # macOS
os.system(f"open '{fileResolve}'")
else:
os.system(f"xdg-open '{fileResolve}'")
os.system(f"xdg-open '{fileResolve}'")
30 changes: 27 additions & 3 deletions app/view/setting_interface.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding:utf-8
import os
import sys
from pathlib import Path

from PySide6.QtCore import Qt, QUrl, QResource
from PySide6.QtGui import QDesktopServices
Expand Down Expand Up @@ -264,7 +265,7 @@ def __onAutoRunCardChecked(self, value: bool):
<string>app.ghost.downloader</string>
<key>ProgramArguments</key>
<array>
<string>{QApplication.applicationFilePath()}</string>
<string>'{QApplication.applicationFilePath()}'</string>
<string>--silence</string>
</array>
<key>RunAtLoad</key>
Expand All @@ -273,10 +274,33 @@ def __onAutoRunCardChecked(self, value: bool):
</plist>""")
else:
os.remove(f"/Users/{os.getlogin()}/Library/LaunchAgents/app.ghost.downloader.plist")
elif sys.platform == "linux":
if value:
autoStartPath = Path(f'/home/{os.getlogin()}/.config/autostart/')
if not autoStartPath.exists():
autoStartPath.mkdir(parents=True, exist_ok=True)

with open(f"/home/{os.getlogin()}/.config/autostart/gd3.desktop", "w", encoding="utf-8") as f:
_ = (f"""[Desktop Entry]
Type=Application
Version={VERSION}
Name=Ghost Downloader 3
Comment=A multi-threading downloader with QThread based on PySide6
Exec="'{QApplication.applicationFilePath()}' --slience"
StartupNotify=false
Terminal=false
""")
print(_)
f.write(_)
f.flush()

else:
os.remove(f"/home/{os.getlogin()}/.config/autostart/gd3.desktop")

else:
InfoBar.warning(
title='注意',
content=f"该功能仅在 Windows/macOS 平台有效.",
title='警告',
content=f"鬼知道你用的是什么平台?",
orient=Qt.Horizontal,
isClosable=True,
position=InfoBarPosition.TOP,
Expand Down
55 changes: 16 additions & 39 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys

from PySide6.QtWidgets import QApplication
from qframelesswindow.utils import getSystemAccentColor

app = QApplication(sys.argv)

Expand Down Expand Up @@ -91,50 +92,26 @@ def exceptionHandler(type, value, traceback): # 自定义错误捕捉函数
setTheme(Theme.DARK if darkdetect.isDark() else Theme.LIGHT, save=False)

# Get Theme Color
try:
# Windows Only
if sys.platform == "win32":
import ctypes
# 定义用于获取主题色的函数
ctypes.windll.dwmapi.DwmGetColorizationColor.restype = ctypes.c_ulong
ctypes.windll.dwmapi.DwmGetColorizationColor.argtypes = [ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_bool)]

color = ctypes.c_ulong()
opaque = ctypes.c_bool()

# 获取主题颜色值
ctypes.windll.dwmapi.DwmGetColorizationColor(ctypes.byref(color), ctypes.byref(opaque))

# 将颜色值转换为RGB元组
b, g, r = color.value % 256, (color.value >> 8) % 256, (color.value >> 16) % 256

setThemeColor(QColor(r, g, b), save=False)

elif sys.platform == "darwin": # macOS Only
# 咱就是说为什么苹果要把开发者文档做成英文,出个中文版不好吗?
# TM的让我找得好苦…… - By YHX (#17)
import objc # PyObjC
from Foundation import NSBundle
# try:
# 上游仅支持 Windows 和 macOS
if sys.platform == "win32" and "darwin":
setThemeColor(getSystemAccentColor(), save=False)
if sys.platform == "linux":

# 加载AppKit框架
AppKit = NSBundle.bundleWithIdentifier_('com.apple.AppKit')
if 'KDE_SESSION_UID' in os.environ: # KDE Plasma

# 获取NSColor类
NSColor = AppKit.classNamed_('NSColor')
NSColorSpace = AppKit.classNamed_('NSColorSpace')
import configparser
config = configparser.ConfigParser()

# 获取当前主题色
color = NSColor.controlAccentColor() #md就是这个API让我找了好久……
# 欸,这时还不能用,因为现在这是NSColor Catalog color,还要转换!
color = color.colorUsingColorSpace_(NSColorSpace.sRGBColorSpace())
# 获取颜色的 RGB 分量, 并将颜色分量转换为 0-255 范围
r, g, b = int(color.redComponent() * 255), int(color.greenComponent() * 255), int(color.blueComponent() * 255)
config.read(f"/home/{os.getlogin()}/.config/kdeglobals")

setThemeColor(QColor(r, g, b), save=False)
# 获取 DecorationFocus 的值
if 'Colors:Window' in config:
color = list(map(int, config.get('Colors:Window', 'DecorationFocus').split(",")))
setThemeColor(QColor(*color))

except Exception as e:
logger.error(f"Cannot get theme color: {e}")
# except Exception as e:
# logger.error(f"Cannot get theme color: {e}")

# create main window
w = MainWindow()
Expand Down

0 comments on commit 4d0f903

Please sign in to comment.