diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index b809569..7cb4db4 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -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) -> Builder { // 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.add_key_val(key, value); +} + +// Another example command using managed state +#[tauri::command] +#[specta::specta] +fn store_read_key(key: String, store: State) -> Option { + store.read_key(&key) +} diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index ff201b8..983b6ca 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -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"); + } + _ => {} + }) } diff --git a/src-tauri/src/state.rs b/src-tauri/src/state.rs new file mode 100644 index 0000000..5492b7b --- /dev/null +++ b/src-tauri/src/state.rs @@ -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>, +} +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 { + 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) -> Builder { + let store = Store { + store: Mutex::from(HashMap::new()), + }; + + builder.manage(store) +} diff --git a/src/lib/layout/BaseHeader.svelte b/src/lib/layout/BaseHeader.svelte index 1ead1bb..aae3c94 100644 --- a/src/lib/layout/BaseHeader.svelte +++ b/src/lib/layout/BaseHeader.svelte @@ -26,11 +26,8 @@
  • - - Call Tauri + + IPC
  • diff --git a/src/lib/router/Router.svelte b/src/lib/router/Router.svelte index 96990d6..ba3f85a 100644 --- a/src/lib/router/Router.svelte +++ b/src/lib/router/Router.svelte @@ -2,12 +2,12 @@ import Router from 'svelte-spa-router'; import Index from '$lib/router/routes/Index.svelte'; - import CallTauri from '$router/routes/CallTauri.svelte'; + import IPC from '$lib/router/routes/IPC.svelte'; import Versions from '$router/routes/Versions.svelte'; const routes = { '/': Index, - '/#call_tauri': CallTauri, + '/#ipc': IPC, '/#versions': Versions }; diff --git a/src/lib/router/routes/CallTauri.svelte b/src/lib/router/routes/CallTauri.svelte deleted file mode 100644 index 4aa8871..0000000 --- a/src/lib/router/routes/CallTauri.svelte +++ /dev/null @@ -1,47 +0,0 @@ - - -
    -
    - -
    - {#key message} -

    - {message} -

    - {/key} -
    -
    -
    - -

    {hashOutput}

    -
    -
    diff --git a/src/lib/router/routes/IPC.svelte b/src/lib/router/routes/IPC.svelte new file mode 100644 index 0000000..67b984e --- /dev/null +++ b/src/lib/router/routes/IPC.svelte @@ -0,0 +1,92 @@ + + +
    +
    + +
    + {#key message} +

    + {message} +

    + {/key} +
    +
    +
    + +

    {hashOutput}

    +
    + +
    + + + + + +

    + {storeMessage} +

    +
    +