import React, { useMemo } from 'react'; import clsx from 'clsx'; import { Tooltip } from '@heroui/tooltip'; import { useTheme } from '@/hooks/use-theme'; interface UsagePieProps { systemUsage: number; processUsage: number; title?: string; hasBackground?: boolean; } const UsagePie: React.FC = ({ systemUsage, processUsage, title, hasBackground, }) => { const { theme } = useTheme(); // Ensure values are clean and consistent // Process usage cannot exceed system usage, and system usage cannot be less than process usage. const rawSystem = Math.max(systemUsage || 0, 0); const rawProcess = Math.max(processUsage || 0, 0); const cleanSystem = Math.min(Math.max(rawSystem, rawProcess), 100); const cleanProcess = Math.min(rawProcess, cleanSystem); // SVG Config const size = 100; const strokeWidth = 10; const radius = (size - strokeWidth) / 2; const circumference = 2 * Math.PI * radius; const center = size / 2; // Colors const colors = { qq: '#D33FF0', other: theme === 'dark' ? '#EF8664' : '#EA7D9B', track: theme === 'dark' ? 'rgba(255,255,255,0.08)' : 'rgba(0,0,0,0.05)', }; // Dash Arrays // 1. Total System Usage (QQ + Others) const systemDash = useMemo(() => { return `${(cleanSystem / 100) * circumference} ${circumference}`; }, [cleanSystem, circumference]); // 2. QQ Usage (Subset of System) const processDash = useMemo(() => { return `${(cleanProcess / 100) * circumference} ${circumference}`; }, [cleanProcess, circumference]); // 计算其他进程占用(系统总占用 - QQ占用) const otherUsage = Math.max(cleanSystem - cleanProcess, 0); // Tooltip 内容 const tooltipContent = (
QQ进程: {cleanProcess.toFixed(1)}%
其他进程: {otherUsage.toFixed(1)}%
空闲: {(100 - cleanSystem).toFixed(1)}%
); return (
{/* Track / Free Space */} {/* System Usage (Background for QQ) - effectively "Others" + "QQ" */} {/* QQ Usage - Layered on top */} {/* Center Content */}
{title && ( {title} )}
{Math.round(cleanSystem)} %
); }; export default UsagePie;