hydraveil-gui/gui/v2/ui/popups/terminal_threading.py

118 lines
4.3 KiB
Python
Executable file

from gui.v2.infrastructure.thread_singleton import get_main_thread_dispatcher
from core.observers.Notifications import Notifications, EventType
from gui.v2.ui.styles.css.terminal_style import (
TERMINAL_TITLE_LABEL, OVERALL_STYLE_SHEET_COLORS,
STATUS_LABEL_STYLE, OUTPUT_TEXT_STYLE
)
from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QTextEdit,
QLabel)
from PyQt6.QtGui import QFont, QColor
from PyQt6.QtCore import Qt, QTimer
from PyQt6.QtWidgets import QGraphicsDropShadowEffect
from PyQt6.QtGui import QTextCharFormat
mainthread = get_main_thread_dispatcher()
INITIAL_PLACEHOLDER = "◄ WAITING FOR TASK START ►"
DISPLAYED_TITLE = "▓░ NOTIFICATION TERMINAL ░▓"
INIT_WINDOW_TITLE = "SYNC PROCESS"
class NotificationTerminalUI(QDialog):
def __init__(self):
super().__init__()
self.synchronized = False
self.worker_thread = None
self.task_success = False
Notifications.subscribe(EventType.STATUS_UPDATE, self.add_notification)
Notifications.subscribe(EventType.SYNC_COMPLETE, self.on_synchronized)
self.init_ui()
def init_ui(self):
self.setWindowTitle(INIT_WINDOW_TITLE)
self.setGeometry(100, 100, 700, 600)
self.setModal(True)
layout = QVBoxLayout(self)
layout.setSpacing(15)
layout.setContentsMargins(30, 30, 30, 30)
self.setStyleSheet(OVERALL_STYLE_SHEET_COLORS)
# Title Label
title_label = QLabel(DISPLAYED_TITLE)
title_label.setFont(QFont("Courier New", 14, QFont.Weight.Bold))
title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
title_label.setStyleSheet(TERMINAL_TITLE_LABEL)
title_shadow = QGraphicsDropShadowEffect()
title_shadow.setBlurRadius(15)
title_shadow.setColor(QColor(0, 255, 136, 200))
title_shadow.setOffset(0, 0)
title_label.setGraphicsEffect(title_shadow)
layout.addWidget(title_label)
# Scrollable text display
self.output_text = QTextEdit()
self.output_text.setReadOnly(True)
self.output_text.setFont(QFont("Courier New", 10))
self.output_text.setMinimumHeight(400)
self.output_text.setStyleSheet(OUTPUT_TEXT_STYLE)
layout.addWidget(self.output_text)
# Status label
self.status_label = QLabel(INITIAL_PLACEHOLDER)
self.status_label.setFont(QFont("Courier New", 9))
self.status_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.status_label.setStyleSheet(STATUS_LABEL_STYLE)
layout.addWidget(self.status_label)
# Button layout
# button_layout = QHBoxLayout()
# # clear_btn = QPushButton("CLEAR")
# # clear_btn.setFont(QFont("Courier New", 10, QFont.Weight.Bold))
# # clear_btn.setMinimumHeight(40)
# # clear_btn.setCursor(Qt.CursorShape.PointingHandCursor)
# # clear_btn.setStyleSheet(CLEAR_BUTTON_STYLE)
# # clear_btn.clicked.connect(self.clear_output)
# shadow = QGraphicsDropShadowEffect()
# shadow.setBlurRadius(15)
# shadow.setColor(QColor(0, 212, 255))
# shadow.setOffset(0, 0)
# clear_btn.setGraphicsEffect(shadow)
# button_layout.addWidget(clear_btn)
# button_layout.addStretch()
# layout.addLayout(button_layout)
@mainthread
def add_notification(self, message: str, *args, **kwargs):
"""Queue to main thread safely."""
prefix = "Connect/Sync"
color = "#00ff88"
cursor = self.output_text.textCursor()
cursor.movePosition(cursor.MoveOperation.End)
fmt = QTextCharFormat()
fmt.setForeground(QColor(color))
cursor.insertText(f"[{prefix}] {message}\n", fmt)
self.output_text.setTextCursor(cursor)
self.output_text.ensureCursorVisible()
@mainthread
def set_status(self, text: str):
"""This runs on the main thread, safely."""
self.status_label.setText(text)
@mainthread
def on_synchronized(self, message: str):
"""Handle synchronized notification and exit."""
self.add_notification("SYNC", message, "#00ff88")
self.set_status("◄ SYNCHRONIZED - CLOSING ►")
QTimer.singleShot(500, self.accept)
# "Sync complete ✓"