Add option to trigger "selection changed" callback in realtime

This commit is contained in:
Rob Watson 2022-01-15 18:14:16 +01:00
parent bb3366ac9a
commit f386e12f72
4 changed files with 29 additions and 13 deletions

View File

@ -214,10 +214,18 @@ function App(): JSX.Element {
}; };
// handler called when the selection in the main waveform view is changed. // handler called when the selection in the main waveform view is changed.
const handleWaveformSelectionChange = (newSelection: Frames) => { const handleWaveformSelectionChange = (
newSelection: Frames,
final: boolean
) => {
if (mediaSet == null) {
return;
}
setSelection(newSelection); setSelection(newSelection);
if (mediaSet == null) { // only update playback position when the selection is final.
if (!final) {
return; return;
} }

View File

@ -14,7 +14,7 @@ interface Props {
styles: Styles; styles: Styles;
position: number | null; position: number | null;
selection: Selection; selection: Selection;
onSelectionChange: (selection: Selection) => void; onSelectionChange: (selection: Selection, final: boolean) => void;
} }
enum Mode { enum Mode {
@ -79,6 +79,10 @@ export const HudCanvas: React.FC<Props> = ({
}; };
}, [mode, newSelection]); }, [mode, newSelection]);
useEffect(() => {
onSelectionChange({ ...newSelection }, mode == Mode.Normal);
}, [mode, newSelection]);
// draw the overview HUD // draw the overview HUD
useEffect(() => { useEffect(() => {
requestAnimationFrame(() => { requestAnimationFrame(() => {
@ -279,8 +283,6 @@ export const HudCanvas: React.FC<Props> = ({
handleEmptySelectionAction(); handleEmptySelectionAction();
return; return;
} }
onSelectionChange({ ...newSelection });
}; };
const handleEmptySelectionAction = () => { const handleEmptySelectionAction = () => {
@ -289,7 +291,8 @@ export const HudCanvas: React.FC<Props> = ({
setNewSelection({ ...selection }); setNewSelection({ ...selection });
break; break;
case EmptySelectionAction.SelectNothing: case EmptySelectionAction.SelectNothing:
onSelectionChange({ start: 0, end: 0 }); setMode(Mode.Normal);
setNewSelection({ start: 0, end: 0 });
break; break;
} }
}; };

View File

@ -56,7 +56,10 @@ export const Overview: React.FC<Props> = ({
// handlers // handlers
// convert selection change from canvas pixels to frames, and trigger callback. // convert selection change from canvas pixels to frames, and trigger callback.
const handleSelectionChange = ({ start, end }: Selection) => { const handleSelectionChange = ({ start, end }: Selection, final: boolean) => {
if (!final) {
return;
}
onSelectionChange({ onSelectionChange({
start: Math.round((start / CanvasLogicalWidth) * mediaSet.audioFrames), start: Math.round((start / CanvasLogicalWidth) * mediaSet.audioFrames),
end: Math.round((end / CanvasLogicalWidth) * mediaSet.audioFrames), end: Math.round((end / CanvasLogicalWidth) * mediaSet.audioFrames),

View File

@ -10,7 +10,7 @@ interface Props {
mediaSet: MediaSet; mediaSet: MediaSet;
position: VideoPosition; position: VideoPosition;
viewport: Frames; viewport: Frames;
onSelectionChange: (selection: Selection) => void; onSelectionChange: (selection: Selection, final: boolean) => void;
} }
export const CanvasLogicalWidth = 2000; export const CanvasLogicalWidth = 2000;
@ -91,17 +91,19 @@ export const Waveform: React.FC<Props> = ({
// handlers // handlers
const handleSelectionChange = (selection: Selection) => { const handleSelectionChange = (selection: Selection, final: boolean) => {
setSelectedPixels(selection);
const framesPerPixel = (viewport.end - viewport.start) / CanvasLogicalWidth; const framesPerPixel = (viewport.end - viewport.start) / CanvasLogicalWidth;
const selectedFrames = { const selectedFrames = {
start: Math.round(viewport.start + selection.start * framesPerPixel), start: Math.round(viewport.start + selection.start * framesPerPixel),
end: Math.round(viewport.start + selection.end * framesPerPixel), end: Math.round(viewport.start + selection.end * framesPerPixel),
}; };
setSelectedFrames(selectedFrames); if (final) {
onSelectionChange(selectedFrames); setSelectedPixels(selection);
setSelectedFrames(selectedFrames);
}
onSelectionChange(selectedFrames, final);
}; };
// helpers // helpers