From 545ac72faa3a7d2c913cff1ab988c65d04e9f809 Mon Sep 17 00:00:00 2001 From: Rob Watson Date: Sun, 12 Dec 2021 11:04:23 +0100 Subject: [PATCH] HudCanvas: handle select-nothing --- frontend/src/HudCanvas.tsx | 22 ++++++++++++++++++++-- frontend/src/Overview.tsx | 3 ++- frontend/src/Waveform.tsx | 3 ++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/frontend/src/HudCanvas.tsx b/frontend/src/HudCanvas.tsx index e23a1f9..2c60362 100644 --- a/frontend/src/HudCanvas.tsx +++ b/frontend/src/HudCanvas.tsx @@ -1,4 +1,4 @@ -import { useState, useEffect, useRef, MouseEvent } from 'react'; +import { useState, useEffect, useRef, useCallback, MouseEvent } from 'react'; interface Styles { borderLineWidth: number; @@ -11,6 +11,7 @@ interface Props { width: number; height: number; zIndex: number; + emptySelectionAction: EmptySelectionAction; styles: Styles; position: number | null; selection: Selection; @@ -32,6 +33,11 @@ enum HoverState { OverSelection, } +export enum EmptySelectionAction { + SelectNothing, + SelectPrevious, +} + export interface Selection { start: number; end: number; @@ -43,6 +49,7 @@ export const HudCanvas: React.FC = ({ width, height, zIndex, + emptySelectionAction, styles: { borderLineWidth, borderStrokeStyle, @@ -271,13 +278,24 @@ export const HudCanvas: React.FC = ({ setCursor('auto'); if (newSelection.start == newSelection.end) { - setNewSelection({ start: 0, end: 0 }); + handleEmptySelectionAction(); return; } onSelectionChange({ ...newSelection }); }; + const handleEmptySelectionAction = useCallback(() => { + switch (emptySelectionAction) { + case EmptySelectionAction.SelectPrevious: + setNewSelection({ ...selection }); + break; + case EmptySelectionAction.SelectNothing: + onSelectionChange({ start: 0, end: 0 }); + break; + } + }, [selection]); + const handleMouseLeave = (_evt: MouseEvent) => { setHoverState(HoverState.Normal); }; diff --git a/frontend/src/Overview.tsx b/frontend/src/Overview.tsx index 21cb049..7dcc689 100644 --- a/frontend/src/Overview.tsx +++ b/frontend/src/Overview.tsx @@ -2,7 +2,7 @@ import { useState, useEffect, useCallback } from 'react'; import { MediaSet } from './generated/media_set'; import { Frames, VideoPosition } from './App'; import { WaveformCanvas } from './WaveformCanvas'; -import { HudCanvas } from './HudCanvas'; +import { HudCanvas, EmptySelectionAction } from './HudCanvas'; import { Observable } from 'rxjs'; export interface Selection { @@ -103,6 +103,7 @@ export const Overview: React.FC = ({ width={CanvasLogicalWidth} height={CanvasLogicalHeight} zIndex={1} + emptySelectionAction={EmptySelectionAction.SelectPrevious} styles={hudStyles} position={positionPixels} selection={selectedPixels} diff --git a/frontend/src/Waveform.tsx b/frontend/src/Waveform.tsx index 2253cd5..fae374d 100644 --- a/frontend/src/Waveform.tsx +++ b/frontend/src/Waveform.tsx @@ -2,7 +2,7 @@ import { useEffect, useState, useCallback } from 'react'; import { Frames, VideoPosition, newRPC } from './App'; import { MediaSetServiceClientImpl, MediaSet } from './generated/media_set'; import { WaveformCanvas } from './WaveformCanvas'; -import { Selection, HudCanvas } from './HudCanvas'; +import { Selection, HudCanvas, EmptySelectionAction } from './HudCanvas'; import { from, Observable } from 'rxjs'; import { bufferCount } from 'rxjs/operators'; @@ -167,6 +167,7 @@ export const Waveform: React.FC = ({ width={CanvasLogicalWidth} height={CanvasLogicalHeight} zIndex={1} + emptySelectionAction={EmptySelectionAction.SelectNothing} styles={hudStyles} position={positionPixels} selection={selectedPixels}