Skip to content

Commit

Permalink
fix: reregister of hotkey not working
Browse files Browse the repository at this point in the history
  • Loading branch information
0PandaDEV committed Jan 10, 2025
1 parent 617c173 commit 416e2f0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 32 deletions.
1 change: 0 additions & 1 deletion plugins/settings.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { invoke } from "@tauri-apps/api/core";
import type { Settings } from "~/types/types";

export default defineNuxtPlugin(() => {
return {
Expand Down
5 changes: 3 additions & 2 deletions src-tauri/src/api/hotkeys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub fn setup(app_handle: tauri::AppHandle) {
}

app_handle.listen("update-shortcut", move |event| {
let payload_str = event.payload();
let payload_str = event.payload().replace("\\\"", "\"");
let trimmed_str = payload_str.trim_matches('"');

if let Some(old_hotkey) = REGISTERED_HOTKEY.lock().unwrap().take() {
let manager_guard = HOTKEY_MANAGER.lock().unwrap();
Expand All @@ -52,7 +53,7 @@ pub fn setup(app_handle: tauri::AppHandle) {
}
}

let payload: Vec<String> = serde_json::from_str(payload_str).unwrap_or_default();
let payload: Vec<String> = serde_json::from_str(trimmed_str).unwrap_or_default();

if let Err(e) = register_shortcut(&payload) {
eprintln!("Error re-registering shortcut: {:?}", e);
Expand Down
32 changes: 5 additions & 27 deletions src-tauri/src/db/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,6 @@ pub async fn initialize_settings(pool: &SqlitePool) -> Result<(), Box<dyn std::e
Ok(())
}

#[tauri::command]
pub async fn save_keybind(
app_handle: tauri::AppHandle,
pool: tauri::State<'_, SqlitePool>,
keybind: Vec<String>
) -> Result<(), String> {
app_handle.emit("update-shortcut", &keybind).map_err(|e| e.to_string())?;

let json = serde_json::to_string(&keybind).map_err(|e| e.to_string())?;

sqlx
::query("INSERT OR REPLACE INTO settings (key, value) VALUES ('keybind', ?)")
.bind(json)
.execute(&*pool).await
.map_err(|e| e.to_string())?;

let _ = app_handle.track_event(
"keybind_saved",
Some(serde_json::json!({
"keybind": keybind
}))
);

Ok(())
}

#[tauri::command]
pub async fn get_setting(
pool: tauri::State<'_, SqlitePool>,
Expand All @@ -74,7 +48,7 @@ pub async fn save_setting(
sqlx
::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
.bind(key.clone())
.bind(value)
.bind(value.clone())
.execute(&*pool).await
.map_err(|e| e.to_string())?;

Expand All @@ -85,6 +59,10 @@ pub async fn save_setting(
}))
);

if key == "keybind" {
let _ = app_handle.emit("update-shortcut", &value).map_err(|e| e.to_string())?;
}

Ok(())
}

Expand Down
2 changes: 0 additions & 2 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,6 @@ fn main() {
db::history::read_image,
db::settings::get_setting,
db::settings::save_setting,
db::settings::save_keybind,
db::settings::get_keybind,
utils::commands::fetch_page_meta
]
)
Expand Down

0 comments on commit 416e2f0

Please sign in to comment.