diff --git a/src/components/ProgressMonitor/ProgressMonitor.tsx b/src/components/ProgressMonitor/ProgressMonitor.tsx index 2d26bad..1cf3d6e 100644 --- a/src/components/ProgressMonitor/ProgressMonitor.tsx +++ b/src/components/ProgressMonitor/ProgressMonitor.tsx @@ -71,12 +71,12 @@ export function ProgressMonitor() { const isMaskTraceComplete = machineStatus === MachineStatus.MASK_TRACE_COMPLETE; - // Use PEN stitch count as fallback when machine reports 0 total stitches - const totalStitches = patternInfo - ? patternInfo.totalStitches === 0 && displayPattern?.penStitches - ? displayPattern.penStitches.stitches.length - : patternInfo.totalStitches - : 0; + // Use our own PEN stitch count as the source of truth for total stitches. + // The machine's patternInfo.totalStitches uses a different counting method than + // currentStitch (e.g. excludes lock stitches), so it can't be used as the max. + const totalStitches = displayPattern?.penStitches + ? displayPattern.penStitches.stitches.length + : (patternInfo?.totalStitches ?? 0); // Use adjustedStitchIndex (from step control) when available, otherwise machine-reported const currentStitch = @@ -156,8 +156,10 @@ export function ProgressMonitor() { totalStitches={totalStitches} lastRolledBackError={lastRolledBackError} colorBlocks={colorBlocks} - onAdjustPosition={adjustStitchPosition} - onSetPosition={setStitchPosition} + onAdjustPosition={(offset) => + adjustStitchPosition(offset, totalStitches) + } + onSetPosition={(index) => setStitchPosition(index, totalStitches)} /> )} diff --git a/src/stores/useMachineStore.ts b/src/stores/useMachineStore.ts index de9faa3..10ba897 100644 --- a/src/stores/useMachineStore.ts +++ b/src/stores/useMachineStore.ts @@ -65,8 +65,8 @@ interface MachineState { startSewing: () => Promise; resumeSewing: () => Promise; deletePattern: () => Promise; - setStitchPosition: (index: number) => Promise; - adjustStitchPosition: (offset: number) => Promise; + setStitchPosition: (index: number, maxStitches?: number) => Promise; + adjustStitchPosition: (offset: number, maxStitches?: number) => Promise; // Initialization initialize: () => void; @@ -338,11 +338,11 @@ export const useMachineStore = create((set, get) => ({ }, // Set stitch position to an absolute index - setStitchPosition: async (index: number) => { + setStitchPosition: async (index: number, maxStitches?: number) => { const { isConnected, service, patternInfo } = get(); if (!isConnected) return; - const totalStitches = patternInfo?.totalStitches || 0; + const totalStitches = maxStitches ?? patternInfo?.totalStitches ?? 0; const clamped = Math.max(0, Math.min(index, totalStitches)); try { @@ -359,11 +359,11 @@ export const useMachineStore = create((set, get) => ({ }, // Adjust stitch position by a relative offset - adjustStitchPosition: async (offset: number) => { + adjustStitchPosition: async (offset: number, maxStitches?: number) => { const { sewingProgress, adjustedStitchIndex } = get(); const currentIndex = adjustedStitchIndex ?? sewingProgress?.currentStitch ?? 0; - await get().setStitchPosition(currentIndex + offset); + await get().setStitchPosition(currentIndex + offset, maxStitches); }, // Handle automatic stitch rollback for thread errors