ch11ng / exwm

Emacs X Window Manager
2.85k stars 134 forks source link

frame-focus-state not working with exwm? #599

Closed dakra closed 5 years ago

dakra commented 5 years ago

frame-focus-state doesn't seem to work in the Emacs instance that runs Exwm.

I have the following in my code:

(defun statusbar--refresh-with-focus-check ()
  "Like `statusbar-refresh' but check `frame-focus-state' first."
  (when (frame-focus-state)
    (statusbar-refresh)))
(add-function :after after-focus-change-function #'statusbar--refresh-with-focus-check)))

If I use the focus-in-hook it works fine, but Emacs 27 says it's obsolete and I try to do it the "proper way".

If I start another Emacs instance (in exwm or not), the code works as expected. Only in my main Emacs that has exwm-mode on it doesn't work.

ch11ng commented 5 years ago

In EXWM Emacs frames are not switched the normal way so the focus-in & focus-out events may not get generated. Actually we call focus-in-hook & focus-out-hook explicitly to workaround this problem. There's another hook you can reply on: exwm-workspace-switch-hook. It get called every time you switch to another workspace.

dakra commented 5 years ago

OK, thanks. I'll close this issue now as I simply use exwm-workspace-switch-hook for my own package.

But maybe it's good to keep in mind that from Emacs 27 onwards focus-in/out-hook is deprecated and they say you should advice after-focus-change-function instead. Maybe exwm should also call this function manually like the focus-in/out hook right now to keep compatibility with other packages.

medranocalvo commented 5 years ago

I'm reopening since this will stop working once 27 is out. Perhaps a forward-looking solution is invoking handle-focus-in/out, temporarily bounding internal-handle-focus-in to ignore. Probably worth asking on emacs-devel.

ch11ng commented 5 years ago

@medranocalvo Thanks for the suggestion. I've updated exwm-workspace-switch to include that. It seems internal-handle-focus-in will generate a switch-frame event at most so I simply leave it there.

@dakra Please verify if after-focus-change-function and frame-focus-state works now.

medranocalvo commented 5 years ago

@ch11ng thank you, will try as well.

dakra commented 5 years ago

I tested it with my statusbar package and there after-focus-change-function is now working properly as well :+1: