Skip to content

Commit 726a9b6

Browse files
committed
feat: 关闭行为选择
1 parent 7d3be9e commit 726a9b6

2 files changed

Lines changed: 70 additions & 4 deletions

File tree

aao/app.py

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,56 @@ def _poll_hotkeys(self) -> None:
388388
def set_log_handler(self, handler: QtLogHandler) -> None:
389389
self.farm_page.set_log_handler(handler)
390390

391+
def _get_close_action(self) -> str:
392+
"""点 X 时的行为:读 settings,无偏好则弹窗询问。
393+
394+
返回 "minimize" 或 "exit"。
395+
"""
396+
from PySide6.QtWidgets import QCheckBox, QDialog, QHBoxLayout, QPushButton, QVBoxLayout
397+
398+
from aao.ui.settings_page import load_settings, save_settings
399+
400+
s = load_settings()
401+
saved = s.get("close_action", "") # "minimize" / "exit" / "" (每次问)
402+
if saved in ("minimize", "exit"):
403+
return saved
404+
405+
# 首次/未设偏好:弹自定义对话框
406+
self._hide_overlay()
407+
dlg = QDialog(self)
408+
dlg.setWindowTitle("关闭窗口")
409+
dl = QVBoxLayout(dlg)
410+
btn_row = QHBoxLayout()
411+
btn_exit = QPushButton("退出程序")
412+
btn_minimize = QPushButton("最小化到托盘")
413+
btn_row.addWidget(btn_exit)
414+
btn_row.addWidget(btn_minimize)
415+
dl.addLayout(btn_row)
416+
chk = QCheckBox("不再询问(可在设置中修改)")
417+
dl.addWidget(chk)
418+
419+
choice = "exit"
420+
421+
def _on_exit():
422+
nonlocal choice
423+
choice = "exit"
424+
dlg.accept()
425+
426+
def _on_minimize():
427+
nonlocal choice
428+
choice = "minimize"
429+
dlg.accept()
430+
431+
btn_exit.clicked.connect(_on_exit)
432+
btn_minimize.clicked.connect(_on_minimize)
433+
dlg.exec()
434+
self._show_overlay()
435+
436+
if chk.isChecked():
437+
s["close_action"] = choice
438+
save_settings(s)
439+
return choice
440+
391441
def _restore_from_tray(self) -> None:
392442
"""从托盘还原窗口。"""
393443
self.showNormal()
@@ -409,10 +459,15 @@ def _quit_from_tray(self) -> None:
409459

410460
def closeEvent(self, event: QCloseEvent) -> None: # noqa: D401
411461
if not self._force_quit:
412-
# 普通关闭=隐藏到托盘(不退出,凹图可继续)
413-
event.ignore()
414-
self.hide()
415-
self.tray.show_message("ArknightsAutoOperator", "已在后台运行,双击托盘图标恢复。")
462+
# 点 X:按用户偏好退出或最小化
463+
action = self._get_close_action()
464+
if action == "minimize":
465+
event.ignore()
466+
self.hide()
467+
self.tray.show_message("ArknightsAutoOperator", "已在后台运行,双击托盘图标恢复。")
468+
else:
469+
# 走真退出路径
470+
self._force_quit = True
416471
return
417472
# 真退出(_quit_from_tray 已做清理,或窗口在可见时被强制关闭)
418473
self.hotkey_timer.stop()

aao/ui/settings_page.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ def _build_ui(self) -> None:
234234
rl.addLayout(btn_row)
235235
root.addWidget(res_box)
236236

237+
# --- 软件设置 ---
238+
ui_box = QGroupBox("软件设置")
239+
ui_form = QFormLayout(ui_box)
240+
self.cb_close_action = QComboBox()
241+
self.cb_close_action.addItems(["每次询问", "最小化到托盘", "退出程序"])
242+
ui_form.addRow("关闭窗口时:", self.cb_close_action)
243+
root.addWidget(ui_box)
244+
237245
root.addStretch()
238246

239247
# 日志
@@ -396,6 +404,8 @@ def _load(self) -> None:
396404
if s.get("port"):
397405
self.edit_port.setText(str(s["port"]))
398406
self.chk_api.setChecked(s.get("api", True))
407+
close_map = {"": 0, "minimize": 1, "exit": 2}
408+
self.cb_close_action.setCurrentIndex(close_map.get(s.get("close_action", ""), 0))
399409
if s.get("proxy"):
400410
self.edit_proxy.setText(str(s["proxy"]))
401411
if s.get("github_token_enc"):
@@ -420,6 +430,7 @@ def _on_save(self) -> None:
420430
"port": port,
421431
"api": self.chk_api.isChecked(),
422432
"proxy": self.edit_proxy.text().strip(),
433+
"close_action": ["", "minimize", "exit"][self.cb_close_action.currentIndex()],
423434
}
424435
)
425436
token = self.edit_github_token.text().strip()

0 commit comments

Comments
 (0)