Skip to content

The only downloader you need. 下载器的集大成者。

记忆窗口位置 (#56)

XiaoYouChR2000fd8e429a
diff --git a/app/common/config.py b/app/common/config.py
index 5cc6562e..7f75ee6a 100644
--- a/app/common/config.py
+++ b/app/common/config.py
@@ -2,10 +2,10 @@
import sys
from re import compile
-from PySide6.QtCore import QDir
+from PySide6.QtCore import QDir, QRect
from qfluentwidgets import (QConfig, ConfigItem, OptionsConfigItem, BoolValidator,
OptionsValidator, RangeConfigItem, RangeValidator,
- FolderValidator, ConfigValidator)
+ FolderValidator, ConfigValidator, ConfigSerializer)
class ProxyValidator(ConfigValidator):
@@ -23,6 +23,28 @@ class ProxyValidator(ConfigValidator):
def correct(self, value) -> str:
return value if self.validate(value) else "Auto"
+class GeometryValidator(ConfigValidator): # geometry 为程序的位置和大小, 保存为字符串 "x,y,w,h," 默认为 Default
+ def validate(self, value: QRect) -> bool:
+ if value == "Default":
+ return True
+ if type(value) == QRect:
+ return True
+
+ def correct(self, value) -> str:
+ return value if self.validate(value) else "Default"
+
+class GeometrySerializer(ConfigSerializer): # 将字符串 "x,y,w,h," 转换为QRect (x, y, w, h), "Default" 除外
+ def serialize(self, value: QRect) -> str:
+ if value == "Default":
+ return value
+ return f"{value.x()},{value.y()},{value.width()},{value.height()}"
+
+ def deserialize(self, value: str) -> QRect:
+ if value == "Default":
+ return value
+ x, y, w, h = map(int, value.split(","))
+ return QRect(x, y, w, h)
+
class Config(QConfig):
""" Config of application """
# download
@@ -38,8 +60,8 @@ class Config(QConfig):
enableBrowserExtension = ConfigItem("Browser", "EnableBrowserExtension", False, BoolValidator())
# personalization
- # backgroundEffect = OptionsConfigItem("Personalization", "BackgroundEffect", "Mica", OptionsValidator(["Acrylic", "Mica", "MicaBlur", "MicaAlt", "Transparent", "Aero", "None"]))
if sys.platform == "win32":
+ # backgroundEffect = OptionsConfigItem("Personalization", "BackgroundEffect", "Mica", OptionsValidator(["Acrylic", "Mica", "MicaBlur", "MicaAlt", "Transparent", "Aero", "None"]))
backgroundEffect = OptionsConfigItem("Personalization", "BackgroundEffect", "Mica", OptionsValidator(["Acrylic", "Mica", "MicaBlur", "MicaAlt", "Aero"]))
dpiScale = OptionsConfigItem(
"Personalization", "DpiScale", "Auto", OptionsValidator([1, 1.25, 1.5, 1.75, 2, "Auto"]), restart=True)
@@ -47,7 +69,7 @@ class Config(QConfig):
# software
checkUpdateAtStartUp = ConfigItem("Software", "CheckUpdateAtStartUp", True, BoolValidator())
autoRun = ConfigItem("Software", "AutoRun", False, BoolValidator())
-
+ geometry = ConfigItem("Software", "Geometry", "Default", GeometryValidator(), GeometrySerializer()) # 保存程序的位置和大小, Validator 在 mainWindow 中设置
# 程序运行路径
appPath = "./"
diff --git a/app/view/main_window.py b/app/view/main_window.py
index e9e415b6..7f0dd16a 100644
--- a/app/view/main_window.py
+++ b/app/view/main_window.py
@@ -161,7 +161,24 @@ class MainWindow(MSFluentWindow):
self.addSubInterface(self.settingInterface, FIF.SETTING, "设置", position=NavigationItemPosition.BOTTOM)
def initWindow(self):
- self.resize(960, 780)
+
+ if cfg.geometry.value == "Default":
+ self.resize(960, 780)
+ desktop = QApplication.screens()[0].availableGeometry()
+ w, h = desktop.width(), desktop.height()
+ self.move(w//2 - self.width()//2, h//2 - self.height()//2)
+ else:
+ try:
+ self.setGeometry(cfg.get(cfg.geometry))
+ except Exception as e:
+ logger.error(f"Failed to restore geometry: {e}")
+ cfg.set(cfg.geometry, "Default")
+
+ self.resize(960, 780)
+ desktop = QApplication.screens()[0].availableGeometry()
+ w, h = desktop.width(), desktop.height()
+ self.move(w//2 - self.width()//2, h//2 - self.height()//2)
+
self.setWindowIcon(QIcon(':/image/logo.png'))
self.setWindowTitle('Ghost Downloader')
@@ -170,10 +187,8 @@ class MainWindow(MSFluentWindow):
self.splashScreen.setIconSize(QSize(106, 106))
self.splashScreen.raise_()
- desktop = QApplication.screens()[0].availableGeometry()
- w, h = desktop.width(), desktop.height()
- self.move(w//2 - self.width()//2, h//2 - self.height()//2)
self.show()
+
QApplication.processEvents()
@@ -184,6 +199,9 @@ class MainWindow(MSFluentWindow):
def closeEvent(self, event):
# 拦截关闭事件,隐藏窗口而不是退出
event.ignore()
+ # 保存窗口位置
+ cfg.set(cfg.geometry, self.geometry())
+
self.hide()
def nativeEvent(self, eventType, message):