Merge release v0.0.2 (#60)
* update v0.0.1 * update v0.0.1 * add rust cache * rename step * fix test workflow * change to build * add password * move app.jpeg * bump & update name * remove logo * untrack .vscode * remove icon from readme * use npx * rename file and do dom element check * formating * added rust files * generated bindings * rename to ipc * add more examples * move file * new img * refactor * add clippy and tests * imrpove clippy * add semi * enable release profile * refactor * first changeset * version changeset * version app * move version in front of tagname * do not error on local * run after build * use another workflow for clippy
This commit is contained in:
committed by
GitHub
parent
fa8b6cd215
commit
12365282a3
57
src-tauri/src/commands.rs
Normal file
57
src-tauri/src/commands.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
#![allow(clippy::needless_pass_by_value)]
|
||||
// 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, 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_command_handlers(builder: Builder<Wry>) -> Builder<Wry> {
|
||||
// Specta generating typed binding interfaces
|
||||
#[cfg(debug_assertions)]
|
||||
ts::export(
|
||||
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,
|
||||
store_set_key,
|
||||
store_read_key
|
||||
])
|
||||
}
|
||||
|
||||
// An example command
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
fn hello_tauri() -> String {
|
||||
"Hi from Tauri".to_owned()
|
||||
}
|
||||
|
||||
// Another command
|
||||
#[tauri::command]
|
||||
#[specta::specta]
|
||||
fn hash256sum(hash_input: String) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(hash_input.as_bytes());
|
||||
let result = hasher.finalize();
|
||||
format!("{result:X}")
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
10
src-tauri/src/error.rs
Normal file
10
src-tauri/src/error.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
#![allow(unused)]
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum Error {
|
||||
#[error("{0}")]
|
||||
Other(String),
|
||||
|
||||
#[error(transparent)]
|
||||
IO(#[from] std::io::Error),
|
||||
}
|
||||
@@ -3,35 +3,31 @@
|
||||
windows_subsystem = "windows"
|
||||
)]
|
||||
|
||||
use sha2::{Digest, Sha256};
|
||||
use tauri::RunEvent;
|
||||
use commands::register_command_handlers;
|
||||
use state::register_managed_state;
|
||||
use tauri::{Builder as TauriBuilder, RunEvent};
|
||||
|
||||
mod commands;
|
||||
mod error;
|
||||
mod prelude;
|
||||
mod state;
|
||||
|
||||
fn main() {
|
||||
let app = tauri::Builder::default()
|
||||
.plugin(tauri_plugin_window_state::Builder::default().build())
|
||||
.invoke_handler(tauri::generate_handler![called_from_js, hash256sum])
|
||||
.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
// App builder
|
||||
let app = TauriBuilder::default().plugin(tauri_plugin_window_state::Builder::default().build());
|
||||
|
||||
app.run(|_, e| match e {
|
||||
RunEvent::Ready => {
|
||||
println!("Window is ready");
|
||||
}
|
||||
_ => {}
|
||||
})
|
||||
}
|
||||
// Register app commands
|
||||
let app = register_command_handlers(app);
|
||||
|
||||
#[tauri::command]
|
||||
fn called_from_js() -> String {
|
||||
// The print macro is problematic in release environment (crashes the application if not ran from a terminal)
|
||||
// println!("Returning from tauri");
|
||||
"Hi from Tauri".to_owned()
|
||||
}
|
||||
// Register app managed state
|
||||
let app = register_managed_state(app);
|
||||
|
||||
#[tauri::command]
|
||||
fn hash256sum(hash_input: String) -> String {
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(hash_input.as_bytes());
|
||||
let result = hasher.finalize();
|
||||
format!("{:X}", result)
|
||||
// Run the app
|
||||
app.build(tauri::generate_context!())
|
||||
.expect("error while running tauri application")
|
||||
.run(|_, e| {
|
||||
if matches!(e, RunEvent::Ready) {
|
||||
println!("Window is ready");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
6
src-tauri/src/prelude.rs
Normal file
6
src-tauri/src/prelude.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
#![allow(unused)]
|
||||
pub use crate::error::Error;
|
||||
|
||||
pub type Result<T> = core::result::Result<T, Error>;
|
||||
|
||||
pub struct W<T>(pub T);
|
||||
34
src-tauri/src/state.rs
Normal file
34
src-tauri/src/state.rs
Normal file
@@ -0,0 +1,34 @@
|
||||
// This module shows examples of how to use managed custom state.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::string::ToString;
|
||||
use std::sync::Mutex;
|
||||
|
||||
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)]
|
||||
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> {
|
||||
self.store
|
||||
.lock()
|
||||
.expect("cannot lock store")
|
||||
.get(key)
|
||||
.map(ToString::to_string)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user