根据不同账号 显示的首页优化 规划界面 悬浮显示 状态信息 全局急停状态统一 割刀 根据新方案对接 两个状态 任务路线 显示为 抽拉式一行一个 xbox组件优化数据发送,重构数据
This commit is contained in:
@@ -1,122 +1,33 @@
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
|
||||
interface SteerButtonProps {
|
||||
icon: React.ReactNode;
|
||||
label: string;
|
||||
color: string;
|
||||
onPressStart?: () => void;
|
||||
onPressEnd?: () => void;
|
||||
interval?: number;
|
||||
// 受控高亮:由父组件统一管理,保证同组按钮互斥(不会同时显示为按下)
|
||||
active?: boolean;
|
||||
disabled?: boolean;
|
||||
onToggle?: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* 锁存按钮(受控):
|
||||
* 高亮状态完全由 active 决定,点击只触发 onToggle。
|
||||
* 持续发送 / 互斥逻辑由父组件统一管理。
|
||||
*/
|
||||
const SteerButton = ({
|
||||
icon,
|
||||
label,
|
||||
color,
|
||||
onPressStart,
|
||||
onPressEnd,
|
||||
interval = 100,
|
||||
active = false,
|
||||
disabled = false,
|
||||
onToggle,
|
||||
}: SteerButtonProps) => {
|
||||
const [pressed, setPressed] = useState(false);
|
||||
|
||||
// 是否处于按下状态
|
||||
const holdingRef = useRef(false);
|
||||
|
||||
// 定时发送句柄
|
||||
const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
|
||||
// 保存最新的回调,避免定时器使用过期闭包
|
||||
const onPressStartRef = useRef(onPressStart);
|
||||
const onPressEndRef = useRef(onPressEnd);
|
||||
|
||||
useEffect(() => {
|
||||
onPressStartRef.current = onPressStart;
|
||||
}, [onPressStart]);
|
||||
|
||||
useEffect(() => {
|
||||
onPressEndRef.current = onPressEnd;
|
||||
}, [onPressEnd]);
|
||||
|
||||
// 停止长按
|
||||
const stopHold = useCallback(() => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
holdingRef.current = false;
|
||||
|
||||
if (timerRef.current !== null) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
|
||||
setPressed(false);
|
||||
|
||||
// 只在有效按压结束时发送一次释放数据
|
||||
onPressEndRef.current?.();
|
||||
}, []);
|
||||
|
||||
// 开始长按
|
||||
const startHold = useCallback(() => {
|
||||
// 防止重复触发
|
||||
if (holdingRef.current) return;
|
||||
|
||||
holdingRef.current = true;
|
||||
setPressed(true);
|
||||
|
||||
// 立即发送第一帧
|
||||
onPressStartRef.current?.();
|
||||
|
||||
// 持续发送
|
||||
const scheduleNext = () => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
timerRef.current = setTimeout(() => {
|
||||
if (!holdingRef.current) return;
|
||||
|
||||
onPressStartRef.current?.();
|
||||
|
||||
scheduleNext();
|
||||
}, interval);
|
||||
};
|
||||
|
||||
scheduleNext();
|
||||
}, [interval]);
|
||||
|
||||
// 组件卸载时清理定时器
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
holdingRef.current = false;
|
||||
|
||||
if (timerRef.current !== null) {
|
||||
clearTimeout(timerRef.current);
|
||||
timerRef.current = null;
|
||||
}
|
||||
};
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<button
|
||||
type="button"
|
||||
aria-label={label}
|
||||
aria-pressed={pressed}
|
||||
onPointerDown={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
// 仅响应鼠标左键,触摸和笔不受影响
|
||||
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
||||
|
||||
// 捕获指针,确保在按钮外松开也能收到事件
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
|
||||
startHold();
|
||||
}}
|
||||
onPointerUp={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
stopHold();
|
||||
}}
|
||||
onPointerCancel={stopHold}
|
||||
onLostPointerCapture={stopHold}
|
||||
aria-pressed={active}
|
||||
disabled={disabled}
|
||||
onClick={onToggle}
|
||||
onContextMenu={(e) => e.preventDefault()}
|
||||
style={{
|
||||
flex: '1 1 0',
|
||||
@@ -128,15 +39,18 @@ const SteerButton = ({
|
||||
minHeight: 46,
|
||||
padding: '6px 4px',
|
||||
borderRadius: 8,
|
||||
border: `1px solid ${pressed ? color : `${color}55`}`,
|
||||
border: `1px solid ${active ? color : `${color}55`}`,
|
||||
background: active ? `${color}22` : 'transparent',
|
||||
color,
|
||||
cursor: 'pointer',
|
||||
cursor: disabled ? 'not-allowed' : 'pointer',
|
||||
opacity: disabled ? 0.4 : 1,
|
||||
userSelect: 'none',
|
||||
touchAction: 'none',
|
||||
transform: pressed ? 'scale(0.94)' : 'scale(1)',
|
||||
boxShadow: pressed ? `0 0 14px ${color}66` : 'none',
|
||||
transform: active ? 'scale(0.97)' : 'scale(1)',
|
||||
boxShadow: active ? `0 0 14px ${color}66, inset 0 0 8px ${color}22` : 'none',
|
||||
fontWeight: active ? 700 : 500,
|
||||
transition:
|
||||
'transform .08s, background .12s, border-color .12s, box-shadow .12s',
|
||||
'transform .08s, background .12s, border-color .12s, box-shadow .12s, font-weight .12s, opacity .12s',
|
||||
}}
|
||||
>
|
||||
<span style={{ display: 'inline-flex', fontSize: 16, lineHeight: 1 }}>
|
||||
|
||||
Reference in New Issue
Block a user