Merge remote-tracking branch 'origin/v1' into chore/merge-v1-into-v2

This commit is contained in:
FabianLars
2024-04-15 18:23:45 +02:00
65 changed files with 1075 additions and 371 deletions
+4
View File
@@ -19,6 +19,7 @@ serde_json = { workspace = true }
tauri = { workspace = true }
log = { workspace = true }
thiserror = { workspace = true }
semver = { version = "1", optional = true }
[target."cfg(target_os = \"windows\")".dependencies.windows-sys]
version = "0.52"
@@ -34,3 +35,6 @@ features = [
[target."cfg(target_os = \"linux\")".dependencies]
zbus = "4"
[features]
semver = ["dep:semver"]
@@ -8,8 +8,8 @@ edition = "2021"
rust-version = "1.75"
[dependencies]
serde_json = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tauri = { workspace = true }
tauri-plugin-single-instance = { path = "../../../" }
tauri-plugin-cli = { path = "../../../../cli" }
+3
View File
@@ -24,6 +24,9 @@ mod platform_impl;
#[path = "platform_impl/macos.rs"]
mod platform_impl;
#[cfg(feature = "semver")]
mod semver_compat;
pub(crate) type SingleInstanceCallback<R> =
dyn FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sync + 'static;
@@ -4,6 +4,9 @@
#![cfg(target_os = "linux")]
#[cfg(feature = "semver")]
use crate::semver_compat::semver_compat_string;
use crate::SingleInstanceCallback;
use tauri::{
plugin::{self, TauriPlugin},
@@ -28,6 +31,15 @@ impl<R: Runtime> SingleInstanceDBus<R> {
}
}
#[cfg(feature = "semver")]
fn dbus_id(config: &Config, version: semver::Version) -> String {
let mut id = config.identifier.replace(['.', '-'], "_");
id.push('_');
id.push_str(semver_compat_string(version).as_str());
id
}
#[cfg(not(feature = "semver"))]
fn dbus_id(config: &Config) -> String {
config.identifier.replace(['.', '-'], "_")
}
@@ -35,7 +47,11 @@ fn dbus_id(config: &Config) -> String {
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app, _api| {
#[cfg(feature = "semver")]
let id = dbus_id(app.config(), app.package_info().version.clone());
#[cfg(not(feature = "semver"))]
let id = dbus_id(app.config());
let single_instance_dbus = SingleInstanceDBus {
callback: f,
app_handle: app.clone(),
@@ -88,7 +104,15 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
if let Some(connection) = manager.try_state::<ConnectionHandle>() {
let dbus_name = format!("org.{}.SingleInstance", dbus_id(manager.config()));
#[cfg(feature = "semver")]
let id = dbus_id(
manager.config(),
manager.app_handle().package_info().version.clone(),
);
#[cfg(not(feature = "semver"))]
let id = dbus_id(manager.config());
let dbus_name = format!("org.{id}.SingleInstance",);
let _ = connection.0.release_name(dbus_name);
}
}
@@ -10,6 +10,8 @@ use std::{
path::PathBuf,
};
#[cfg(feature = "semver")]
use crate::semver_compat::semver_compat_string;
use crate::SingleInstanceCallback;
use tauri::{
plugin::{self, TauriPlugin},
@@ -19,7 +21,7 @@ use tauri::{
pub fn init<R: Runtime>(cb: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app, _api| {
let socket = socket_path(app.config());
let socket = socket_path(app.config(), app.package_info());
// Notify the singleton which may or may not exist.
match notify_singleton(&socket) {
@@ -53,12 +55,19 @@ pub fn init<R: Runtime>(cb: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
}
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
let socket = socket_path(manager.config());
let socket = socket_path(manager.config(), manager.package_info());
socket_cleanup(&socket);
}
fn socket_path(config: &Config) -> PathBuf {
fn socket_path(config: &Config, _package_info: &tauri::PackageInfo) -> PathBuf {
let identifier = config.identifier.replace(['.', '-'].as_ref(), "_");
#[cfg(feature = "semver")]
let identifier = format!(
"{identifier}_{}",
semver_compat_string(_package_info.version.clone()),
);
// Use /tmp as socket path must be shorter than 100 chars.
PathBuf::from(format!("/tmp/{}_si.sock", identifier))
}
@@ -4,6 +4,9 @@
#![cfg(target_os = "windows")]
#[cfg(feature = "semver")]
use crate::semver_compat::semver_compat_string;
use crate::SingleInstanceCallback;
use std::ffi::CStr;
use tauri::{
@@ -33,7 +36,13 @@ const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app, _api| {
let id = &app.config().identifier;
#[allow(unused_mut)]
let mut id = app.config().identifier.clone();
#[cfg(feature = "semver")]
{
id.push('_');
id.push_str(semver_compat_string(app.package_info().version.clone()).as_str());
}
let class_name = encode_wide(format!("{id}-sic"));
let window_name = encode_wide(format!("{id}-siw"));
@@ -0,0 +1,21 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#![cfg(feature = "semver")]
/// Takes a version and spits out a String with trailing _x, thus only considering the digits
/// relevant regarding semver compatibility
pub fn semver_compat_string(version: semver::Version) -> String {
// for pre-release always treat each version separately
if !version.pre.is_empty() {
return version.to_string().replace(['.', '-'], "_");
}
match version.major {
0 => match version.minor {
0 => format!("0_0_{}", version.patch),
_ => format!("0_{}_x", version.minor),
},
_ => format!("{}_x_x", version.major),
}
}