From 700c501b0887b136424d343ccd1d7f2fcfaeec62 Mon Sep 17 00:00:00 2001 From: Mikael Finstad Date: Sat, 15 Feb 2020 00:01:34 +0800 Subject: [PATCH] Automatically start NEW cut segment #168 --- src/renderer.jsx | 73 ++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 31 deletions(-) diff --git a/src/renderer.jsx b/src/renderer.jsx index 8a4762ec..116aa006 100644 --- a/src/renderer.jsx +++ b/src/renderer.jsx @@ -155,7 +155,48 @@ const App = memo(() => { return formatDuration({ seconds: sec, fps: timecodeShowFrames ? detectedFps : undefined }); } - const setCutStart = useCallback(() => setCutTime('start', currentTime), [setCutTime, currentTime]); + const getCutSeg = useCallback((i) => cutSegments[i !== undefined ? i : currentSeg], + [currentSeg, cutSegments]); + + const getCutStartTime = useCallback((i) => getCutSeg(i).start, [getCutSeg]); + const getCutEndTime = useCallback((i) => getCutSeg(i).end, [getCutSeg]); + + const addCutSegment = useCallback(() => { + const cutStartTime = getCutStartTime(); + const cutEndTime = getCutEndTime(); + + if (cutStartTime === undefined && cutEndTime === undefined) return; + + const suggestedStart = currentTime; + const suggestedEnd = suggestedStart + 10; + + const cutSegmentsNew = [ + ...cutSegments, + createSegment({ + start: currentTime, + end: suggestedEnd <= duration ? suggestedEnd : undefined, + }), + ]; + + const currentSegNew = cutSegmentsNew.length - 1; + setCutSegments(cutSegmentsNew); + setCurrentSeg(currentSegNew); + }, [ + getCutEndTime, getCutStartTime, cutSegments, currentTime, duration, + ]); + + const setCutStart = useCallback(() => { + const curSeg = getCutSeg(); + // https://github.com/mifi/lossless-cut/issues/168 + // If we are after the end of the last segment in the timeline, + // add a new segment that starts at currentTime + if (curSeg.start != null && curSeg.end != null && currentTime > curSeg.end) { + addCutSegment(); + } else { + setCutTime('start', currentTime); + } + }, [setCutTime, currentTime, getCutSeg, addCutSegment]); + const setCutEnd = useCallback(() => setCutTime('end', currentTime), [setCutTime, currentTime]); async function setOutputDir() { @@ -223,12 +264,6 @@ const App = memo(() => { setRotationPreviewRequested(true); } - const getCutSeg = useCallback((i) => cutSegments[i !== undefined ? i : currentSeg], - [currentSeg, cutSegments]); - - const getCutStartTime = useCallback((i) => getCutSeg(i).start, [getCutSeg]); - const getCutEndTime = useCallback((i) => getCutSeg(i).end, [getCutSeg]); - const getApparentCutStartTime = useCallback((i) => { const cutStartTime = getCutStartTime(i); if (cutStartTime !== undefined) return cutStartTime; @@ -266,30 +301,6 @@ const App = memo(() => { const toggleKeyframeCut = () => setKeyframeCut(val => !val); const toggleAutoMerge = () => setAutoMerge(val => !val); - const addCutSegment = useCallback(() => { - const cutStartTime = getCutStartTime(); - const cutEndTime = getCutEndTime(); - - if (cutStartTime === undefined && cutEndTime === undefined) return; - - const suggestedStart = currentTime; - const suggestedEnd = suggestedStart + 10; - - const cutSegmentsNew = [ - ...cutSegments, - createSegment({ - start: currentTime, - end: suggestedEnd <= duration ? suggestedEnd : undefined, - }), - ]; - - const currentSegNew = cutSegmentsNew.length - 1; - setCutSegments(cutSegmentsNew); - setCurrentSeg(currentSegNew); - }, [ - getCutEndTime, getCutStartTime, cutSegments, currentTime, duration, - ]); - const removeCutSegment = useCallback(() => { if (cutSegments.length < 2) return;