mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-04-23 11:36:13 +02:00
refactor!: always close second instances
Always close second instances and remove the ability to to close them manually we can bring it later if it is needed
This commit is contained in:
@@ -5,12 +5,9 @@
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.plugin(tauri_plugin_single_instance::init(
|
||||
|argv, cwd, close_new_instance| {
|
||||
println!("{argv:?}, {cwd}");
|
||||
close_new_instance();
|
||||
},
|
||||
))
|
||||
.plugin(tauri_plugin_single_instance::init(|argv, cwd| {
|
||||
println!("{argv:?}, {cwd}");
|
||||
}))
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
||||
+2
-6
@@ -10,13 +10,9 @@ mod platform_impl;
|
||||
#[path = "platform_impl/macos.rs"]
|
||||
mod platform_impl;
|
||||
|
||||
pub(crate) type SingleInstanceCallback =
|
||||
dyn FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + Sync + 'static;
|
||||
pub(crate) type SingleInstanceCallback = dyn FnMut(Vec<String>, String) + Send + Sync + 'static;
|
||||
|
||||
pub fn init<
|
||||
R: Runtime,
|
||||
F: FnMut(Vec<String>, String, Box<dyn FnOnce()>) + Send + Sync + 'static,
|
||||
>(
|
||||
pub fn init<R: Runtime, F: FnMut(Vec<String>, String) + Send + Sync + 'static>(
|
||||
f: F,
|
||||
) -> TauriPlugin<R> {
|
||||
platform_impl::init(Box::new(f))
|
||||
|
||||
+17
-33
@@ -1,7 +1,6 @@
|
||||
#![cfg(target_os = "linux")]
|
||||
|
||||
use crate::SingleInstanceCallback;
|
||||
use std::{cell::RefCell, rc::Rc};
|
||||
use tauri::{
|
||||
plugin::{self, TauriPlugin},
|
||||
Manager, RunEvent, Runtime,
|
||||
@@ -12,7 +11,6 @@ use zbus::{
|
||||
};
|
||||
|
||||
struct ConnectionHandle(Connection);
|
||||
const CLOSE_NEW_INSTANCE_ID: u32 = 1542;
|
||||
|
||||
struct SingleInstanceDBus {
|
||||
callback: Box<SingleInstanceCallback>,
|
||||
@@ -20,19 +18,8 @@ struct SingleInstanceDBus {
|
||||
|
||||
#[dbus_interface(name = "org.SingleInstance.DBus")]
|
||||
impl SingleInstanceDBus {
|
||||
fn execute_callback(&mut self, argv: Vec<String>, cwd: String) -> u32 {
|
||||
let ret = Rc::new(RefCell::new(1));
|
||||
let ret_c = Rc::clone(&ret);
|
||||
|
||||
(self.callback)(
|
||||
argv,
|
||||
cwd,
|
||||
Box::new(move || {
|
||||
let mut ret = ret_c.borrow_mut();
|
||||
*ret = CLOSE_NEW_INSTANCE_ID;
|
||||
}),
|
||||
);
|
||||
ret.take()
|
||||
fn execute_callback(&mut self, argv: Vec<String>, cwd: String) {
|
||||
(self.callback)(argv, cwd);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,25 +43,22 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
|
||||
app.manage(ConnectionHandle(connection));
|
||||
}
|
||||
Err(zbus::Error::NameTaken) => {
|
||||
let connection = Connection::session().unwrap();
|
||||
if let Ok(m) = connection.call_method(
|
||||
Some(dbus_name.as_str()),
|
||||
dbus_path.as_str(),
|
||||
Some("org.SingleInstance.DBus"),
|
||||
"ExecuteCallback",
|
||||
&(
|
||||
std::env::args().collect::<Vec<String>>(),
|
||||
std::env::current_dir()
|
||||
.unwrap_or_default()
|
||||
.to_str()
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
) {
|
||||
let reply: u32 = m.body().unwrap_or_default();
|
||||
if reply == CLOSE_NEW_INSTANCE_ID {
|
||||
std::process::exit(0);
|
||||
}
|
||||
if let Ok(connection) = Connection::session() {
|
||||
let _ = connection.call_method(
|
||||
Some(dbus_name.as_str()),
|
||||
dbus_path.as_str(),
|
||||
Some("org.SingleInstance.DBus"),
|
||||
"ExecuteCallback",
|
||||
&(
|
||||
std::env::args().collect::<Vec<String>>(),
|
||||
std::env::current_dir()
|
||||
.unwrap_or_default()
|
||||
.to_str()
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
);
|
||||
}
|
||||
std::process::exit(0)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#![cfg(target_os = "windows")]
|
||||
|
||||
use crate::SingleInstanceCallback;
|
||||
use std::{cell::RefCell, ffi::CStr, rc::Rc};
|
||||
use std::ffi::CStr;
|
||||
use tauri::{
|
||||
plugin::{self, TauriPlugin},
|
||||
Manager, RunEvent, Runtime,
|
||||
@@ -25,7 +25,6 @@ struct MutexHandle(isize);
|
||||
struct TargetWindowHandle(isize);
|
||||
|
||||
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
|
||||
const CLOSE_NEW_INSTANCE_ID: isize = 1542;
|
||||
|
||||
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
|
||||
plugin::Builder::new("single-instance")
|
||||
@@ -64,10 +63,8 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
|
||||
cbData: bytes.len() as _,
|
||||
lpData: bytes.as_ptr() as _,
|
||||
};
|
||||
let ret = SendMessageW(hwnd, WM_COPYDATA, 0, &cds as *const _ as _);
|
||||
if ret == CLOSE_NEW_INSTANCE_ID {
|
||||
std::process::exit(0);
|
||||
}
|
||||
SendMessageW(hwnd, WM_COPYDATA, 0, &cds as *const _ as _);
|
||||
std::process::exit(0);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
@@ -107,25 +104,15 @@ unsafe extern "system" fn single_instance_window_proc(
|
||||
|
||||
match msg {
|
||||
WM_COPYDATA => {
|
||||
let ret = Rc::new(RefCell::new(1));
|
||||
|
||||
let cds_ptr = lparam as *const COPYDATASTRUCT;
|
||||
if (*cds_ptr).dwData == WMCOPYDATA_SINGLE_INSTANCE_DATA {
|
||||
let data = CStr::from_ptr((*cds_ptr).lpData as _).to_string_lossy();
|
||||
let mut s = data.split("|");
|
||||
let cwd = s.next().unwrap();
|
||||
let args = s.into_iter().map(|s| s.to_string()).collect();
|
||||
let ret_c = Rc::clone(&ret);
|
||||
(*callback_ptr)(
|
||||
args,
|
||||
cwd.to_string(),
|
||||
Box::new(move || {
|
||||
let mut ret = ret_c.borrow_mut();
|
||||
*ret = CLOSE_NEW_INSTANCE_ID;
|
||||
}),
|
||||
);
|
||||
(*callback_ptr)(args, cwd.to_string());
|
||||
}
|
||||
ret.take()
|
||||
1
|
||||
}
|
||||
|
||||
WM_DESTROY => {
|
||||
|
||||
Reference in New Issue
Block a user