tauri-plugin-single-window 1.2.0

Desktop-only Tauri plugin that prevents duplicate app launches and redirects activation to the existing instance.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use std::{
    fs,
    io::{self, BufRead, BufReader, IsTerminal, Write},
    path::{Path, PathBuf},
    thread,
    time::Duration,
};

use interprocess::local_socket::{
    prelude::*, GenericFilePath, GenericNamespaced, ListenerOptions, Name, Stream,
};
use serde::de::DeserializeOwned;
use sysinfo::{get_current_pid, Pid, System};
use tauri::{plugin::PluginApi, AppHandle, Manager, Runtime};

const ACTIVATION_RETRY_COUNT: usize = 10;
const ACTIVATION_RETRY_DELAY: Duration = Duration::from_millis(50);

pub fn init<R: Runtime, C: DeserializeOwned>(
    app: &AppHandle<R>,
    _api: PluginApi<R, C>,
    config: &crate::Config,
    callback: Option<crate::ActivationCallback<R>>,
) -> crate::Result<SingleInstance<R>> {
    let process_name = app.package_info().name.clone();
    let pid_file_path = pid_file_path(&process_name);
    let current_exe = std::env::current_exe().ok();
    let activation_payload = current_activation_payload(&process_name);
    cleanup_stale_pid_file(&pid_file_path, current_exe.as_deref());
    let target_window_label = config.target_window_label.clone();
    let focus_existing_window = config.focus_existing_window;

    let listener = match bind_listener(&process_name) {
        Ok(listener) => listener,
        Err(error) if error.kind() == io::ErrorKind::AddrInUse => {
            let recorded_pid = read_primary_pid(&pid_file_path);
            let duplicate_pid =
                recorded_pid.filter(|pid| is_primary_process(*pid, current_exe.as_deref()));
            let notified_primary =
                notify_primary_instance(&process_name, &activation_payload).is_ok();
            let notice_pid = duplicate_pid.or(if notified_primary { recorded_pid } else { None });

            if duplicate_pid.is_some() || notified_primary {
                print_duplicate_notice(
                    &process_name,
                    notice_pid,
                    config.duplicate_notice.as_deref(),
                );
                terminate_duplicate_process(app);
            }

            return Err(error.into());
        }
        Err(error) => return Err(error.into()),
    };

    write_primary_pid(&pid_file_path)?;
    spawn_activation_listener(
        app.clone(),
        listener,
        focus_existing_window,
        target_window_label.clone(),
        callback.clone(),
    );

    Ok(SingleInstance {
        app: app.clone(),
        pid_file_path,
        process_name,
        focus_existing_window,
        target_window_label,
        callback,
    })
}

/// Access to the single-instance APIs.
pub struct SingleInstance<R: Runtime> {
    app: AppHandle<R>,
    pid_file_path: PathBuf,
    process_name: String,
    focus_existing_window: bool,
    target_window_label: Option<String>,
    callback: Option<crate::ActivationCallback<R>>,
}

impl<R: Runtime> Drop for SingleInstance<R> {
    fn drop(&mut self) {
        let current_pid = std::process::id();
        let recorded_pid = read_primary_pid(&self.pid_file_path).map(Pid::as_u32);

        if recorded_pid == Some(current_pid) {
            let _ = fs::remove_file(&self.pid_file_path);
        }
    }
}

impl<R: Runtime> SingleInstance<R> {
    pub(crate) fn handle_remote_activation(&self) {
        focus_existing_window(
            &self.app,
            self.focus_existing_window,
            self.target_window_label.as_deref(),
            Some(remote_activation_payload(&self.process_name)),
            self.callback.clone(),
        );
    }
}

fn bind_listener(process_name: &str) -> io::Result<interprocess::local_socket::Listener> {
    ListenerOptions::new()
        .name(socket_name(process_name)?)
        .try_overwrite(true)
        .create_sync()
}

fn notify_primary_instance(
    process_name: &str,
    payload: &crate::ActivationPayload,
) -> io::Result<()> {
    let name = socket_name(process_name)?;
    let mut last_error = None;
    let body = serde_json::to_string(payload)
        .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;

    for _ in 0..ACTIVATION_RETRY_COUNT {
        match Stream::connect(name.borrow()) {
            Ok(mut stream) => {
                stream.write_all(body.as_bytes())?;
                stream.write_all(b"\n")?;
                stream.flush()?;
                return Ok(());
            }
            Err(error) => {
                last_error = Some(error);
                thread::sleep(ACTIVATION_RETRY_DELAY);
            }
        }
    }

    Err(last_error.unwrap_or_else(|| io::Error::other("failed to connect to primary instance")))
}

fn spawn_activation_listener<R: Runtime>(
    app: AppHandle<R>,
    listener: interprocess::local_socket::Listener,
    should_focus_window: bool,
    target_window_label: Option<String>,
    callback: Option<crate::ActivationCallback<R>>,
) {
    thread::spawn(move || {
        for connection in listener.incoming() {
            match connection {
                Ok(stream) => {
                    let payload = read_activation_payload(stream);
                    focus_existing_window(
                        &app,
                        should_focus_window,
                        target_window_label.as_deref(),
                        payload,
                        callback.clone(),
                    );
                }
                Err(_) => continue,
            }
        }
    });
}

fn focus_existing_window<R: Runtime>(
    app: &AppHandle<R>,
    should_focus_window: bool,
    target_window_label: Option<&str>,
    payload: Option<crate::ActivationPayload>,
    callback: Option<crate::ActivationCallback<R>>,
) {
    let app = app.clone();
    let app_handle = app.clone();
    let target_window_label = target_window_label.map(str::to_owned);
    let _ = app.run_on_main_thread(move || {
        #[cfg(target_os = "macos")]
        let _ = app_handle.show();

        if should_focus_window {
            if let Some(window) = find_target_window(&app_handle, target_window_label.as_deref()) {
                let _ = window.unminimize();
                let _ = window.show();
                let _ = window.set_focus();
            }
        }

        if let (Some(payload), Some(callback)) = (payload, callback) {
            callback(app_handle.clone(), payload);
        }
    });
}

fn read_activation_payload(
    stream: interprocess::local_socket::Stream,
) -> Option<crate::ActivationPayload> {
    let mut reader = BufReader::new(stream);
    let mut payload = String::new();

    if reader.read_line(&mut payload).ok()? == 0 {
        return None;
    }

    parse_activation_payload(payload.trim())
}

fn is_primary_process(pid: Pid, current_exe: Option<&Path>) -> bool {
    let mut system = System::new_all();
    system.refresh_all();

    let current_pid = match get_current_pid() {
        Ok(pid) => pid,
        Err(_) => return false,
    };

    if pid == current_pid {
        return false;
    }

    let Some(process) = system.process(pid) else {
        return false;
    };

    match (current_exe, process.exe()) {
        (Some(current_exe), Some(process_exe)) => same_executable(current_exe, process_exe),
        _ => true,
    }
}

fn same_executable(current_exe: &Path, process_exe: &Path) -> bool {
    if process_exe == current_exe {
        return true;
    }

    let canonical_current = fs::canonicalize(current_exe).ok();
    let canonical_process = fs::canonicalize(process_exe).ok();

    if canonical_current.is_some() && canonical_current == canonical_process {
        return true;
    }

    current_exe.file_name() == process_exe.file_name()
}

fn normalized_process_name(name: &str) -> String {
    name.trim_end_matches(".exe")
        .replace('_', "-")
        .to_ascii_lowercase()
}

fn socket_name(process_name: &str) -> io::Result<Name<'static>> {
    let endpoint = format!(
        "tauri-plugin-single-window-{}",
        normalized_process_name(process_name)
    );

    if GenericNamespaced::is_supported() {
        endpoint
            .to_ns_name::<GenericNamespaced>()
            .map(Name::into_owned)
    } else {
        socket_path(&endpoint)?
            .to_fs_name::<GenericFilePath>()
            .map(Name::into_owned)
    }
}

fn socket_path(endpoint: &str) -> io::Result<PathBuf> {
    let mut path = std::env::temp_dir();
    path.push(format!("{endpoint}.sock"));
    Ok(path)
}

fn pid_file_path(process_name: &str) -> PathBuf {
    let mut path = std::env::temp_dir();
    path.push(format!(
        "tauri-plugin-single-window-{}.pid",
        normalized_process_name(process_name)
    ));
    path
}

fn read_primary_pid(path: &Path) -> Option<Pid> {
    fs::read_to_string(path).ok()?.trim().parse().ok()
}

fn write_primary_pid(path: &Path) -> io::Result<()> {
    fs::write(path, std::process::id().to_string())
}

fn cleanup_stale_pid_file(path: &Path, current_exe: Option<&Path>) {
    let contents = match fs::read_to_string(path) {
        Ok(contents) => contents,
        Err(error) if error.kind() == io::ErrorKind::NotFound => return,
        Err(error) => {
            print_cleanup_warning(path, &format!("failed to read PID file: {error}"));
            return;
        }
    };

    if let Some(reason) = stale_pid_reason(contents.trim(), current_exe) {
        remove_stale_pid_file(path, reason);
    }
}

fn remove_stale_pid_file(path: &Path, reason: &str) {
    if let Err(error) = fs::remove_file(path) {
        if error.kind() != io::ErrorKind::NotFound {
            print_cleanup_warning(
                path,
                &format!("failed to delete stale PID file after cleanup ({reason}): {error}"),
            );
        }
    }
}

fn print_cleanup_warning(path: &Path, message: &str) {
    if std::io::stderr().is_terminal() {
        eprintln!("Warning: {message}: {}", path.display());
    }
}

fn stale_pid_reason(contents: &str, current_exe: Option<&Path>) -> Option<&'static str> {
    match contents.parse::<Pid>() {
        Ok(pid) if is_primary_process(pid, current_exe) => None,
        Ok(_) => Some("PID file pointed to a non-primary process"),
        Err(_) => Some("PID file contained invalid data"),
    }
}

fn current_activation_payload(process_name: &str) -> crate::ActivationPayload {
    crate::ActivationPayload {
        process_name: process_name.to_string(),
        pid: std::process::id(),
        argv: std::env::args_os()
            .map(|arg| arg.to_string_lossy().into_owned())
            .collect(),
        cwd: std::env::current_dir()
            .ok()
            .map(|path| path.to_string_lossy().into_owned()),
    }
}

fn remote_activation_payload(process_name: &str) -> crate::ActivationPayload {
    crate::ActivationPayload {
        process_name: process_name.to_string(),
        pid: std::process::id(),
        argv: Vec::new(),
        cwd: None,
    }
}

fn parse_activation_payload(payload: &str) -> Option<crate::ActivationPayload> {
    serde_json::from_str(payload).ok()
}

fn find_target_window<R: Runtime>(
    app: &AppHandle<R>,
    target_window_label: Option<&str>,
) -> Option<tauri::WebviewWindow<R>> {
    let preferred_label = target_window_label.unwrap_or("main");

    app.get_webview_window(preferred_label)
        .or_else(|| {
            if target_window_label.is_some() && preferred_label != "main" {
                app.get_webview_window("main")
            } else {
                None
            }
        })
        .or_else(|| app.webview_windows().into_values().next())
}

fn terminate_duplicate_process<R: Runtime>(app: &AppHandle<R>) -> ! {
    app.cleanup_before_exit();
    std::process::exit(0);
}

fn print_duplicate_notice(process_name: &str, pid: Option<Pid>, template: Option<&str>) {
    let message = render_duplicate_notice(process_name, pid, template);

    if io::stderr().is_terminal() {
        eprintln!("{message}");
    } else if io::stdout().is_terminal() {
        println!("{message}");
    }
}

fn render_duplicate_notice(process_name: &str, pid: Option<Pid>, template: Option<&str>) -> String {
    let pid_text = pid.map(|pid| pid.to_string()).unwrap_or_default();

    if let Some(template) = template {
        return template
            .replace("{{proc}}", process_name)
            .replace("{{pid}}", &pid_text);
    }

    if let Some(pid) = pid {
        format!(
            "Detected existing process '{process_name}' (pid {pid}): focusing the existing window and exiting."
        )
    } else {
        format!(
            "Detected existing process `{process_name}`: focusing the existing window and exiting."
        )
    }
}

#[cfg(test)]
mod tests;