sp-hydra-veil-gui/gui/v2/ui/pages/protocol_page.py
2026-08-21 11:39:18 -04:00

114 lines
4.5 KiB
Python
Executable file

import os
from PyQt6.QtWidgets import QPushButton, QLabel, QButtonGroup
from PyQt6.QtGui import QPixmap, QIcon
from PyQt6.QtCore import Qt
from PyQt6 import QtCore
from gui.v2.ui.pages.Page import Page
class ProtocolPage(Page):
SINGBOX_PROTOCOLS = ("hysteria2", "vless")
PROTOCOL_BUTTON_ASSETS = {
"hysteria2": "hystria2",
}
def __init__(self, page_stack, main_window=None, parent=None):
super().__init__("Protocol", page_stack, main_window, parent)
self.main_window = main_window
self.selected_protocol = None
self.btn_path = main_window.btn_path
self.selected_protocol_icon = None
self.connection_manager = main_window.connection_manager
self.button_back.setVisible(True)
self.update_status = main_window
self.replace_click_handler(self.button_back, self.reverse)
self.replace_click_handler(self.button_go, self.go_selected)
self.coming_soon_label = QLabel("Coming soon", self)
self.coming_soon_label.setGeometry(210, 50, 200, 40)
self.coming_soon_label.setStyleSheet("font-size: 22px;")
self.coming_soon_label.setVisible(False)
self.title.setGeometry(585, 40, 185, 40)
self.title.setText("Pick a Protocol")
self.display.setGeometry(QtCore.QRect(0, 50, 580, 435))
self.create_interface_elements()
if self.connection_manager.is_synced():
self.enable_protocol_buttons()
def create_interface_elements(self):
self.buttonGroup = QButtonGroup(self)
self.buttons = []
self.selected_page_name = None
for j, (object_type, icon_name, page_name, geometry) in enumerate([
(QPushButton, "wireguard", "wireguard", (585, 80, 185, 75)),
(QPushButton, "hysteria2", "location", (585, 160, 185, 75)),
(QPushButton, "vless", "location", (585, 240, 185, 75)),
(QPushButton, "residential", "residential", (585, 320, 185, 75)),
(QPushButton, "hidetor", "hidetor", (585, 400, 185, 75))
]):
boton = object_type(self)
boton.setGeometry(*geometry)
boton.setIconSize(boton.size())
boton.setCheckable(True)
boton.setDisabled(True)
boton.setIcon(
QIcon(self._button_asset(icon_name)))
self.buttons.append(boton)
self.buttonGroup.addButton(boton, j)
boton.clicked.connect(
lambda _, name=page_name, protocol=icon_name: self.show_protocol(name, protocol))
def _button_asset(self, protocol):
asset_name = self.PROTOCOL_BUTTON_ASSETS.get(protocol, protocol)
return os.path.join(self.btn_path, f"{asset_name}_button.png")
def _display_asset(self, protocol):
full_size_path = os.path.join(self.btn_path, f"{protocol}.png")
if os.path.exists(full_size_path):
return full_size_path
return self._button_asset(protocol)
def enable_protocol_buttons(self):
for button in self.buttons:
button.setDisabled(False)
def update_swarp_json(self):
data = {"protocol": self.selected_protocol_icon}
if self.selected_protocol_icon in self.SINGBOX_PROTOCOLS:
data["connection"] = "system-wide"
self.update_status.write_data(data)
def show_protocol(self, page_name, protocol):
self.update_status.clear_data()
self.display.setPixmap(QPixmap(self._display_asset(protocol)).scaled(
self.display.size(), Qt.AspectRatioMode.KeepAspectRatio))
self.selected_protocol_icon = protocol
self.selected_page_name = page_name
if protocol in ["wireguard", "hidetor", *self.SINGBOX_PROTOCOLS]:
self.button_go.setVisible(True)
self.coming_soon_label.setVisible(False)
else:
self.button_go.setVisible(False)
self.coming_soon_label.setVisible(True)
self.update_swarp_json()
def go_selected(self):
if self.selected_page_name:
self.custom_window.navigator.navigate(self.selected_page_name)
self.display.clear()
for boton in self.buttons:
boton.setChecked(False)
self.button_go.setVisible(False)
def find_menu_page(self):
return self.custom_window.navigator.get_cached("menu")
def reverse(self):
self.display.clear()
for boton in self.buttons:
boton.setChecked(False)
self.button_go.setVisible(False)
self.custom_window.navigator.navigate("menu")