Send all keys typed in content page instead of just one key

Tab messages are not always received in order
This commit is contained in:
petoncle 2023-10-29 00:27:39 +02:00
parent 5779121d5c
commit 6b61b69455
3 changed files with 43 additions and 30 deletions

View file

@ -82,6 +82,8 @@ const commandline_state = {
keyEvents: new Array<MinimalKey>(),
refresh_completions,
state,
keysFromContentProcess: [],
consumedKeyCount: 0,
clInputFocused: false,
clInputNormalEventsReceived: false,
clInputInitialValue: "",
@ -172,43 +174,48 @@ export function focus() {
commandline_state.clInput.addEventListener("blur", noblur)
commandline_state.clInputFocused = true
Messaging.messageOwnTab("cl_input_focused", "unused")
const keysFromContentProcess = commandline_state.keysFromContentProcess;
if (keysFromContentProcess.length !== 0) {
logger.debug("Consuming " + JSON.stringify(keysFromContentProcess));
for (const key of keysFromContentProcess) {
logger.debug("Consuming " + JSON.stringify(keysFromContentProcess.slice(commandline_state.consumedKeyCount)));
for (let keyIndex = commandline_state.consumedKeyCount; keyIndex < keysFromContentProcess.length; keyIndex++) {
const key = keysFromContentProcess[keyIndex];
commandline_state.clInput.value += key
clInputValueChanged()
commandline_state.consumedKeyCount++;
}
keysFromContentProcess = []
}
}
let keysFromContentProcess: string[] = []
export function bufferUntilClInputFocused([key]) {
logger.debug("Command line process received content process keydown event: " + key)
export function bufferUntilClInputFocused(bufferedClInputKeys) {
logger.debug("Command line process received content process keys: " + bufferedClInputKeys)
if (bufferedClInputKeys.length < commandline_state.keysFromContentProcess.length)
return
commandline_state.keysFromContentProcess = bufferedClInputKeys;
const keysFromContentProcess = commandline_state.keysFromContentProcess;
if (commandline_state.clInputFocused) {
logger.debug("Dispatching received keydown event " + key + " since clInputFocused is true," +
" clInputNormalEventsReceived = " + commandline_state.clInputNormalEventsReceived +
" contentProcessKeysReceivedAfterClInputFocused = " + commandline_state.contentProcessKeysReceivedAfterClInputFocused +
" clInputInitialValue = " + commandline_state.clInputInitialValue)
if (!commandline_state.clInputNormalEventsReceived) {
// Insert at the end.
commandline_state.clInput.value += key
for (let keyIndex = commandline_state.consumedKeyCount; keyIndex < keysFromContentProcess.length; keyIndex++) {
const key = keysFromContentProcess[keyIndex];
logger.debug("Dispatching received keydown event " + key + " since clInputFocused is true," +
" clInputNormalEventsReceived = " + commandline_state.clInputNormalEventsReceived +
" contentProcessKeysReceivedAfterClInputFocused = " + commandline_state.contentProcessKeysReceivedAfterClInputFocused +
" clInputInitialValue = " + commandline_state.clInputInitialValue)
if (!commandline_state.clInputNormalEventsReceived) {
// Insert at the end.
commandline_state.clInput.value += key
} else {
// Normal keys are assumed to always be at the end.
let normalKeyCount = commandline_state.contentProcessKeysReceivedAfterClInputFocused;
let initialCommandLength = commandline_state.clInputInitialValue.length;
// Insert received key at the beginning.
commandline_state.clInput.value =
commandline_state.clInput.value.substring(0, initialCommandLength + normalKeyCount)
+ key
+ commandline_state.clInput.value.substring(initialCommandLength + normalKeyCount)
commandline_state.contentProcessKeysReceivedAfterClInputFocused++;
}
commandline_state.consumedKeyCount++;
clInputValueChanged()
}
else {
// Normal keys are assumed to always be at the end.
let normalKeyCount = commandline_state.contentProcessKeysReceivedAfterClInputFocused;
let initialCommandLength = commandline_state.clInputInitialValue.length;
// Insert received key at the beginning.
commandline_state.clInput.value =
commandline_state.clInput.value.substring(0, initialCommandLength + normalKeyCount)
+ key
+ commandline_state.clInput.value.substring(initialCommandLength + normalKeyCount)
commandline_state.contentProcessKeysReceivedAfterClInputFocused++;
}
clInputValueChanged()
}
else {
keysFromContentProcess.push(key);
}
}

View file

@ -195,6 +195,7 @@ function* ParserController() {
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 clInputFocused to false")
clInputFocused = false
bufferedClInputKeys = []
}
break
} else {
@ -221,6 +222,7 @@ Messaging.addListener("cl_input_focused", () => {
clInputFocused = true
})
let clInputFocused: boolean = true
let bufferedClInputKeys: string[] = []
export const generator = ParserController() // var rather than let stops weirdness in repl.
generator.next()
@ -231,8 +233,9 @@ export function acceptKey(keyevent: KeyboardEvent) {
let isCharacterKey = keyevent.key.length == 1
&& !keyevent.metaKey && !keyevent.ctrlKey && !keyevent.altKey && !keyevent.metaKey;
if (isCharacterKey) {
logger.debug("Sending keyboardEvent for buffering ", keyevent)
Messaging.messageOwnTab("commandline_frame", "bufferUntilClInputFocused", [keyevent.key]);
bufferedClInputKeys.push(keyevent.key);
logger.debug("Sending keyboardEvent for buffering ", bufferedClInputKeys)
Messaging.messageOwnTab("commandline_frame", "bufferUntilClInputFocused", [ bufferedClInputKeys ]);
}
keyevent.preventDefault()
keyevent.stopImmediatePropagation()

View file

@ -139,8 +139,11 @@ export function getCommandlineFns(cmdline_state: {
cmdline_state.activeCompletions = undefined
cmdline_state.isVisible = false
console.debug("Called hide_and_clear()")
cmdline_state.keysFromContentProcess = []
cmdline_state.consumedKeyCount = 0
cmdline_state.clInputFocused = false
cmdline_state.clInputNormalEventsReceived = false
cmdline_state.clInputInitialValue = ""
cmdline_state.contentProcessKeysReceivedAfterClInputFocused = 0
},