Skip to content

Commit 8df41d0

Browse files
Make EAF support Wayland native.
1 parent 5c50a75 commit 8df41d0

File tree

2 files changed

+86
-71
lines changed

2 files changed

+86
-71
lines changed

Diff for: core/view.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ def __init__(self, buffer, view_info):
3535
self.buffer = buffer
3636

3737
# Init widget attributes.
38-
if get_emacs_func_result("eaf-emacs-not-use-reparent-technology", []):
38+
if get_emacs_func_result("eaf-emacs-running-in-wayland-native", []):
39+
self.setWindowFlags(Qt.FramelessWindowHint | Qt.BypassWindowManagerHint)
40+
elif get_emacs_func_result("eaf-emacs-not-use-reparent-technology", []):
3941
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint | Qt.NoDropShadowWindowHint)
4042
else:
4143
self.setWindowFlags(Qt.FramelessWindowHint)

Diff for: eaf.el

+83-70
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,17 @@ A hashtable, key is url and value is title.")
564564
(defun eaf--follow-system-dpi ()
565565
(if (and (getenv "WAYLAND_DISPLAY") (not (string= (getenv "WAYLAND_DISPLAY") "")))
566566
(progn
567-
;; We need manually set scale factor when at Gnome/Wayland environment.
568-
;; It is important to set QT_AUTO_SCREEN_SCALE_FACTOR=0
569-
;; otherwise Qt which explicitly force high DPI enabling get scaled TWICE.
570-
(setenv "QT_AUTO_SCREEN_SCALE_FACTOR" "0")
567+
(cond ((eaf-emacs-running-in-wayland-native)
568+
;; Wayland native need to set QT_AUTO_SCREEN_SCALE_FACTOR=1
569+
;; otherwise Qt window only have half of screen.
570+
(setenv "QT_AUTO_SCREEN_SCALE_FACTOR" "1"))
571+
(t
572+
;; XWayland need to set QT_AUTO_SCREEN_SCALE_FACTOR=0
573+
;; otherwise Qt which explicitly force high DPI enabling get scaled TWICE.
574+
(setenv "QT_AUTO_SCREEN_SCALE_FACTOR" "0")))
571575
;; Set EAF application scale factor.
572576
(setenv "QT_SCALE_FACTOR" "1")
573-
;; Force xwayland to ensure SWay works.
577+
;; Use XCB for input event transfer.
574578
(setenv "QT_QPA_PLATFORM" "xcb"))
575579
(setq process-environment
576580
(seq-filter
@@ -811,24 +815,24 @@ to edit EAF keybindings!" fun fun)))
811815
(set-keymap-parent map eaf-mode-map*))
812816
(cl-loop for (key . fun) in (reverse keybinding)
813817
do (define-key map (kbd key)
814-
(cond
815-
;; If command is normal symbol, just call it directly.
816-
((symbolp fun)
817-
fun)
818-
819-
;; If command is string and include - , it's elisp function, use `intern' build elisp function from function name.
820-
((string-match "-" fun)
821-
(intern fun))
822-
823-
;; If command prefix with js_, call JavaScript function directly.
824-
((string-prefix-p "js_" fun)
825-
(eaf--make-js-proxy-function fun))
826-
827-
;; If command is not built-in function and not include char '-'
828-
;; it's command in python side, build elisp proxy function to call it.
829-
(t
830-
(eaf--make-py-proxy-function fun))
831-
))
818+
(cond
819+
;; If command is normal symbol, just call it directly.
820+
((symbolp fun)
821+
fun)
822+
823+
;; If command is string and include - , it's elisp function, use `intern' build elisp function from function name.
824+
((string-match "-" fun)
825+
(intern fun))
826+
827+
;; If command prefix with js_, call JavaScript function directly.
828+
((string-prefix-p "js_" fun)
829+
(eaf--make-js-proxy-function fun))
830+
831+
;; If command is not built-in function and not include char '-'
832+
;; it's command in python side, build elisp proxy function to call it.
833+
(t
834+
(eaf--make-py-proxy-function fun))
835+
))
832836
finally return map))))
833837

834838
(defun eaf--get-app-bindings (app-name)
@@ -918,111 +922,120 @@ In this situation, we use 'stay on top' technicality that show EAF window when E
918922
919923
'Stay on top' technicality is not perfect like 'cross-process reparent' technicality,
920924
provide at least one way to let everyone experience EAF. ;)"
921-
(or (eq system-type 'darwin) ;macOS
922-
(and (eq window-system 'pgtk) ;Wayland native
923-
(fboundp 'pgtk-backend-display-class)
924-
(string-equal (pgtk-backend-display-class) "GdkWaylandDisplay"))
925-
(not (display-graphic-p)) ;Terminal emulator
925+
(or (eq system-type 'darwin) ;macOS
926+
(eaf-emacs-running-in-wayland-native) ;Wayland native
927+
(not (display-graphic-p)) ;Terminal emulator
926928
))
927929

930+
(defun eaf-emacs-running-in-wayland-native ()
931+
(and (eq window-system 'pgtk)
932+
(fboundp 'pgtk-backend-display-class)
933+
(string-equal (pgtk-backend-display-class) "GdkWaylandDisplay")))
934+
928935
(eval-when-compile
929936
(when (eaf-emacs-not-use-reparent-technology)
930937
(cond
931938
((eq system-type 'darwin)
932-
(defcustom eaf--stay-on-top-safe-focus-change t
939+
(defcustom eaf--mac-safe-focus-change t
933940
"Whether to verify the active application on Emacs frame focus change.
934941
935942
Only set this to nil if you do not use the mouse inside EAF buffers.
936943
The benefit of setting this to nil is that application switching
937944
is a lot faster but could be buggy."
938945
:type 'boolean)
939946

940-
(defvar eaf--stay-on-top-switch-to-python nil
947+
(defvar eaf--mac-switch-to-python nil
941948
"Record if Emacs should switch to Python process.")
942949

943-
(defvar eaf--stay-on-top-has-focus t
950+
(defvar eaf--mac-has-focus t
944951
"Record if Emacs has focus.")
945952

946-
(defvar eaf--stay-on-top-unsafe-focus-change-timer nil
953+
(defvar eaf--mac-unsafe-focus-change-timer nil
947954
"Use timer to ignore spurious focus events.
948955
949-
This is only used when `eaf--stay-on-top-safe-focus-change' is nil.
956+
This is only used when `eaf--mac-safe-focus-change' is nil.
950957
951958
See
952959
https://old.reddit.com/r/emacs/comments/\
953960
kxsgtn/ignore_spurious_focus_events_for/")
954961

955-
(defun eaf--stay-on-top-unsafe-focus-change-handler ()
962+
(defun eaf--mac-unsafe-focus-change-handler ()
956963
;; ignore errors related to
957964
;; (wrong-type-argument eaf-epc-manager nil)
958965
(ignore-errors
959966
(if (frame-focus-state)
960-
(eaf--stay-on-top-unsafe-focus-in)
961-
(eaf--stay-on-top-unsafe-focus-out)))
962-
(setq eaf--stay-on-top-unsafe-focus-change-timer nil))
967+
(eaf--mac-unsafe-focus-in)
968+
(eaf--mac-unsafe-focus-out)))
969+
(setq eaf--mac-unsafe-focus-change-timer nil))
963970

964-
(defun eaf--stay-on-top-focus-change ()
971+
(defun eaf--mac-focus-change ()
965972
"Manage Emacs's focus change."
966-
(if eaf--stay-on-top-safe-focus-change
973+
(if eaf--mac-safe-focus-change
967974
(let ((front (shell-command-to-string "app-frontmost --name")))
968975
(cond
969976
((string= "Python\n" front)
970-
(setq eaf--stay-on-top-switch-to-python t))
977+
(setq eaf--mac-switch-to-python t))
971978

972979
((string= "Emacs\n" front)
973980
(cond
974-
(eaf--stay-on-top-switch-to-python
975-
(setq eaf--stay-on-top-switch-to-python nil))
976-
((not eaf--stay-on-top-has-focus)
977-
(run-with-timer 0.1 nil #'eaf--stay-on-top-focus-in))
978-
(eaf--stay-on-top-has-focus
979-
(eaf--stay-on-top-focus-out))))
980-
(t (eaf--stay-on-top-focus-out))))
981-
(setq eaf--stay-on-top-unsafe-focus-change-timer
982-
(unless eaf--stay-on-top-unsafe-focus-change-timer
981+
(eaf--mac-switch-to-python
982+
(setq eaf--mac-switch-to-python nil))
983+
((not eaf--mac-has-focus)
984+
(run-with-timer 0.1 nil #'eaf--mac-focus-in))
985+
(eaf--mac-has-focus
986+
(eaf--mac-focus-out))))
987+
(t (eaf--mac-focus-out))))
988+
(setq eaf--mac-unsafe-focus-change-timer
989+
(unless eaf--mac-unsafe-focus-change-timer
983990
(run-at-time 0.06 nil
984-
#'eaf--stay-on-top-unsafe-focus-change-handler)))))
991+
#'eaf--mac-unsafe-focus-change-handler)))))
985992

986-
(defun eaf--stay-on-top-replace-eaf-buffers ()
993+
(defun eaf--mac-replace-eaf-buffers ()
987994
(dolist (window (window-list))
988995
(select-window window)
989996
(when (eq major-mode 'eaf-mode)
990997
(get-buffer-create "*eaf temp*")
991998
(switch-to-buffer "*eaf temp*" t))))
992999

993-
(defun eaf--stay-on-top-focus-in ()
994-
(setq eaf--stay-on-top-has-focus t)
1000+
(defun eaf--mac-focus-in ()
1001+
(setq eaf--mac-has-focus t)
9951002
(ignore-errors
9961003
(set-window-configuration
997-
(frame-parameter (selected-frame) 'eaf--stay-on-top-frame))
1004+
(frame-parameter (selected-frame) 'eaf--mac-frame))
9981005
(bury-buffer "*eaf temp*")))
9991006

1000-
(defun eaf--stay-on-top-focus-out (&optional frame)
1001-
(when eaf--stay-on-top-has-focus
1002-
(setq eaf--stay-on-top-has-focus nil)
1007+
(defun eaf--mac-focus-out (&optional frame)
1008+
(when eaf--mac-has-focus
1009+
(setq eaf--mac-has-focus nil)
10031010
(set-frame-parameter (or frame (selected-frame))
1004-
'eaf--stay-on-top-frame (current-window-configuration))
1005-
(eaf--stay-on-top-replace-eaf-buffers)))
1011+
'eaf--mac-frame (current-window-configuration))
1012+
(eaf--mac-replace-eaf-buffers)))
10061013

1007-
(defun eaf--stay-on-top-unsafe-focus-in ()
1014+
(defun eaf--mac-unsafe-focus-in ()
10081015
(eaf-call-async "show_top_views")
10091016
(set-window-configuration
1010-
(frame-parameter (selected-frame) 'eaf--stay-on-top-frame)))
1017+
(frame-parameter (selected-frame) 'eaf--mac-frame)))
10111018

1012-
(defun eaf--stay-on-top-unsafe-focus-out (&optional frame)
1019+
(defun eaf--mac-unsafe-focus-out (&optional frame)
10131020
(eaf-call-async "hide_top_views")
1014-
(set-frame-parameter (or frame (selected-frame)) 'eaf--stay-on-top-frame
1021+
(set-frame-parameter (or frame (selected-frame)) 'eaf--mac-frame
10151022
(current-window-configuration)))
10161023

1017-
(defun eaf--stay-on-top-delete-frame-handler (frame)
1018-
(if eaf--stay-on-top-safe-focus-change
1019-
(eaf--stay-on-top-focus-out frame)
1020-
(eaf--stay-on-top-unsafe-focus-out frame)))
1024+
(defun eaf--mac-delete-frame-handler (frame)
1025+
(if eaf--mac-safe-focus-change
1026+
(eaf--mac-focus-out frame)
1027+
(eaf--mac-unsafe-focus-out frame)))
10211028

1022-
(add-function :after after-focus-change-function #'eaf--stay-on-top-focus-change)
1023-
(add-to-list 'delete-frame-functions #'eaf--stay-on-top-delete-frame-handler))
1029+
(add-function :after after-focus-change-function #'eaf--mac-focus-change)
1030+
(add-to-list 'delete-frame-functions #'eaf--mac-delete-frame-handler))
10241031
(t
1025-
(message "Wayland native code is not supported yet.")))))
1032+
(defun eaf--wayland-focus-change ()
1033+
"Manage Emacs's focus change."
1034+
(if (frame-focus-state)
1035+
(eaf-call-async "show_top_views")
1036+
(eaf-call-async "hide_top_views")))
1037+
(add-function :after after-focus-change-function #'eaf--wayland-focus-change)
1038+
))))
10261039

10271040
(defun eaf-monitor-configuration-change (&rest _)
10281041
"EAF function to respond when detecting a window configuration change."

0 commit comments

Comments
 (0)