-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
169 lines (136 loc) · 5.49 KB
/
main.py
File metadata and controls
169 lines (136 loc) · 5.49 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Windows Update管理工具 - 主程序入口
面向小白用户的一站式Windows Update管理工具
支持双主题、后台运行、进程隐藏等功能
"""
import sys
import os
import logging
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtGui import QIcon
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from src.ui.main_window import MainWindow
from src.core.theme_manager import ThemeManager
from src.core.background_manager import BackgroundManager
from src.core.config_manager import ConfigManager
from src.utils.logger import setup_logger
class WindowsUpdateManager:
"""Windows Update管理工具主类"""
def __init__(self):
"""初始化应用程序"""
self.app = None
self.main_window = None
self.theme_manager = None
self.background_manager = None
self.config_manager = None
self.logger = None
def setup_application(self):
"""设置应用程序基础配置"""
# 创建QApplication实例
self.app = QApplication(sys.argv)
self.app.setApplicationName("Windows Update管理工具")
self.app.setApplicationVersion("1.0.0")
self.app.setOrganizationName("WindowsUpdateManager")
# 设置应用程序图标
icon_path = os.path.join(os.path.dirname(__file__), "resources", "icons", "app_icon.ico")
if os.path.exists(icon_path):
self.app.setWindowIcon(QIcon(icon_path))
# 设置高DPI支持
# 注意:PyQt6中这些属性可能已经默认启用,或者名称有所变化
try:
self.app.setAttribute(Qt.ApplicationAttribute.AA_EnableHighDpiScaling, True)
except AttributeError:
# PyQt6中可能不需要手动设置
pass
try:
self.app.setAttribute(Qt.ApplicationAttribute.AA_UseHighDpiPixmaps, True)
except AttributeError:
# PyQt6中可能不需要手动设置
pass
def initialize_managers(self):
"""初始化各个管理器"""
# 初始化日志系统
self.logger = setup_logger()
self.logger.info("应用程序启动")
# 初始化配置管理器
self.config_manager = ConfigManager()
# 初始化主题管理器
self.theme_manager = ThemeManager(self.config_manager)
# 初始化后台管理器
self.background_manager = BackgroundManager(self.config_manager)
def create_main_window(self):
"""创建主窗口"""
self.main_window = MainWindow(
theme_manager=self.theme_manager,
background_manager=self.background_manager,
config_manager=self.config_manager
)
# 连接主题变化信号
self.theme_manager.theme_changed.connect(self.main_window.apply_theme)
# 应用初始主题
self.main_window.apply_theme()
def setup_background_behavior(self):
"""设置后台运行行为"""
if self.config_manager.get_setting('background_run', True):
# 如果启用后台运行,设置关闭到托盘
self.main_window.setWindowFlags(
self.main_window.windowFlags() | Qt.WindowType.WindowMinimizeButtonHint
)
# 连接后台管理器信号
self.background_manager.show_window_requested.connect(self.main_window.show_from_tray)
self.background_manager.quit_requested.connect(self.quit_application)
def show_main_window(self):
"""显示主窗口"""
# 检查是否应该启动时隐藏到后台
if (self.config_manager.get_setting('background_run', True) and
self.config_manager.get_setting('start_minimized', False)):
# 启动时最小化到托盘
self.background_manager.minimize_to_tray()
else:
# 正常显示窗口
self.main_window.show()
self.main_window.raise_()
self.main_window.activateWindow()
def quit_application(self):
"""退出应用程序"""
self.logger.info("应用程序退出")
# 清理后台管理器
if self.background_manager:
self.background_manager.cleanup()
# 退出应用
self.app.quit()
def run(self):
"""运行应用程序"""
try:
# 设置应用程序
self.setup_application()
# 初始化管理器
self.initialize_managers()
# 创建主窗口
self.create_main_window()
# 设置后台行为
self.setup_background_behavior()
# 显示主窗口
self.show_main_window()
# 启动事件循环
return self.app.exec()
except Exception as e:
if self.logger:
self.logger.error(f"应用程序运行出错: {str(e)}")
else:
print(f"应用程序运行出错: {str(e)}")
return 1
def main():
"""主函数入口"""
# 创建应用程序实例
app_manager = WindowsUpdateManager()
# 运行应用程序
exit_code = app_manager.run()
# 退出
sys.exit(exit_code)
if __name__ == "__main__":
main()