145 lines
5.2 KiB
Python
145 lines
5.2 KiB
Python
import sys
|
|
from pathlib import Path
|
|
import logging
|
|
import os
|
|
from PIL import Image
|
|
from PyQt5.QtWidgets import (QMainWindow, QWidget, QVBoxLayout, QHBoxLayout,
|
|
QLabel, QLineEdit, QPushButton, QFrame, QTextEdit,
|
|
QMessageBox, QApplication, QSystemTrayIcon, QMenu,
|
|
QDialog, QProgressBar, QStyle)
|
|
from PyQt5.QtCore import Qt, QTimer, QThread, pyqtSignal
|
|
from PyQt5.QtGui import QIcon, QPixmap
|
|
import time
|
|
import requests
|
|
from urllib.parse import quote
|
|
import subprocess
|
|
|
|
sys.path.append(str(Path(__file__).parent.parent))
|
|
|
|
from utils.config import Config
|
|
from utils.version_manager import VersionManager
|
|
from account_switcher import AccountSwitcher
|
|
|
|
class MainWindow(QMainWindow):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.config = Config()
|
|
self.switcher = AccountSwitcher()
|
|
self.version_manager = VersionManager()
|
|
|
|
# 添加激活状态缓存
|
|
self._activation_status = None # 缓存的激活状态
|
|
self._status_timer = None # 状态更新定时器
|
|
|
|
# 添加心跳定时器
|
|
self._heartbeat_timer = QTimer()
|
|
self._heartbeat_timer.timeout.connect(self.send_heartbeat)
|
|
self._heartbeat_timer.start(5 * 60 * 1000) # 每5分钟发送一次心跳
|
|
|
|
# 添加请求锁,防止重复提交
|
|
self._is_requesting = False
|
|
self._last_request_time = 0
|
|
self._request_cooldown = 2 # 请求冷却时间(秒)
|
|
|
|
version = get_version()
|
|
cursor_version = self.switcher.get_cursor_version()
|
|
self.setWindowTitle(f"听泉Cursor助手 v{version} (本机Cursor版本: {cursor_version})")
|
|
self.setMinimumSize(600, 680) # 增加最小宽度到600
|
|
|
|
# 设置窗口图标
|
|
icon_path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "icon", "two.ico")
|
|
if os.path.exists(icon_path):
|
|
self.window_icon = QIcon(icon_path)
|
|
if not self.window_icon.isNull():
|
|
self.setWindowIcon(self.window_icon)
|
|
logging.info(f"成功设置窗口图标: {icon_path}")
|
|
else:
|
|
logging.warning("图标文件加载失败")
|
|
|
|
# 创建系统托盘图标
|
|
self.create_tray_icon()
|
|
|
|
# 创建主窗口部件
|
|
central_widget = QWidget()
|
|
self.setCentralWidget(central_widget)
|
|
|
|
# 设置主窗口样式
|
|
central_widget.setStyleSheet("""
|
|
QWidget {
|
|
background: qlineargradient(x1:0, y1:0, x2:0, y2:1,
|
|
stop:0 #f8f9fa,
|
|
stop:0.5 #ffffff,
|
|
stop:1 #f8f9fa);
|
|
}
|
|
QFrame {
|
|
border: none;
|
|
background: transparent;
|
|
}
|
|
""")
|
|
|
|
# 创建主布局
|
|
main_layout = QVBoxLayout(central_widget)
|
|
main_layout.setSpacing(12) # 减小区域间距
|
|
main_layout.setContentsMargins(20, 15, 20, 15) # 调整外边距
|
|
|
|
# 启动时检查一次状态
|
|
QTimer.singleShot(0, self.check_status)
|
|
|
|
# 启动时自动检查更新
|
|
QTimer.singleShot(1000, self.check_for_updates)
|
|
|
|
def send_heartbeat(self):
|
|
"""发送心跳请求"""
|
|
if not self._check_request_throttle():
|
|
return
|
|
|
|
def heartbeat_func():
|
|
return self.switcher.send_heartbeat()
|
|
|
|
# 创建工作线程
|
|
self.heartbeat_worker = ApiWorker(heartbeat_func)
|
|
self.heartbeat_worker.finished.connect(self.on_heartbeat_complete)
|
|
self.heartbeat_worker.start()
|
|
|
|
def on_heartbeat_complete(self, result):
|
|
"""心跳完成回调"""
|
|
success, message = result
|
|
self._request_complete()
|
|
|
|
if success:
|
|
logging.info(f"心跳发送成功: {message}")
|
|
# 更新状态显示
|
|
self.check_status()
|
|
else:
|
|
logging.error(f"心跳发送失败: {message}")
|
|
|
|
def closeEvent(self, event):
|
|
"""窗口关闭事件"""
|
|
try:
|
|
if hasattr(self, 'tray_icon') and self.tray_icon.isVisible():
|
|
event.ignore()
|
|
self.hide()
|
|
# 确保托盘图标显示
|
|
self.tray_icon.show()
|
|
self.tray_icon.showMessage(
|
|
"听泉Cursor助手",
|
|
"程序已最小化到系统托盘",
|
|
QSystemTrayIcon.Information,
|
|
2000
|
|
)
|
|
else:
|
|
# 如果托盘图标不可用,则正常退出
|
|
if self._status_timer:
|
|
self._status_timer.stop()
|
|
if hasattr(self, '_heartbeat_timer'):
|
|
self._heartbeat_timer.stop()
|
|
event.accept()
|
|
|
|
except Exception as e:
|
|
logging.error(f"处理关闭事件时发生错误: {str(e)}")
|
|
# 发生错误时,接受关闭事件
|
|
if self._status_timer:
|
|
self._status_timer.stop()
|
|
if hasattr(self, '_heartbeat_timer'):
|
|
self._heartbeat_timer.stop()
|
|
event.accept() |