mirror of
https://github.com/tauri-apps/tauri.git
synced 2026-04-03 10:11:15 +02:00
feat(cli): use plugin::Builder syntax on the plugin template (#3606)
This commit is contained in:
committed by
GitHub
parent
983ccb815b
commit
f7acb061e4
6
.changes/cli.rs-template-plugin-builder.md
Normal file
6
.changes/cli.rs-template-plugin-builder.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
"cli.rs": patch
|
||||
"cli.js": patch
|
||||
---
|
||||
|
||||
Change the `plugin init` templates to use the new `tauri::plugin::Builder` syntax.
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
|
||||
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
@@ -1,13 +1,9 @@
|
||||
{{#if license_header}}
|
||||
{{ license_header }}
|
||||
{{/if}}
|
||||
use tauri::{plugin::Plugin, Runtime};
|
||||
use tauri::{plugin::{Builder, TauriPlugin}, runtime::Runtime};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct YourPlugin {}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for YourPlugin {
|
||||
fn name(&self) -> &'static str {
|
||||
"{{ plugin_name }}"
|
||||
}
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("{{ plugin_name }}").build()
|
||||
}
|
||||
|
||||
@@ -10,5 +10,4 @@ exclude = ["/examples", "/webview-dist", "/webview-src", "node_modules"]
|
||||
[dependencies]
|
||||
tauri = {{{ tauri_dep }}}
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
thiserror = "1.0"
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::YourPlugin::default())
|
||||
.plugin(tauri_plugin_{{ plugin_name_snake_case }}::init())
|
||||
.run(tauri::generate_context!())
|
||||
.expect("failed to run app");
|
||||
}
|
||||
|
||||
@@ -3,8 +3,11 @@
|
||||
{{/if}}
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
use serde_json::Value as JsonValue;
|
||||
use tauri::{command, plugin::Plugin, AppHandle, Invoke, Manager, Runtime, State, Window};
|
||||
use tauri::{
|
||||
command,
|
||||
plugin::{Builder, TauriPlugin},
|
||||
AppHandle, Manager, Runtime, State, Window,
|
||||
};
|
||||
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
|
||||
@@ -38,30 +41,13 @@ async fn execute<R: Runtime>(
|
||||
Ok("success".to_string())
|
||||
}
|
||||
|
||||
/// Tauri plugin.
|
||||
pub struct YourPlugin<R: Runtime> {
|
||||
invoke_handler: Box<dyn Fn(Invoke<R>) + Send + Sync>,
|
||||
}
|
||||
|
||||
impl<R: Runtime> Default for YourPlugin<R> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
invoke_handler: Box::new(tauri::generate_handler![execute]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: Runtime> Plugin<R> for YourPlugin<R> {
|
||||
fn name(&self) -> &'static str {
|
||||
"{{ plugin_name }}"
|
||||
}
|
||||
|
||||
fn initialize(&mut self, app: &AppHandle<R>, _config: JsonValue) -> tauri::plugin::Result<()> {
|
||||
app.manage(MyState::default());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extend_api(&mut self, message: Invoke<R>) {
|
||||
(self.invoke_handler)(message)
|
||||
}
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("{{ plugin_name }}")
|
||||
.invoke_handler(tauri::generate_handler![execute])
|
||||
.setup(|app| {
|
||||
app.manage(MyState::default());
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user