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(jarvisbar): pin the bar window DPI-UNAWARE - no more shrink/jump/drag-offset

Ruben Lütke49ca117564bc
diff --git a/jarvis/core/win32_dpi.py b/jarvis/core/win32_dpi.py
index 312b971324..258ce5ab60 100644
--- a/jarvis/core/win32_dpi.py
+++ b/jarvis/core/win32_dpi.py
@@ -56,3 +56,43 @@ def ensure_dpi_awareness() -> None:
logger.warning("Could not set DPI awareness", exc_info=True)
finally:
_DPI_AWARENESS_SET = True
+
+
+def pin_thread_dpi_unaware() -> bool:
+ """Pin the CALLING thread's DPI-awareness context to UNAWARE (Windows).
+
+ Every top-level window the thread creates afterwards carries the UNAWARE
+ context PER WINDOW: Windows bitmap-scales it to the monitor's scale factor
+ (the familiar upscaled look on a 150 % display) and virtualises its
+ coordinate space — and, critically, a later PROCESS-level awareness flip
+ (pywebview's ``webview.start()`` calls ``user32.SetProcessDPIAware()`` at
+ runtime, ``webview/platforms/winforms.py``) can no longer strip that
+ scaling off the window. That flip is what made the JarvisBar shrink to
+ ~2/3, jump position, and drag with a large cursor offset (recurring,
+ boot-race dependent).
+
+ The pin only survives such a flip when the process is ALREADY DPI-aware,
+ so call :func:`ensure_dpi_awareness` first. The context is deliberately
+ NOT restored — call this only from a thread fully owned by the unaware
+ surface (e.g. the bar's dedicated Tk mainloop thread).
+
+ Returns True when the pin took effect; False (graceful no-op) off Windows
+ or when the API is unavailable (pre-Windows-10-1607).
+ """
+ if os.name != "nt":
+ return False
+ try:
+ import ctypes # noqa: PLC0415
+
+ set_ctx = ctypes.windll.user32.SetThreadDpiAwarenessContext
+ set_ctx.restype = ctypes.c_void_p
+ set_ctx.argtypes = [ctypes.c_void_p]
+ # DPI_AWARENESS_CONTEXT_UNAWARE == (DPI_AWARENESS_CONTEXT)-1
+ prev = set_ctx(ctypes.c_void_p(-1))
+ if prev is None:
+ logger.debug("SetThreadDpiAwarenessContext(-1) returned NULL")
+ return False
+ return True
+ except (OSError, AttributeError):
+ logger.debug("SetThreadDpiAwarenessContext unavailable", exc_info=True)
+ return False
diff --git a/jarvis/ui/jarvisbar/overlay.py b/jarvis/ui/jarvisbar/overlay.py
index f0c0510752..24191cebd3 100644
--- a/jarvis/ui/jarvisbar/overlay.py
+++ b/jarvis/ui/jarvisbar/overlay.py
@@ -235,20 +235,33 @@ class JarvisBarOverlay:
from PIL import ImageTk # noqa: F401 — fail fast here if Pillow missing
- # Per-monitor DPI awareness MUST be set before THIS surface's Tk root is
- # created — exactly like the orb (ui/orb/overlay.py). Without it, on a
- # scaled display (e.g. 150%) the window's geometry coordinate space and
- # the pointer-event space (``event.x_root``) drift apart, so dragging the
- # bar makes it run AWAY from the cursor toward the bottom-right and become
- # uncontrollable (the HiDPI "drag-teleport"). The bar boots in its own
- # thread, often before any other code has set process DPI awareness, so
- # it must assert it here itself. ``ensure_dpi_awareness`` is idempotent
- # and a no-op off Windows, so this is safe even when the process is
- # already aware.
+ # DPI strategy (two steps, order matters — both no-ops off Windows):
+ #
+ # 1. ``ensure_dpi_awareness()`` — the PROCESS must be DPI-aware before
+ # the per-window pin below can hold. Also fixes the HiDPI
+ # "drag-teleport" (geometry vs pointer-event space drift) when the
+ # bar wins the boot race.
+ # 2. ``pin_thread_dpi_unaware()`` — pin THIS thread (the bar's
+ # dedicated Tk mainloop thread) to the UNAWARE context so the Tk
+ # root created below carries it per-window: Windows scales the bar
+ # to the monitor's factor (its familiar size on a 150 % display)
+ # and keeps its coordinate space virtualised/consistent. Without
+ # the pin the window follows the PROCESS context, and pywebview's
+ # ``webview.start()`` flips that at runtime
+ # (``user32.SetProcessDPIAware()``) — a boot race that shrank the
+ # bar to ~2/3 raw pixels, jumped its position, and desynced
+ # dragging (recurring bug, GIF forensic 2026-07-02). With the pin
+ # the bar looks identical to its historical (unaware-built) self
+ # and is immune to any later process-level flip.
try:
- from jarvis.core.win32_dpi import ensure_dpi_awareness
+ from jarvis.core.win32_dpi import (
+ ensure_dpi_awareness,
+ pin_thread_dpi_unaware,
+ )
ensure_dpi_awareness()
+ if pin_thread_dpi_unaware():
+ log.debug("jarvisbar Tk thread pinned DPI-UNAWARE (per-window)")
except Exception: # noqa: BLE001 — never block the bar on a DPI hiccup
log.debug("jarvisbar DPI-awareness setup skipped", exc_info=True)
diff --git a/tests/unit/ui/jarvisbar/test_dpi_awareness.py b/tests/unit/ui/jarvisbar/test_dpi_awareness.py
index fe5525969a..c85abb3e14 100644
--- a/tests/unit/ui/jarvisbar/test_dpi_awareness.py
+++ b/tests/unit/ui/jarvisbar/test_dpi_awareness.py
@@ -41,13 +41,18 @@ class _FakeCanvas:
def bind(self, *a, **k) -> None: ...
-def test_start_sets_dpi_awareness_before_creating_tk_root(monkeypatch) -> None:
+def _run_start_with_probes(monkeypatch) -> list[str]:
+ """Run ``start()`` against the fake Tk, recording the DPI-call order."""
order: list[str] = []
monkeypatch.setattr(
"jarvis.core.win32_dpi.ensure_dpi_awareness",
lambda: order.append("dpi"),
)
+ monkeypatch.setattr(
+ "jarvis.core.win32_dpi.pin_thread_dpi_unaware",
+ lambda: order.append("pin") or True,
+ )
def _fake_tk(*_a, **_k) -> _FakeTkRoot:
order.append("tk")
@@ -64,6 +69,11 @@ def test_start_sets_dpi_awareness_before_creating_tk_root(monkeypatch) -> None:
monkeypatch.setattr(bar, "_schedule_frame_watchdog", lambda: None)
bar.start()
+ return order
+
+
+def test_start_sets_dpi_awareness_before_creating_tk_root(monkeypatch) -> None:
+ order = _run_start_with_probes(monkeypatch)
assert "dpi" in order, "start() must set DPI awareness"
assert "tk" in order, "start() must create the Tk root"
@@ -71,3 +81,43 @@ def test_start_sets_dpi_awareness_before_creating_tk_root(monkeypatch) -> None:
"DPI awareness must be set BEFORE the Tk root is created "
"(HiDPI drag-teleport fix)"
)
+
+
+def test_start_pins_thread_unaware_after_awareness_before_tk_root(monkeypatch) -> None:
+ """Regression (bar shrinks to ~2/3 / jumps / drag-offset, recurring): the
+ bar window used to follow the PROCESS DPI context, so it lost its 150 %
+ upscaling whenever pywebview's ``webview.start()`` flipped the process
+ awareness at runtime — a boot race that shrank the bar to raw pixels and
+ desynced the drag coordinates. The fix pins the bar's Tk thread (and thus
+ its window, per-window) to the UNAWARE context so Windows scales it
+ permanently and later process flips cannot touch it. Order matters: the
+ pin only holds when the process is already aware (ensure first), and it
+ must happen BEFORE the Tk root exists."""
+ order = _run_start_with_probes(monkeypatch)
+
+ assert "pin" in order, "start() must pin the bar thread DPI-UNAWARE"
+ assert order.index("dpi") < order.index("pin") < order.index("tk"), (
+ "expected ensure_dpi_awareness -> pin_thread_dpi_unaware -> tk.Tk(); "
+ f"got {order}"
+ )
+
+
+def test_pin_thread_dpi_unaware_is_safe_everywhere() -> None:
+ """The pin helper never raises: True (pinned) on Windows with the modern
+ API, False on other platforms / old Windows. Run in a throwaway thread so
+ the test runner's own thread context is never mutated."""
+ import os
+ import threading
+
+ from jarvis.core.win32_dpi import pin_thread_dpi_unaware
+
+ result: list[bool] = []
+ t = threading.Thread(target=lambda: result.append(pin_thread_dpi_unaware()))
+ t.start()
+ t.join(timeout=5)
+
+ assert result, "pin helper must return, not hang"
+ if os.name != "nt":
+ assert result[0] is False # graceful no-op off Windows
+ else:
+ assert isinstance(result[0], bool)