Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions tauri/src-tauri/src/hud.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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();
}
Expand Down
31 changes: 28 additions & 3 deletions tauri/src/Hud.test.tsx
Original file line number Diff line number Diff line change
@@ -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<typeof import("./lib/hud")>(), nativeHud: false, hudCall: api.call }))
vi.mock("./components/account-avatar", () => ({ AccountAvatar: () => <span data-testid="plush-avatar" /> }))
vi.mock("./components/account-avatar", () => ({
AccountAvatar: () => {
api.avatarRendered()
return <span data-testid="plush-avatar" />
},
}))
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(() => {
Expand All @@ -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(<Hud />)
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(<Hud />)
const avatar = await screen.findByRole("button", { name: "Fixture: Open" })
Expand Down
12 changes: 10 additions & 2 deletions tauri/src/Hud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,25 @@ export default function Hud() {
const popupTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
const hoverTimer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined)
const shell = useRef<HTMLElement>(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)
Expand All @@ -66,6 +71,9 @@ export default function Hud() {
try {
const next = await hudCall<HudView>("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)
Expand Down
Loading