Skip to content

Commit 744c0b2

Browse files
committed
refactor(terminal): dynamic keyboard passthrough from command bindings
Replace hardcoded PHOENIX_SHORTCUTS and EDITOR_SHORTCUTS with a dynamic set built from KeyBindingManager bindings for VIEW_TERMINAL and CMD_KEYBOARD_NAV_UI_OVERLAY. Rebuilds automatically when user changes keybindings. Extract logic into _setupPhoenixShortcuts().
1 parent b1c0d18 commit 744c0b2

1 file changed

Lines changed: 64 additions & 44 deletions

File tree

  • src/extensionsIntegrated/Terminal

src/extensionsIntegrated/Terminal/main.js

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -147,50 +147,7 @@ define(function (require, exports, module) {
147147
// Dropdown chevron button toggles shell selector
148148
$panel.find(".terminal-flyout-dropdown-btn").on("click", _onDropdownButtonClick);
149149

150-
// When the terminal is focused, route all keyboard events to the
151-
// terminal instead of letting Phoenix keybindings intercept them.
152-
// Only a few essential shortcuts are passed back to Phoenix.
153-
const PHOENIX_SHORTCUTS = [
154-
{ctrlKey: true, key: "p"}, // Command Palette
155-
{key: "f4"} // Switch terminals
156-
];
157-
KeyBindingManager.addGlobalKeydownHook(function (event) {
158-
if (event.type !== "keydown") {
159-
return false;
160-
}
161-
// Only intercept when a terminal textarea is focused
162-
const el = document.activeElement;
163-
if (!el || !$contentArea[0].contains(el)) {
164-
return false;
165-
}
166-
167-
const ctrlOrMeta = event.ctrlKey || event.metaKey;
168-
const key = event.key.toLowerCase();
169-
170-
// Ctrl+K (Cmd+K on mac): clear terminal scrollback
171-
if (ctrlOrMeta && !event.shiftKey && key === "k") {
172-
event.preventDefault();
173-
_clearActiveTerminal();
174-
return true;
175-
}
176-
177-
// Show clear buffer hint on Ctrl+L
178-
if (ctrlOrMeta && !event.shiftKey && key === "l") {
179-
_showClearBufferHintToast();
180-
}
181-
182-
// Let Phoenix handle these specific shortcuts
183-
for (const shortcut of PHOENIX_SHORTCUTS) {
184-
const ctrlMatch = shortcut.ctrlKey ? ctrlOrMeta : !ctrlOrMeta;
185-
const shiftMatch = shortcut.shiftKey ? event.shiftKey : !event.shiftKey;
186-
if (ctrlMatch && shiftMatch && key === shortcut.key.toLowerCase()) {
187-
return false; // Let Phoenix handle it
188-
}
189-
}
190-
191-
// Block Phoenix from handling everything else — let xterm get it
192-
return true;
193-
});
150+
_setupPhoenixShortcuts();
194151

195152
// Refresh process info when the tab bar gains focus or mouse enters
196153
$panel.find(".terminal-tab-bar").on("mouseenter", _refreshAllProcesses);
@@ -671,6 +628,69 @@ define(function (require, exports, module) {
671628
_refreshAllProcesses();
672629
}
673630

631+
/**
632+
* Set up keyboard shortcut routing so that when the terminal is focused,
633+
* all keys go to the terminal except shortcuts bound to specific Phoenix
634+
* commands (e.g. toggle terminal, keyboard nav overlay).
635+
*/
636+
function _setupPhoenixShortcuts() {
637+
// Commands whose shortcuts should pass through to Phoenix
638+
// even when the terminal is focused.
639+
const PASSTHROUGH_COMMANDS = [
640+
Commands.VIEW_TERMINAL,
641+
Commands.CMD_KEYBOARD_NAV_UI_OVERLAY
642+
];
643+
644+
// Build a set of shortcut strings, rebuilt when bindings change.
645+
let passthroughShortcuts = new Set();
646+
function rebuild() {
647+
passthroughShortcuts = new Set();
648+
for (const cmdId of PASSTHROUGH_COMMANDS) {
649+
for (const binding of KeyBindingManager.getKeyBindings(cmdId)) {
650+
if (binding.key) {
651+
passthroughShortcuts.add(binding.key);
652+
}
653+
}
654+
}
655+
}
656+
rebuild();
657+
KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_ADDED, rebuild);
658+
KeyBindingManager.on(KeyBindingManager.EVENT_KEY_BINDING_REMOVED, rebuild);
659+
660+
KeyBindingManager.addGlobalKeydownHook(function (event, shortcut) {
661+
if (event.type !== "keydown") {
662+
return false;
663+
}
664+
const el = document.activeElement;
665+
if (!el || !$contentArea[0].contains(el)) {
666+
return false;
667+
}
668+
669+
const ctrlOrMeta = event.ctrlKey || event.metaKey;
670+
const key = event.key.toLowerCase();
671+
672+
// Ctrl+K (Cmd+K on mac): clear terminal scrollback
673+
if (ctrlOrMeta && !event.shiftKey && key === "k") {
674+
event.preventDefault();
675+
_clearActiveTerminal();
676+
return true;
677+
}
678+
679+
// Show clear buffer hint on Ctrl+L
680+
if (ctrlOrMeta && !event.shiftKey && key === "l") {
681+
_showClearBufferHintToast();
682+
}
683+
684+
// Let Phoenix handle shortcuts bound to passthrough commands
685+
if (shortcut && passthroughShortcuts.has(shortcut)) {
686+
return false;
687+
}
688+
689+
// Block Phoenix from handling everything else — let xterm get it
690+
return true;
691+
});
692+
}
693+
674694
/**
675695
* Update all terminal themes (after editor theme change)
676696
*/

0 commit comments

Comments
 (0)