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

Loading…
Cancel
Save