Skip to content

Commit e3efbc5

Browse files
committed
Update ClutUI_Nextgen_Main.py
1 parent 36cee2a commit e3efbc5

File tree

1 file changed

+96
-20
lines changed

1 file changed

+96
-20
lines changed

ClutUI_Nextgen_Main.py

Lines changed: 96 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33
'''
44

55
from PySide6.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout
6-
from PySide6.QtCore import Qt, QTimer
7-
from PySide6.QtGui import QIcon
6+
from PySide6.QtCore import Qt, QTimer, QSize
7+
from PySide6.QtGui import QIcon, QWindow
88
from core.utils.initialization_manager import InitializationManager
99
from core.log.log_manager import log
1010
from core.utils.notif import Notification, NotificationType
1111
from core.ui.title_bar import TitleBar
1212
from core.window.window_manager import WindowManager
1313
from core.i18n import i18n
14+
from core.pages_core.pages_effect import PagesEffect
15+
from core.utils.resource_manager import ResourceManager
16+
from core.utils.yiyanapi import YiyanAPI
1417
import sys
1518
import os
19+
import json
20+
import ctypes
1621

1722
class MainWindow(QMainWindow):
1823
def __init__(self):
@@ -23,9 +28,29 @@ def __init__(self):
2328
log.info(i18n.get_text("init_window"))
2429

2530
# 设置应用图标
26-
icon = QIcon("resources/logo.png")
27-
self.setWindowIcon(icon)
28-
QApplication.setWindowIcon(icon)
31+
resource_manager = ResourceManager()
32+
icon = resource_manager.get_icon("logo")
33+
if icon:
34+
# 设置窗口图标
35+
self.setWindowIcon(icon)
36+
QApplication.setWindowIcon(icon)
37+
38+
# 设置任务栏图标
39+
if os.name == 'nt': # Windows系统
40+
try:
41+
# 获取窗口句柄
42+
hwnd = self.winId().__int__()
43+
44+
# 加载图标文件
45+
icon_path = resource_manager.get_resource_path(os.path.join("resources", "logo.png"))
46+
if os.path.exists(icon_path):
47+
# 设置任务栏图标
48+
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("ClutUI.Nextgen")
49+
log.info("成功设置任务栏图标")
50+
except Exception as e:
51+
log.error(f"设置任务栏图标失败: {str(e)}")
52+
53+
log.info("成功加载应用图标")
2954

3055
# 设置窗口基本属性
3156
self.setWindowTitle(i18n.get_text("app_title", "ClutUI Nextgen"))
@@ -43,6 +68,9 @@ def __init__(self):
4368
main_layout.setContentsMargins(0, 0, 0, 0)
4469
main_layout.setSpacing(0)
4570

71+
# 先隐藏主窗口部件,避免初始化时的闪烁
72+
main_widget.hide()
73+
4674
self.title_bar = TitleBar(self)
4775
self.title_bar.title_label.setText(i18n.get_text("app_title_full", "ClutUI Next Generation"))
4876
main_layout.addWidget(self.title_bar)
@@ -90,21 +118,77 @@ def __init__(self):
90118
self._cleanup_timer.timeout.connect(self._finish_close)
91119
self._notifications = []
92120

93-
# 注册语言变更回调
94-
i18n.add_language_change_callback(self._on_language_changed)
121+
# 连接语言变更信号
122+
i18n.language_changed.connect(self._on_language_changed)
123+
124+
# 预先应用一次模糊效果
125+
PagesEffect.apply_blur_effect(self)
126+
127+
# 使用QTimer延迟应用背景效果并显示窗口
128+
QTimer.singleShot(50, self._init_background_effect)
129+
130+
# 异步加载一言并显示欢迎通知
131+
self._show_welcome_notification()
95132

96-
def _on_language_changed(self):
133+
def _show_welcome_notification(self):
134+
self.yiyan_api = YiyanAPI()
135+
# 连接信号
136+
self.yiyan_api.hitokoto_ready.connect(
137+
lambda text: self.show_notification(
138+
text=text,
139+
type=NotificationType.TIPS,
140+
duration=3000
141+
)
142+
)
143+
# 开始异步获取
144+
initial_text = self.yiyan_api.get_hitokoto_async()
145+
# 显示初始通知
146+
self.show_notification(
147+
text=initial_text,
148+
type=NotificationType.TIPS,
149+
duration=3000
150+
)
151+
152+
def _init_background_effect(self):
153+
try:
154+
with open('config.json', 'r') as f:
155+
config = json.loads(f.read())
156+
effect = config.get('background_effect', 'effect_none')
157+
158+
if effect == 'effect_none':
159+
PagesEffect.remove_effects(self)
160+
elif effect == 'effect_mica':
161+
PagesEffect.apply_mica_effect(self)
162+
elif effect == 'effect_gaussian':
163+
PagesEffect.apply_gaussian_blur(self)
164+
elif effect == 'effect_blur':
165+
PagesEffect.apply_blur_effect(self)
166+
except:
167+
# 如果配置读取失败,默认使用无效果
168+
PagesEffect.remove_effects(self)
169+
170+
# 显示主窗口部件
171+
self.centralWidget().show()
172+
173+
def _apply_saved_background_effect(self):
174+
self._init_background_effect()
175+
176+
def _on_language_changed(self, lang=None):
97177
self.setWindowTitle(i18n.get_text("app_title", "ClutUI Nextgen"))
98178
self.title_bar.title_label.setText(i18n.get_text("app_title_full", "ClutUI Next Generation"))
99179
# 通知页面管理器更新所有页面的文本
100180
self.pages_manager.update_all_pages_text()
101181

102-
def closeEvent(self, event):
103-
WindowManager.handle_close_event(self, event)
104-
105182
def _finish_close(self):
106183
WindowManager.finish_close(self)
107-
i18n.remove_language_change_callback(self._on_language_changed)
184+
# 断开信号连接
185+
try:
186+
i18n.language_changed.disconnect(self._on_language_changed)
187+
except:
188+
pass
189+
190+
def closeEvent(self, event):
191+
WindowManager.handle_close_event(self, event)
108192

109193
def switch_page(self, page_name):
110194
WindowManager.switch_page(self, page_name)
@@ -129,15 +213,7 @@ def show_notification(self, text, type=NotificationType.TIPS, duration=1000):
129213
window.show()
130214
log.info(i18n.get_text("app_started"))
131215

132-
notification = Notification(
133-
text=i18n.get_text("welcome_notification"),
134-
type=NotificationType.TIPS,
135-
parent=window
136-
)
137-
notification.show_notification()
138-
139216
exit_code = app.exec()
140-
window._finish_close()
141217
sys.exit(exit_code)
142218

143219
except Exception as e:

0 commit comments

Comments
 (0)