app v1
This commit is contained in:
139
core/app.py
139
core/app.py
@@ -1,4 +1,7 @@
|
|||||||
# v3.1.0 app.py
|
# v3.1.0 app.py
|
||||||
|
import atexit
|
||||||
|
import signal
|
||||||
|
import traceback
|
||||||
import contextlib
|
import contextlib
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
@@ -33,6 +36,7 @@ from PyQt6.QtGui import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from PyQt6.QtWidgets import (
|
from PyQt6.QtWidgets import (
|
||||||
|
QApplication,
|
||||||
QFrame,
|
QFrame,
|
||||||
QMenu,
|
QMenu,
|
||||||
QSizeGrip,
|
QSizeGrip,
|
||||||
@@ -137,6 +141,8 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__()
|
super().__init__()
|
||||||
|
self._is_quitting = False
|
||||||
|
self._force_close = False
|
||||||
|
|
||||||
self._closing = False
|
self._closing = False
|
||||||
|
|
||||||
@@ -185,6 +191,9 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
self.finalize_startup,
|
self.finalize_startup,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.register_shutdown_hooks()
|
||||||
|
|
||||||
|
|
||||||
def configure_font(self) -> None:
|
def configure_font(self) -> None:
|
||||||
self.font_object = QFont()
|
self.font_object = QFont()
|
||||||
|
|
||||||
@@ -264,17 +273,38 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
|
|
||||||
self.setWindowFlags(
|
self.setWindowFlags(
|
||||||
Qt.WindowType.Tool
|
Qt.WindowType.Tool
|
||||||
| Qt.WindowType.WindowStaysOnTopHint
|
|
||||||
| Qt.WindowType.FramelessWindowHint
|
| Qt.WindowType.FramelessWindowHint
|
||||||
|
| Qt.WindowType.WindowStaysOnTopHint
|
||||||
|
| Qt.WindowType.CustomizeWindowHint
|
||||||
)
|
)
|
||||||
|
|
||||||
self.setAttribute(
|
self.setAttribute(
|
||||||
Qt.WidgetAttribute.WA_TranslucentBackground,
|
Qt.WidgetAttribute.WA_TranslucentBackground,
|
||||||
True,
|
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)
|
self.setAutoFillBackground(False)
|
||||||
|
|
||||||
|
|
||||||
appearance = (
|
appearance = (
|
||||||
self.profile["appearance"]
|
self.profile["appearance"]
|
||||||
)
|
)
|
||||||
@@ -434,6 +464,7 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
| Qt.AlignmentFlag.AlignRight,
|
| Qt.AlignmentFlag.AlignRight,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
self.titleBar.hide()
|
||||||
def build_tray(self) -> None:
|
def build_tray(self) -> None:
|
||||||
if not self.profile[
|
if not self.profile[
|
||||||
"behavior"
|
"behavior"
|
||||||
@@ -474,7 +505,7 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
)
|
)
|
||||||
|
|
||||||
quit_action.triggered.connect(
|
quit_action.triggered.connect(
|
||||||
self.close
|
self.safe_exit
|
||||||
)
|
)
|
||||||
|
|
||||||
menu.addAction(open_settings)
|
menu.addAction(open_settings)
|
||||||
@@ -844,25 +875,17 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
"open_settings_failure"
|
"open_settings_failure"
|
||||||
)
|
)
|
||||||
|
|
||||||
def nativeEvent(
|
def nativeEvent(self, eventType, message):
|
||||||
self,
|
|
||||||
eventType,
|
|
||||||
message,
|
|
||||||
):
|
|
||||||
if getattr(
|
|
||||||
self,
|
|
||||||
"_closing",
|
|
||||||
False,
|
|
||||||
):
|
|
||||||
return False, 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
return super().nativeEvent(
|
return super().nativeEvent(eventType, message)
|
||||||
eventType,
|
except KeyboardInterrupt:
|
||||||
message,
|
self.safe_exit()
|
||||||
)
|
return False, 0
|
||||||
|
except SystemExit:
|
||||||
|
self.safe_exit()
|
||||||
|
return False, 0
|
||||||
except Exception:
|
except Exception:
|
||||||
|
traceback.print_exc()
|
||||||
return False, 0
|
return False, 0
|
||||||
|
|
||||||
def resizeEvent(self, event) -> None:
|
def resizeEvent(self, event) -> None:
|
||||||
@@ -921,22 +944,80 @@ class StickyNoteApp(AcrylicWindow):
|
|||||||
)
|
)
|
||||||
|
|
||||||
def closeEvent(self, event) -> None:
|
def closeEvent(self, event) -> None:
|
||||||
self._closing = True
|
if self._is_quitting or self._force_close:
|
||||||
|
try:
|
||||||
with contextlib.suppress(Exception):
|
|
||||||
self.save_state()
|
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()
|
||||||
|
|
||||||
|
try:
|
||||||
|
if hasattr(self, "save_timer"):
|
||||||
self.save_timer.stop()
|
self.save_timer.stop()
|
||||||
|
|
||||||
with contextlib.suppress(Exception):
|
if hasattr(self, "preview_timer"):
|
||||||
self.preview_timer.stop()
|
self.preview_timer.stop()
|
||||||
|
except Exception:
|
||||||
|
traceback.print_exc()
|
||||||
|
|
||||||
with contextlib.suppress(Exception):
|
try:
|
||||||
self.opacity_anim.stop()
|
|
||||||
|
|
||||||
with contextlib.suppress(Exception):
|
|
||||||
if hasattr(self, "tray"):
|
if hasattr(self, "tray"):
|
||||||
self.tray.hide()
|
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