user active app reporting

This commit is contained in:
2026-01-26 11:16:55 +08:00
parent 76943f3362
commit 0b1686653c
10 changed files with 224 additions and 2 deletions

94
src/events/user-status.ts Normal file
View File

@@ -0,0 +1,94 @@
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
import { writable } from "svelte/store";
export type FriendUserStatus = {
activeApp: string;
state: "idle" | "resting";
};
export const friendsUserStatuses = writable<Record<string, FriendUserStatus>>({});
let unlistenStatus: UnlistenFn | null = null;
let unlistenFriendDisconnected: UnlistenFn | null = null;
let isListening = false;
export async function initUserStatusListeners() {
if (isListening) return;
try {
unlistenStatus = await listen<unknown>("friend-user-status", (event) => {
let payload = event.payload as any;
if (typeof payload === "string") {
try {
payload = JSON.parse(payload);
} catch (error) {
console.error("Failed to parse friend-user-status payload", error);
return;
}
}
const userId = payload?.userId as string | undefined;
const status = payload?.status as FriendUserStatus | undefined;
if (!userId || !status) return;
if (typeof status.activeApp !== "string" || status.activeApp.trim() === "") return;
if (status.state !== "idle" && status.state !== "resting") return;
friendsUserStatuses.update((current) => ({
...current,
[userId]: {
activeApp: status.activeApp,
state: status.state,
},
}));
});
unlistenFriendDisconnected = await listen<[{ userId: string }] | { userId: string } | string>(
"friend-disconnected",
(event) => {
let payload = event.payload as any;
if (typeof payload === "string") {
try {
payload = JSON.parse(payload);
} catch (error) {
console.error("Failed to parse friend-disconnected payload", error);
return;
}
}
const data = Array.isArray(payload) ? payload[0] : payload;
const userId = data?.userId as string | undefined;
if (!userId) return;
friendsUserStatuses.update((current) => {
const next = { ...current };
delete next[userId];
return next;
});
},
);
isListening = true;
} catch (error) {
console.error("Failed to initialize user status listeners", error);
throw error;
}
}
export function stopUserStatusListeners() {
if (unlistenStatus) {
unlistenStatus();
unlistenStatus = null;
}
if (unlistenFriendDisconnected) {
unlistenFriendDisconnected();
unlistenFriendDisconnected = null;
}
isListening = false;
}
if (import.meta.hot) {
import.meta.hot.dispose(() => {
stopUserStatusListeners();
});
}

View File

@@ -8,6 +8,7 @@
initSceneInteractiveListener,
stopSceneInteractiveListener,
} from "../events/scene-interactive";
import { initUserStatusListeners, stopUserStatusListeners } from "../events/user-status";
let { children } = $props();
if (browser) {
@@ -17,6 +18,7 @@
await initAppDataListener();
await initSceneInteractiveListener();
await initInteractionListeners();
await initUserStatusListeners();
} catch (err) {
console.error("Failed to initialize event listeners:", err);
}
@@ -26,6 +28,7 @@
stopCursorTracking();
stopSceneInteractiveListener();
stopInteractionListeners();
stopUserStatusListeners();
});
}
</script>

View File

@@ -6,6 +6,7 @@
} from "../../events/cursor";
import { appData } from "../../events/app-data";
import { sceneInteractive } from "../../events/scene-interactive";
import { friendsUserStatuses } from "../../events/user-status";
import { invoke } from "@tauri-apps/api/core";
@@ -33,11 +34,25 @@
return friend?.friend.activeDoll;
}
function getFriendStatus(userId: string) {
return $friendsUserStatuses[userId];
}
let appMetadata: AppMetadata | null = $state(null);
onMount(() => {
const unlisten = listen<AppMetadata>("active-app-changed", (event) => {
appMetadata = event.payload;
const activeAppValue =
appMetadata?.localized ?? appMetadata?.unlocalized ?? "";
if (activeAppValue.trim()) {
invoke("send_user_status_cmd", {
activeApp: activeAppValue,
state: "idle",
}).catch((error) => {
console.error("Failed to send user status", error);
});
}
});
return () => {
@@ -86,14 +101,20 @@
<div class="flex flex-col gap-2">
<div>
{#each Object.entries($friendsCursorPositions) as [userId, position]}
{@const status = getFriendStatus(userId)}
<div class="badge py-3 text-xs text-left flex flex-row gap-2">
<span class="font-bold">{getFriendById(userId).name}</span>
<div class="flex flex-col font-mono">
<div class="flex flex-row font-mono gap-2">
<span>
({position.mapped.x.toFixed(3)}, {position.mapped.y.toFixed(
3,
)})
</span>
{#if status}
<span>
{status.state} in {status.activeApp}
</span>
{/if}
</div>
</div>
{/each}