Skip to content

Commit 41aa94f

Browse files
committed
修复更新通知窗口自动关闭的问题
1 parent cfa1ad6 commit 41aa94f

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

app/common/update_notification.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from app.common.config import get_theme_icon, load_custom_font, check_for_updates, VERSION, themeColor
88
from app.common.path_utils import path_manager, open_file
99

10-
def show_update_notification(latest_version):
10+
def show_update_notification(latest_version, auto_close=False):
1111
"""显示更新通知窗口"""
1212
if hasattr(QApplication.instance(), 'update_notification_window'):
1313
# 如果窗口已存在则激活它
@@ -19,19 +19,21 @@ def show_update_notification(latest_version):
1919
return
2020

2121
# 创建新的通知窗口
22-
notification_window = UpdateNotification(latest_version)
22+
notification_window = UpdateNotification(latest_version, auto_close=auto_close)
2323
QApplication.instance().update_notification_window = notification_window
2424
notification_window.show()
2525

2626
class UpdateNotification(QDialog):
2727
"""自定义更新通知窗口"""
28-
def __init__(self, latest_version):
28+
def __init__(self, latest_version, auto_close=False):
2929
super().__init__(parent=None, flags=Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.Tool)
3030
self.latest_version = latest_version
31-
self.duration = 15000 # 默认显示15秒
31+
self.auto_close = auto_close
32+
self.duration = 30000 # 默认显示30秒(如果启用自动关闭)
3233
self.init_ui()
3334
self.init_animation()
34-
self.start_auto_close_timer()
35+
if self.auto_close:
36+
self.start_auto_close_timer()
3537

3638
def init_ui(self):
3739
"""初始化UI界面"""
@@ -214,8 +216,8 @@ def close_with_animation(self):
214216
self.group_animation.start()
215217

216218
def mousePressEvent(self, event):
217-
"""鼠标按下事件 - 重置自动关闭定时器"""
218-
if event.button() == Qt.LeftButton:
219+
"""鼠标按下事件 - 重置自动关闭定时器(仅在启用自动关闭时)"""
220+
if event.button() == Qt.LeftButton and self.auto_close and hasattr(self, 'timer'):
219221
self.timer.start(self.duration)
220222
super().mousePressEvent(event)
221223

app/view/SecRandom.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,38 @@ def show_update_notification(latest_version):
6060
else:
6161
app = QApplication.instance()
6262

63-
# 创建并显示通知窗口
64-
notification = UpdateNotification(latest_version)
65-
notification.show()
63+
# 检查是否已有通知窗口存在
64+
if hasattr(app, 'update_notification_window') and app.update_notification_window:
65+
# 如果窗口已存在则激活它
66+
notification_window = app.update_notification_window
67+
if notification_window.isHidden():
68+
notification_window.show()
69+
notification_window.raise_()
70+
notification_window.activateWindow()
71+
logger.info(f"更新通知窗口已激活,版本: {latest_version}")
72+
return
73+
74+
# 创建新的通知窗口
75+
notification = UpdateNotification(latest_version, auto_close=False)
76+
app.update_notification_window = notification
77+
6678
# 防止通知窗口关闭时程序退出
6779
original_quit_setting = app.quitOnLastWindowClosed()
6880
app.setQuitOnLastWindowClosed(False)
69-
notification.destroyed.connect(lambda: app.setQuitOnLastWindowClosed(original_quit_setting))
81+
82+
# 窗口关闭时恢复原始设置并清理引用
83+
def on_notification_closed():
84+
app.setQuitOnLastWindowClosed(original_quit_setting)
85+
if hasattr(app, 'update_notification_window'):
86+
del app.update_notification_window
87+
88+
# 连接信号
89+
notification.destroyed.connect(on_notification_closed)
90+
91+
# 显示通知窗口
92+
notification.show()
93+
notification.raise_()
94+
7095
logger.info(f"自定义更新通知已显示,版本: {latest_version}")
7196

7297
except ImportError as e:

0 commit comments

Comments
 (0)