feat: add function to stop checking for single instance (#13)

pull/72/head
Lucas Fernandes Nogueira 3 years ago committed by GitHub
parent 2dfc09139c
commit 50a7472bbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,4 +1,4 @@
use tauri::{plugin::TauriPlugin, AppHandle, Runtime};
use tauri::{plugin::TauriPlugin, AppHandle, Manager, Runtime};
#[cfg(target_os = "windows")]
#[path = "platform_impl/windows.rs"]
@ -18,3 +18,7 @@ pub fn init<R: Runtime, F: FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sy
) -> TauriPlugin<R> {
platform_impl::init(Box::new(f))
}
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
platform_impl::destroy(manager)
}

@ -1,9 +1,11 @@
#![cfg(target_os = "linux")]
use std::sync::Arc;
use crate::SingleInstanceCallback;
use tauri::{
plugin::{self, TauriPlugin},
AppHandle, Manager, RunEvent, Runtime,
AppHandle, Config, Manager, RunEvent, Runtime,
};
use zbus::{
blocking::{Connection, ConnectionBuilder},
@ -24,8 +26,8 @@ impl<R: Runtime> SingleInstanceDBus<R> {
}
}
fn dbus_id<R: Runtime>(app: &AppHandle<R>) -> String {
app.config()
fn dbus_id(config: Arc<Config>) -> String {
config
.tauri
.bundle
.identifier
@ -36,7 +38,7 @@ fn dbus_id<R: Runtime>(app: &AppHandle<R>) -> String {
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance")
.setup(|app| {
let id = dbus_id(app);
let id = dbus_id(app.config());
let single_instance_dbus = SingleInstanceDBus {
callback: f,
app_handle: app.clone(),
@ -80,11 +82,15 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
})
.on_event(|app, event| {
if let RunEvent::Exit = event {
if let Some(connection) = app.try_state::<ConnectionHandle>() {
let dbus_name = format!("org.{}.SingleInstance", dbus_id(app));
let _ = connection.0.release_name(dbus_name);
}
destroy(app);
}
})
.build()
}
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()));
let _ = connection.0.release_name(dbus_name);
}
}

@ -3,8 +3,10 @@
use crate::SingleInstanceCallback;
use tauri::{
plugin::{self, TauriPlugin},
Runtime,
Manager, Runtime,
};
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
plugin::Builder::new("single-instance").build()
}
pub fn destroy<R: Runtime, M: Manager<R>>(_manager: &M) {}

@ -84,20 +84,24 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback<R>>) -> TauriPlugin<R> {
})
.on_event(|app, event| {
if let RunEvent::Exit = event {
if let Some(hmutex) = app.try_state::<MutexHandle>() {
unsafe {
ReleaseMutex(hmutex.0);
CloseHandle(hmutex.0);
}
}
if let Some(hwnd) = app.try_state::<TargetWindowHandle>() {
unsafe { DestroyWindow(hwnd.0) };
}
destroy(app);
}
})
.build()
}
pub fn destroy<R: Runtime, M: Manager<R>>(manager: &M) {
if let Some(hmutex) = manager.try_state::<MutexHandle>() {
unsafe {
ReleaseMutex(hmutex.0);
CloseHandle(hmutex.0);
}
}
if let Some(hwnd) = manager.try_state::<TargetWindowHandle>() {
unsafe { DestroyWindow(hwnd.0) };
}
}
unsafe extern "system" fn single_instance_window_proc<R: Runtime>(
hwnd: HWND,
msg: u32,

Loading…
Cancel
Save