add more examples

This commit is contained in:
Fractal-Tess
2023-05-18 10:44:36 +03:00
parent a177cf16c2
commit e5bb812718
7 changed files with 174 additions and 71 deletions

View File

@@ -1,19 +1,28 @@
// This module shows examples of how to use IPC command handlers that can be invoked from the frontend.
use sha2::{Digest, Sha256};
use specta::collect_types;
use tauri::{Builder, Wry};
use tauri::{Builder, State, Wry};
use tauri_specta::ts;
use crate::state::Store;
// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers.
pub fn register_commands(builder: Builder<Wry>) -> Builder<Wry> {
// Specta generating typed binding interfaces
#[cfg(debug_assertions)]
ts::export(
collect_types![hello_tauri, hash256sum],
"../src/lib/bindings.ts",
collect_types![hello_tauri, hash256sum, store_set_key, store_read_key],
"../src/lib/ipc.ts",
)
.expect("unable to generate specta types");
builder.invoke_handler(tauri::generate_handler![hash256sum, hello_tauri])
builder.invoke_handler(tauri::generate_handler![
hash256sum,
hello_tauri,
store_set_key,
store_read_key
])
}
// An example command
@@ -32,3 +41,17 @@ fn hash256sum(hash_input: String) -> String {
let result = hasher.finalize();
format!("{:X}", result)
}
// Example command using managed state
#[tauri::command]
#[specta::specta]
fn store_set_key(key: String, value: String, store: State<Store>) -> () {
store.add_key_val(key, value);
}
// Another example command using managed state
#[tauri::command]
#[specta::specta]
fn store_read_key(key: String, store: State<Store>) -> Option<String> {
store.read_key(&key)
}

View File

@@ -4,25 +4,31 @@
)]
use commands::register_commands;
use prelude::*;
use state::register_managed_state;
use tauri::{Builder as TauriBuilder, RunEvent};
use tauri::RunEvent;
mod commands;
mod error;
mod prelude;
mod state;
fn main() {
let app =
tauri::Builder::default().plugin(tauri_plugin_window_state::Builder::default().build());
// App builder
let app = TauriBuilder::default().plugin(tauri_plugin_window_state::Builder::default().build());
let app = register_commands(app)
.build(tauri::generate_context!())
.expect("error while running tauri application");
// Register app commands
let app = register_commands(app);
app.run(|_, e| match e {
RunEvent::Ready => {
println!("Window is ready");
}
_ => {}
})
// Register app managed state
let app = register_managed_state(app);
// Run the app
app.build(tauri::generate_context!())
.expect("error while running tauri application")
.run(|_, e| match e {
RunEvent::Ready => {
println!("Window is ready");
}
_ => {}
})
}

32
src-tauri/src/state.rs Normal file
View File

@@ -0,0 +1,32 @@
// This module shows examples of how to use managed custom state.
use std::{collections::HashMap, sync::Mutex};
use tauri::{Builder, Wry};
pub struct Store {
store: Mutex<HashMap<String, String>>,
}
impl Store {
pub fn add_key_val(&self, key: String, val: String) {
self.store
.lock()
.expect("cannot lock store")
.insert(key, val);
}
pub fn read_key(&self, key: &String) -> Option<String> {
match self.store.lock().expect("cannot lock store").get(key) {
Some(s) => Some(s.to_string()),
None => None,
}
}
}
// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers.
pub fn register_managed_state(builder: Builder<Wry>) -> Builder<Wry> {
let store = Store {
store: Mutex::from(HashMap::new()),
};
builder.manage(store)
}