From 55a143a7d857ad62d0deefd974e4118de0b3a8a8 Mon Sep 17 00:00:00 2001 From: Theodore Cooper <63190431+the0cp@users.noreply.github.com> Date: Tue, 6 Feb 2024 14:35:41 +0800 Subject: [PATCH 1/2] closeEvent & Restore Function (#19) * closeEvent 1. update the closeEvent: - display a dialog with "Exit" and "Minimize" buttons upon clicking the "X", with "Exit" triggering the onExit() function and "Minimize" hiding the window to the system tray. - add a checkbox "don't ask me again" 2. delete changeEvent() 3. add function to restore settings --- src/GUI/AppGUI.py | 57 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/GUI/AppGUI.py b/src/GUI/AppGUI.py index ce461fd..1423e65 100644 --- a/src/GUI/AppGUI.py +++ b/src/GUI/AppGUI.py @@ -68,6 +68,7 @@ class SettingsKey(Enum): CPUThresholdTemp = "app/fan/cpu/threshold_temp" GPUFanSpeed = "app/fan/gpu/speed" GPUThresholdTemp = "app/fan/gpu/threshold_temp" + MinimizeOnCloseFlag = "app/exit" def errorExit(message: str, message2: str = None) -> None: if not QtWidgets.QApplication.instance(): @@ -128,8 +129,10 @@ def autorunTaskRun(action: Literal['add', 'remove']) -> None: addToAutorunAction.triggered.connect(lambda: autorunTaskRun('add')) removeFromAutorunAction = menu.addAction("Disable autorun") removeFromAutorunAction.triggered.connect(lambda: autorunTaskRun('remove')) + restoreAction = menu.addAction("Restore Default") + restoreAction.triggered.connect(self.clearAppSettings) exitAction = menu.addAction("Exit") - exitAction.triggered.connect(self.close) + exitAction.triggered.connect(self.onExit) tray = QtWidgets.QSystemTrayIcon(self) tray.setIcon(trayIcon) tray.setContextMenu(menu) @@ -296,6 +299,41 @@ def updateOutput(): self._loadAppSettings() def closeEvent(self, event): + exit_value = self.settings.value(SettingsKey.MinimizeOnCloseFlag.value) + if exit_value == 1: + self.onExit() + elif exit_value == 2: + event.ignore() + self.hide() + else: + msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question, 'Exit', + "Do you want to exit or minimize to tray?", QtWidgets.QMessageBox.Yes | + QtWidgets.QMessageBox.No, self) + + exit_button = msg_box.button(QtWidgets.QMessageBox.Yes) + exit_button.setText('Exit') + + hide_button = msg_box.button(QtWidgets.QMessageBox.No) + hide_button.setText('Minimize') + + cbClose = QtWidgets.QCheckBox('Don\'t ask me again.', self) + msg_box.setCheckBox(cbClose) + + reply = msg_box.exec_() + + if reply == QtWidgets.QMessageBox.Yes: + if cbClose.isChecked(): + self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 1) + self.onExit() + else: + if cbClose.isChecked(): + self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 2) + event.ignore() + self.hide() + + # onExit() connected to systray_Exit + def onExit(self): + # print("exit") self._saveAppSettings() # Set mode to Balanced before exit self._updateGaugesTask.stop() @@ -303,7 +341,7 @@ def closeEvent(self, event): self._modeSwitch.setChecked(ThermalMode.Balanced.value) if prevMode != ThermalMode.Balanced.value: alert("Mode changed", "Thermal mode has been reset to Balanced") - event.accept() + sys.exit(1) def _saveAppSettings(self): self.settings.setValue(SettingsKey.Mode.value, self._modeSwitch.getChecked()) @@ -324,14 +362,13 @@ def _loadAppSettings(self): savedTemp = self.settings.value(SettingsKey.GPUThresholdTemp.value) if savedTemp is not None: self._limitTempGPU.setCurrentText(str(savedTemp)) - def changeEvent(self, event): - # Intercept minimize event, hide window - if event.type() == QtCore.QEvent.WindowStateChange: - if self.windowState() & QtCore.Qt.WindowMinimized: - self.hide() - else: - self.show() - super().changeEvent(event) + def clearAppSettings(self): + msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Restore Default', + "Do you want to restore all settings to default?", QtWidgets.QMessageBox.Yes | + QtWidgets.QMessageBox.No, self) + reply = msg_box.exec_() + if reply == QtWidgets.QMessageBox.Yes: + self.settings.clear() def testWMIsupport(self): try: From 95a02345e9c864b957e53038230c056627244968 Mon Sep 17 00:00:00 2001 From: Alexander Chernoskutov Date: Tue, 6 Feb 2024 12:52:16 +0400 Subject: [PATCH 2/2] Code cleanup --- src/GUI/AppGUI.py | 82 ++++++++++++++++++++--------------------------- 1 file changed, 35 insertions(+), 47 deletions(-) diff --git a/src/GUI/AppGUI.py b/src/GUI/AppGUI.py index 1423e65..a9497a9 100644 --- a/src/GUI/AppGUI.py +++ b/src/GUI/AppGUI.py @@ -1,6 +1,6 @@ import sys, os, time from enum import Enum -from typing import Callable, Literal +from typing import Callable, Literal, Optional, Tuple from PySide6 import QtCore, QtGui, QtWidgets from Backend.AWCCThermal import AWCCThermal, NoAWCCWMIClass, CannotInstAWCCWMI from GUI.QRadioButtonSet import QRadioButtonSet @@ -36,16 +36,29 @@ def autorunTask(action: Literal['add', 'remove']) -> int: else: return os.system(removeCmd) -def alert(title: str, message: str, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information, *, message2: str = None) -> None: - msg = QtWidgets.QMessageBox() +def alert(title: str, message: str, type: QtWidgets.QMessageBox.Icon = QtWidgets.QMessageBox.Icon.Information, *, message2: Optional[str] = None) -> None: + msg = QtWidgets.QMessageBox(type, title, message) msg.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON))) - msg.setIcon(type) - msg.setWindowTitle(title) - msg.setText(message) if message2: msg.setInformativeText(message2) msg.setStandardButtons(QtWidgets.QMessageBox.Ok) msg.exec() +def confirm(title: str, message: str, options: Optional[Tuple[str, str]] = None, dontAskAgain: bool = False) -> Tuple[bool, Optional[bool]]: + msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question, title, message, QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No) + msg.setWindowIcon(QtGui.QIcon(resourcePath(GUI_ICON))) + + if options is not None: + msg.button(QtWidgets.QMessageBox.Yes).setText(options[0]) + msg.button(QtWidgets.QMessageBox.No).setText(options[1]) + + cbDontAskAgain = None + if dontAskAgain: + cbDontAskAgain = QtWidgets.QCheckBox('Don\'t ask me again.', msg) + msg.setCheckBox(cbDontAskAgain) + + return (msg.exec_() == QtWidgets.QMessageBox.Yes, cbDontAskAgain is not None and cbDontAskAgain.isChecked() or None) + + class QPeriodic: def __init__(self, parent: QtCore.QObject, periodMs: int, callback: Callable) -> None: self._tmr = QtCore.QTimer(parent) @@ -68,9 +81,9 @@ class SettingsKey(Enum): CPUThresholdTemp = "app/fan/cpu/threshold_temp" GPUFanSpeed = "app/fan/gpu/speed" GPUThresholdTemp = "app/fan/gpu/threshold_temp" - MinimizeOnCloseFlag = "app/exit" + MinimizeOnCloseFlag = "app/minimize_on_close_flag" -def errorExit(message: str, message2: str = None) -> None: +def errorExit(message: str, message2: Optional[str] = None) -> None: if not QtWidgets.QApplication.instance(): QtWidgets.QApplication([]) alert("Oh-oh", message, QtWidgets.QMessageBox.Icon.Critical, message2 = message2) @@ -299,41 +312,24 @@ def updateOutput(): self._loadAppSettings() def closeEvent(self, event): - exit_value = self.settings.value(SettingsKey.MinimizeOnCloseFlag.value) - if exit_value == 1: - self.onExit() - elif exit_value == 2: + minimizeOnClose = self.settings.value(SettingsKey.MinimizeOnCloseFlag.value) + if minimizeOnClose is None: + # minimizeOnClose is not set, prompt user + (toExit, dontAskAgain) = confirm("Exit", "Do you want to exit or minimize to tray?", ("Exit", "Minimize"), True) + minimizeOnClose = not toExit + if dontAskAgain: + self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, minimizeOnClose) + + if minimizeOnClose: event.ignore() self.hide() else: - msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Question, 'Exit', - "Do you want to exit or minimize to tray?", QtWidgets.QMessageBox.Yes | - QtWidgets.QMessageBox.No, self) - - exit_button = msg_box.button(QtWidgets.QMessageBox.Yes) - exit_button.setText('Exit') - - hide_button = msg_box.button(QtWidgets.QMessageBox.No) - hide_button.setText('Minimize') - - cbClose = QtWidgets.QCheckBox('Don\'t ask me again.', self) - msg_box.setCheckBox(cbClose) - - reply = msg_box.exec_() - - if reply == QtWidgets.QMessageBox.Yes: - if cbClose.isChecked(): - self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 1) - self.onExit() - else: - if cbClose.isChecked(): - self.settings.setValue(SettingsKey.MinimizeOnCloseFlag.value, 2) - event.ignore() - self.hide() + self.onExit() + return # onExit() connected to systray_Exit def onExit(self): - # print("exit") + print("exit") self._saveAppSettings() # Set mode to Balanced before exit self._updateGaugesTask.stop() @@ -363,18 +359,10 @@ def _loadAppSettings(self): if savedTemp is not None: self._limitTempGPU.setCurrentText(str(savedTemp)) def clearAppSettings(self): - msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Restore Default', - "Do you want to restore all settings to default?", QtWidgets.QMessageBox.Yes | - QtWidgets.QMessageBox.No, self) - reply = msg_box.exec_() - if reply == QtWidgets.QMessageBox.Yes: + (isYes, _) = confirm("Reset to Default", "Do you want to reset all settings to default?", ("Reset", "Cancel")) + if isYes: self.settings.clear() - def testWMIsupport(self): - try: - pass - except: - pass def runApp(startMinimized = False) -> int: app = QtWidgets.QApplication([])