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