Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WRQ-30519: Added accelerator to spectrum color picker #1659

Merged
merged 10 commits into from
Aug 9, 2024
50 changes: 36 additions & 14 deletions ColorPickerPOC/SpectrumIndicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
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';

Expand All @@ -11,6 +11,9 @@
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);

Check warning on line 16 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L14-L16

Added lines #L14 - L16 were not covered by tests

// resume spotlight when indicator is not active
useEffect(() => {
Expand All @@ -36,7 +39,22 @@
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);

Check warning on line 47 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L47

Added line #L47 was not covered by tests
}
} else {
setStepValue(1);

Check warning on line 50 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L49-L50

Added lines #L49 - L50 were not covered by tests
}
} else {
setHolding(true);
setPrevKey(keyCode);

Check warning on line 54 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L52-L54

Added lines #L52 - L54 were not covered by tests
}
}
}, [canvasRef, holding, isIndicatorActive, prevKey, setIndicatorBgColor, setIsIndicatorActive, stepValue, x, y]);

const handleOnKeyUp = useCallback(({keyCode}) => {
if (is('down', keyCode)) {
Expand All @@ -52,31 +70,35 @@
const hexColor = getHexColorFromGradient(canvasRef, x, y);
selectedColorHandler(hexColor);
}

setHolding(false);
setPrevKey('');
setStepValue(1);

Check warning on line 76 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L74-L76

Added lines #L74 - L76 were not covered by tests
}, [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);

Check warning on line 81 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L81

Added line #L81 was not covered by tests
}
}, [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);

Check warning on line 87 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L87

Added line #L87 was not covered by tests
}
}, [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);

Check warning on line 93 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L93

Added line #L93 was not covered by tests
}
}, [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);

Check warning on line 99 in ColorPickerPOC/SpectrumIndicator.js

View check run for this annotation

Codecov / codecov/patch

ColorPickerPOC/SpectrumIndicator.js#L99

Added line #L99 was not covered by tests
}
}, [isIndicatorActive, setY, y]);
}, [isIndicatorActive, setY, stepValue, y]);

return (
<SpottableDiv
Expand Down