Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
5.0 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import type {KeybindConfig, KeyCombo} from '@app/features/input/state/InputKeybind';
import {shouldPreferLayoutKey} from '@fluxer/desktop_ipc/src/GlobalShortcutKeyNames';

export type KeyboardShortcutEvent = Pick<KeyboardEvent, 'key' | 'ctrlKey' | 'altKey' | 'shiftKey' | 'metaKey'> &
	Partial<Pick<KeyboardEvent, 'code'>>;
export type KeyboardShortcutPressEvent = KeyboardShortcutEvent & Pick<KeyboardEvent, 'repeat'>;

export const normalizeKeyboardShortcutKey = (key: string): string => {
	if (key === ' ') return 'space';
	if (key === 'Break') return 'pause';
	return key.toLowerCase();
};
const isPrintableNonLetterShortcutKey = (key: string | undefined | null): boolean =>
	!!key && key.length === 1 && !/^[a-z]$/i.test(key);
const latinKeyCode = (key: string): string | null => {
	if (/^[a-z]$/i.test(key)) return `Key${key.toUpperCase()}`;
	if (/^[0-9]$/.test(key)) return `Digit${key}`;
	return null;
};
const isNonAsciiLayoutKey = (key: string): boolean => key.length === 1 && key.charCodeAt(0) > 0x7f;
const hasTriggerKey = (combo: KeyCombo): boolean => (combo.key ?? '') !== '' || (combo.code ?? '') !== '';
const keyboardEventIsModifierKey = (
	event: Pick<KeyboardShortcutEvent, 'key' | 'code'>,
	modifier: 'ctrl' | 'alt' | 'shift' | 'meta',
): boolean => {
	switch (modifier) {
		case 'ctrl':
			return event.key === 'Control' || event.code === 'ControlLeft' || event.code === 'ControlRight';
		case 'alt':
			return event.key === 'Alt' || event.code === 'AltLeft' || event.code === 'AltRight';
		case 'shift':
			return event.key === 'Shift' || event.code === 'ShiftLeft' || event.code === 'ShiftRight';
		case 'meta':
			return event.key === 'Meta' || event.key === 'OS' || event.code === 'MetaLeft' || event.code === 'MetaRight';
	}
};
export const keyboardEventTriggerMatchesCombo = (combo: KeyCombo, event: KeyboardShortcutEvent): boolean => {
	const expectedLayoutKey = combo.key || combo.code;
	if (shouldPreferLayoutKey(combo) && expectedLayoutKey) {
		if (normalizeKeyboardShortcutKey(event.key) === normalizeKeyboardShortcutKey(expectedLayoutKey)) return true;
		const hasCommandModifier = Boolean(combo.ctrl || combo.ctrlOrMeta || combo.alt || combo.meta);
		if (hasCommandModifier && isNonAsciiLayoutKey(event.key) && event.code === latinKeyCode(expectedLayoutKey)) {
			return true;
		}
		return Boolean(combo.code && event.code === combo.code && combo.ctrlOrMeta && combo.alt);
	}
	const expectedCode = combo.code;
	if (expectedCode) {
		return event.code === expectedCode;
	}
	const expectedKey = expectedLayoutKey;
	if (!expectedKey) return false;
	return normalizeKeyboardShortcutKey(event.key) === normalizeKeyboardShortcutKey(expectedKey);
};
export const keyboardEventMatchesCombo = (
	combo: KeyCombo,
	event: KeyboardShortcutEvent,
	options: {isMacOS?: boolean} = {},
): boolean => {
	if (combo.modifierOnly || !hasTriggerKey(combo)) return false;
	const isMacOS = options.isMacOS ?? false;
	const expectsCtrl = Boolean(combo.ctrl || (combo.ctrlOrMeta && !isMacOS));
	const expectsMeta = Boolean(combo.meta || (combo.ctrlOrMeta && isMacOS));
	if (event.ctrlKey !== expectsCtrl) return false;
	if (event.metaKey !== expectsMeta) return false;
	if (event.altKey !== Boolean(combo.alt)) return false;
	const triggerMatches = keyboardEventTriggerMatchesCombo(combo, event);
	if (!triggerMatches) return false;
	if (event.shiftKey !== Boolean(combo.shift)) {
		if (combo.shift || !event.shiftKey || !isPrintableNonLetterShortcutKey(combo.key)) return false;
	}
	return true;
};
export const keyboardEventStartsComboPress = (
	combo: KeyCombo,
	event: KeyboardShortcutPressEvent,
	options: {isMacOS?: boolean} = {},
): boolean => !event.repeat && keyboardEventMatchesCombo(combo, event, options);
export const keyboardEventCanRecoverStaleMacMetaPress = (
	combo: KeyCombo,
	event: KeyboardShortcutPressEvent,
	options: {isMacOS?: boolean} = {},
): boolean => {
	const isMacOS = options.isMacOS ?? false;
	return isMacOS && Boolean(combo.ctrlOrMeta) && event.metaKey && keyboardEventStartsComboPress(combo, event, options);
};
export const keyboardEventReleasesComboModifier = (
	combo: KeyCombo,
	event: KeyboardShortcutEvent,
	options: {isMacOS?: boolean} = {},
): boolean => {
	if (combo.modifierOnly || !hasTriggerKey(combo)) return false;
	const isMacOS = options.isMacOS ?? false;
	const expectsCtrl = Boolean(combo.ctrl || (combo.ctrlOrMeta && !isMacOS));
	const expectsMeta = Boolean(combo.meta || (combo.ctrlOrMeta && isMacOS));
	if (expectsCtrl && keyboardEventIsModifierKey(event, 'ctrl')) return true;
	if (expectsMeta && keyboardEventIsModifierKey(event, 'meta')) return true;
	if (combo.alt && keyboardEventIsModifierKey(event, 'alt')) return true;
	if (combo.shift && keyboardEventIsModifierKey(event, 'shift')) return true;
	return false;
};
export const shouldAllowLocalShortcutForChannelTextarea = (
	entry: Pick<KeybindConfig, 'editableFocusBehavior'>,
	textareaValue: string,
): boolean => {
	if (entry.editableFocusBehavior === 'allow') return true;
	if (entry.editableFocusBehavior === 'allow_when_empty') return textareaValue.trim().length === 0;
	return false;
};