From e74f12f85b0f83d413da13b9a9f295a35eec399f Mon Sep 17 00:00:00 2001 From: Qiang Fang Date: Fri, 31 Jan 2020 17:50:38 +0800 Subject: refactor(3p/exwm): Improve detection of focus changes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * exwm-input.el: (exwm-input--on-buffer-list-update): Keep track of last selected window and buffer, update focus only when any of those changes. (exwm-input--update-focus-defer): Add commentary. (exwm-input--update-focus-window-buffer): Add variable. Imported from https://github.com/ch11ng/exwm/pull/737 Copyright-paperwork-exempt: yes Co-Author: Adrián Medraño Calvo Change-Id: I3e53bcf45f04d0f9a88b757dffefe6de20daadfb Reviewed-on: https://cl.tvl.fyi/c/depot/+/3561 Tested-by: BuildkiteCI Reviewed-by: tazjin --- third_party/exwm/exwm-input.el | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'third_party/exwm/exwm-input.el') diff --git a/third_party/exwm/exwm-input.el b/third_party/exwm/exwm-input.el index decfc8128cc9..1120e81eba26 100644 --- a/third_party/exwm/exwm-input.el +++ b/third_party/exwm/exwm-input.el @@ -138,8 +138,16 @@ defined in `exwm-mode-map' here." "Timer for deferring the update of input focus.") (defvar exwm-input--update-focus-window nil "The (Emacs) window to be focused. +It also helps us discern whether a `buffer-list-update-hook' was caused by a +different window having been selected. + This value should always be overwritten.") +(defvar exwm-input--update-focus-window-buffer nil + "Buffer displayed in `exwm-input--update-focus-window'. +Helps us discern whether a `buffer-list-update-hook' was caused by the selected +window switching to a different buffer.") + (defvar exwm-input--echo-area-timer nil "Timer for detecting echo area dirty.") (defvar exwm-input--event-hook nil @@ -296,13 +304,25 @@ ARGS are additional arguments to CALLBACK." (defun exwm-input--on-buffer-list-update () "Run in `buffer-list-update-hook' to track input focus." - (when (and (not (exwm-workspace--client-p)) - (not exwm-input--skip-buffer-list-update)) - (exwm--log "current-buffer=%S selected-window=%S" - (current-buffer) (selected-window)) - (redirect-frame-focus (selected-frame) nil) - (setq exwm-input--update-focus-window (selected-window)) - (exwm-input--update-focus-defer))) + ;; `buffer-list-update-hook' is invoked by several functions + ;; (`get-buffer-create', `select-window', `with-temp-buffer', etc.), but we + ;; just want to notice when a different window has been selected, or when the + ;; selected window displays a different buffer, so that we can set the focus + ;; to the associated X window (in case of an `exwm-mode' buffer). In order to + ;; differentiate, we keep track of the last selected window and buffer in the + ;; `exwm-input--update-focus-window' and + ;; `exwm-input--update-focus-window-buffer' variables. + (let* ((win (selected-window)) + (buf (window-buffer win))) + (when (and (not (exwm-workspace--client-p)) + (not exwm-input--skip-buffer-list-update) + (not (and (eq exwm-input--update-focus-window win) + (eq exwm-input--update-focus-window-buffer buf)))) + (exwm--log "selected-window=%S current-buffer=%S" win buf) + (setq exwm-input--update-focus-window win) + (setq exwm-input--update-focus-window-buffer buf) + (redirect-frame-focus (selected-frame) nil) + (exwm-input--update-focus-defer)))) (defun exwm-input--update-focus-defer () "Defer updating input focus." -- cgit 1.4.1