Tauri is a popular framework for building cross-platform desktop applications using web technologies. To help developers get the most out of Tauri, a cheatsheet has been created, gathering essential commands, events, permissions, and state management techniques in one place. This resource aims to simplify the development process, making it easier for developers to focus on creating high-quality applications.
Tauri Commands
Tauri commands enable seamless communication between the Rust backend and the frontend of an application. These commands can be defined using the #[tauri::command] macro and can handle various tasks, such as greeting a user, reading files, or fetching data. For example, #[tauri::command] fn greet(name: String) -> String { format!("Hello, {}!", name) } defines a command that takes a name as input and returns a greeting message. To call this command from the frontend, developers can use the invoke function from the @tauri-apps/api/core module, passing the command name and any required arguments.
Events
Events in Tauri facilitate bidirectional communication between the Rust backend and the frontend. The window.emit function can be used to send events from the Rust side to the frontend, while the listen function from the @tauri-apps/api/event module allows the frontend to listen for events emitted by the Rust backend. For instance, window.emit("my-event", serde_json::json!({"key": "value"})).unwrap(); sends an event named "my-event" with a JSON payload to the frontend, which can then be handled using the listen function.
State Management
Effective state management is crucial in Tauri applications. The tauri::State trait provides a way to manage application state, allowing developers to define and register custom state structs. For example, pub struct AppState { pub counter: Mutex, } defines a state struct with a counter field, which can then be registered using the manage method of the tauri::Builder. The state can be accessed and modified within commands using the tauri::State trait.
Permissions and Capabilities
Tauri v2 introduces a new permissions system based on capabilities. The src-tauri/capabilities/main.json file defines the capabilities and permissions for an application. For instance, { "identifier": "main-capability", "windows": ["main"], "permissions": [ "core:default", "shell:allow-execute", "shell:allow-stdin", "fs:allow-read-files", "fs:allow-write-files", "fs:allow-app-cache-write", "dialog:allow-open", "dialog:allow-save", "notification:default" ] } specifies the permissions for the main window of an application.
Conclusion
The Tauri cheatsheet provides a valuable resource for developers looking to streamline their application development workflow. By mastering Tauri commands, events, permissions, and state management, developers can create high-quality, cross-platform desktop applications with ease. As the Tauri ecosystem continues to evolve, staying up-to-date with the latest developments and best practices will be essential for building successful applications.
AI summary
Tauri v2 uygulamaları oluştururken kullanılan komutlar, olaylar, izinler ve durum yönetimi hakkında bilgi veren bir kılavuz.