diff --git a/scanslicer/entry.py b/scanslicer/entry.py index 997ecd7..f38d69e 100644 --- a/scanslicer/entry.py +++ b/scanslicer/entry.py @@ -63,3 +63,37 @@ def resolve_runtime_backend(backend): # Fall back to Wayland when auto-detection has no clear signal. return "wayland" + + +# configure_qt_backend: function definition for ScanSlicer behavior. +def configure_qt_backend(backend): + if backend == "wayland": + os.environ["QT_QPA_PLATFORM"] = "wayland" + # Prevent accidental X11/XWayland routing in mixed environments. + os.environ.pop("DISPLAY", None) + elif backend == "x11": + os.environ["QT_QPA_PLATFORM"] = "xcb" + else: + raise RuntimeError(f"Unsupported backend '{backend}'.") + + is_appimage = bool(os.environ.get("APPIMAGE")) + + # In sandboxed desktop runtimes (Flatpak/Snap), prefer portal integration + # for file dialogs and desktop services. + sandboxed = bool( + os.environ.get("FLATPAK_ID") + or os.environ.get("SNAP") + ) + if sandboxed: + os.environ.setdefault("QT_QPA_PLATFORMTHEME", "xdgdesktopportal") + elif is_appimage: + # AppImage is not a strict sandbox and some portal backends can emit: + # "Connection already associated with an application ID". + # Prefer GTK theme integration and avoid forced portal registration. + os.environ.setdefault("QT_QPA_PLATFORMTHEME", "gtk3") + os.environ.setdefault("QT_NO_XDG_DESKTOP_PORTAL", "1") + + # In containers, prefer GTK platform theme so Qt can follow host theme + # settings when available (for example dark/light mode). + if os.path.exists("/.dockerenv"): + os.environ.setdefault("QT_QPA_PLATFORMTHEME", "gtk3")