File size: 2,037 Bytes
52efc7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | /**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { useStdin, useStdout } from 'ink';
import { useEffect, useState } from 'react';
import { useKeypress } from './useKeypress.js';
// ANSI escape codes to enable/disable terminal focus reporting
export const ENABLE_FOCUS_REPORTING = '\x1b[?1004h';
export const DISABLE_FOCUS_REPORTING = '\x1b[?1004l';
// ANSI escape codes for focus events
export const FOCUS_IN = '\x1b[I';
export const FOCUS_OUT = '\x1b[O';
export const useFocus = (): {
isFocused: boolean;
hasReceivedFocusEvent: boolean;
} => {
const { stdin } = useStdin();
const { stdout } = useStdout();
const [isFocused, setIsFocused] = useState(true);
const [hasReceivedFocusEvent, setHasReceivedFocusEvent] = useState(false);
useEffect(() => {
const handleData = (data: Buffer) => {
const sequence = data.toString();
const lastFocusIn = sequence.lastIndexOf(FOCUS_IN);
const lastFocusOut = sequence.lastIndexOf(FOCUS_OUT);
if (lastFocusIn > lastFocusOut) {
setHasReceivedFocusEvent(true);
setIsFocused(true);
} else if (lastFocusOut > lastFocusIn) {
setHasReceivedFocusEvent(true);
setIsFocused(false);
}
};
// Enable focus reporting
stdout?.write(ENABLE_FOCUS_REPORTING);
stdin?.on('data', handleData);
return () => {
// Disable focus reporting on cleanup
stdout?.write(DISABLE_FOCUS_REPORTING);
stdin?.removeListener('data', handleData);
};
}, [stdin, stdout]);
useKeypress(
(_) => {
if (!isFocused) {
// If the user has typed a key, and we cannot possibly be focused out.
// This is a workaround for some tmux use cases. It is still useful to
// listen for the true FOCUS_IN event as well as that will update the
// focus state earlier than waiting for a keypress.
setIsFocused(true);
}
},
{ isActive: true },
);
return {
isFocused,
hasReceivedFocusEvent,
};
};
|