A tiny friend in your Mac's notch and on your iPhone that keeps an eye on your AI coding agents: Claude Code, Codex, Cursor, Gemini CLI, Antigravity and more. Approve from the notch or your Lock Screen.
Swift ai-agentsanthropicantigravityclaudeclaude-codecodexcursordynamic-islandgemini-cliiosiphonlinuxmacosmacos-appmenubar-appnotchopen-sourceswiftswiftuiwindows
Windows and Linux: the Coucou window stops animating when hidden (#792)
Claude5cccfc015f04
diff --git a/windows/src-tauri/src/lib.rs b/windows/src-tauri/src/lib.rsindex b4ea3cb8..a6ee57f7 100644--- a/windows/src-tauri/src/lib.rs+++ b/windows/src-tauri/src/lib.rs@@ -39,7 +39,7 @@ mod voice;mod webview_drop;use std::process::Command;-use std::sync::atomic::Ordering;+use std::sync::atomic::{AtomicBool, Ordering};use std::sync::{Arc, Mutex};use serde::Serialize;@@ -653,19 +653,44 @@ fn create_settings_window(app: &AppHandle) {.build(){Ok(win) => {- // Closing it must only hide it, or it could never be reopened.- let hidden = win.clone();- win.on_window_event(move |event| {- if let tauri::WindowEvent::CloseRequested { api, .. } = event {+ let this = win.clone();+ win.on_window_event(move |event| match event {+ // Closing it must only hide it, or it could never be reopened.+ tauri::WindowEvent::CloseRequested { api, .. } => {api.prevent_close();- let _ = hidden.hide();+ let _ = this.hide();+ set_hub_shown(&this, false);}+ // Minimised (a 0×0 resize on Windows) or back from it.+ tauri::WindowEvent::Resized(_) | tauri::WindowEvent::Focused(true) => {+ let on = this.is_visible().unwrap_or(false) && !this.is_minimized().unwrap_or(false);+ set_hub_shown(&this, on);+ }+ _ => {}});}Err(err) => log::line(format!("Coucou window failed: {err}")),}}+/// Whether the Coucou window is on screen (shown and not minimised). Its page+/// animates its canvases only then (hub/hero.ts): WebKitGTK can keep+/// `document.visibilityState` at "visible" for a hidden window, and its+/// animation loop then kept a core busy with the window closed (#792).+static HUB_SHOWN: AtomicBool = AtomicBool::new(false);++fn set_hub_shown(win: &tauri::WebviewWindow, shown: bool) {+ if HUB_SHOWN.swap(shown, Ordering::Relaxed) != shown {+ let _ = win.emit_to(HUB_LABEL, "hub-visible", shown);+ }+}++/// The page asks once at load, in case it missed the event.+#[tauri::command]+fn hub_shown() -> bool {+ HUB_SHOWN.load(Ordering::Relaxed)+}+pub fn show_settings_window(app: &AppHandle) {show_hub(app, Some("general"));}@@ -683,6 +708,7 @@ pub fn show_hub(app: &AppHandle, page: Option<&str>) {let _ = win.unminimize();let _ = win.show();let _ = win.set_focus();+ set_hub_shown(&win, true);}/// The argument the login item starts Coucou with.@@ -738,6 +764,7 @@ pub fn run() {focus_window,reposition,fit_zoom,+ hub_shown,list_monitors,open_url,open_in_vscode,diff --git a/windows/src/core/flow.ts b/windows/src/core/flow.tsindex 6c2cb6cb..8464e65a 100644--- a/windows/src/core/flow.ts+++ b/windows/src/core/flow.ts@@ -184,6 +184,8 @@ export type VoiceOutput = "system" | "elevenlabs" | "off";export const FlowBridge = {status: () => call<FlowStatus>("flow_status"),+ /** The Coucou window is on screen (shown, not minimised); `hub-visible` tells when it changes. */+ hubShown: () => call<boolean>("hub_shown"),setPrefs: (prefs: FlowPrefs) => call<void>("flow_set_prefs", { prefs }),history: () => call<FlowEntry[]>("flow_history"),deleteEntry: (id: string) => call<void>("flow_delete", { id }),diff --git a/windows/src/hub/frames.ts b/windows/src/hub/frames.tsnew file mode 100644index 00000000..1fe03279--- /dev/null+++ b/windows/src/hub/frames.ts@@ -0,0 +1,25 @@+// When the Coucou window's canvases are drawn (hub/hero.ts), kept apart from+// the DOM so it can be tested. The rule is the island's: nothing on screen,+// nothing drawn, and no frame asked for.++export interface Box {+ top: number;+ left: number;+ bottom: number;+ right: number;+ width: number;+ height: number;+}++/** Whether the loop should ask for another frame: something to draw, the+ * window shown (Rust says, see `hub-visible`) and the page visible (WebKitGTK+ * does not always say so for a hidden window, #792). */+export function shouldAnimate(live: number, pageVisibility: string, windowShown: boolean): boolean {+ return live > 0 && windowShown && pageVisibility === "visible";+}++/** Whether a canvas at `box` shows at least one pixel of a `width`×`height`+ * viewport: one scrolled out of the page, or not laid out, is not drawn. */+export function onScreen(box: Box, width: number, height: number): boolean {+ return box.width > 0 && box.height > 0 && box.bottom > 0 && box.right > 0 && box.top < height && box.left < width;+}diff --git a/windows/src/hub/hero.ts b/windows/src/hub/hero.tsindex ec1900a4..b0dce33f 100644--- a/windows/src/hub/hero.ts+++ b/windows/src/hub/hero.ts@@ -4,10 +4,12 @@// its floor — they hop, look around, wave, and react when poked. Also the// glowing cards and the Mochis of empty pages. Port of FlowHubMochis.swift.//-// One animation loop draws every live canvas; it stops when none is on screen-// or the window is hidden, so a hidden window costs nothing.+// One animation loop draws every live canvas; it stops when none is left or+// the window is hidden, so a hidden window costs nothing, and skips the ones+// scrolled out of sight.import { BotEngine, hexToRGB } from "../mochi/engine";+import { onScreen, shouldAnimate } from "./frames";import type { BotEmoteName, BotStateName } from "../core/layout";import type { Outfit } from "../mochi/wardrobe";@@ -23,6 +25,12 @@ const live = new Set<Animated>();let frame: number | null = null;let last = performance.now();const start = performance.now();+/** The Coucou window is on screen, as Rust last said (`setWindowShown`). */+let windowShown = true;++function running(): boolean {+ return shouldAnimate(live.size, document.visibilityState, windowShown);+}function loop(now: number) {frame = null;@@ -36,13 +44,13 @@ function loop(now: number) {continue;}a.shown = true;- a.draw(dt, t);+ if (onScreen(a.el.getBoundingClientRect(), window.innerWidth, window.innerHeight)) a.draw(dt, t);}- if (live.size > 0 && document.visibilityState === "visible") frame = requestAnimationFrame(loop);+ if (running()) frame = requestAnimationFrame(loop);}function wake() {- if (frame === null && live.size > 0 && document.visibilityState === "visible") {+ if (frame === null && running()) {last = performance.now();frame = requestAnimationFrame(loop);}@@ -50,6 +58,17 @@ function wake() {document.addEventListener("visibilitychange", wake);+/** The window was shown or hidden (the `hub-visible` event): hidden, the loop+ * stops at once; shown, it starts again. */+export function setWindowShown(shown: boolean) {+ windowShown = shown;+ if (!shown && frame !== null) {+ cancelAnimationFrame(frame);+ frame = null;+ }+ wake();+}+function add(a: Animated) {live.add(a);wake();@@ -68,9 +87,17 @@ function fit(canvas: HTMLCanvasElement): { w: number; h: number; ctx: CanvasRendreturn { w, h, ctx };}+/** "r,g,b" of each colour, read once: the particles ask every frame. */+const channels = new Map<string, string>();+function rgba(hex: string, alpha: number): string {- const [r, g, b] = hexToRGB(hex);- return `rgba(${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)},${Math.max(0, Math.min(1, alpha))})`;+ let rgb = channels.get(hex);+ if (rgb === undefined) {+ const [r, g, b] = hexToRGB(hex);+ rgb = `${Math.round(r * 255)},${Math.round(g * 255)},${Math.round(b * 255)}`;+ channels.set(hex, rgb);+ }+ return `rgba(${rgb},${Math.max(0, Math.min(1, alpha))})`;}// ── The backdrop ──────────────────────────────────────────────────────────────diff --git a/windows/src/hub/shell.ts b/windows/src/hub/shell.tsindex 6e1222ce..252a0f10 100644--- a/windows/src/hub/shell.ts+++ b/windows/src/hub/shell.ts@@ -8,7 +8,7 @@ import { h, clear } from "../views/dom";import { t } from "../i18n/i18n";import { onEvent } from "../core/bridge";import { FlowBridge, type FlowPrefs, type FlowStatus, type ModelsStatus } from "../core/flow";-import { hero, liveMochi } from "./hero";+import { hero, liveMochi, setWindowShown } from "./hero";import { icon, keyLabel } from "./ui";export type PageId =@@ -84,6 +84,11 @@ const DEFAULT_STATUS: FlowStatus = {export async function startHub(root: HTMLElement, opts: HubOptions) {document.body.classList.add("hub-window");+ // The window is created hidden and only shown and hidden afterwards: its+ // canvases animate only while it is on screen (#792).+ await onEvent<boolean>("hub-visible", setWindowShown);+ const shown = await FlowBridge.hubShown();+ if (shown !== null) setWindowShown(shown);let current: PageId = "dictation";let status: FlowStatus = (await FlowBridge.status()) ?? DEFAULT_STATUS;const items = new Map<PageId, HTMLElement>();diff --git a/windows/tests/hub-frames.test.mjs b/windows/tests/hub-frames.test.mjsnew file mode 100644index 00000000..f980a9b0--- /dev/null+++ b/windows/tests/hub-frames.test.mjs@@ -0,0 +1,33 @@+// When the Coucou window draws its canvases (src/hub/frames.ts): only while it+// is on screen, and only the canvases in sight. Issue #792: a hidden window+// kept a WebKitWebProcess busy on Linux.++import { test } from "node:test";+import assert from "node:assert/strict";+import { onScreen, shouldAnimate } from "../src/hub/frames.ts";++test("the loop runs only with something to draw, the window shown and the page visible", () => {+ assert.equal(shouldAnimate(3, "visible", true), true);+ assert.equal(shouldAnimate(0, "visible", true), false);+ // WebKitGTK can still say "visible" for a hidden window: Rust's word wins.+ assert.equal(shouldAnimate(3, "visible", false), false);+ assert.equal(shouldAnimate(3, "hidden", true), false);+ assert.equal(shouldAnimate(3, "hidden", false), false);+});++const box = (top, left, width, height) => ({ top, left, width, height, bottom: top + height, right: left + width });++test("a canvas in the window is drawn", () => {+ assert.equal(onScreen(box(0, 0, 800, 200), 1100, 720), true);+ // Half scrolled out at the top or the bottom: still drawn.+ assert.equal(onScreen(box(-150, 240, 800, 200), 1100, 720), true);+ assert.equal(onScreen(box(600, 240, 800, 200), 1100, 720), true);+});++test("a canvas scrolled out of sight, or not laid out, is skipped", () => {+ assert.equal(onScreen(box(-200, 240, 800, 200), 1100, 720), false);+ assert.equal(onScreen(box(720, 240, 800, 200), 1100, 720), false);+ assert.equal(onScreen(box(900, 240, 800, 200), 1100, 720), false);+ assert.equal(onScreen(box(0, 1100, 300, 200), 1100, 720), false);+ assert.equal(onScreen(box(100, 240, 0, 0), 1100, 720), false);+});