Skip to content

Commit dabaefa

Browse files
committed
优化 **浮动通知窗口**,预计算大小避免闪烁
1 parent c990858 commit dabaefa

2 files changed

Lines changed: 38 additions & 15 deletions

File tree

CHANGELOG/v2.2.0/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ v2.0 - Koharu(小鸟游星野) release 3
1919
- 优化 **TEMP 清理**,仅清理 TEMP 文件夹
2020
- 优化 **历史记录表格**,未设置性别/小组则隐藏对应列
2121
- 优化 **音乐播放延迟**,减少文件读取和音频流初始化开销
22+
- 优化 **浮动通知窗口**,预计算大小避免闪烁
2223

2324
## 🐛 修复问题
2425

app/common/notification/notification_service.py

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -812,43 +812,65 @@ def update_content(
812812
if not student_labels:
813813
return
814814

815-
# 清除所有旧控件
816-
while self.content_layout.count():
817-
item = self.content_layout.takeAt(0)
818-
widget = item.widget()
819-
if widget:
820-
widget.deleteLater()
821-
822-
# 添加新控件
815+
# 预计算窗口大小
816+
total_height = 0
817+
max_width = 0
823818
for label in student_labels:
824-
# 如果有字体设置,则应用到标签上
819+
# 应用字体设置
825820
if font_settings_group:
826-
# 检查是否使用全局字体
827821
use_global_font = readme_settings_async(
828822
font_settings_group, "use_global_font"
829823
)
830824
custom_font = None
831-
if use_global_font == 1: # 不使用全局字体,使用自定义字体
825+
if use_global_font == 1:
832826
custom_font = readme_settings_async(
833827
font_settings_group, "custom_font"
834828
)
835829
if custom_font and hasattr(label, "setStyleSheet"):
836-
# 获取当前样式表并添加字体设置
837830
current_style = label.styleSheet()
838831
label.setStyleSheet(
839832
f"font-family: '{custom_font}'; {current_style}"
840833
)
834+
# 计算标签大小
835+
label.adjustSize()
836+
size_hint = label.sizeHint()
837+
total_height += size_hint.height()
838+
max_width = max(max_width, size_hint.width())
839+
840+
# 加上布局间距和边距
841+
spacing = self.content_layout.spacing()
842+
margins = self.content_layout.contentsMargins()
843+
total_height += spacing * (len(student_labels) - 1)
844+
total_height += margins.top() + margins.bottom()
845+
max_width += margins.left() + margins.right()
846+
847+
# 加上倒计时标签的高度
848+
if self.countdown_label:
849+
countdown_height = self.countdown_label.sizeHint().height()
850+
total_height += countdown_height + spacing
851+
852+
# 清除所有旧控件
853+
while self.content_layout.count():
854+
item = self.content_layout.takeAt(0)
855+
widget = item.widget()
856+
if widget:
857+
widget.deleteLater()
858+
859+
# 添加新控件
860+
for label in student_labels:
841861
self.content_layout.addWidget(label)
842862

863+
# 设置窗口大小
864+
window_width = max(300, max_width + 30)
865+
window_height = min(600, total_height + 30)
866+
self.setFixedSize(window_width, window_height)
867+
843868
# 确保颜色与当前主题同步
844869
try:
845870
self._on_theme_changed()
846871
except Exception as e:
847872
logger.exception("更新内容时同步主题时出错(已忽略): {}", e)
848873

849-
# 调整窗口大小以适应内容
850-
self.adjustSize()
851-
852874
# 检查窗口是否已经显示
853875
if self.isVisible():
854876
# 如果窗口已经显示,只更新内容和透明度,不重新定位窗口

0 commit comments

Comments
 (0)