resolve friends' grid to absolute

This commit is contained in:
2025-12-16 22:27:55 +08:00
parent 8185b2c9a1
commit edeb1f4d78
4 changed files with 70 additions and 33 deletions

View File

@@ -1,6 +1,6 @@
use device_query::{DeviceEvents, DeviceEventsHandler};
use once_cell::sync::OnceCell;
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
@@ -10,7 +10,7 @@ use ts_rs::TS;
use crate::{get_app_handle, lock_r, state::FDOLL};
#[derive(Clone, Serialize, TS)]
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct CursorPosition {
@@ -18,7 +18,7 @@ pub struct CursorPosition {
pub y: i32,
}
#[derive(Clone, Serialize, TS)]
#[derive(Debug, Clone, Serialize, TS)]
#[serde(rename_all = "camelCase")]
#[ts(export)]
pub struct CursorPositions {

View File

@@ -5,12 +5,28 @@ use tracing::{error, info};
use crate::{
get_app_handle, lock_r, lock_w, models::app_config::AppConfig,
services::cursor::CursorPosition, state::FDOLL,
services::cursor::{grid_to_absolute_position, CursorPosition, CursorPositions},
state::FDOLL,
};
use serde::{Deserialize, Serialize};
#[allow(non_camel_case_types)] // pretend to be a const like in js
pub struct WS_EVENT;
#[derive(Debug, Deserialize)]
struct IncomingFriendCursorPayload {
#[serde(rename = "userId")]
user_id: String,
position: CursorPosition,
}
#[derive(Clone, Serialize)]
#[serde(rename_all = "camelCase")]
struct OutgoingFriendCursorPayload {
user_id: String,
position: CursorPositions,
}
impl WS_EVENT {
pub const CURSOR_REPORT_POSITION: &str = "cursor-report-position";
pub const FRIEND_REQUEST_RECEIVED: &str = "friend-request-received";
@@ -69,10 +85,38 @@ 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();
Payload::Text(values) => {
// values is Vec<serde_json::Value>
if let Some(first_value) = values.first() {
let incoming_data: Result<IncomingFriendCursorPayload, _> = serde_json::from_value(first_value.clone());
match incoming_data {
Ok(friend_data) => {
// We received grid coordinates (mapped)
let mapped_pos = &friend_data.position;
// Convert grid coordinates back to absolute screen coordinates (raw)
let raw_pos = grid_to_absolute_position(mapped_pos);
let outgoing_payload = OutgoingFriendCursorPayload {
user_id: friend_data.user_id.clone(),
position: CursorPositions {
raw: raw_pos,
mapped: mapped_pos.clone(),
},
};
get_app_handle()
.emit(WS_EVENT::FRIEND_CURSOR_POSITION, outgoing_payload)
.unwrap();
}
Err(e) => {
error!("Failed to parse friend cursor position data: {}", e);
}
}
} else {
error!("Received empty text payload for friend cursor position");
}
}
_ => error!("Received unexpected payload format for friend cursor position"),
}