-
Notifications
You must be signed in to change notification settings - Fork 30
/
main.py
127 lines (90 loc) · 3.32 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# coding:utf-8
import os
# 创建 Application
import sys
from PySide6.QtWidgets import QApplication
from qfluentwidgets import qconfig
from app.common.config import cfg
# 设置程序运行路径, 便于调试
if "--debug" in sys.argv: # 调试时候请使用相对路径!
cfg.appPath = "./"
qconfig.load('./Ghost Downloader 配置文件.json', cfg)
else: # 编译后
cfg.appPath = os.path.dirname(sys.executable)
qconfig.load('{}/Ghost Downloader 配置文件.json'.format(os.path.dirname(sys.executable)), cfg)
def exceptionHandler(type, value, traceback): # 自定义错误捕捉函数
logger.exception(f"意料之外的错误! {type}: {value}. Traceback: {traceback}")
sys.excepthook = exceptionHandler
# 必须在 QApplication 创建前设置缩放比例
if cfg.get(cfg.dpiScale) == "Auto":
pass
else:
os.environ["QT_ENABLE_HIGHDPI_SCALING"] = "0"
os.environ["QT_SCALE_FACTOR"] = str(cfg.get(cfg.dpiScale))
app = QApplication(sys.argv)
# 检测程序重复运行
from PySide6.QtCore import QSharedMemory
# 尝试访问
sharedMemory = QSharedMemory()
sharedMemory.setKey("Ghost Downloader")
if sharedMemory.attach(): # 访问成功, 说明程序正在运行
if sys.platform == "win32":
import win32gui
import win32con
hWnd = win32gui.FindWindow(None, "Ghost Downloader")
win32gui.ShowWindow(hWnd, 1)
# 发送自定义信息唤醒窗口
# WM_CUSTOM = win32con.WM_USER + 1
# win32gui.SendMessage(hWnd, WM_CUSTOM, 0, 0)
win32gui.SendMessage(hWnd, win32con.WM_USER + 1, 0, 0)
win32gui.SetForegroundWindow(hWnd)
sys.exit(-1)
# 创建 SharedMemory
sharedMemory.create(1)
# Starting Program
import time
import warnings
import darkdetect
from PySide6.QtGui import QColor
from loguru import logger
from qframelesswindow.utils import getSystemAccentColor
from qfluentwidgets import setTheme, Theme, setThemeColor
# noinspection PyUnresolvedReferences
import Res_rc
from app.common.methods import loadPlugins
from app.view.main_window import MainWindow
# 防止 Mica 背景失效
# app.setAttribute(Qt.AA_DontCreateNativeWidgetSiblings)
# config loguru
logger.add('{}/Ghost Downloader 运行日志.log'.format(cfg.appPath), rotation="512 KB")
logger.info(f"Ghost Downloader is launched at {time.time_ns()}")
warnings.warn = logger.warning
# Enable Theme
setTheme(Theme.DARK if darkdetect.isDark() else Theme.LIGHT, save=False)
# Get Theme Color
# try:
# 上游仅支持 Windows 和 macOS
if sys.platform == "win32" or "darwin":
setThemeColor(getSystemAccentColor(), save=False)
if sys.platform == "linux":
if 'KDE_SESSION_UID' in os.environ: # KDE Plasma
import configparser
config = configparser.ConfigParser()
config.read(f"/home/{os.getlogin()}/.config/kdeglobals")
# 获取 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}")
# create main window
w = MainWindow()
# loading plugins
pluginsPath=os.path.join(cfg.appPath, "plugins")
loadPlugins(w, pluginsPath)
try: # 静默启动
if "--silence" in sys.argv:
w.hide()
except:
w.show()
sys.exit(app.exec())