Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(capture): drop GTK shadow margins from X11 window rects

rubenluetke10-beepa2694d24ecf1
diff --git a/jarvis/platform/window_state.py b/jarvis/platform/window_state.py
index f6cd17366b..ad0d6758f2 100644
--- a/jarvis/platform/window_state.py
+++ b/jarvis/platform/window_state.py
@@ -1747,7 +1747,46 @@ def _window_frame_rect_linux(win: WindowInfo) -> tuple[int, int, int, int] | Non
return None
if width <= 0 or height <= 0:
return None
- return (left, top, width, height)
+ return _trim_gtk_frame_extents(int(win.handle), (left, top, width, height))
+
+
+def _trim_gtk_frame_extents(
+ window_id: int, rect: tuple[int, int, int, int]
+) -> tuple[int, int, int, int]:
+ """Drop a GTK client-side-decoration shadow from an X11 window rect.
+
+ GTK windows that draw their own title bar (GNOME apps) make the X window
+ larger than what the user sees, by the invisible shadow margins they
+ publish in ``_GTK_FRAME_EXTENTS`` (left, right, top, bottom). Without the
+ trim a window appshot showed a band of desktop around the window. A
+ window without the property, or a host without python-xlib, keeps the
+ rect as it is.
+ """
+ try:
+ from Xlib import X, display # type: ignore[import-not-found] # noqa: PLC0415
+ except ImportError:
+ return rect
+ conn = None
+ try:
+ conn = display.Display()
+ window = conn.create_resource_object("window", window_id)
+ prop = window.get_full_property(conn.intern_atom("_GTK_FRAME_EXTENTS"), X.AnyPropertyType)
+ values = list(getattr(prop, "value", []) or [])
+ except Exception: # noqa: BLE001 - an unreadable property keeps the plain rect
+ log.debug("GTK frame extents unreadable for window %s", window_id, exc_info=True)
+ return rect
+ finally:
+ if conn is not None:
+ conn.close()
+ if len(values) != 4:
+ return rect
+ left_m, right_m, top_m, bottom_m = (max(0, int(v)) for v in values)
+ left, top, width, height = rect
+ trimmed_w = width - left_m - right_m
+ trimmed_h = height - top_m - bottom_m
+ if trimmed_w <= 0 or trimmed_h <= 0:
+ return rect
+ return (left + left_m, top + top_m, trimmed_w, trimmed_h)
def _resolve_macos_ax_window(win: WindowInfo) -> tuple[object | None, str]:
diff --git a/tests/unit/platform/test_gtk_frame_extents.py b/tests/unit/platform/test_gtk_frame_extents.py
new file mode 100644
index 0000000000..e09b9223b5
--- /dev/null
+++ b/tests/unit/platform/test_gtk_frame_extents.py
@@ -0,0 +1,66 @@
+"""GTK windows that draw their own title bar publish shadow margins; captures drop them."""
+
+from __future__ import annotations
+
+import sys
+from types import ModuleType, SimpleNamespace
+
+import pytest
+
+from jarvis.platform import window_state
+
+
+def _fake_xlib(monkeypatch: pytest.MonkeyPatch, extents: list[int] | None) -> list[str]:
+ closed: list[str] = []
+
+ class Window:
+ def get_full_property(self, _atom, _type):
+ return None if extents is None else SimpleNamespace(value=extents)
+
+ class Display:
+ def create_resource_object(self, kind, window_id):
+ assert (kind, window_id) == ("window", 0x2A00007)
+ return Window()
+
+ def intern_atom(self, name):
+ assert name == "_GTK_FRAME_EXTENTS"
+ return 1
+
+ def close(self):
+ closed.append("closed")
+
+ xlib = ModuleType("Xlib")
+ xlib.X = SimpleNamespace(AnyPropertyType=0)
+ xlib.display = SimpleNamespace(Display=Display)
+ monkeypatch.setitem(sys.modules, "Xlib", xlib)
+ return closed
+
+
+def test_the_shadow_margins_are_cut_off(monkeypatch: pytest.MonkeyPatch) -> None:
+ closed = _fake_xlib(monkeypatch, [23, 23, 15, 31])
+
+ rect = window_state._trim_gtk_frame_extents(0x2A00007, (100, 50, 846, 646)) # noqa: SLF001
+
+ assert rect == (123, 65, 800, 600)
+ assert closed == ["closed"]
+
+
[email protected]("extents", [None, [], [5, 5]])
+def test_a_window_without_the_property_keeps_its_rect(
+ monkeypatch: pytest.MonkeyPatch, extents: list[int] | None
+) -> None:
+ _fake_xlib(monkeypatch, extents)
+ rect = (100, 50, 846, 646)
+ assert window_state._trim_gtk_frame_extents(0x2A00007, rect) == rect # noqa: SLF001
+
+
+def test_margins_larger_than_the_window_are_ignored(monkeypatch: pytest.MonkeyPatch) -> None:
+ _fake_xlib(monkeypatch, [500, 500, 0, 0])
+ rect = (0, 0, 800, 600)
+ assert window_state._trim_gtk_frame_extents(0x2A00007, rect) == rect # noqa: SLF001
+
+
+def test_without_python_xlib_the_rect_is_kept(monkeypatch: pytest.MonkeyPatch) -> None:
+ monkeypatch.setitem(sys.modules, "Xlib", None) # import raises ImportError
+ rect = (1, 2, 3, 4)
+ assert window_state._trim_gtk_frame_extents(7, rect) == rect # noqa: SLF001