From 9b88d7bf6293832679d9af3b9f98d437cc491646 Mon Sep 17 00:00:00 2001 From: wrestle-R Date: Wed, 25 Nov 2026 10:20:26 +0530 Subject: [PATCH] made orbit do less work between changes --- tauri/src-tauri/src/hud.rs | 12 ++++++++---- tauri/src/Hud.test.tsx | 31 ++++++++++++++++++++++++++++--- tauri/src/Hud.tsx | 12 ++++++++++-- 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/tauri/src-tauri/src/hud.rs b/tauri/src-tauri/src/hud.rs index f6936db..26fd61f 100644 --- a/tauri/src-tauri/src/hud.rs +++ b/tauri/src-tauri/src/hud.rs @@ -61,6 +61,13 @@ pub fn hud_snapshot(app: tauri::AppHandle, state: State<'_, HudState>) -> Result .filter(|snapshot| snapshot.client == "desktop"), ); snapshots.sort_by_key(|snapshot| (snapshot.start_time, snapshot.pid)); + let mut snapshots_by_profile = BTreeMap::<_, Vec<_>>::new(); + for snapshot in &snapshots { + snapshots_by_profile + .entry(snapshot.profile_id.as_str()) + .or_default() + .push(snapshot); + } let acknowledgments = state .acknowledgments .lock() @@ -94,10 +101,7 @@ pub fn hud_snapshot(app: tauri::AppHandle, state: State<'_, HudState>) -> Result }, ); } - for snapshot in snapshots - .iter() - .filter(|snapshot| snapshot.profile_id == id) - { + for snapshot in snapshots_by_profile.get(id.as_str()).into_iter().flatten() { if snapshot.limits.is_some() { limits = snapshot.limits.clone(); } diff --git a/tauri/src/Hud.test.tsx b/tauri/src/Hud.test.tsx index 2da3121..534b27e 100644 --- a/tauri/src/Hud.test.tsx +++ b/tauri/src/Hud.test.tsx @@ -1,11 +1,16 @@ -import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react" +import { act, cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react" import { afterEach, beforeEach, expect, it, vi } from "vitest" import Hud from "./Hud" import { defaultHudSettings, sessionSlots, type HudSession, type HudView } from "./lib/hud" -const api = vi.hoisted(() => ({ call: vi.fn() })) +const api = vi.hoisted(() => ({ call: vi.fn(), avatarRendered: vi.fn() })) vi.mock("./lib/hud", async importOriginal => ({ ...await importOriginal(), nativeHud: false, hudCall: api.call })) -vi.mock("./components/account-avatar", () => ({ AccountAvatar: () => })) +vi.mock("./components/account-avatar", () => ({ + AccountAvatar: () => { + api.avatarRendered() + return + }, +})) const session = (id: string, needsAttention = false): HudSession => ({ id, title: `Session ${id}`, cwd: "/tmp", status: "idle", attention: needsAttention ? "mayNeedYou" : null, needsAttention, eventKey: `account/${id}/1`, epoch: 1, tokens: null }) let view: HudView beforeEach(() => { @@ -14,6 +19,26 @@ beforeEach(() => { api.call.mockReset().mockImplementation((method: string) => Promise.resolve(method === "hud_snapshot" ? view : undefined)) }) afterEach(cleanup) +it("keeps unchanged refreshes idle and still displays new attention", async () => { + vi.useFakeTimers() + try { + render() + await act(async () => {}) + expect(screen.getByRole("button", { name: "Fixture: Open" })).toBeVisible() + api.avatarRendered.mockClear() + + view = structuredClone(view) + await act(() => vi.advanceTimersByTimeAsync(1500)) + expect(api.call).toHaveBeenCalledTimes(3) + expect(api.avatarRendered).not.toHaveBeenCalled() + + view.accounts[0].sessions = [session("one", true)] + await act(() => vi.advanceTimersByTimeAsync(1500)) + expect(screen.getByRole("button", { name: "Open chat needing attention" })).toBeVisible() + } finally { + vi.useRealTimers() + } +}) it("hovers for quota and clicks to open the exact saved account/chat", async () => { render() const avatar = await screen.findByRole("button", { name: "Fixture: Open" }) diff --git a/tauri/src/Hud.tsx b/tauri/src/Hud.tsx index 4fbbdbf..97f4d1c 100644 --- a/tauri/src/Hud.tsx +++ b/tauri/src/Hud.tsx @@ -32,20 +32,25 @@ export default function Hud() { const popupTimer = useRef | undefined>(undefined) const hoverTimer = useRef | undefined>(undefined) const shell = useRef(null) + const snapshot = useRef("") + const inputRegions = useRef("") const dragging = useRef(false) const pointerDown = useRef(false) const moving = useRef(false) const endDrag = () => { pointerDown.current = false; if (dragging.current) { dragging.current = false; void hudCall("hud_drag", { phase: "end" }).catch(error => setError(String(error))) } } useLayoutEffect(() => { if (!nativeHud || !shell.current) return - let previous = "" const update = () => { const rectangles = [...(shell.current?.querySelectorAll(".hud-strip,.hud-panel,.hud-popup,.hud-error") ?? [])].map(element => { const { x, y, width, height } = element.getBoundingClientRect() return { x, y, width, height } }) const encoded = JSON.stringify(rectangles) - if (previous !== encoded) { previous = encoded; void hudCall("hud_set_input_regions", { rectangles }) } + if (inputRegions.current === encoded) return + inputRegions.current = encoded + void hudCall("hud_set_input_regions", { rectangles }).catch(() => { + if (inputRegions.current === encoded) inputRegions.current = "" + }) } const observer = new ResizeObserver(update) observer.observe(shell.current) @@ -66,6 +71,9 @@ export default function Hud() { try { const next = await hudCall("hud_snapshot") if (!active) return + const encoded = JSON.stringify(next) + if (snapshot.current === encoded) return + snapshot.current = encoded setView(next) const attention = next.accounts.flatMap(account => orderedSessions(account.sessions).filter(s => s.needsAttention && !seen.current.has(s.eventKey)).map(session => ({ account, session }))) for (const event of attention) seen.current.add(event.session.eventKey)