mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-09-22 21:30:44 +02:00
feat: pass an AppHandle to the callback
This commit is contained in:
+2
-2
@@ -8,10 +8,10 @@ rust-version = "1.57"
|
|||||||
exclude = ["/examples"]
|
exclude = ["/examples"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tauri = { version = "1.0.0-rc.10" }
|
tauri = { version = "1.0.0-rc.16" }
|
||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
|
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
|
||||||
version = "0.35"
|
version = "0.36"
|
||||||
features = [
|
features = [
|
||||||
"Win32_System_Threading",
|
"Win32_System_Threading",
|
||||||
"Win32_System_DataExchange",
|
"Win32_System_DataExchange",
|
||||||
|
|||||||
@@ -9,6 +9,6 @@
|
|||||||
"author": "",
|
"author": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@tauri-apps/cli": "^1.0.0-rc.0"
|
"@tauri-apps/cli": "^1.0.0-rc.15"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+325
-404
File diff suppressed because it is too large
Load Diff
@@ -10,11 +10,11 @@ rust-version = "1.57"
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
serde = { version = "1.0", features = [ "derive" ] }
|
serde = { version = "1.0", features = [ "derive" ] }
|
||||||
tauri = { version = "1.0.0-rc.6", features = ["api-all"] }
|
tauri = { version = "1.0.0-rc.16", features = ["api-all"] }
|
||||||
tauri-plugin-single-instance = { path = "../../../" }
|
tauri-plugin-single-instance = { path = "../../../" }
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
tauri-build = { version = "1.0.0-rc.5", features = [] }
|
tauri-build = { version = "1.0.0-rc.14", features = [] }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "custom-protocol" ]
|
default = [ "custom-protocol" ]
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
tauri::Builder::default()
|
tauri::Builder::default()
|
||||||
.plugin(tauri_plugin_single_instance::init(|argv, cwd| {
|
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
|
||||||
println!("{argv:?}, {cwd}");
|
println!("{}, {argv:?}, {cwd}", app.package_info().name);
|
||||||
}))
|
}))
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|||||||
+4
-3
@@ -1,4 +1,4 @@
|
|||||||
use tauri::{plugin::TauriPlugin, Runtime};
|
use tauri::{plugin::TauriPlugin, AppHandle, Runtime};
|
||||||
|
|
||||||
#[cfg(target_os = "windows")]
|
#[cfg(target_os = "windows")]
|
||||||
#[path = "platform_impl/windows.rs"]
|
#[path = "platform_impl/windows.rs"]
|
||||||
@@ -10,9 +10,10 @@ mod platform_impl;
|
|||||||
#[path = "platform_impl/macos.rs"]
|
#[path = "platform_impl/macos.rs"]
|
||||||
mod platform_impl;
|
mod platform_impl;
|
||||||
|
|
||||||
pub(crate) type SingleInstanceCallback = dyn FnMut(Vec<String>, String) + Send + Sync + 'static;
|
pub(crate) type SingleInstanceCallback<R> =
|
||||||
|
dyn FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sync + 'static;
|
||||||
|
|
||||||
pub fn init<R: Runtime, F: FnMut(Vec<String>, String) + Send + Sync + 'static>(
|
pub fn init<R: Runtime, F: FnMut(&AppHandle<R>, Vec<String>, String) + Send + Sync + 'static>(
|
||||||
f: F,
|
f: F,
|
||||||
) -> TauriPlugin<R> {
|
) -> TauriPlugin<R> {
|
||||||
platform_impl::init(Box::new(f))
|
platform_impl::init(Box::new(f))
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
use crate::SingleInstanceCallback;
|
use crate::SingleInstanceCallback;
|
||||||
use tauri::{
|
use tauri::{
|
||||||
|
AppHandle,
|
||||||
plugin::{self, TauriPlugin},
|
plugin::{self, TauriPlugin},
|
||||||
Manager, RunEvent, Runtime,
|
Manager, RunEvent, Runtime,
|
||||||
};
|
};
|
||||||
@@ -12,22 +13,26 @@ use zbus::{
|
|||||||
|
|
||||||
struct ConnectionHandle(Connection);
|
struct ConnectionHandle(Connection);
|
||||||
|
|
||||||
struct SingleInstanceDBus {
|
struct SingleInstanceDBus<R: Runtime> {
|
||||||
callback: Box<SingleInstanceCallback>,
|
callback: Box<SingleInstanceCallback<R>>,
|
||||||
|
app_handle: AppHandle<R>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[dbus_interface(name = "org.SingleInstance.DBus")]
|
#[dbus_interface(name = "org.SingleInstance.DBus")]
|
||||||
impl SingleInstanceDBus {
|
impl<R:Runtime> SingleInstanceDBus<R> {
|
||||||
fn execute_callback(&mut self, argv: Vec<String>, cwd: String) {
|
fn execute_callback(&mut self, argv: Vec<String>, cwd: String) {
|
||||||
(self.callback)(argv, cwd);
|
(self.callback)(&self.app_handle, argv, cwd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> 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 app_name = app.package_info().name.clone();
|
let app_name = app.package_info().name.clone();
|
||||||
let single_instance_dbus = SingleInstanceDBus { callback: f };
|
let single_instance_dbus = SingleInstanceDBus {
|
||||||
|
callback: f,
|
||||||
|
app_handle: app.clone(),
|
||||||
|
};
|
||||||
let dbus_name = format!("org.{}.SingleInstance", app_name);
|
let dbus_name = format!("org.{}.SingleInstance", app_name);
|
||||||
let dbus_path = format!("/org/{}/SingleInstance", app_name);
|
let dbus_path = format!("/org/{}/SingleInstance", app_name);
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use crate::SingleInstanceCallback;
|
|||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use tauri::{
|
use tauri::{
|
||||||
plugin::{self, TauriPlugin},
|
plugin::{self, TauriPlugin},
|
||||||
Manager, RunEvent, Runtime,
|
AppHandle, Manager, RunEvent, Runtime,
|
||||||
};
|
};
|
||||||
use windows_sys::Win32::{
|
use windows_sys::Win32::{
|
||||||
Foundation::{CloseHandle, GetLastError, ERROR_ALREADY_EXISTS, HWND, LPARAM, LRESULT, WPARAM},
|
Foundation::{CloseHandle, GetLastError, ERROR_ALREADY_EXISTS, HWND, LPARAM, LRESULT, WPARAM},
|
||||||
@@ -26,7 +26,7 @@ struct TargetWindowHandle(isize);
|
|||||||
|
|
||||||
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
|
const WMCOPYDATA_SINGLE_INSTANCE_DATA: usize = 1542;
|
||||||
|
|
||||||
pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> 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 app_name = &app.package_info().name;
|
let app_name = &app.package_info().name;
|
||||||
@@ -70,8 +70,14 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
|
|||||||
} else {
|
} else {
|
||||||
app.manage(MutexHandle(hmutex));
|
app.manage(MutexHandle(hmutex));
|
||||||
|
|
||||||
let hwnd = create_event_target_window(&class_name, &window_name);
|
let hwnd = create_event_target_window::<R>(&class_name, &window_name);
|
||||||
unsafe { SetWindowLongPtrW(hwnd, GWL_USERDATA, Box::into_raw(Box::new(f)) as _) };
|
unsafe {
|
||||||
|
SetWindowLongPtrW(
|
||||||
|
hwnd,
|
||||||
|
GWL_USERDATA,
|
||||||
|
Box::into_raw(Box::new((app.clone(), f))) as _,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
app.manage(TargetWindowHandle(hwnd));
|
app.manage(TargetWindowHandle(hwnd));
|
||||||
}
|
}
|
||||||
@@ -94,13 +100,15 @@ pub fn init<R: Runtime>(f: Box<SingleInstanceCallback>) -> TauriPlugin<R> {
|
|||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe extern "system" fn single_instance_window_proc(
|
unsafe extern "system" fn single_instance_window_proc<R: Runtime>(
|
||||||
hwnd: HWND,
|
hwnd: HWND,
|
||||||
msg: u32,
|
msg: u32,
|
||||||
wparam: WPARAM,
|
wparam: WPARAM,
|
||||||
lparam: LPARAM,
|
lparam: LPARAM,
|
||||||
) -> LRESULT {
|
) -> LRESULT {
|
||||||
let callback_ptr = GetWindowLongPtrW(hwnd, GWL_USERDATA) as *mut Box<SingleInstanceCallback>;
|
let data_ptr = GetWindowLongPtrW(hwnd, GWL_USERDATA)
|
||||||
|
as *mut (AppHandle<R>, Box<SingleInstanceCallback<R>>);
|
||||||
|
let (app_handle, callback) = &mut *data_ptr;
|
||||||
|
|
||||||
match msg {
|
match msg {
|
||||||
WM_COPYDATA => {
|
WM_COPYDATA => {
|
||||||
@@ -110,25 +118,25 @@ unsafe extern "system" fn single_instance_window_proc(
|
|||||||
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();
|
||||||
(*callback_ptr)(args, cwd.to_string());
|
callback(&app_handle, args, cwd.to_string());
|
||||||
}
|
}
|
||||||
1
|
1
|
||||||
}
|
}
|
||||||
|
|
||||||
WM_DESTROY => {
|
WM_DESTROY => {
|
||||||
let _ = Box::from_raw(callback_ptr);
|
let _ = Box::from_raw(data_ptr);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
_ => DefWindowProcW(hwnd, msg, wparam, lparam),
|
_ => DefWindowProcW(hwnd, msg, wparam, lparam),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_event_target_window(class_name: &str, window_name: &str) -> HWND {
|
fn create_event_target_window<R: Runtime>(class_name: &str, window_name: &str) -> HWND {
|
||||||
unsafe {
|
unsafe {
|
||||||
let class = WNDCLASSEXW {
|
let class = WNDCLASSEXW {
|
||||||
cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
|
cbSize: std::mem::size_of::<WNDCLASSEXW>() as u32,
|
||||||
style: 0,
|
style: 0,
|
||||||
lpfnWndProc: Some(single_instance_window_proc),
|
lpfnWndProc: Some(single_instance_window_proc::<R>),
|
||||||
cbClsExtra: 0,
|
cbClsExtra: 0,
|
||||||
cbWndExtra: 0,
|
cbWndExtra: 0,
|
||||||
hInstance: GetModuleHandleW(std::ptr::null()),
|
hInstance: GetModuleHandleW(std::ptr::null()),
|
||||||
|
|||||||
Reference in New Issue
Block a user