tauri_plugin_playwright/
lib.rs1use std::collections::HashMap;
2use std::sync::Arc;
3use tokio::sync::Mutex;
4
5use tauri::{
6 plugin::{Builder, TauriPlugin},
7 Manager, Runtime,
8};
9
10mod commands;
11mod native_capture;
12mod server;
13
14use server::PendingResults;
15
16pub fn init<R: Runtime>() -> TauriPlugin<R> {
17 init_with_config(PluginConfig::default())
18}
19
20pub fn init_with_config<R: Runtime>(config: PluginConfig) -> TauriPlugin<R> {
21 let pending: PendingResults = Arc::new(Mutex::new(HashMap::new()));
22 let pending_for_setup = Arc::clone(&pending);
23
24 Builder::new("playwright")
25 .js_init_script("window.__PW_ACTIVE__ = true;".to_string())
26 .invoke_handler(tauri::generate_handler![commands::pw_result])
27 .setup(move |app, _api| {
28 app.manage(pending_for_setup.clone());
29 server::start(
30 app.clone(),
31 Arc::clone(&pending_for_setup),
32 config.socket_path.clone(),
33 config.tcp_port,
34 config.window_label.clone(),
35 );
36 Ok(())
37 })
38 .build()
39}
40
41#[derive(Debug, Clone)]
42pub struct PluginConfig {
43 pub socket_path: Option<String>,
44 pub tcp_port: Option<u16>,
45 pub window_label: Option<String>,
46}
47
48impl Default for PluginConfig {
49 fn default() -> Self {
50 Self {
51 socket_path: Some("/tmp/tauri-playwright.sock".to_string()),
52 tcp_port: None,
53 window_label: None,
54 }
55 }
56}
57
58impl PluginConfig {
59 pub fn new() -> Self { Self::default() }
60 pub fn socket_path(mut self, path: impl Into<String>) -> Self { self.socket_path = Some(path.into()); self }
61 pub fn tcp_port(mut self, port: u16) -> Self { self.tcp_port = Some(port); self }
62 pub fn window_label(mut self, label: impl Into<String>) -> Self { self.window_label = Some(label.into()); self }
63}