friends cursor broadcast system (?)
This commit is contained in:
@@ -17,6 +17,7 @@ impl WS_EVENT {
|
|||||||
pub const FRIEND_REQUEST_ACCEPTED: &str = "friend-request-accepted";
|
pub const FRIEND_REQUEST_ACCEPTED: &str = "friend-request-accepted";
|
||||||
pub const FRIEND_REQUEST_DENIED: &str = "friend-request-denied";
|
pub const FRIEND_REQUEST_DENIED: &str = "friend-request-denied";
|
||||||
pub const UNFRIENDED: &str = "unfriended";
|
pub const UNFRIENDED: &str = "unfriended";
|
||||||
|
pub const FRIEND_CURSOR_POSITION: &str = "friend-cursor-position";
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_friend_request_received(payload: Payload, _socket: RawClient) {
|
fn on_friend_request_received(payload: Payload, _socket: RawClient) {
|
||||||
@@ -65,6 +66,17 @@ fn on_unfriended(payload: Payload, _socket: RawClient) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn on_friend_cursor_position(payload: Payload, _socket: RawClient) {
|
||||||
|
match payload {
|
||||||
|
Payload::Text(str) => {
|
||||||
|
get_app_handle()
|
||||||
|
.emit(WS_EVENT::FRIEND_CURSOR_POSITION, str)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
_ => todo!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn report_cursor_data(cursor_position: CursorPosition) {
|
pub async fn report_cursor_data(cursor_position: CursorPosition) {
|
||||||
let client = {
|
let client = {
|
||||||
let guard = lock_r!(FDOLL);
|
let guard = lock_r!(FDOLL);
|
||||||
@@ -81,7 +93,7 @@ pub async fn report_cursor_data(cursor_position: CursorPosition) {
|
|||||||
match async_runtime::spawn_blocking(move || {
|
match async_runtime::spawn_blocking(move || {
|
||||||
client.emit(
|
client.emit(
|
||||||
WS_EVENT::CURSOR_REPORT_POSITION,
|
WS_EVENT::CURSOR_REPORT_POSITION,
|
||||||
Payload::Text(vec![json!({ "position": cursor_position })]),
|
Payload::Text(vec![json!(cursor_position)]),
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
.await
|
.await
|
||||||
@@ -132,6 +144,7 @@ pub async fn build_ws_client(app_config: &AppConfig) -> rust_socketio::client::C
|
|||||||
)
|
)
|
||||||
.on(WS_EVENT::FRIEND_REQUEST_DENIED, on_friend_request_denied)
|
.on(WS_EVENT::FRIEND_REQUEST_DENIED, on_friend_request_denied)
|
||||||
.on(WS_EVENT::UNFRIENDED, on_unfriended)
|
.on(WS_EVENT::UNFRIENDED, on_unfriended)
|
||||||
|
.on(WS_EVENT::FRIEND_CURSOR_POSITION, on_friend_cursor_position)
|
||||||
.auth(json!({ "token": token }))
|
.auth(json!({ "token": token }))
|
||||||
.connect()
|
.connect()
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,13 +2,24 @@ import { invoke } from "@tauri-apps/api/core";
|
|||||||
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
|
||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
import type { CursorPositions } from "../types/bindings/CursorPositions";
|
import type { CursorPositions } from "../types/bindings/CursorPositions";
|
||||||
|
import type { CursorPosition } from "../types/bindings/CursorPosition";
|
||||||
|
|
||||||
export let cursorPositionOnScreen = writable<CursorPositions>({
|
export let cursorPositionOnScreen = writable<CursorPositions>({
|
||||||
raw: { x: 0, y: 0 },
|
raw: { x: 0, y: 0 },
|
||||||
mapped: { x: 0, y: 0 },
|
mapped: { x: 0, y: 0 },
|
||||||
});
|
});
|
||||||
|
|
||||||
let unlisten: UnlistenFn | null = null;
|
export type FriendCursorPosition = {
|
||||||
|
userId: string;
|
||||||
|
position: CursorPosition;
|
||||||
|
};
|
||||||
|
|
||||||
|
export let friendsCursorPositions = writable<Record<string, CursorPosition>>(
|
||||||
|
{},
|
||||||
|
);
|
||||||
|
|
||||||
|
let unlistenCursor: UnlistenFn | null = null;
|
||||||
|
let unlistenFriendCursor: UnlistenFn | null = null;
|
||||||
let isListening = false;
|
let isListening = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,9 +37,44 @@ export async function initCursorTracking() {
|
|||||||
await invoke("start_cursor_tracking");
|
await invoke("start_cursor_tracking");
|
||||||
|
|
||||||
// Listen to cursor position events (each window subscribes independently)
|
// Listen to cursor position events (each window subscribes independently)
|
||||||
unlisten = await listen<CursorPositions>("cursor-position", (event) => {
|
unlistenCursor = await listen<CursorPositions>(
|
||||||
|
"cursor-position",
|
||||||
|
(event) => {
|
||||||
cursorPositionOnScreen.set(event.payload);
|
cursorPositionOnScreen.set(event.payload);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
// Listen to friend cursor position events
|
||||||
|
unlistenFriendCursor = await listen<[FriendCursorPosition]>(
|
||||||
|
"friend-cursor-position",
|
||||||
|
(event) => {
|
||||||
|
// payload might be a string JSON if it comes directly from rust_socketio Payload::Text
|
||||||
|
let payload = event.payload;
|
||||||
|
|
||||||
|
if (typeof payload === "string") {
|
||||||
|
try {
|
||||||
|
payload = JSON.parse(payload);
|
||||||
|
} catch (e) {
|
||||||
|
console.error(
|
||||||
|
"[Cursor] Failed to parse friend cursor position payload:",
|
||||||
|
e,
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rust socket.io client returns payload as an array of arguments
|
||||||
|
// Since we only send one argument { userId, position }, it's the first element
|
||||||
|
const data = Array.isArray(payload) ? payload[0] : payload;
|
||||||
|
|
||||||
|
friendsCursorPositions.update((current) => {
|
||||||
|
return {
|
||||||
|
...current,
|
||||||
|
[data.userId]: data.position,
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
isListening = true;
|
isListening = true;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -42,11 +88,15 @@ export async function initCursorTracking() {
|
|||||||
* Note: This doesn't stop the Rust-side tracking, just stops this window from receiving events.
|
* Note: This doesn't stop the Rust-side tracking, just stops this window from receiving events.
|
||||||
*/
|
*/
|
||||||
export function stopCursorTracking() {
|
export function stopCursorTracking() {
|
||||||
if (unlisten) {
|
if (unlistenCursor) {
|
||||||
unlisten();
|
unlistenCursor();
|
||||||
unlisten = null;
|
unlistenCursor = null;
|
||||||
isListening = false;
|
|
||||||
}
|
}
|
||||||
|
if (unlistenFriendCursor) {
|
||||||
|
unlistenFriendCursor();
|
||||||
|
unlistenFriendCursor = null;
|
||||||
|
}
|
||||||
|
isListening = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle HMR (Hot Module Replacement) cleanup
|
// Handle HMR (Hot Module Replacement) cleanup
|
||||||
|
|||||||
@@ -1,5 +1,14 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { cursorPositionOnScreen } from "../../events/cursor";
|
import {
|
||||||
|
cursorPositionOnScreen,
|
||||||
|
friendsCursorPositions,
|
||||||
|
} from "../../events/cursor";
|
||||||
|
import { appData } from "../../events/app-data";
|
||||||
|
|
||||||
|
function getFriendName(userId: string) {
|
||||||
|
const friend = $appData?.friends?.find((f) => f.friend.id === userId);
|
||||||
|
return friend ? friend.friend.name : userId.slice(0, 8) + "...";
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="w-svw h-svh p-4 relative">
|
<div class="w-svw h-svh p-4 relative">
|
||||||
@@ -19,6 +28,20 @@
|
|||||||
.mapped.y})
|
.mapped.y})
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{#if Object.keys($friendsCursorPositions).length > 0}
|
||||||
|
<div class="mt-4 flex flex-col gap-2">
|
||||||
|
<p class="text-sm font-semibold opacity-75">Friends Online</p>
|
||||||
|
<div>
|
||||||
|
{#each Object.entries($friendsCursorPositions) as [userId, position]}
|
||||||
|
<div class="bg-base-200/50 p-2 rounded text-xs text-left">
|
||||||
|
<span class="font-bold">{getFriendName(userId)}</span>
|
||||||
|
<span class="font-mono ml-2">({position.x}, {position.y})</span>
|
||||||
|
</div>
|
||||||
|
{/each}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user