Skip to content

Commit

Permalink
Input: Alternately, do not block on input dispatch.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmercouris committed Mar 30, 2024
1 parent d058c17 commit 519d380
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
44 changes: 11 additions & 33 deletions source/input.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ KEYCODE-LESS-DISPLAY (KEYCODE-DISPLAY)."
"Default `input-scrip-dispatcher'. Logs the skipped key."
(log:debug "Skipping input event key ~s" keyspecs))

(export-always 'consume-input-event-p)
(defun consume-input-event-p (buffer)
"Should the input event be consumed?"
(with-accessors ((key-stack key-stack)) buffer
(let ((bound-function (keymaps:lookup-key key-stack (current-keymaps buffer))))
(if (or (keymaps:keymap-p bound-function)
(typep bound-function '(and (not null) (or symbol command))))
t
nil))))

(export-always 'dispatch-input-event)
(defun dispatch-input-event (event buffer)
"Dispatch keys in BUFFER `key-stack'.
Expand All @@ -140,45 +130,33 @@ Return nil to forward to renderer or non-nil otherwise."
(when (input-buffer-p buffer)
(setf (last-event buffer) event))
(when (prompt-buffer-p buffer)
;; Prompt buffer updating must happen on a separate thread.
(run-thread "update-prompt-buffer"
(update-prompt-input buffer
(ps-eval :buffer buffer
(ps:chain (nyxt/ps:qs document "#input") value)))))
(update-prompt-input buffer (ps-eval :buffer buffer
(ps:chain (nyxt/ps:qs document "#input") value)))))
(multiple-value-bind (bound-function matching-keymap translated-key)
(the keyscheme:nyxt-keymap-value
(keymaps:lookup-key key-stack (current-keymaps buffer)))
(keymaps:lookup-key key-stack (current-keymaps buffer))
(declare (ignore matching-keymap))
(cond
((keymaps:keymap-p bound-function)
(echo "Pressed keys: ~a" (keyspecs-without-keycode key-stack))
(log:debug "Prefix binding ~a" (keyspecs key-stack translated-key))
(log:debug "Prefix binding ~a." (keyspecs key-stack translated-key))
t)

((typep bound-function '(and (not null) (or symbol command)))
(let ((command (typecase bound-function
(symbol (symbol-function (resolve-user-symbol bound-function :command)))
(command bound-function))))
(check-type command command)
(log:debug "Found key binding ~a to ~a" (keyspecs key-stack translated-key) bound-function)
;; We save the last key separately to keep it available to the
;; command even after key-stack has been reset in the other
;; thread.
(log:debug "Found key binding ~a to ~a." (keyspecs key-stack translated-key) bound-function)
(setf (last-key buffer) (first key-stack))
(unwind-protect
(funcall (command-dispatcher *browser*) command)
;; We must reset the key-stack on errors or else all subsequent
;; keypresses will keep triggering the same erroring command.
(setf key-stack nil))
(run-thread "run-command"
(unwind-protect
(funcall (command-dispatcher *browser*) command)
(setf key-stack nil)))
t))

((or (and (input-buffer-p buffer) (forward-input-events-p buffer))
(pointer-event-p (first (last key-stack))))
(log:debug "Forward key ~s" (keyspecs key-stack))
(log:debug "Forward key ~s." (keyspecs key-stack))
(setf key-stack nil)
nil)

(t
(log:debug "Fallback forward key ~s" (keyspecs key-stack))
(log:debug "Fallback forward key ~s." (keyspecs key-stack))
(setf key-stack nil)
nil))))))
4 changes: 1 addition & 3 deletions source/renderer/electron.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -460,9 +460,7 @@ Return nil when key must be discarded, e.g. for modifiers."
(when key-string
(alex:appendf (key-stack sender) (list (key)))
(run-thread "on-signal-key-press" (on-signal-key-press sender (key)))
(if (consume-input-event-p sender)
(progn (run-thread "dispatch-input-event" (dispatch-input-event event sender)) t)
nil)))))
(dispatch-input-event event sender)))))

(defmethod on-signal-key-release-event ((sender electron-window) event)
(declare (ignore sender event)))
Expand Down

0 comments on commit 519d380

Please sign in to comment.