From 8c7b9fe5215b67f115aa052f5be26cc943e419ec Mon Sep 17 00:00:00 2001 From: admin Date: Sun, 24 May 2026 01:54:09 +0000 Subject: [PATCH] app v1 --- core/app.py | 143 ++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 112 insertions(+), 31 deletions(-) diff --git a/core/app.py b/core/app.py index c137065..0ab0503 100644 --- a/core/app.py +++ b/core/app.py @@ -1,4 +1,7 @@ # v3.1.0 app.py +import atexit +import signal +import traceback import contextlib import json import logging @@ -33,6 +36,7 @@ from PyQt6.QtGui import ( ) from PyQt6.QtWidgets import ( + QApplication, QFrame, QMenu, QSizeGrip, @@ -137,6 +141,8 @@ class StickyNoteApp(AcrylicWindow): def __init__(self): super().__init__() + self._is_quitting = False + self._force_close = False self._closing = False @@ -185,6 +191,9 @@ class StickyNoteApp(AcrylicWindow): self.finalize_startup, ) + self.register_shutdown_hooks() + + def configure_font(self) -> None: self.font_object = QFont() @@ -264,17 +273,38 @@ class StickyNoteApp(AcrylicWindow): self.setWindowFlags( Qt.WindowType.Tool - | Qt.WindowType.WindowStaysOnTopHint | Qt.WindowType.FramelessWindowHint + | Qt.WindowType.WindowStaysOnTopHint + | Qt.WindowType.CustomizeWindowHint ) self.setAttribute( Qt.WidgetAttribute.WA_TranslucentBackground, True, ) + self.setAttribute( + Qt.WidgetAttribute.WA_NoSystemBackground, + True, + ) + + self.setWindowFlag( + Qt.WindowType.WindowMinMaxButtonsHint, + False, + ) + + self.setWindowFlag( + Qt.WindowType.WindowCloseButtonHint, + False, + ) + + self.setWindowFlag( + Qt.WindowType.CustomizeWindowHint, + True, + ) self.setAutoFillBackground(False) + appearance = ( self.profile["appearance"] ) @@ -295,7 +325,7 @@ class StickyNoteApp(AcrylicWindow): self.winId(), "00000001", ) - + except Exception: logging.exception( "window_effect_failure" @@ -434,6 +464,7 @@ class StickyNoteApp(AcrylicWindow): | Qt.AlignmentFlag.AlignRight, ) + self.titleBar.hide() def build_tray(self) -> None: if not self.profile[ "behavior" @@ -474,7 +505,7 @@ class StickyNoteApp(AcrylicWindow): ) quit_action.triggered.connect( - self.close + self.safe_exit ) menu.addAction(open_settings) @@ -844,25 +875,17 @@ class StickyNoteApp(AcrylicWindow): "open_settings_failure" ) - def nativeEvent( - self, - eventType, - message, - ): - if getattr( - self, - "_closing", - False, - ): - return False, 0 - + def nativeEvent(self, eventType, message): try: - return super().nativeEvent( - eventType, - message, - ) - + return super().nativeEvent(eventType, message) + except KeyboardInterrupt: + self.safe_exit() + return False, 0 + except SystemExit: + self.safe_exit() + return False, 0 except Exception: + traceback.print_exc() return False, 0 def resizeEvent(self, event) -> None: @@ -921,22 +944,80 @@ class StickyNoteApp(AcrylicWindow): ) def closeEvent(self, event) -> None: - self._closing = True + if self._is_quitting or self._force_close: + try: + self.save_state() + except Exception: + traceback.print_exc() - with contextlib.suppress(Exception): + event.accept() + return + + event.ignore() + self.hide() + + def register_shutdown_hooks(self) -> None: + app = QApplication.instance() + + def handle_shutdown(*_) -> None: + self.safe_exit() + + atexit.register(handle_shutdown) + + for sig in (signal.SIGINT, signal.SIGTERM): + try: + signal.signal(sig, handle_shutdown) + except Exception: + pass + + if app is not None: + app.aboutToQuit.connect( + lambda: setattr( + self, + "_is_quitting", + True, + ) + ) + + def safe_exit(self) -> None: + if self._is_quitting: + return + + self._is_quitting = True + + try: self.save_state() + except Exception: + traceback.print_exc() - with contextlib.suppress(Exception): - self.save_timer.stop() + try: + if hasattr(self, "save_timer"): + self.save_timer.stop() - with contextlib.suppress(Exception): - self.preview_timer.stop() + if hasattr(self, "preview_timer"): + self.preview_timer.stop() + except Exception: + traceback.print_exc() - with contextlib.suppress(Exception): - self.opacity_anim.stop() - - with contextlib.suppress(Exception): + try: if hasattr(self, "tray"): self.tray.hide() + self.tray.deleteLater() + except Exception: + traceback.print_exc() - super().closeEvent(event) \ No newline at end of file + try: + self.hide() + self.deleteLater() + except Exception: + traceback.print_exc() + + app = QApplication.instance() + + if app is not None: + app.quit() + + try: + app.quit() + except: + sys.exit(0) \ No newline at end of file