@@ -128,19 +128,29 @@ def _init_native_engine(self, config):
128128 f"Cause: { details } \n "
129129 )
130130
131+ orig_wayland = None
132+ orig_winit = None
133+ orig_gdk = None
134+ is_wayland = False
135+
131136 if sys .platform .startswith ("linux" ):
132137 self .logger .warning ("Linux Native Engine is still experimental." )
133138 import os
134139
135140 session_type = os .environ .get ("XDG_SESSION_TYPE" , "" ).lower ()
136141 if session_type == "wayland" :
137- if not os .environ .get ("WINIT_UNIX_BACKEND" ):
138- self .logger .info (
139- "Wayland session detected, forcing WINIT_UNIX_BACKEND=x11 for better native stability."
140- )
141- os .environ ["WINIT_UNIX_BACKEND" ] = "x11"
142- if not os .environ .get ("GDK_BACKEND" ):
143- os .environ ["GDK_BACKEND" ] = "x11"
142+ is_wayland = True
143+ self .logger .info (
144+ "Wayland session detected. Forcing X11 backend temporarily to initialize native webview."
145+ )
146+ orig_wayland = os .environ .get ("WAYLAND_DISPLAY" )
147+ orig_winit = os .environ .get ("WINIT_UNIX_BACKEND" )
148+ orig_gdk = os .environ .get ("GDK_BACKEND" )
149+
150+ if "WAYLAND_DISPLAY" in os .environ :
151+ del os .environ ["WAYLAND_DISPLAY" ]
152+ os .environ ["WINIT_UNIX_BACKEND" ] = "x11"
153+ os .environ ["GDK_BACKEND" ] = "x11"
144154
145155 raw_url = config .get ("url" , "" )
146156 debug = config .get ("debug" , False )
@@ -196,6 +206,22 @@ def _init_native_engine(self, config):
196206 ) from e
197207
198208 raise NativeEngineError (f"Failed to initialize Native WebView: { e } " ) from e
209+ finally :
210+ if is_wayland :
211+ import os
212+
213+ if orig_wayland is not None :
214+ os .environ ["WAYLAND_DISPLAY" ] = orig_wayland
215+ else :
216+ os .environ .pop ("WAYLAND_DISPLAY" , None )
217+ if orig_winit is not None :
218+ os .environ ["WINIT_UNIX_BACKEND" ] = orig_winit
219+ else :
220+ os .environ .pop ("WINIT_UNIX_BACKEND" , None )
221+ if orig_gdk is not None :
222+ os .environ ["GDK_BACKEND" ] = orig_gdk
223+ else :
224+ os .environ .pop ("GDK_BACKEND" , None )
199225
200226 def _setup_native_window (self , config ):
201227 """Applies window settings and platform helpers for native engine."""
0 commit comments