import { invoke } from '@tauri-apps/api/core'; import { Component, createMemo, For, JSX, splitProps, useContext } from 'solid-js'; import { DisplayInfo } from '../../models/display-info.model'; import { ledStripStore } from '../../stores/led-strip.store'; import { Borders } from '../../constants/border'; import { LedType } from '../../models/led-strip-config'; import { LedStripConfigurationContext } from '../../contexts/led-strip-configuration.context'; import { useLanguage } from '../../i18n/index'; type LedCountControlItemProps = { displayId: number; border: Borders; label: string; }; const LedCountControlItem: Component = (props) => { const [stripConfiguration, { setHoveredStripPart }] = useContext(LedStripConfigurationContext); const { t } = useLanguage(); const config = createMemo(() => { return ledStripStore.strips.find( (s) => s.display_id === props.displayId && s.border === props.border ); }); const handleDecrease = () => { if (config()) { invoke('patch_led_strip_len', { displayId: props.displayId, border: props.border, deltaLen: -1, }).catch((e) => { console.error(e); }); } }; const handleIncrease = () => { if (config()) { invoke('patch_led_strip_len', { displayId: props.displayId, border: props.border, deltaLen: 1, }).catch((e) => { console.error(e); }); } }; const handleInputChange = (e: Event) => { const target = e.target as HTMLInputElement; const newValue = parseInt(target.value); const currentLen = config()?.len || 0; if (!isNaN(newValue) && newValue >= 0 && newValue <= 1000) { const deltaLen = newValue - currentLen; if (deltaLen !== 0) { invoke('patch_led_strip_len', { displayId: props.displayId, border: props.border, deltaLen: deltaLen, }).catch((e) => { console.error(e); // Reset input value on error target.value = currentLen.toString(); }); } } else { // Reset invalid input target.value = (config()?.len || 0).toString(); } }; const handleLedTypeChange = (e: Event) => { const target = e.target as HTMLSelectElement; const newType = target.value as LedType; invoke('patch_led_strip_type', { displayId: props.displayId, border: props.border, ledType: newType, }).catch((e) => { console.error(e); }); }; const onMouseEnter = () => { setHoveredStripPart({ displayId: props.displayId, border: props.border, }); }; const onMouseLeave = () => { setHoveredStripPart(null); }; return (
{props.label}
{ if (e.key === 'Enter') { handleInputChange(e); } }} />
); }; type LedCountControlPanelProps = { display: DisplayInfo; } & JSX.HTMLAttributes; export const LedCountControlPanel: Component = (props) => { const [localProps, rootProps] = splitProps(props, ['display']); const { t } = useLanguage(); const borders: { border: Borders; label: string }[] = [ { border: 'Top', label: t('ledConfig.top') }, { border: 'Bottom', label: t('ledConfig.bottom') }, { border: 'Left', label: t('ledConfig.left') }, { border: 'Right', label: t('ledConfig.right') }, ]; return (
{t('ledConfig.ledCountControl')}
{t('ledConfig.display')} {localProps.display.id}
{(item) => ( )}
💡 {t('ledConfig.controlTip')}
); };