Skip to content
This repository was archived by the owner on Jan 3, 2026. It is now read-only.

Commit e6a6690

Browse files
committed
feat: use rust-end config
1 parent 9234bda commit e6a6690

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src-tauri/src/lib.rs

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
mod tray;
22

33
use enigo::{Direction, Enigo, Key, Keyboard, Settings};
4-
use tauri::{AppHandle, Manager};
4+
use tauri::{AppHandle, Manager, Runtime};
55
use tauri_plugin_autostart::MacosLauncher;
6+
use tauri_plugin_store::StoreExt;
67
use window_vibrancy::{apply_acrylic, apply_mica};
78

9+
use serde_json::json;
10+
811
#[tauri::command]
912
async fn input_text(text: &str) -> Result<(), String> {
1013
let mut enigo = Enigo::new(&Settings::default()).unwrap();
@@ -21,6 +24,45 @@ async fn simulate_paste() -> Result<(), String> {
2124
Ok(())
2225
}
2326

27+
#[tauri::command]
28+
async fn get_key_from_store<R: Runtime>(
29+
app: tauri::AppHandle<R>,
30+
_window: tauri::Window<R>,
31+
key: String,
32+
fallback: serde_json::Value,
33+
) -> Result<serde_json::Value, String> {
34+
let stores = app.store("store.bin");
35+
let store = match stores {
36+
Ok(store) => store,
37+
Err(_) => return Ok(fallback),
38+
};
39+
if store.has(key.clone()) {
40+
let value = store
41+
.get(key.clone())
42+
.expect("Failed to get value from store");
43+
Ok(value)
44+
} else {
45+
Ok(fallback)
46+
}
47+
}
48+
49+
#[tauri::command]
50+
async fn set_key_to_store<R: Runtime>(
51+
app: tauri::AppHandle<R>,
52+
_window: tauri::Window<R>,
53+
key: String,
54+
value: serde_json::Value,
55+
) -> Result<(), String> {
56+
let stores = app.store("store.bin");
57+
let store = match stores {
58+
Ok(store) => store,
59+
Err(_) => return Err("Failed to get store".to_string()),
60+
};
61+
store.set(key.clone(), json!(value));
62+
store.save().expect("Failed to save store");
63+
Ok(())
64+
}
65+
2466
fn show_window(app: &AppHandle) {
2567
let windows = app.webview_windows();
2668

@@ -65,7 +107,9 @@ pub fn run() {
65107
.plugin(tauri_plugin_opener::init())
66108
.invoke_handler(tauri::generate_handler![
67109
input_text,
68-
simulate_paste
110+
simulate_paste,
111+
get_key_from_store,
112+
set_key_to_store
69113
])
70114
.run(tauri::generate_context!())
71115
.expect("error while running tauri application");

src/composables/useConfig.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { ref } from 'vue';
2-
import { Store } from '@tauri-apps/plugin-store';
2+
import { Store } from '@/libs/store';
33
import { tryParse } from '@/libs/utils';
44
import type { CoreMessage } from 'ai';
55

6+
67
export interface AIConfig {
78
enabled: boolean;
89
apiKey: string;

src/libs/bridges.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,15 @@ export async function inputText(text: string) {
77
export async function simulatePaste(){
88
return await invoke("simulate_paste");
99
}
10+
11+
export type JsonObject = { [key: string]: JsonValue };
12+
export type JsonArray = JsonValue[];
13+
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
14+
15+
export function getStoreKey(key: string, fallback: JsonValue): Promise<JsonValue> {
16+
return invoke("get_key_from_store", { key, fallback });
17+
}
18+
19+
export function setStoreKey(key: string, value: JsonValue): Promise<void> {
20+
return invoke("set_key_in_store", { key, value });
21+
}

src/libs/store.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { getStoreKey, JsonValue, setStoreKey } from "./bridges";
2+
3+
/**
4+
* StoreBackend used to move the store impl to different backend without changing usages
5+
*/
6+
export class Store {
7+
static async load(_ignored: string): Promise<Store> {
8+
return new Store();
9+
}
10+
11+
close() { }
12+
13+
save() { }
14+
15+
getRaw(key:string, fallback: JsonValue = null): Promise<JsonValue> {
16+
return getStoreKey(key, fallback);
17+
}
18+
19+
get<T>(key: string, fallback: JsonValue = null): Promise<T> {
20+
return this.getRaw(key, fallback) as Promise<T>;
21+
}
22+
23+
set(key: string, value: JsonValue = null): Promise<void> {
24+
return setStoreKey(key, value);
25+
}
26+
27+
has(key: string):Promise<boolean> {
28+
return getStoreKey(key, null).then(v => v !== null);
29+
}
30+
31+
async delete(key: string): Promise<void> {
32+
if(await this.has(key)) {
33+
return setStoreKey(key, null);
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)