centralize tauri event names
This commit is contained in:
85
src-tauri/src/services/app_events.rs
Normal file
85
src-tauri/src/services/app_events.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use serde::Serialize;
|
||||
#[allow(unused_imports)]
|
||||
use std::{fs, path::Path};
|
||||
use strum::{AsRefStr, EnumIter};
|
||||
use ts_rs::TS;
|
||||
|
||||
#[derive(Serialize, TS, EnumIter, AsRefStr)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
#[ts(export)]
|
||||
pub enum AppEvents {
|
||||
CursorPosition,
|
||||
SceneInteractive,
|
||||
AppDataRefreshed,
|
||||
SetInteractionOverlay,
|
||||
EditDoll,
|
||||
CreateDoll,
|
||||
UserStatusChanged,
|
||||
}
|
||||
|
||||
impl AppEvents {
|
||||
pub fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
AppEvents::CursorPosition => "cursor-position",
|
||||
AppEvents::SceneInteractive => "scene-interactive",
|
||||
AppEvents::AppDataRefreshed => "app-data-refreshed",
|
||||
AppEvents::SetInteractionOverlay => "set-interaction-overlay",
|
||||
AppEvents::EditDoll => "edit-doll",
|
||||
AppEvents::CreateDoll => "create-doll",
|
||||
AppEvents::UserStatusChanged => "user-status-changed",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn export_bindings_appeventsconsts() {
|
||||
use strum::IntoEnumIterator;
|
||||
|
||||
let some_export_dir = std::env::var("TS_RS_EXPORT_DIR")
|
||||
.ok()
|
||||
.map(|s| Path::new(&s).to_owned());
|
||||
|
||||
let Some(export_dir) = some_export_dir else {
|
||||
eprintln!("TS_RS_EXPORT_DIR not set, skipping constants export");
|
||||
return;
|
||||
};
|
||||
|
||||
let to_kebab_case = |s: &str| -> String {
|
||||
let mut result = String::new();
|
||||
for (i, c) in s.chars().enumerate() {
|
||||
if c.is_uppercase() {
|
||||
if i > 0 {
|
||||
result.push('-');
|
||||
}
|
||||
result.push(c.to_lowercase().next().unwrap());
|
||||
} else {
|
||||
result.push(c);
|
||||
}
|
||||
}
|
||||
result
|
||||
};
|
||||
|
||||
let mut lines = vec![
|
||||
r#"// Auto-generated constants - DO NOT EDIT"#.to_string(),
|
||||
r#"// Generated from Rust AppEvents enum"#.to_string(),
|
||||
"".to_string(),
|
||||
"export const AppEvents = {".to_string(),
|
||||
];
|
||||
|
||||
for variant in AppEvents::iter() {
|
||||
let name = variant.as_ref();
|
||||
let kebab = to_kebab_case(name);
|
||||
lines.push(format!(" {}: \"{}\",", name, kebab));
|
||||
}
|
||||
|
||||
lines.push("} as const;".to_string());
|
||||
lines.push("".to_string());
|
||||
lines.push("export type AppEvents = typeof AppEvents[keyof typeof AppEvents];".to_string());
|
||||
|
||||
let constants_content = lines.join("\n");
|
||||
|
||||
let constants_path = export_dir.join("AppEventsConstants.ts");
|
||||
if let Err(e) = fs::write(&constants_path, constants_content) {
|
||||
eprintln!("Failed to write {}: {}", constants_path.display(), e);
|
||||
}
|
||||
}
|
||||
@@ -8,7 +8,7 @@ use tokio::sync::mpsc;
|
||||
use tracing::{debug, error, info, warn};
|
||||
use ts_rs::TS;
|
||||
|
||||
use crate::{get_app_handle, lock_r, state::FDOLL};
|
||||
use crate::{get_app_handle, lock_r, services::app_events::AppEvents, state::FDOLL};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, TS)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
@@ -103,7 +103,7 @@ async fn init_cursor_tracking_i() -> Result<(), String> {
|
||||
crate::services::ws::report_cursor_data(mapped_for_ws).await;
|
||||
|
||||
// 2. Broadcast to local windows
|
||||
if let Err(e) = app_handle.emit("cursor-position", &positions) {
|
||||
if let Err(e) = app_handle.emit(AppEvents::CursorPosition.as_str(), &positions) {
|
||||
error!("Failed to emit cursor position event: {:?}", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use tauri::WebviewWindow;
|
||||
use tauri::{Emitter, Listener, Manager};
|
||||
use tracing::{error, info};
|
||||
|
||||
use crate::get_app_handle;
|
||||
use crate::{get_app_handle, services::app_events::AppEvents};
|
||||
|
||||
static APP_MENU_WINDOW_LABEL: &str = "app_menu";
|
||||
|
||||
@@ -76,17 +76,17 @@ pub async fn open_doll_editor_window(doll_id: Option<String>) {
|
||||
// Ensure overlay is active on parent (redundancy for safety)
|
||||
#[cfg(target_os = "macos")]
|
||||
if let Some(parent) = app_handle.get_webview_window(APP_MENU_WINDOW_LABEL) {
|
||||
if let Err(e) = parent.emit("set-interaction-overlay", true) {
|
||||
if let Err(e) = parent.emit(AppEvents::SetInteractionOverlay.as_str(), true) {
|
||||
error!("Failed to ensure interaction overlay on parent: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
// Emit event to update context
|
||||
if let Some(id) = doll_id {
|
||||
if let Err(e) = window.emit("edit-doll", id) {
|
||||
if let Err(e) = window.emit(AppEvents::EditDoll.as_str(), id) {
|
||||
error!("Failed to emit edit-doll event: {}", e);
|
||||
}
|
||||
} else if let Err(e) = window.emit("create-doll", ()) {
|
||||
} else if let Err(e) = window.emit(AppEvents::CreateDoll.as_str(), ()) {
|
||||
error!("Failed to emit create-doll event: {}", e);
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ pub async fn open_doll_editor_window(doll_id: Option<String>) {
|
||||
let app_handle_clone = get_app_handle().clone();
|
||||
|
||||
// Emit event to show overlay
|
||||
if let Err(e) = parent.emit("set-interaction-overlay", true) {
|
||||
if let Err(e) = parent.emit(AppEvents::SetInteractionOverlay.as_str(), true) {
|
||||
error!("Failed to emit set-interaction-overlay event: {}", e);
|
||||
}
|
||||
|
||||
@@ -169,7 +169,7 @@ pub async fn open_doll_editor_window(doll_id: Option<String>) {
|
||||
parent.unlisten(id);
|
||||
}
|
||||
// Remove overlay if we failed
|
||||
let _ = parent.emit("set-interaction-overlay", false);
|
||||
let _ = parent.emit(AppEvents::SetInteractionOverlay.as_str(), false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -204,7 +204,9 @@ pub async fn open_doll_editor_window(doll_id: Option<String>) {
|
||||
parent.unlisten(id);
|
||||
}
|
||||
// Remove overlay
|
||||
if let Err(e) = parent.emit("set-interaction-overlay", false) {
|
||||
if let Err(e) = parent
|
||||
.emit(AppEvents::SetInteractionOverlay.as_str(), false)
|
||||
{
|
||||
error!("Failed to remove interaction overlay: {}", e);
|
||||
}
|
||||
}
|
||||
@@ -230,7 +232,7 @@ pub async fn open_doll_editor_window(doll_id: Option<String>) {
|
||||
if let Some(id) = parent_focus_listener_id {
|
||||
parent.unlisten(id);
|
||||
}
|
||||
let _ = parent.emit("set-interaction-overlay", false);
|
||||
let _ = parent.emit(AppEvents::SetInteractionOverlay.as_str(), false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ use tauri::Manager;
|
||||
|
||||
use crate::get_app_handle;
|
||||
|
||||
pub mod app_events;
|
||||
pub mod app_menu;
|
||||
pub mod auth;
|
||||
pub mod client_config_manager;
|
||||
@@ -10,10 +11,10 @@ pub mod doll_editor;
|
||||
pub mod health_manager;
|
||||
pub mod health_monitor;
|
||||
pub mod interaction;
|
||||
pub mod petpet;
|
||||
pub mod presence_modules;
|
||||
pub mod scene;
|
||||
pub mod sprite_recolor;
|
||||
pub mod petpet;
|
||||
pub mod welcome;
|
||||
pub mod ws;
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ use tauri::{Emitter, Manager};
|
||||
use tauri_plugin_positioner::WindowExt;
|
||||
use tracing::{error, info, warn};
|
||||
|
||||
use crate::get_app_handle;
|
||||
use crate::{get_app_handle, services::app_events::AppEvents};
|
||||
|
||||
pub static SCENE_WINDOW_LABEL: &str = "scene";
|
||||
pub static SPLASH_WINDOW_LABEL: &str = "splash";
|
||||
@@ -69,7 +69,7 @@ pub fn update_scene_interactive(interactive: bool, should_click: bool) {
|
||||
}
|
||||
}
|
||||
|
||||
if let Err(e) = window.emit("scene-interactive", &interactive) {
|
||||
if let Err(e) = window.emit(AppEvents::SceneInteractive.as_str(), &interactive) {
|
||||
error!("Failed to emit scene interactive event: {}", e);
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -7,6 +7,8 @@ use tracing::warn;
|
||||
|
||||
use crate::services::presence_modules::models::PresenceStatus;
|
||||
|
||||
use crate::services::app_events::AppEvents;
|
||||
|
||||
use super::{emitter, types::WS_EVENT};
|
||||
|
||||
/// User status payload sent to WebSocket server
|
||||
@@ -17,8 +19,6 @@ pub struct UserStatusPayload {
|
||||
pub state: String,
|
||||
}
|
||||
|
||||
pub static USER_STATUS_CHANGED: &str = "user-status-changed";
|
||||
|
||||
/// Debouncer for user status reports
|
||||
static USER_STATUS_REPORT_DEBOUNCE: Lazy<Mutex<Option<JoinHandle<()>>>> =
|
||||
Lazy::new(|| Mutex::new(None));
|
||||
@@ -32,7 +32,7 @@ pub async fn report_user_status(status: UserStatusPayload) {
|
||||
handle.abort();
|
||||
}
|
||||
|
||||
emitter::emit_to_frontend(USER_STATUS_CHANGED, &status);
|
||||
emitter::emit_to_frontend(AppEvents::UserStatusChanged.as_str(), &status);
|
||||
|
||||
// Schedule new report after 500ms
|
||||
let handle = async_runtime::spawn(async move {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use crate::{
|
||||
get_app_handle, lock_r, lock_w,
|
||||
remotes::{dolls::DollsRemote, friends::FriendRemote, user::UserRemote},
|
||||
services::app_events::AppEvents,
|
||||
state::FDOLL,
|
||||
};
|
||||
use std::{collections::HashSet, sync::LazyLock};
|
||||
@@ -210,7 +211,9 @@ pub async fn init_app_data_scoped(scope: AppDataRefreshScope) {
|
||||
let app_data_clone = guard.user_data.clone();
|
||||
drop(guard); // Drop lock before emitting to prevent potential deadlocks
|
||||
|
||||
if let Err(e) = get_app_handle().emit("app-data-refreshed", &app_data_clone) {
|
||||
if let Err(e) =
|
||||
get_app_handle().emit(AppEvents::AppDataRefreshed.as_str(), &app_data_clone)
|
||||
{
|
||||
warn!("Failed to emit app-data-refreshed event: {}", e);
|
||||
use tauri_plugin_dialog::MessageDialogBuilder;
|
||||
use tauri_plugin_dialog::{DialogExt, MessageDialogKind};
|
||||
|
||||
Reference in New Issue
Block a user