app v1
This commit is contained in:
141
core/app.py
141
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"]
|
||||
)
|
||||
@@ -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)
|
||||
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)
|
||||
Reference in New Issue
Block a user