This commit is contained in:
Fractal-Tess
2023-05-19 03:58:42 +03:00
parent 175c51b653
commit 5b53d2989d
3 changed files with 18 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
#![allow(clippy::needless_pass_by_value)]
// This module shows examples of how to use IPC command handlers that can be invoked from the frontend. // This module shows examples of how to use IPC command handlers that can be invoked from the frontend.
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
use specta::collect_types; use specta::collect_types;
use tauri::{Builder, State, Wry}; use tauri::{Builder, State, Wry};
@@ -8,7 +8,7 @@ use tauri_specta::ts;
use crate::state::Store; use crate::state::Store;
// Exports a function for the tauri app instance to use and register all commands defined as frontend IPC command handlers. // 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> { pub fn register_command_handlers(builder: Builder<Wry>) -> Builder<Wry> {
// Specta generating typed binding interfaces // Specta generating typed binding interfaces
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
ts::export( ts::export(
@@ -39,7 +39,7 @@ fn hash256sum(hash_input: String) -> String {
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();
hasher.update(hash_input.as_bytes()); hasher.update(hash_input.as_bytes());
let result = hasher.finalize(); let result = hasher.finalize();
format!("{:X}", result) format!("{result:X}")
} }
// Example command using managed state // Example command using managed state

View File

@@ -3,7 +3,7 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use commands::register_commands; use commands::register_command_handlers;
use state::register_managed_state; use state::register_managed_state;
use tauri::{Builder as TauriBuilder, RunEvent}; use tauri::{Builder as TauriBuilder, RunEvent};
@@ -17,7 +17,7 @@ fn main() {
let app = TauriBuilder::default().plugin(tauri_plugin_window_state::Builder::default().build()); let app = TauriBuilder::default().plugin(tauri_plugin_window_state::Builder::default().build());
// Register app commands // Register app commands
let app = register_commands(app); let app = register_command_handlers(app);
// Register app managed state // Register app managed state
let app = register_managed_state(app); let app = register_managed_state(app);
@@ -26,8 +26,8 @@ fn main() {
app.build(tauri::generate_context!()) app.build(tauri::generate_context!())
.expect("error while running tauri application") .expect("error while running tauri application")
.run(|_, e| { .run(|_, e| {
if let RunEvent::Ready = e { if matches!(e, RunEvent::Ready) {
println!("Window is ready"); println!("Window is ready");
} }
}) });
} }

View File

@@ -1,9 +1,18 @@
// This module shows examples of how to use managed custom state. // This module shows examples of how to use managed custom state.
use std::{collections::HashMap, sync::Mutex}; use std::collections::HashMap;
use std::string::ToString;
use std::sync::Mutex;
use tauri::{Builder, Wry}; use tauri::{Builder, Wry};
// 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::default();
builder.manage(store)
}
#[derive(Default)] #[derive(Default)]
pub struct Store { pub struct Store {
store: Mutex<HashMap<String, String>>, store: Mutex<HashMap<String, String>>,
@@ -20,13 +29,6 @@ impl Store {
.lock() .lock()
.expect("cannot lock store") .expect("cannot lock store")
.get(key) .get(key)
.map(|val| val.to_string()) .map(ToString::to_string)
} }
} }
// 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::default();
builder.manage(store)
}