import React, { useEffect, useState } from "react"; import { Modal } from "./components/modal"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faArrowRight } from "@fortawesome/free-solid-svg-icons"; import { getApp } from "../../olympusapp"; import { OlympusState } from "../../constants/constants"; import { Shortcut } from "../../shortcut/shortcut"; import { BindShortcutRequestEvent, ShortcutsChangedEvent } from "../../events"; export function KeybindModal(props: { open: boolean }) { const [shortcuts, setShortcuts] = useState({} as { [key: string]: Shortcut }); const [shortcut, setShortcut] = useState(null as null | Shortcut); const [code, setCode] = useState(null as null | string); const [shiftKey, setShiftKey] = useState(false as boolean | undefined); const [ctrlKey, setCtrlKey] = useState(false as boolean | undefined); const [altKey, setAltKey] = useState(false as boolean | undefined); useEffect(() => { ShortcutsChangedEvent.on((shortcuts) => setShortcuts({ ...shortcuts })); BindShortcutRequestEvent.on((shortcut) => setShortcut(shortcut)); document.addEventListener("keydown", (ev) => { if (ev.code) { setCode(ev.code); if (ev.code.indexOf("Control") < 0) setCtrlKey(ev.ctrlKey); else setCtrlKey(false); if (ev.code.indexOf("Shift") < 0) setShiftKey(ev.shiftKey); else setShiftKey(false); if (ev.code.indexOf("Alt") < 0) setAltKey(ev.altKey); else setAltKey(false); } }); }, []); useEffect(() => { setCode(shortcut?.getOptions().code ?? null); setShiftKey(shortcut?.getOptions().shiftKey); setAltKey(shortcut?.getOptions().altKey); setCtrlKey(shortcut?.getOptions().ctrlKey); }, [shortcut]); let available: null | boolean = code ? true : null; let inUseShortcuts: Shortcut[] = []; for (let id in shortcuts) { if ( id !== shortcut?.getId() && code === shortcuts[id].getOptions().code && ((shiftKey === undefined && shortcuts[id].getOptions().shiftKey !== undefined) || (shiftKey !== undefined && shortcuts[id].getOptions().shiftKey === undefined) || shiftKey === shortcuts[id].getOptions().shiftKey) && ((altKey === undefined && shortcuts[id].getOptions().altKey !== undefined) || (altKey !== undefined && shortcuts[id].getOptions().altKey === undefined) || altKey === shortcuts[id].getOptions().altKey) && ((ctrlKey === undefined && shortcuts[id].getOptions().ctrlKey !== undefined) || (ctrlKey !== undefined && shortcuts[id].getOptions().ctrlKey === undefined) || ctrlKey === shortcuts[id].getOptions().ctrlKey) ) { available = false; inUseShortcuts.push(shortcuts[id]); } } return (
{shortcut?.getOptions().label} Press the key you want to bind to this event
{ctrlKey && "Ctrl + "} {altKey && "Alt + "} {shiftKey && "Shift + "} {code}
{available === true &&
Keybind is free!
} {available === false && (
Keybind is already in use:{" "}
{inUseShortcuts.map((shortcut) => ( {shortcut.getOptions().label} ))}
A key combination can be assigned to multiple actions, and all bound actions will fire
)}
{shortcut && ( )}
); }