Attempt to buffer keyboard events until command line is opened

This commit is contained in:
petoncle 2023-10-27 23:37:54 +02:00
parent f224e6cf44
commit 4a31b3ac20
4 changed files with 122 additions and 59 deletions

View file

@ -162,9 +162,42 @@ const noblur = () => setTimeout(() => commandline_state.clInput.focus(), 0)
/** @hidden **/
export function focus() {
setTimeout(() => {
logger.info("Called focus() after 2000ms")
Messaging.messageOwnTab("clInputFocused", "unused")
commandline_state.clInput.focus()
commandline_state.clInput.removeEventListener("blur", noblur)
commandline_state.clInput.addEventListener("blur", noblur)
if (buffer.length !== 0) {
logger.info("Dispatching " + JSON.stringify(buffer));
buffer.forEach(e => processKeyboardEvent(e))
buffer.splice(-1)
}
}, 2000)
}
let buffer: KeyboardEvent[] = []
export function bufferUntil([ code,
key,
altKey,
ctrlKey,
metaKey,
shiftKey]) {
const keyevent = new KeyboardEvent('keydown', {
code: code,
key: key,
altKey: altKey,
ctrlKey: ctrlKey,
metaKey: metaKey,
shiftKey: shiftKey,
})
logger.info("Received keyboardEvent for buffering", keyevent)
if (window.document.activeElement === commandline_state.clInput) {
processKeyboardEvent(keyevent)
}
else {
buffer.push(keyevent);
}
}
/** @hidden **/
@ -182,12 +215,19 @@ let prev_cmd_called_history = false
// Save programmer time by generating an immediately resolved promise
// eslint-disable-next-line @typescript-eslint/no-empty-function
const QUEUE: Promise<any>[] = [(async () => {})()]
logger.info("Setting event listeners of commandline_state.clInput")
/** @hidden **/
commandline_state.clInput.addEventListener(
"keydown",
function (keyevent: KeyboardEvent) {
if (!keyevent.isTrusted) return
processKeyboardEvent(keyevent);
},
true,
)
function processKeyboardEvent(keyevent: KeyboardEvent) {
commandline_state.keyEvents.push(minimalKeyFromKeyboardEvent(keyevent))
const response = keyParser(commandline_state.keyEvents)
if (response.isMatch) {
@ -240,9 +280,7 @@ commandline_state.clInput.addEventListener(
} else {
commandline_state.keyEvents = response.keys
}
},
true,
)
}
export function refresh_completions(exstr) {
if (!commandline_state.activeCompletions) enableCompletions()

View file

@ -63,7 +63,7 @@ export function show(hidehover = false) {
*
* Inspired by VVimpulation: https://github.com/amedama41/vvimpulation/commit/53065d015d1e9a892496619b51be83771f57b3d5
*/
logger.info("Called show()")
if (hidehover) {
const a = window.document.createElement("A")
;(a as any).href = ""
@ -98,7 +98,7 @@ export function hide() {
export function focus() {
try {
cmdline_iframe.focus()
// cmdline_iframe.focus()
} catch (e) {
// Note: We can't use cmdline_logger.error because it will try to log
// the error in the commandline, which will need to focus() it again,

View file

@ -14,6 +14,7 @@ import * as hinting from "@src/content/hinting"
import * as gobblemode from "@src/parsers/gobblemode"
import * as generic from "@src/parsers/genericmode"
import * as nmode from "@src/parsers/nmode"
import * as Messaging from "@src/lib/messaging";
const logger = new Logger("controller")
@ -191,6 +192,10 @@ function* ParserController() {
if (response.exstr) {
exstr = response.exstr
if (exstr.startsWith("fillcmdline ")) {
logger.info("Setting bufferUntilClInputFocused to true")
bufferUntilClInputFocused = true
}
break
} else {
keyEvents = response.keys
@ -211,11 +216,30 @@ function* ParserController() {
}
}
}
Messaging.addListener("clInputFocused", () => {
logger.info("Callback clInputFocused")
bufferUntilClInputFocused = false
})
let bufferUntilClInputFocused: boolean = false
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.info("bufferUntilClInputFocused = " + bufferUntilClInputFocused)
if (bufferUntilClInputFocused) {
logger.info("Sending keyboardEvent for buffering " + keyevent.key)
Messaging.messageOwnTab("commandline_frame", "bufferUntil",
[keyevent.code,
keyevent.key,
keyevent.altKey,
keyevent.ctrlKey,
keyevent.metaKey,
keyevent.shiftKey]
);
keyevent.preventDefault()
keyevent.stopImmediatePropagation()
canceller.push(keyevent)
} else
return generator.next(keyevent)
}

View file

@ -15,6 +15,7 @@ export type TabMessageType =
| "lock"
| "alive"
| "tab_changes"
| "clInputFocused"
export type NonTabMessageType =
| "owntab_background"