From 719bbf7330fa3caf7222f17ff419c80281cc1292 Mon Sep 17 00:00:00 2001 From: paul-beldean-lgp <77739822+paul-beldean-lgp@users.noreply.github.com> Date: Fri, 9 Aug 2024 12:59:08 +0300 Subject: [PATCH] WRQ-30519: Added accelerator to spectrum color picker (#1659) * *WIP* implement spectrum color picker * added positionPointer, wrapped ColorPicker in Spottable, code cleanup * fixed spottable div * *WIP* added 5-way navigation to indicator * color selection via 5-way, code cleanup, fix lint * added accelerator to spectrum color picker * fixed review issues --------- Co-authored-by: Adrian Cocoara Co-authored-by: Daniel Stoian --- ColorPickerPOC/SpectrumIndicator.js | 50 +++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/ColorPickerPOC/SpectrumIndicator.js b/ColorPickerPOC/SpectrumIndicator.js index 1dc5625b40..d03f51f4aa 100644 --- a/ColorPickerPOC/SpectrumIndicator.js +++ b/ColorPickerPOC/SpectrumIndicator.js @@ -2,7 +2,7 @@ import {is} from '@enact/core/keymap'; import Spottable from '@enact/spotlight/Spottable'; import spotlight from '@enact/spotlight'; import PropTypes from 'prop-types'; -import {useCallback, useEffect} from 'react'; +import {useCallback, useEffect, useState} from 'react'; import {getHexColorFromGradient} from './utils'; @@ -11,6 +11,9 @@ import css from './ColorPickerSpectrum.module.less'; const SpottableDiv = Spottable('div'); const CircleIndicator = ({bgColor, canvasRef, isIndicatorActive, selectedColorHandler, setIsIndicatorActive, setIndicatorBgColor, setX, setY, x, y}) => { + const [holding, setHolding] = useState(false); + const [prevKey, setPrevKey] = useState(''); + const [stepValue, setStepValue] = useState(1); // resume spotlight when indicator is not active useEffect(() => { @@ -36,7 +39,22 @@ const CircleIndicator = ({bgColor, canvasRef, isIndicatorActive, selectedColorHa const hexColor = getHexColorFromGradient(canvasRef, x, y); setIndicatorBgColor(hexColor); } - }, [canvasRef, x, y, isIndicatorActive, setIsIndicatorActive, setIndicatorBgColor]); + + if (isIndicatorActive) { + if (holding) { + if (prevKey === keyCode) { + if (stepValue < 10) { + setStepValue(prevValue => prevValue + 1); + } + } else { + setStepValue(1); + } + } else { + setHolding(true); + setPrevKey(keyCode); + } + } + }, [canvasRef, holding, isIndicatorActive, prevKey, setIndicatorBgColor, setIsIndicatorActive, stepValue, x, y]); const handleOnKeyUp = useCallback(({keyCode}) => { if (is('down', keyCode)) { @@ -52,31 +70,35 @@ const CircleIndicator = ({bgColor, canvasRef, isIndicatorActive, selectedColorHa const hexColor = getHexColorFromGradient(canvasRef, x, y); selectedColorHandler(hexColor); } + + setHolding(false); + setPrevKey(''); + setStepValue(1); }, [canvasRef, selectedColorHandler, x, y]); const handleSpotlightDown = useCallback(() => { - if (isIndicatorActive && y < canvasRef.current.clientHeight - 1) { - setY(y++); + if (isIndicatorActive && y + stepValue <= canvasRef.current.clientHeight - 1) { + setY(y + stepValue); } - }, [canvasRef, isIndicatorActive, setY, y]); + }, [canvasRef, isIndicatorActive, setY, stepValue, y]); const handleSpotlightLeft = useCallback(() => { - if (isIndicatorActive && x > 0) { - setX(x--); + if (isIndicatorActive && x - stepValue >= 0) { + setX(x - stepValue); } - }, [isIndicatorActive, setX, x]); + }, [isIndicatorActive, setX, stepValue, x]); const handleSpotlightRight = useCallback(() => { - if (isIndicatorActive && x < canvasRef.current.clientWidth) { - setX(x++); + if (isIndicatorActive && x + stepValue <= canvasRef.current.clientWidth) { + setX(x + stepValue); } - }, [canvasRef, isIndicatorActive, setX, x]); + }, [canvasRef, isIndicatorActive, setX, stepValue, x]); const handleSpotlightUp = useCallback(() => { - if (isIndicatorActive && y > 0) { - setY(y--); + if (isIndicatorActive && y - stepValue >= 0) { + setY(y - stepValue); } - }, [isIndicatorActive, setY, y]); + }, [isIndicatorActive, setY, stepValue, y]); return (