From f82d1041ae84fd2ba91d836632565442d3c2afe4 Mon Sep 17 00:00:00 2001 From: Wind-Explorer Date: Mon, 31 Aug 2026 01:28:14 +0800 Subject: [PATCH] reset to default skin btn --- src-tauri/src/lib.rs | 1 + src-tauri/src/profile/mod.rs | 51 +++++++++++++++++++ src/lib/bindings.ts | 3 ++ .../control-panel/scene-panel.svelte | 33 +++++++++--- 4 files changed, 82 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/lib.rs b/src-tauri/src/lib.rs index cb5c092..d622f9a 100644 --- a/src-tauri/src/lib.rs +++ b/src-tauri/src/lib.rs @@ -75,6 +75,7 @@ fn specta_builder() -> tauri_specta::Builder { remotes::delete_remote, profile::get_profile, profile::update_profile, + profile::reset_profile_skin, keypair::get_public_key, network::list_statuses, network::list_friend_statuses, diff --git a/src-tauri/src/profile/mod.rs b/src-tauri/src/profile/mod.rs index 5228bf7..e4494f0 100644 --- a/src-tauri/src/profile/mod.rs +++ b/src-tauri/src/profile/mod.rs @@ -52,6 +52,15 @@ async fn update( get(database, public_key).await } +async fn reset_skin(database: &AppDatabase, public_key: &str) -> Result { + sqlx::query("UPDATE profile SET skin_hash = NULL WHERE id = ?1") + .bind(PROFILE_ID) + .execute(database.pool()) + .await?; + + get(database, public_key).await +} + fn emit_changed(handle: &AppHandle, profile: User) -> Result<(), String> { ProfileChanged { profile } .emit(handle) @@ -94,6 +103,22 @@ pub async fn update_profile( Ok(profile) } +#[tauri::command] +#[specta::specta] +pub async fn reset_profile_skin( + handle: AppHandle, + database: State<'_, AppDatabase>, + keypair: State<'_, AppKeypair>, + network: State<'_, Network>, +) -> Result { + let profile = reset_skin(&database, keypair.public_key()) + .await + .map_err(db::command_error)?; + network.update_profile(profile.clone()); + emit_changed(&handle, profile.clone())?; + Ok(profile) +} + #[cfg(test)] mod tests { use sqlx::sqlite::SqlitePoolOptions; @@ -162,4 +187,30 @@ mod tests { assert_eq!(columns, vec!["id", "display_name", "skin_hash"]); } + + #[tokio::test] + async fn resetting_skin_preserves_the_display_name() { + let database = database().await; + update( + &database, + "public-key", + "Wind".to_owned(), + Some("a".repeat(64)), + ) + .await + .expect("set profile skin"); + + let profile = reset_skin(&database, "public-key") + .await + .expect("reset profile skin"); + + assert_eq!( + profile, + User { + id: "public-key".to_owned(), + display_name: "Wind".to_owned(), + skin_hash: None, + } + ); + } } diff --git a/src/lib/bindings.ts b/src/lib/bindings.ts index 01ea008..09b82bf 100644 --- a/src/lib/bindings.ts +++ b/src/lib/bindings.ts @@ -38,6 +38,9 @@ async getProfile() : Promise { async updateProfile(displayName: string, skinData: number[] | null) : Promise { return await TAURI_INVOKE("update_profile", { displayName, skinData }); }, +async resetProfileSkin() : Promise { + return await TAURI_INVOKE("reset_profile_skin"); +}, async getPublicKey() : Promise { return await TAURI_INVOKE("get_public_key"); }, diff --git a/src/lib/components/control-panel/scene-panel.svelte b/src/lib/components/control-panel/scene-panel.svelte index 7493701..6acb1fd 100644 --- a/src/lib/components/control-panel/scene-panel.svelte +++ b/src/lib/components/control-panel/scene-panel.svelte @@ -18,6 +18,7 @@ let skinFile = $state(null); let skinPreviewUrl = $state(null); let skinInput = $state(null); + let pendingSkinReset = $state(false); let dirty = $state(false); let busy = $state(false); let error = $state(""); @@ -45,7 +46,7 @@ } function updateDirty() { - dirty = hasConfigurationChanges() || skinFile !== null; + dirty = hasConfigurationChanges() || skinFile !== null || pendingSkinReset; error = ""; } @@ -58,16 +59,24 @@ function selectSkinFile(file: File | null) { clearSkinDraft(); + pendingSkinReset = false; skinFile = file; skinPreviewUrl = file ? URL.createObjectURL(file) : null; updateDirty(); } + function resetSkin() { + clearSkinDraft(); + pendingSkinReset = true; + updateDirty(); + } + function reset() { puppetScale = $sceneConfiguration.puppetScale; puppetOpacity = $sceneConfiguration.puppetOpacity; puppetMovementMode = $sceneConfiguration.puppetMovementMode; clearSkinDraft(); + pendingSkinReset = false; dirty = false; error = ""; } @@ -78,7 +87,10 @@ busy = true; error = ""; try { - if (skinFile) { + if (pendingSkinReset) { + await commands.resetProfileSkin(); + pendingSkinReset = false; + } else if (skinFile) { const currentProfile = await commands.getProfile(); const skinData = Array.from( new Uint8Array(await skinFile.arrayBuffer()), @@ -100,7 +112,8 @@ dirty = false; return true; } catch (cause) { - dirty = hasConfigurationChanges() || skinFile !== null; + dirty = + hasConfigurationChanges() || skinFile !== null || pendingSkinReset; error = String(cause); return false; } finally { @@ -141,7 +154,7 @@
-
+
+