diff --git a/src/commandline_frame.ts b/src/commandline_frame.ts index ede63f97..9d95232f 100644 --- a/src/commandline_frame.ts +++ b/src/commandline_frame.ts @@ -163,12 +163,20 @@ const noblur = () => setTimeout(() => commandline_state.clInput.focus(), 0) /** @hidden **/ export function focus() { logger.debug("Called focus()") - Messaging.messageOwnTab("cl_input_focused", "unused") commandline_state.clInput.focus() commandline_state.clInput.removeEventListener("blur", noblur) commandline_state.clInput.addEventListener("blur", noblur) - if (keysFromContentProcess.length !== 0) { - logger.debug("Dispatching " + JSON.stringify(keysFromContentProcess)); + Messaging.messageOwnTab("cl_input_focused", "unused") + tryConsumingBufferedKeysFromContentProcess(); +} + +function isClInputReady() { + return window.document.activeElement === commandline_state.clInput; +} + +function tryConsumingBufferedKeysFromContentProcess() { + if (keysFromContentProcess.length !== 0 && isClInputReady()) { + logger.debug("Consuming " + JSON.stringify(keysFromContentProcess)); keysFromContentProcess.forEach(key => commandline_state.clInput.value += key) keysFromContentProcess = [] } @@ -176,8 +184,8 @@ export function focus() { let keysFromContentProcess: string[] = [] export function bufferUntilClInputFocused([key]) { - logger.debug("Received keyboardEvent for buffering " + key) - if (window.document.activeElement === commandline_state.clInput) { + logger.debug("Command line process received content process keydown event: " + key) + if (isClInputReady()) { commandline_state.clInput.value += key } else { @@ -359,16 +367,22 @@ export function fillcmdline( trailspace = true, ffocus = true, ) { - if (trailspace) commandline_state.clInput.value = newcommand + " " - else commandline_state.clInput.value = newcommand - commandline_state.isVisible = true - let result = Promise.resolve([]) - // Focus is lost for some reason. - if (ffocus) { - focus() - result = refresh_completions(commandline_state.clInput.value) - } - return result + setTimeout(() => { + logger.debug("Called commandline_frame fillcmdline after 2000ms") + // 1. Initialize clInput value. + // 2. Focus it. + // 3. Stop forwarding key events from content process to command line process. + if (trailspace) commandline_state.clInput.value = newcommand + " " + else commandline_state.clInput.value = newcommand + commandline_state.isVisible = true + let result = Promise.resolve([]) + // Focus is lost for some reason. + if (ffocus) { + focus() + result = refresh_completions(commandline_state.clInput.value) + } + // return result + }, 2000); } /** @hidden **/ diff --git a/src/content/controller_content.ts b/src/content/controller_content.ts index 7eece206..0f811812 100644 --- a/src/content/controller_content.ts +++ b/src/content/controller_content.ts @@ -193,8 +193,8 @@ function* ParserController() { if (response.exstr) { exstr = response.exstr if (exstr.startsWith("fillcmdline ")) { // Ugh. That's ugly. I needed a way to know if this command is going to open the cmdline. - logger.debug("Setting bufferUntilClInputFocused to true") - bufferUntilClInputFocused = true + logger.debug("Setting clInputFocused to false") + clInputFocused = false } break } else { @@ -218,16 +218,16 @@ function* ParserController() { } Messaging.addListener("cl_input_focused", () => { logger.debug("Callback cl_input_focused") - bufferUntilClInputFocused = false + clInputFocused = true }) -let bufferUntilClInputFocused: boolean = false +let clInputFocused: boolean = true export const generator = ParserController() // var rather than let stops weirdness in repl. generator.next() /** Feed keys to the ParserController */ export function acceptKey(keyevent: KeyboardEvent) { - logger.debug("bufferUntilClInputFocused = " + bufferUntilClInputFocused) - if (bufferUntilClInputFocused) { + logger.debug("clInputFocused = " + clInputFocused) + if (!clInputFocused) { let isCharacterKey = keyevent.key.length == 1 && !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey; if (isCharacterKey) { diff --git a/src/excmds.ts b/src/excmds.ts index a71dd89f..29b51dde 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -3882,16 +3882,16 @@ export function hidecmdline() { //#content export function fillcmdline(...strarr: string[]) { const str = strarr.join(" ") - showcmdline() - return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str]) + showcmdline(false) + return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, true/*trailspace*/, true/*focus*/]) } /** Set the current value of the commandline to string *without* a trailing space */ //#content export function fillcmdline_notrail(...strarr: string[]) { const str = strarr.join(" ") - showcmdline() - return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, false]) + showcmdline(false) + return Messaging.messageOwnTab("commandline_frame", "fillcmdline", [str, , false/*trailspace*/, true/*focus*/]) } /** Show and fill the command line without focusing it */