Files
FRAISEMOE-Addons-Installer-…/source/core/managers/window_manager.py
hyb-oyqq 6a4c6ca1f1 feat(core): 优化线程管理和清理机制
- 在主窗口中添加优雅的线程清理逻辑,确保在退出时安全停止所有后台线程,避免潜在的资源泄漏。
- 更新离线模式管理器和哈希线程,增强对线程引用的管理,确保在操作完成后及时清理引用。
- 改进补丁检测器,支持在离线模式下的补丁状态检查,提升用户体验和系统稳定性。
- 增强日志记录,确保在关键操作中提供详细的调试信息,便于后续排查和用户反馈。
2025-08-11 14:42:38 +08:00

149 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from PySide6.QtCore import Qt, QPoint, QRect, QSize
from PySide6.QtGui import QPainterPath, QRegion
class WindowManager:
"""窗口管理器类,用于处理窗口的基本行为,如拖拽、调整大小和圆角设置"""
def __init__(self, parent_window):
"""初始化窗口管理器
Args:
parent_window: 父窗口实例
"""
self.window = parent_window
self.ui = parent_window.ui
# 拖动窗口相关变量
self._drag_position = QPoint()
self._is_dragging = False
# 窗口比例
self.aspect_ratio = 16 / 9
self.updateRoundedCorners = True
# 设置圆角窗口
self.setRoundedCorners()
def setRoundedCorners(self):
"""设置窗口圆角"""
# 实现圆角窗口
path = QPainterPath()
path.addRoundedRect(self.window.rect(), 20, 20)
mask = QRegion(path.toFillPolygon().toPolygon())
self.window.setMask(mask)
# 更新resize事件时更新圆角
self.updateRoundedCorners = True
def handle_mouse_press(self, event):
"""处理鼠标按下事件
Args:
event: 鼠标事件
"""
if event.button() == Qt.MouseButton.LeftButton:
# 只有当鼠标在标题栏区域时才可以拖动
if hasattr(self.ui, 'title_bar') and self.ui.title_bar.geometry().contains(event.position().toPoint()):
self._is_dragging = True
self._drag_position = event.globalPosition().toPoint() - self.window.frameGeometry().topLeft()
event.accept()
def handle_mouse_move(self, event):
"""处理鼠标移动事件
Args:
event: 鼠标事件
"""
if event.buttons() & Qt.MouseButton.LeftButton and self._is_dragging:
self.window.move(event.globalPosition().toPoint() - self._drag_position)
event.accept()
def handle_mouse_release(self, event):
"""处理鼠标释放事件
Args:
event: 鼠标事件
"""
if event.button() == Qt.MouseButton.LeftButton:
self._is_dragging = False
event.accept()
def handle_resize(self, event):
"""当窗口大小改变时更新圆角和维持纵横比
Args:
event: 窗口大小改变事件
"""
# 计算基于当前宽度的合适高度以维持16:9比例
new_width = event.size().width()
new_height = int(new_width / self.aspect_ratio)
if new_height != event.size().height():
# 阻止变形,保持比例
self.window.resize(new_width, new_height)
# 更新主容器大小
if hasattr(self.ui, 'main_container'):
self.ui.main_container.setGeometry(0, 0, new_width, new_height)
# 更新内容容器大小
if hasattr(self.ui, 'content_container'):
self.ui.content_container.setGeometry(0, 0, new_width, new_height)
# 更新标题栏宽度和高度
if hasattr(self.ui, 'title_bar'):
self.ui.title_bar.setGeometry(0, 0, new_width, 35)
# 更新菜单区域
if hasattr(self.ui, 'menu_area'):
self.ui.menu_area.setGeometry(0, 35, new_width, 30)
# 更新内容区域大小
if hasattr(self.ui, 'inner_content'):
self.ui.inner_content.setGeometry(0, 65, new_width, new_height - 65)
# 更新背景图大小
if hasattr(self.ui, 'Mainbg'):
self.ui.Mainbg.setGeometry(0, 0, new_width, new_height - 65)
if hasattr(self.ui, 'loadbg'):
self.ui.loadbg.setGeometry(0, 0, new_width, new_height - 65)
# 调整按钮位置 - 固定在右侧
right_margin = 20 # 减小右边距,使按钮更靠右
if hasattr(self.ui, 'button_container'):
btn_width = 211 # 扩大后的容器宽度
btn_height = 111 # 扩大后的容器高度
x_pos = new_width - btn_width - right_margin
y_pos = int((new_height - 65) * 0.18) - 10 # 从0.28改为0.18,向上移动
self.ui.button_container.setGeometry(x_pos, y_pos, btn_width, btn_height)
# 添加禁/启用补丁按钮容器的位置调整
if hasattr(self.ui, 'toggle_patch_container'):
btn_width = 211 # 扩大后的容器宽度
btn_height = 111 # 扩大后的容器高度
x_pos = new_width - btn_width - right_margin
y_pos = int((new_height - 65) * 0.36) - 10 # 从0.46改为0.36,向上移动
self.ui.toggle_patch_container.setGeometry(x_pos, y_pos, btn_width, btn_height)
# 添加卸载补丁按钮容器的位置调整
if hasattr(self.ui, 'uninstall_container'):
btn_width = 211 # 扩大后的容器宽度
btn_height = 111 # 扩大后的容器高度
x_pos = new_width - btn_width - right_margin
y_pos = int((new_height - 65) * 0.54) - 10 # 从0.64改为0.54,向上移动
self.ui.uninstall_container.setGeometry(x_pos, y_pos, btn_width, btn_height)
if hasattr(self.ui, 'exit_container'):
btn_width = 211 # 扩大后的容器宽度
btn_height = 111 # 扩大后的容器高度
x_pos = new_width - btn_width - right_margin
y_pos = int((new_height - 65) * 0.72) - 10 # 从0.82改为0.72,向上移动
self.ui.exit_container.setGeometry(x_pos, y_pos, btn_width, btn_height)
# 更新圆角
if hasattr(self, 'updateRoundedCorners') and self.updateRoundedCorners:
path = QPainterPath()
path.addRoundedRect(self.window.rect(), 20, 20)
mask = QRegion(path.toFillPolygon().toPolygon())
self.window.setMask(mask)