mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-22 08:03:40 +00:00
39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import { useEffect } from "react";
|
|
import { netcattyBridge } from "../../infrastructure/services/netcattyBridge";
|
|
import { installTerminalKeyboardFocusTracking } from "./terminalKeyboardFocus";
|
|
|
|
type TerminalHotkeyScheme = "disabled" | "mac" | "pc";
|
|
|
|
export function useTerminalKeyboardFocus(
|
|
enabled = true,
|
|
hotkeyScheme?: TerminalHotkeyScheme,
|
|
): void {
|
|
useEffect(() => {
|
|
if (!enabled) return undefined;
|
|
const bridge = netcattyBridge.get();
|
|
if (!bridge?.setTerminalKeyboardFocus) return undefined;
|
|
|
|
let lastPublished: boolean | undefined;
|
|
const publish = (focused: boolean) => {
|
|
if (focused === lastPublished) return;
|
|
lastPublished = focused;
|
|
try {
|
|
bridge.setTerminalKeyboardFocus?.(focused, hotkeyScheme);
|
|
} catch {
|
|
// Browser preview or a disposed Electron bridge.
|
|
}
|
|
};
|
|
|
|
const cleanupTracking = installTerminalKeyboardFocusTracking(
|
|
document,
|
|
window,
|
|
publish,
|
|
);
|
|
|
|
return () => {
|
|
cleanupTracking();
|
|
publish(false);
|
|
};
|
|
}, [enabled, hotkeyScheme]);
|
|
}
|