Focus clInput only after its value has been initialized by fillcmdline

This commit is contained in:
petoncle 2023-10-28 19:28:29 +02:00
parent 3f4746760d
commit fcc3d76a3e
3 changed files with 39 additions and 25 deletions

View file

@ -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 **/

View file

@ -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) {

View file

@ -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 */