示例工程 34-global-shortcuts-autostart

34-global-shortcuts-autostart/app/.tsc/shared/accelerator.js

javascript · 4.3 KB

const MODIFIERS = {
    commandorcontrol: "CommandOrControl",
    cmdorctrl: "CommandOrControl",
    command: "Command",
    cmd: "Command",
    meta: "Command",
    super: "Command",
    control: "Control",
    ctrl: "Control",
    alt: "Alt",
    option: "Alt",
    shift: "Shift",
};
const ORDER = [
    "CommandOrControl",
    "Command",
    "Control",
    "Alt",
    "Shift",
];
const NAMED_KEYS = new Set([
    "Space",
    "Tab",
    "Escape",
    "Enter",
    "Backspace",
    "Delete",
    "Insert",
    "Home",
    "End",
    "PageUp",
    "PageDown",
    "Up",
    "Down",
    "Left",
    "Right",
    "PrintScreen",
    "Plus",
    "Minus",
    "Comma",
    "Period",
    "Slash",
    "Backslash",
    "Semicolon",
    "Quote",
    "BracketLeft",
    "BracketRight",
    "Capslock",
    "Numlock",
    "Scrolllock",
].map((key) => key.toLowerCase()));
const KEY_ALIASES = {
    esc: "Escape",
    return: "Enter",
    del: "Delete",
    ins: "Insert",
    pageup: "PageUp",
    pagedown: "PageDown",
    arrowup: "Up",
    arrowdown: "Down",
    arrowleft: "Left",
    arrowright: "Right",
    "+": "Plus",
    "-": "Minus",
    ",": "Comma",
    ".": "Period",
    "/": "Slash",
    "\\": "Backslash",
    ";": "Semicolon",
    "'": "Quote",
    "[": "BracketLeft",
    "]": "BracketRight",
};
function normalizeKey(token) {
    const raw = token.trim();
    if (raw.length === 0)
        return null;
    const lower = raw.toLowerCase();
    if (KEY_ALIASES[lower])
        return KEY_ALIASES[lower];
    if (/^[a-z]$/i.test(raw))
        return raw.toUpperCase();
    if (/^[0-9]$/.test(raw))
        return raw;
    if (/^f([1-9]|1[0-9]|2[0-4])$/i.test(raw))
        return `F${raw.slice(1)}`;
    if (NAMED_KEYS.has(lower)) {
        return lower.charAt(0).toUpperCase() + lower.slice(1);
    }
    return null;
}
export function parseAccelerator(input) {
    const text = input.trim();
    if (text.length === 0) {
        return { ok: false, reason: "EMPTY", detail: "快捷键为空" };
    }
    const tokens = text
        .split("+")
        .map((token) => token.trim())
        .filter((token, index, all) => !(token === "" && index === all.length - 1));
    if (tokens.some((token) => token.length === 0)) {
        return {
            ok: false,
            reason: "UNKNOWN_KEY",
            detail: "存在空的按键片段",
        };
    }
    const modifiers = [];
    const keys = [];
    for (const token of tokens) {
        const modifier = MODIFIERS[token.toLowerCase()];
        if (modifier) {
            if (modifiers.includes(modifier)) {
                return {
                    ok: false,
                    reason: "DUPLICATE_MODIFIER",
                    detail: `修饰键 ${modifier} 重复`,
                };
            }
            modifiers.push(modifier);
            continue;
        }
        const key = normalizeKey(token);
        if (!key) {
            return {
                ok: false,
                reason: "UNKNOWN_KEY",
                detail: `无法识别的按键:${token}`,
            };
        }
        keys.push(key);
    }
    if (keys.length === 0) {
        return { ok: false, reason: "MISSING_KEY", detail: "缺少主按键" };
    }
    if (keys.length > 1) {
        return { ok: false, reason: "EXTRA_KEY", detail: "只能有一个主按键" };
    }
    if (modifiers.length === 0) {
        return {
            ok: false,
            reason: "MISSING_MODIFIER",
            detail: "全局快捷键至少需要一个修饰键",
        };
    }
    if (modifiers.includes("CommandOrControl") &&
        (modifiers.includes("Command") || modifiers.includes("Control"))) {
        return {
            ok: false,
            reason: "MODIFIER_CONFLICT",
            detail: "CommandOrControl 不能与 Command/Control 同时出现",
        };
    }
    const ordered = ORDER.filter((modifier) => modifiers.includes(modifier));
    const key = keys[0];
    return {
        ok: true,
        accelerator: [...ordered, key].join("+"),
        modifiers: ordered,
        key,
    };
}
export function classifyAttempt(input) {
    if (input.threw)
        return "INVALID";
    if (input.ownedByThisApp)
        return "ALREADY_OWNED";
    if (!input.registerReturned)
        return "CONFLICT";
    return "OK";
}
//# sourceMappingURL=accelerator.js.map