openhop

srcSource code

Source code

Every file of openhop's latest version, exactly as published. Read anything, or download it all as a zip.

Commit
c8cfb4c
Date
16 Nov 2026
Files
62 files
Download the source (zip)

zip · 758 KB · SHA-256 6ce65b028777bf92…

openhop_tray.py

741 lines · 30 KB · Python

"""openhop tray icon, app settings, start-with-Windows and notifications.
- Tray icon (pystray, own Win32 message loop in a daemon thread) with a coloured status dot
(green = working, amber = warning, red = needs attention) and a menu:
Open openhop / Get a new IP / Fix everything / Quit openhop.
- Notifications: our own branded popup (src/ui/notify.html, bottom-right, never takes focus);
falls back to the native Windows notification when the popup can't be shown.
- Settings live in %LOCALAPPDATA%\\openhop\\app_settings.json.
"""
from __future__ import annotations
import ctypes
import json
import sys
import threading
import time
from pathlib import Path
from typing import Any, Callable
from ouc_common import DEST_DIR, append_log
# Own user32 instance so argtypes set elsewhere (e.g. GetWindowRect in openhop_app) can't break our calls.
_U32 = ctypes.WinDLL("user32") if sys.platform == "win32" else None
SETTINGS_FILE = DEST_DIR / "app_settings.json"
TRAY_LOG = DEST_DIR / "logs" / "tray.log"
RUN_KEY = r"Software\Microsoft\Windows\CurrentVersion\Run"
RUN_VALUE = "openhop"
DEFAULT_SETTINGS: dict[str, Any] = {
"close_to_tray": True, # X hides the window to the tray
"start_with_windows": True, # tray starts quietly at login
"notify_problems": True,
"notify_hops": True,
"notify_updates": True,
"desktop_brand": True, # openhop.cfd wordmark on OpenCode Desktop's start screen
"hide_limit_banner": True, # hide OpenCode Desktop's "Free limit reached" card (it replaces the chat box)
"auto_update": True, # download new versions from openhop.cfd and install them on restart
"tray_tip_shown": False, # one-time "still running in the tray" tip
"autostart_configured": False, # first-run default applied
}
POLL_SECONDS = 15.0
PROBLEM_REPEAT_SECONDS = 600.0 # same problem notified at most every 10 min
HOP_REPEAT_SECONDS = 45.0
_DOT = {"ok": (46, 204, 113), "warn": (242, 179, 24), "busy": (242, 179, 24), "bad": (231, 76, 60)}
_TOOLTIP = {"ok": "openhop: everything is working", "warn": "openhop: working, with a warning",
"bad": "openhop: needs attention", "unknown": "openhop"}
# ---------------------------------------------------------------- settings
_settings_lock = threading.Lock()
def load_settings() -> dict[str, Any]:
data = dict(DEFAULT_SETTINGS)
try:
raw = json.loads(SETTINGS_FILE.read_text(encoding="utf-8"))
if isinstance(raw, dict):
data.update({k: v for k, v in raw.items() if k in DEFAULT_SETTINGS})
except Exception:
pass
return data
def save_setting(key: str, value: Any) -> dict[str, Any]:
if key not in DEFAULT_SETTINGS:
raise KeyError(key)
with _settings_lock:
data = load_settings()
data[key] = type(DEFAULT_SETTINGS[key])(value)
try:
SETTINGS_FILE.parent.mkdir(parents=True, exist_ok=True)
SETTINGS_FILE.write_text(json.dumps(data, indent=2), encoding="utf-8")
except Exception as exc:
append_log(TRAY_LOG, f"could not save settings: {exc}")
return data
# ---------------------------------------------------------------- start with Windows
def _autostart_command() -> str:
if getattr(sys, "frozen", False):
return f'"{sys.executable}" --tray'
exe = Path(sys.executable)
pyw = exe.with_name("pythonw.exe")
script = Path(__file__).resolve().parent / "openhop_app.py"
return f'"{pyw if pyw.is_file() else exe}" "{script}" --tray'
def autostart_enabled() -> bool:
try:
import winreg
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY) as k:
winreg.QueryValueEx(k, RUN_VALUE)
return True
except Exception:
return False
def set_autostart(enable: bool) -> bool:
try:
import winreg
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, RUN_KEY, 0, winreg.KEY_SET_VALUE) as k:
if enable:
winreg.SetValueEx(k, RUN_VALUE, 0, winreg.REG_SZ, _autostart_command())
else:
try:
winreg.DeleteValue(k, RUN_VALUE)
except FileNotFoundError:
pass
save_setting("start_with_windows", enable)
return True
except Exception as exc:
append_log(TRAY_LOG, f"autostart change failed: {exc}")
return False
def apply_first_run_defaults() -> None:
"""Apply the default 'start with Windows' choice once; afterwards only the user changes it.
Also refresh the command if the app moved (e.g. a new exe in another folder)."""
s = load_settings()
if not s["autostart_configured"]:
set_autostart(bool(s["start_with_windows"]))
save_setting("autostart_configured", True)
elif s["start_with_windows"] and autostart_enabled():
set_autostart(True)
# ---------------------------------------------------------------- tray
class Tray:
"""Tray icon + background status watcher. All callbacks run on the tray thread."""
def __init__(self, api: Any, icon_path: Path, on_open: Callable[[], None], on_quit: Callable[[], None]):
self.api = api
self.icon_path = icon_path
self.on_open = on_open
self.on_quit = on_quit
self.icon = None
self.status = "unknown"
self._base = None
self._stop = threading.Event()
self._last_notified: dict[str, float] = {}
self._bad_streak = 0
self._last_hops: int | None = None
self.notices = NoticePopups(self)
self.menu = TrayMenu(self)
# -- lifecycle
def start(self) -> bool:
try:
import pystray
from PIL import Image
self._base = Image.open(self.icon_path).convert("RGBA").resize((64, 64), Image.LANCZOS) \
if self.icon_path.is_file() else Image.new("RGBA", (64, 64), (242, 179, 24, 255))
menu = pystray.Menu(
pystray.MenuItem("Open openhop", lambda: self.on_open(), default=True),
pystray.MenuItem("Get a new IP", lambda: self._run_bg(self._hop)),
pystray.MenuItem("Fix everything", lambda: self._run_bg(self._fix_all)),
pystray.Menu.SEPARATOR,
pystray.MenuItem("Quit openhop", lambda: self.on_quit()),
)
self.icon = _make_icon(pystray, self._image("unknown"), menu, self._on_right_click)
threading.Thread(target=self.icon.run, name="openhop-tray", daemon=True).start()
threading.Thread(target=self._watch, name="openhop-tray-watch", daemon=True).start()
return True
except Exception as exc:
append_log(TRAY_LOG, f"tray unavailable: {exc}")
self.icon = None
return False
def _on_right_click(self, x: int, y: int) -> bool:
"""Return True if our branded menu handled the click (otherwise the native menu is shown)."""
return self.menu.show(x, y)
def menu_action(self, action: str) -> None:
if action == "open":
self.on_open()
elif action == "hop":
self._run_bg(self._hop)
elif action in ("fix", "fix_all"):
self._run_bg(self._fix_all)
elif action == "quit":
self.on_quit()
def menu_state(self) -> dict:
state = {}
try:
state = self.api.get_state_cached(max_age=60.0)
except Exception:
pass
health = state.get("health") or {}
stats = state.get("stats") or {}
return {"status": health.get("status", self.status), "title": health.get("title", ""),
"sub": health.get("sub", ""), "hops": int(stats.get("hops", 0) or 0),
"last_hop": int(stats.get("last_hop", 0) or 0)}
@property
def alive(self) -> bool:
return self.icon is not None and not self._stop.is_set()
def stop(self) -> None:
self._stop.set()
try:
if self.icon:
self.icon.stop()
except Exception:
pass
# -- icon
def _image(self, status: str):
from PIL import ImageDraw
img = self._base.copy()
color = _DOT.get(status)
if color:
d = ImageDraw.Draw(img)
d.ellipse((38, 38, 63, 63), fill=(31, 33, 37, 255))
d.ellipse((42, 42, 59, 59), fill=color + (255,))
return img
def set_status(self, status: str, title: str = "") -> None:
if not self.icon:
return
if status != self.status:
self.status = status
try:
self.icon.icon = self._image(status)
except Exception:
pass
try:
tip = f"openhop: {title}" if title else _TOOLTIP.get(status, "openhop")
self.icon.title = tip[:120]
except Exception:
pass
# -- actions
@staticmethod
def _run_bg(fn: Callable[[], None]) -> None:
threading.Thread(target=fn, daemon=True).start()
def _hop(self) -> None:
self.notify("info", "Getting a new IP…", "This takes a few seconds.", force=True)
try:
res = self.api.hop_now()
except Exception as exc:
res = {"ok": False, "message": str(exc)}
ok = bool(res.get("ok"))
self.notify("hop" if ok else "problem", "Got a new IP" if ok else "Couldn't get a new IP",
_one_line(res.get("message", "")), force=True)
self._last_hops = None # don't double-notify this hop from the watcher
def _fix_all(self) -> None:
self.notify("info", "Fixing everything…", "Checking each part of openhop.", force=True)
try:
res = self.api.fix_everything()
except Exception as exc:
res = {"ok": False, "message": str(exc)}
self.notify("info" if res.get("ok") else "problem",
"All fixed" if res.get("ok") else "Some problems are left",
_one_line(res.get("message", "")), action_label="" if res.get("ok") else "Open", force=True)
# -- watcher
def _watch(self) -> None:
time.sleep(4.0)
while not self._stop.is_set():
try:
self._tick()
except Exception as exc:
append_log(TRAY_LOG, f"watch error: {exc}")
self._stop.wait(POLL_SECONDS)
def _tick(self) -> None:
state = self.api.get_state_cached(max_age=POLL_SECONDS - 2)
health = state.get("health") or {}
status = health.get("status", "unknown")
self.set_status(status, health.get("title", ""))
settings = load_settings()
# problems: only after two bad polls in a row (ignores blips during a rotation)
self._bad_streak = self._bad_streak + 1 if status == "bad" else 0
if self._bad_streak == 2 and settings["notify_problems"]:
issues = [i for i in health.get("issues", []) if i.get("level") == "bad"]
first = issues[0] if issues else {"title": health.get("title", "openhop needs attention"), "detail": ""}
self.notify("problem", first.get("title", ""), _one_line(first.get("detail", "")),
action_label=first.get("fix_label") or "Open", action=first.get("fix") or "open",
key="problem:" + first.get("title", ""))
# automatic rotations done by the guard
hops = int((state.get("stats") or {}).get("hops", 0) or 0)
if self._last_hops is not None and hops > self._last_hops and settings["notify_hops"]:
if health.get("hunting"):
# Still looking: one honest notice instead of "you can keep going" after every try.
self.notify("hop", "Looking for an IP with free messages",
"The last addresses were already used up. openhop keeps trying; "
"the chat continues by itself in a few minutes.", key="hunt")
else:
self.notify("hop", "Limit hit: switched to a new IP",
"OpenCode can keep going. openhop sent 'continue' for you.", key="hop")
self._last_hops = hops
# -- notifications
def notify(self, kind: str, title: str, body: str, action_label: str = "", action: str = "",
key: str | None = None, force: bool = False, body_action: str = "open") -> None:
now = time.time()
key = key or f"{kind}:{title}"
if not force:
gap = HOP_REPEAT_SECONDS if kind == "hop" else PROBLEM_REPEAT_SECONDS
if now - self._last_notified.get(key, 0) < gap:
return
self._last_notified[key] = now
if self.notices.show(kind, title, body, action_label, action, body_action):
return
try: # native fallback
if self.icon:
self.icon.notify(body or " ", title)
except Exception as exc:
append_log(TRAY_LOG, f"native notification failed: {exc}")
def handle_action(self, action: str) -> None:
if not action or action == "open":
self.on_open()
return
if action == "show_update":
try:
self.api.show_update()
except Exception as exc:
append_log(TRAY_LOG, f"show update failed: {exc}")
self.on_open()
return
if action == "update":
try:
res = self.api.apply_update()
if not res.get("ok"):
self.notify("problem", "Couldn't update", _one_line(res.get("message", "")), force=True)
except Exception as exc:
append_log(TRAY_LOG, f"update action failed: {exc}")
return
try:
res = self.api.fix_issue(action)
ok = bool(res.get("ok"))
self.notify("info" if ok else "problem", "Fixed" if ok else "Couldn't fix it",
_one_line(res.get("message", "")), force=True)
except Exception as exc:
append_log(TRAY_LOG, f"notice action failed: {exc}")
def _one_line(text: str, limit: int = 140) -> str:
text = " ".join(str(text or "").split())
return text if len(text) <= limit else text[: limit - 1] + "…"
# ---------------------------------------------------------------- custom popups
class _NoticeApi:
"""JS API for src/ui/notify.html (hooks: notice_action, notice_close)."""
def __init__(self, popups: "NoticePopups", nid: int):
self._popups = popups
self._nid = nid
def notice_action(self, _id: Any = None) -> None:
self._popups._on_action(self._nid)
def notice_close(self) -> None:
self._popups._close(self._nid)
def notice_body(self, _id: Any = None) -> None:
"""The card itself was clicked (not a button)."""
self._popups._on_action(self._nid, body=True)
class NoticePopups:
"""Branded notification cards (bottom-right). Shown without taking focus or a taskbar button."""
WIDTH, HEIGHT, MARGIN, LIFETIME = 360, 96, 16, 7.0
def __init__(self, tray: Tray):
self.tray = tray
self._seq = 0
self._open: dict[int, tuple[Any, str]] = {}
self._lock = threading.Lock()
@staticmethod
def html_path() -> Path | None:
return _ui_file("notify.html")
def show(self, kind: str, title: str, body: str, action_label: str, action: str,
body_action: str = "open") -> bool:
path = self.html_path()
if path is None:
return False
try:
import webview
if not webview.windows: # GUI loop not running yet
return False
with self._lock:
for nid in list(self._open):
self._close(nid)
self._seq += 1
nid = self._seq
left, top, right, bottom = _work_area()
scale = _dpi_scale()
w, h = int(self.WIDTH * scale), int(self.HEIGHT * scale)
x, y = right - w - int(self.MARGIN * scale), bottom - h - int(self.MARGIN * scale)
title_tag = f"openhop notice {nid}"
win = webview.create_window(
title_tag, html=path.read_text(encoding="utf-8"), js_api=_NoticeApi(self, nid),
width=self.WIDTH, height=self.HEIGHT, x=int(x / scale), y=int(y / scale),
frameless=True, easy_drag=False, resizable=False, on_top=True, focus=False,
shadow=False, transparent=False, background_color="#1f2125",
)
payload = json.dumps({"kind": kind, "title": title, "body": body, "action_label": action_label})
def _shown() -> None:
_make_noactivate_toolwindow(title_tag, (x, y, w, h))
try:
win.evaluate_js(f"window.showNotice && window.showNotice({payload})")
except Exception:
pass
win.events.loaded += _shown
with self._lock:
self._open[nid] = (win, action, body_action or "open")
threading.Timer(self.LIFETIME, lambda: self._close(nid)).start()
return True
except Exception as exc:
append_log(TRAY_LOG, f"custom notice failed, using native: {exc}")
return False
def _close(self, nid: int) -> None:
with self._lock:
item = self._open.pop(nid, None)
if item:
try:
item[0].destroy()
except Exception:
pass
def _on_action(self, nid: int, body: bool = False) -> None:
with self._lock:
item = self._open.get(nid)
action = (item[2] if body else item[1]) if item else ""
self._close(nid)
threading.Thread(target=self.tray.handle_action, args=(action,), daemon=True).start()
# ---------------------------------------------------------------- Win32 helpers
class _POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
class _RECT(ctypes.Structure):
_fields_ = [("left", ctypes.c_long), ("top", ctypes.c_long), ("right", ctypes.c_long), ("bottom", ctypes.c_long)]
def _work_area() -> tuple[int, int, int, int]:
r = _RECT()
_U32.SystemParametersInfoW(0x0030, 0, ctypes.byref(r), 0) # SPI_GETWORKAREA (primary)
return r.left, r.top, r.right, r.bottom
def _dpi_scale() -> float:
try:
return _U32.GetDpiForSystem() / 96.0
except Exception:
return 1.0
def _make_noactivate_toolwindow(title: str, rect: tuple[int, int, int, int] | None = None) -> None:
"""No taskbar button, never steals focus, stays on top."""
user32 = _U32
for _ in range(20):
hwnd = user32.FindWindowW(None, title)
if hwnd:
break
time.sleep(0.05)
else:
return
GWL_EXSTYLE, WS_EX_TOOLWINDOW, WS_EX_NOACTIVATE, WS_EX_APPWINDOW = -20, 0x80, 0x08000000, 0x40000
get = getattr(user32, "GetWindowLongPtrW", user32.GetWindowLongW)
put = getattr(user32, "SetWindowLongPtrW", user32.SetWindowLongW)
get.restype = ctypes.c_ssize_t
put.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_ssize_t]
style = get(ctypes.c_void_p(hwnd), GWL_EXSTYLE)
put(ctypes.c_void_p(hwnd), GWL_EXSTYLE, (style | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE) & ~WS_EX_APPWINDOW)
_round_corners(hwnd)
# HWND_TOPMOST, SWP_NOMOVE|SWP_NOSIZE|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_SHOWWINDOW
if rect: # physical pixels, so DPI scaling can't misplace it
x, y, w, h = rect
user32.SetWindowPos(ctypes.c_void_p(hwnd), ctypes.c_void_p(-1), x, y, w, h, 0x0010 | 0x0020 | 0x0040)
else:
user32.SetWindowPos(ctypes.c_void_p(hwnd), ctypes.c_void_p(-1), 0, 0, 0, 0, 0x0001 | 0x0002 | 0x0010 | 0x0020 | 0x0040)
# ---------------------------------------------------------------- branded tray menu
class _MenuApi:
"""JS API for src/ui/traymenu.html: menu_action(id), menu_close(), menu_state()."""
def __init__(self, menu: "TrayMenu"):
self._menu = menu
def menu_action(self, action: str) -> None:
self._menu.hide()
threading.Thread(target=self._menu.tray.menu_action, args=(str(action),), daemon=True).start()
def menu_close(self) -> None:
# The page reports "blur" when the hidden-icons flyout or the taskbar takes focus back right after the
# click; ignore that first moment (the Python watcher closes the menu on real clicks elsewhere).
if time.time() - self._menu._shown_at < 0.6:
return
self._menu.hide()
def menu_state(self) -> dict:
return self._menu.tray.menu_state()
class TrayMenu:
"""Our own right-click menu: a small frameless window next to the tray icon, closed on blur."""
WIDTH, HEIGHT = 280, 312 # CSS px (the page should fit this box)
TITLE = "openhop menu"
def __init__(self, tray: Tray):
self.tray = tray
self.win = None
self._ready = threading.Event()
self._lock = threading.Lock()
self._visible = False
self._hidden_at = 0.0
self._shown_at = 0.0
def show(self, x: int, y: int) -> bool:
path = _ui_file("traymenu.html")
if path is None:
return False
try:
import webview
except Exception:
return False
if self.win is None and not webview.windows: # GUI loop not running
return False
if self._visible: # right-click again = close
self.hide()
return True
if time.time() - self._hidden_at < 0.3: # this click is what just closed it
return True
threading.Thread(target=self._show, args=(path, x, y), daemon=True).start()
return True
def _show(self, path: Path, x: int, y: int) -> None:
with self._lock:
try:
import webview
if self.win is None:
self.win = webview.create_window(
self.TITLE, html=path.read_text(encoding="utf-8"), js_api=_MenuApi(self),
width=self.WIDTH, height=self.HEIGHT, frameless=True, easy_drag=False,
resizable=False, on_top=True, shadow=False, transparent=False,
background_color="#1f2125", hidden=True,
)
self.win.events.loaded += self._ready.set
if not self._ready.wait(8.0):
raise RuntimeError("menu page did not load")
hwnd = _find_window(self.TITLE)
if not hwnd:
raise RuntimeError("menu window not found")
_set_ex_style(hwnd, add=0x80, remove=0x40000) # tool window: no taskbar button
_round_corners(hwnd)
scale = _dpi_scale()
w, h = int(self.WIDTH * scale), int(self.HEIGHT * scale)
left, top, right, bottom = _work_area()
px = max(left, min(x - w, right - w))
py = max(top, min(y - h, bottom - h))
state = json.dumps(self.tray.menu_state())
self.win.evaluate_js(f"window.renderMenu && window.renderMenu({state})")
user32 = _U32
# Place it while still hidden, show it, then keep checking for a moment: the first time a
# WinForms window is shown, Windows applies its own default spot (top-left) and size AFTER
# our move, which put the menu in the corner and cut off its last items.
_place_window(hwnd, px, py, w, h)
self.win.show()
for _ in range(40):
if user32.IsWindowVisible(ctypes.c_void_p(hwnd)):
break
time.sleep(0.025)
_place_window(hwnd, px, py, w, h, show=True)
user32.SetForegroundWindow(ctypes.c_void_p(hwnd))
self._shown_at = time.time()
self._visible = True
for _ in range(10):
time.sleep(0.05)
if not self._visible:
break
if _window_rect(hwnd) != (px, py, w, h):
_place_window(hwnd, px, py, w, h, show=True)
except Exception as exc:
append_log(TRAY_LOG, f"branded menu failed: {exc}")
return
threading.Thread(target=self._watch_dismiss, args=(hwnd,), daemon=True).start()
def _watch_dismiss(self, hwnd: int) -> None:
"""Close the menu when the user clicks somewhere else or switches to another window."""
user32 = _U32
user32.GetForegroundWindow.restype = ctypes.c_void_p
user32.GetAncestor.restype = ctypes.c_void_p
user32.GetAncestor.argtypes = [ctypes.c_void_p, ctypes.c_uint]
had_focus = False
start = time.time()
while self._visible:
time.sleep(0.08)
fg = user32.GetForegroundWindow() or 0
root = (user32.GetAncestor(ctypes.c_void_p(fg), 2) or 0) if fg else 0 # GA_ROOT
mine = hwnd in (fg, root)
if mine:
had_focus = True
elif had_focus:
self.hide()
break
elif time.time() - start > 0.6:
# never got focus (e.g. Windows refused the switch): close on any click outside the menu
pressed = any(user32.GetAsyncKeyState(vk) & 0x8000 for vk in (0x01, 0x02))
if pressed:
pt = _POINT()
user32.GetCursorPos(ctypes.byref(pt))
lx, ly, lw, lh = _window_rect(hwnd)
if not (lx <= pt.x < lx + lw and ly <= pt.y < ly + lh):
self.hide()
break
def hide(self) -> None:
if not self._visible:
return
self._visible = False
self._hidden_at = time.time()
try:
self.win.hide()
except Exception:
pass
def _make_icon(pystray: Any, image: Any, menu: Any, on_right_click: Callable[[int, int], bool]) -> Any:
"""pystray icon whose right-click opens our branded menu (native menu only as a fallback)."""
try:
from ctypes import wintypes
from pystray import _win32
from pystray._util import win32 as pwin32
class _OpenhopIcon(_win32.Icon):
def _on_notify(self, wparam, lparam): # noqa: N802 (pystray naming)
if lparam == pwin32.WM_RBUTTONUP:
pt = wintypes.POINT()
_U32.GetCursorPos(ctypes.byref(pt))
try:
if on_right_click(pt.x, pt.y):
return None
except Exception as exc:
append_log(TRAY_LOG, f"right-click handler failed: {exc}")
return super()._on_notify(wparam, lparam)
return _OpenhopIcon("openhop", image, _TOOLTIP["unknown"], menu)
except Exception as exc:
append_log(TRAY_LOG, f"custom icon class unavailable, using default: {exc}")
return pystray.Icon("openhop", image, _TOOLTIP["unknown"], menu)
# ---------------------------------------------------------------- app identity (notification header)
APP_ID = "bernatlol.openhop.controller"
def register_app_identity(icon_path: Path) -> None:
"""Make Windows show 'openhop' + our logo (not 'Python') on notifications and the taskbar."""
try:
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(APP_ID)
except Exception:
pass
try:
import winreg
from PIL import Image
png = DEST_DIR / "openhop.png"
if icon_path.is_file() and (not png.is_file() or png.stat().st_mtime < icon_path.stat().st_mtime):
png.parent.mkdir(parents=True, exist_ok=True)
Image.open(icon_path).convert("RGBA").resize((256, 256), Image.LANCZOS).save(png)
with winreg.CreateKey(winreg.HKEY_CURRENT_USER, rf"Software\Classes\AppUserModelId\{APP_ID}") as k:
winreg.SetValueEx(k, "DisplayName", 0, winreg.REG_SZ, "openhop")
if png.is_file():
winreg.SetValueEx(k, "IconUri", 0, winreg.REG_SZ, str(png))
winreg.SetValueEx(k, "IconBackgroundColor", 0, winreg.REG_SZ, "FF1F2125")
except Exception as exc:
append_log(TRAY_LOG, f"could not register app identity: {exc}")
# ---------------------------------------------------------------- shared helpers
def _ui_file(name: str) -> Path | None:
base = Path(getattr(sys, "_MEIPASS", Path(__file__).resolve().parent.parent))
for c in (base / "src" / "ui" / name, base / "ui" / name, Path(__file__).resolve().parent / "ui" / name):
if c.is_file():
return c
return None
def _find_window(title: str, tries: int = 20) -> int:
for _ in range(tries):
hwnd = _U32.FindWindowW(None, title)
if hwnd:
return hwnd
time.sleep(0.05)
return 0
def _window_rect(hwnd: int) -> tuple[int, int, int, int]:
r = _RECT()
_U32.GetWindowRect(ctypes.c_void_p(hwnd), ctypes.byref(r))
return r.left, r.top, r.right - r.left, r.bottom - r.top
def _place_window(hwnd: int, x: int, y: int, w: int, h: int, show: bool = False) -> None:
"""Topmost at x,y with size w,h (physical pixels) without activating it; optionally make it visible."""
flags = 0x0010 | (0x0040 if show else 0) # SWP_NOACTIVATE (| SWP_SHOWWINDOW)
_U32.SetWindowPos(ctypes.c_void_p(hwnd), ctypes.c_void_p(-1), x, y, w, h, flags)
def _round_corners(hwnd: int) -> None:
"""Windows 11: native rounded corners + a subtle border (does nothing on Windows 10)."""
try:
dwm = ctypes.WinDLL("dwmapi")
pref = ctypes.c_int(2) # DWMWA_WINDOW_CORNER_PREFERENCE = DWMWCP_ROUND
dwm.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 33, ctypes.byref(pref), 4)
border = ctypes.c_uint(0x00463F3A) # DWMWA_BORDER_COLOR, COLORREF 0x00BBGGRR = #3a3f46
dwm.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), 34, ctypes.byref(border), 4)
except Exception:
pass
def _set_ex_style(hwnd: int, add: int = 0, remove: int = 0) -> None:
user32 = _U32
get = getattr(user32, "GetWindowLongPtrW", user32.GetWindowLongW)
put = getattr(user32, "SetWindowLongPtrW", user32.SetWindowLongW)
get.restype = ctypes.c_ssize_t
put.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_ssize_t]
style = get(ctypes.c_void_p(hwnd), -20) # GWL_EXSTYLE
put(ctypes.c_void_p(hwnd), -20, (style | add) & ~remove)