Files
niri-ai-sidebar/main.py
Melvin Ragusa 1a80358ffc Fixes GTK4 Layer Shell and Ollama integration issues
Addresses multiple issues related to GTK4 Layer Shell initialization and Ollama integration.

- Reorders initialization to ensure the layer shell is set up before window properties.
- Adds error detection for layer shell initialization failures.
- Implements a focus event handler to prevent focus-out warnings.
- Introduces a launcher script to activate the virtual environment, force native Wayland, and preload the GTK4 Layer Shell library.
- Warns users of incorrect GDK_BACKEND settings.
- Updates the Ollama client to handle responses from both older and newer versions of the Ollama SDK.

These changes improve the application's stability, compatibility, and functionality on Wayland systems.
2025-10-25 21:23:32 +02:00

79 lines
2.2 KiB
Python

"""Entry point for the AI sidebar application."""
from __future__ import annotations
import os
import sys
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk # noqa: E402
from sidebar_window import SidebarWindow # noqa: E402
HEADLESS_ENV_VAR = "AI_SIDEBAR_HEADLESS"
_STARTUP_FAILED = False
def main(argv: list[str] | None = None) -> int:
"""Launch the GTK application and return the exit status."""
args = argv or sys.argv
if os.environ.get(HEADLESS_ENV_VAR) == "1":
print("Headless mode enabled; skipping GTK startup.")
return 0
if not Gtk.init_check():
print(
"Failed to initialize GTK. Ensure a display server is available.",
file=sys.stderr,
)
return 1
if not (os.environ.get("WAYLAND_DISPLAY") or os.environ.get("DISPLAY")):
print(
"No Wayland or X11 display detected. Launch this app inside a graphical session.",
file=sys.stderr,
)
return 1
# Warn if GDK_BACKEND is set to X11 on Wayland systems
if os.environ.get("WAYLAND_DISPLAY") and os.environ.get("GDK_BACKEND") == "x11":
print(
"Warning: GDK_BACKEND is set to 'x11' but you're on Wayland.",
file=sys.stderr,
)
print(
"GTK4 Layer Shell requires native Wayland. Use './run.sh' instead.",
file=sys.stderr,
)
app = Gtk.Application(application_id="ai.sidebar")
app.connect("activate", _on_activate)
status = app.run(args)
return 1 if _STARTUP_FAILED else status
def _on_activate(app: Gtk.Application) -> None:
"""Create and present the main sidebar window when the app activates."""
try:
window = SidebarWindow(application=app)
except RuntimeError as exc:
if "Gtk couldn't be initialized" in str(exc):
print(
"Failed to initialize GTK. Ensure a display server is available.",
file=sys.stderr,
)
global _STARTUP_FAILED
_STARTUP_FAILED = True
app.quit()
return
raise
window.present()
if __name__ == "__main__":
raise SystemExit(main())