shortcuts for raising input events

This commit is contained in:
Joeri Exelmans 2025-10-22 11:48:45 +02:00
parent 1ce7bdb9bd
commit 694380aa18
3 changed files with 56 additions and 31 deletions

View file

@ -44,8 +44,10 @@ export function App() {
const [rtIdx, setRTIdx] = useState<number|undefined>(); const [rtIdx, setRTIdx] = useState<number|undefined>();
const [time, setTime] = useState<TimeMode>({kind: "paused", simtime: 0}); const [time, setTime] = useState<TimeMode>({kind: "paused", simtime: 0});
const [modal, setModal] = useState<ReactElement|null>(null); const [modal, setModal] = useState<ReactElement|null>(null);
const [plantName, setPlantName] = usePersistentState("plant", "dummy"); const [plantName, setPlantName] = usePersistentState("plant", "dummy");
const [zoom, setZoom] = usePersistentState("zoom", 1); const [zoom, setZoom] = usePersistentState("zoom", 1);
const [showKeys, setShowKeys] = usePersistentState("shortcuts", true);
const plant = plants.find(([pn, p]) => pn === plantName)![1]; const plant = plants.find(([pn, p]) => pn === plantName)![1];
@ -249,7 +251,7 @@ export function App() {
> >
<TopPanel <TopPanel
rt={rtIdx === undefined ? undefined : rt[rtIdx]} rt={rtIdx === undefined ? undefined : rt[rtIdx]}
{...{rtIdx, ast, time, setTime, onUndo, onRedo, onInit, onClear, onRaise, onBack, mode, setMode, setModal, zoom, setZoom}} {...{rtIdx, ast, time, setTime, onUndo, onRedo, onInit, onClear, onRaise, onBack, mode, setMode, setModal, zoom, setZoom, showKeys, setShowKeys}}
/> />
</Box> </Box>
{/* Below the top bar: Editor */} {/* Below the top bar: Editor */}
@ -281,7 +283,7 @@ export function App() {
</PersistentDetails> </PersistentDetails>
<PersistentDetails localStorageKey="showInputEvents" initiallyOpen={true}> <PersistentDetails localStorageKey="showInputEvents" initiallyOpen={true}>
<summary>input events</summary> <summary>input events</summary>
<ShowInputEvents inputEvents={ast.inputEvents} onRaise={onRaise} disabled={rtIdx===undefined}/> <ShowInputEvents inputEvents={ast.inputEvents} onRaise={onRaise} disabled={rtIdx===undefined} showKeys={showKeys}/>
</PersistentDetails> </PersistentDetails>
<PersistentDetails localStorageKey="showInternalEvents" initiallyOpen={true}> <PersistentDetails localStorageKey="showInternalEvents" initiallyOpen={true}>
<summary>internal events</summary> <summary>internal events</summary>

View file

@ -72,15 +72,12 @@ export function ShowAST(props: {root: ConcreteState | PseudoState, transitions:
} }
import BoltIcon from '@mui/icons-material/Bolt'; import BoltIcon from '@mui/icons-material/Bolt';
import { KeyInfoHidden, KeyInfoVisible } from "./KeyInfo";
import { useEffect } from "react";
export function ShowInputEvents({inputEvents, onRaise, disabled}: {inputEvents: EventTrigger[], onRaise: (e: string, p: any) => void, disabled: boolean}) { export function ShowInputEvents({inputEvents, onRaise, disabled, showKeys}: {inputEvents: EventTrigger[], onRaise: (e: string, p: any) => void, disabled: boolean, showKeys: boolean}) {
return inputEvents.map(({event, paramName}) => const raiseHandlers = inputEvents.map(({event}) => {
<div key={event+'/'+paramName} className="toolbarGroup"> return () => {
<button
className="inputEvent"
title={`raise this input event`}
disabled={disabled}
onClick={() => {
// @ts-ignore // @ts-ignore
const param = document.getElementById(`input-${event}-param`)?.value; const param = document.getElementById(`input-${event}-param`)?.value;
let paramParsed; let paramParsed;
@ -94,16 +91,41 @@ export function ShowInputEvents({inputEvents, onRaise, disabled}: {inputEvents:
return; return;
} }
onRaise(event, paramParsed); onRaise(event, paramParsed);
}}> };
});
const onKeyDown = (e: KeyboardEvent) => {
const n = (parseInt(e.key)+9) % 10;
if (raiseHandlers[n] !== undefined) {
raiseHandlers[n]();
e.stopPropagation();
e.preventDefault();
}
}
useEffect(() => {
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [raiseHandlers]);
const KeyInfo = showKeys ? KeyInfoVisible : KeyInfoHidden;
return inputEvents.map(({event, paramName}, i) => {
const shortcut = (i+1)%10;
const KI = (i <= 10) ? KeyInfo : KeyInfoHidden;
return <div key={event+'/'+paramName} className="toolbarGroup">
<KI keyInfo={<kbd>{shortcut}</kbd>}>
<button
className="inputEvent"
title={`raise this input event`}
disabled={disabled}
onClick={raiseHandlers[i]}>
<BoltIcon fontSize="small"/> <BoltIcon fontSize="small"/>
{event} {event}
</button> </button>
</KI>
{paramName && {paramName &&
<><input id={`input-${event}-param`} style={{width: 20}} placeholder={paramName}/></> <><input id={`input-${event}-param`} style={{width: 20}} placeholder={paramName}/></>
} }
&nbsp; &nbsp;
</div> </div>;
) })
} }
export function ShowInternalEvents(props: {internalEvents: EventTrigger[]}) { export function ShowInternalEvents(props: {internalEvents: EventTrigger[]}) {

View file

@ -43,12 +43,13 @@ export type TopPanelProps = {
setModal: Dispatch<SetStateAction<ReactElement|null>>, setModal: Dispatch<SetStateAction<ReactElement|null>>,
zoom: number, zoom: number,
setZoom: Dispatch<SetStateAction<number>>, setZoom: Dispatch<SetStateAction<number>>,
showKeys: boolean,
setShowKeys: Dispatch<SetStateAction<boolean>>,
} }
export function TopPanel({rt, rtIdx, time, setTime, onUndo, onRedo, onInit, onClear, onRaise, onBack, ast, mode, setMode, setModal, zoom, setZoom}: TopPanelProps) { export function TopPanel({rt, rtIdx, time, setTime, onUndo, onRedo, onInit, onClear, onRaise, onBack, ast, mode, setMode, setModal, zoom, setZoom, showKeys, setShowKeys}: TopPanelProps) {
const [displayTime, setDisplayTime] = useState("0.000"); const [displayTime, setDisplayTime] = useState("0.000");
const [timescale, setTimescale] = useState(1); const [timescale, setTimescale] = useState(1);
const [showKeys, setShowKeys] = usePersistentState("shortcuts", true);
const KeyInfo = showKeys ? KeyInfoVisible : KeyInfoHidden; const KeyInfo = showKeys ? KeyInfoVisible : KeyInfoHidden;