Skip to content

Commit b357a6c

Browse files
committed
Almost but stuck on windows access denied error
1 parent f01b4e6 commit b357a6c

File tree

9 files changed

+97
-76
lines changed

9 files changed

+97
-76
lines changed

Cargo.toml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
[package]
2-
name = "tauri-plugin-deep-link"
3-
version = "0.1.2"
4-
authors = ["FabianLars <[email protected]>"]
5-
description = "A Tauri plugin for deep linking support"
6-
repository = "https://github.com/FabianLars/tauri-plugin-deep-link"
2+
name = "cargo-deep-link"
3+
version = "0.1.0"
4+
authors = ["FabianLars <[email protected]>", "jf908"]
5+
description = "A library for deep linking support"
6+
repository = "https://github.com/jf908/cargo-deep-link"
77
edition = "2021"
8-
rust-version = "1.64"
8+
rust-version = "1.81"
99
license = "MIT OR Apache-2.0"
1010
readme = "README.md"
1111
include = ["src/**", "Cargo.toml", "LICENSE_*"]
1212

1313
[dependencies]
1414
dirs = "5"
1515
log = "0.4"
16-
once_cell = "1"
17-
tauri-utils = { version = "1" }
1816

1917
[target.'cfg(windows)'.dependencies]
2018
interprocess = { version = "1.2", default-features = false }
@@ -27,3 +25,7 @@ winreg = "0.55.0"
2725

2826
[target.'cfg(target_os = "macos")'.dependencies]
2927
objc2 = "0.4.1"
28+
29+
[dev-dependencies]
30+
eframe = "0.30"
31+
env_logger = "0.11"

README.md

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
# Deep link plugin for Tauri
1+
# cargo-deep-link
22

3-
[![](https://img.shields.io/crates/v/tauri-plugin-deep-link.svg)](https://crates.io/crates/tauri-plugin-deep-link) [![](https://img.shields.io/docsrs/tauri-plugin-deep-link)](https://docs.rs/tauri-plugin-deep-link)
3+
This is a fork of [Tauri Plugin Deep Link v1](https://github.com/FabianLars/tauri-plugin-deep-link) for Rust apps.
44

5-
**This plugin will be migrated to https://github.com/tauri-apps/plugins-workspace/.** `0.1.2` will be the last release in this repo.
6-
7-
~~Temporary solution until https://github.com/tauri-apps/tauri/issues/323 lands.~~
8-
9-
Depending on your use case, for example a `Login with Google` button, you may want to take a look at https://github.com/FabianLars/tauri-plugin-oauth instead. It uses a minimalistic localhost server for the OAuth process instead of custom uri schemes because some oauth providers, like the aforementioned Google, require this setup. Personally, I think it's easier to use too.
10-
11-
Check out the [`example/`](https://github.com/FabianLars/tauri-plugin-deep-link/tree/main/example) directory for a minimal example. You must copy it into an actual tauri app first!
12-
13-
## macOS
14-
15-
In case you're one of the very few people that didn't know this already: macOS hates developers! Not only is that why the macOS implementation took me so long, it also means _you_ have to be a bit more careful if your app targets macOS:
16-
17-
- Read through the methods' platform-specific notes.
18-
- On macOS you need to register the schemes in a `Info.plist` file at build time, the plugin can't change the schemes at runtime.
19-
- macOS apps are in single-instance by default so this plugin will not manually shut down secondary instances in release mode.
20-
- To make development via `tauri dev` a little bit more pleasant, the plugin will work similar-ish to Linux and Windows _in debug mode_ but you will see secondary instances show on the screen for a split second and the event will trigger twice in the primary instance (one of these events will be an empty string). You still have to install a `.app` bundle you got from `tauri build --debug` for this to work!
5+
> Only tested on Windows.

example/main.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.
File renamed without changes.

examples/main.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Disable Windows console on release builds
2+
// #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
3+
4+
use eframe::egui;
5+
6+
fn main() {
7+
// prepare() checks if it's a single instance and tries to send the args otherwise.
8+
// It should always be the first line in your main function (with the exception of loggers or similar)
9+
cargo_deep_link::prepare("de.fabianlars.deep-link-test");
10+
11+
env_logger::init();
12+
13+
cargo_deep_link::listen(|str| {
14+
println!("{:?}", str);
15+
})
16+
.unwrap();
17+
18+
eframe::run_native(
19+
"egui App",
20+
eframe::NativeOptions {
21+
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
22+
..Default::default()
23+
},
24+
Box::new(|_| Ok(Box::<App>::default())),
25+
)
26+
.unwrap();
27+
}
28+
29+
struct App {
30+
args: String,
31+
}
32+
33+
impl Default for App {
34+
fn default() -> Self {
35+
Self {
36+
args: std::env::args().collect::<Vec<_>>().join(" "),
37+
}
38+
}
39+
}
40+
41+
impl eframe::App for App {
42+
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
43+
egui::CentralPanel::default().show(ctx, |ui| {
44+
ui.heading("egui Application");
45+
46+
ui.label(self.args.clone());
47+
48+
if ui.add(egui::Button::new("Register")).clicked() {
49+
// If you need macOS support this must be called in .setup() !
50+
// Otherwise this could be called right after prepare() but then you don't have access to tauri APIs
51+
cargo_deep_link::register(
52+
"test-scheme",
53+
|request| {
54+
println!("{:?}", &request);
55+
},
56+
)
57+
.unwrap(/* If listening to the scheme is optional for your app, you don't want to unwrap here. */);
58+
}
59+
60+
if ui.add(egui::Button::new("Unregister")).clicked() {
61+
cargo_deep_link::unregister("test-scheme").unwrap();
62+
}
63+
});
64+
}
65+
}

src/lib.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use std::io::{ErrorKind, Result};
2-
3-
use once_cell::sync::OnceCell;
1+
use std::{
2+
io::{ErrorKind, Result},
3+
path::PathBuf,
4+
sync::OnceLock,
5+
};
46

57
#[cfg(target_os = "windows")]
68
#[path = "windows.rs"]
@@ -12,7 +14,7 @@ mod platform_impl;
1214
#[path = "macos.rs"]
1315
mod platform_impl;
1416

15-
static ID: OnceCell<String> = OnceCell::new();
17+
static ID: OnceLock<String> = OnceLock::new();
1618

1719
/// This function is meant for use-cases where the default [`prepare()`] function can't be used.
1820
///
@@ -61,3 +63,10 @@ pub fn unregister(scheme: &str) -> Result<()> {
6163
pub fn prepare(identifier: &str) {
6264
platform_impl::prepare(identifier)
6365
}
66+
67+
/// Helper to get current exe path
68+
pub(crate) fn current_exe() -> std::io::Result<PathBuf> {
69+
let path = std::env::current_exe()?;
70+
71+
path.canonicalize()
72+
}

src/linux.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn register<F: FnMut(String) + Send + 'static>(scheme: &str, handler: F) ->
1818

1919
create_dir_all(&target)?;
2020

21-
let exe = tauri_utils::platform::current_exe()?;
21+
let exe = crate::current_exe()?;
2222

2323
let file_name = format!(
2424
"{}-handler.desktop",
@@ -70,7 +70,7 @@ pub fn unregister(_scheme: &str) -> Result<()> {
7070
target.push("applications");
7171
target.push(format!(
7272
"{}-handler.desktop",
73-
tauri_utils::platform::current_exe()?
73+
crate::current_exe()?
7474
.file_name()
7575
.ok_or_else(|| Error::new(
7676
ErrorKind::NotFound,

src/macos.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
fs::remove_file,
33
io::{ErrorKind, Read, Result, Write},
44
os::unix::net::{UnixListener, UnixStream},
5-
sync::Mutex,
5+
sync::{Mutex, OnceLock},
66
};
77

88
use objc2::{
@@ -12,14 +12,13 @@ use objc2::{
1212
runtime::{AnyObject, NSObject},
1313
sel, ClassType,
1414
};
15-
use once_cell::sync::OnceCell;
1615

1716
use crate::ID;
1817

19-
type THandler = OnceCell<Mutex<Box<dyn FnMut(String) + Send + 'static>>>;
18+
type THandler = OnceLock<Mutex<Box<dyn FnMut(String) + Send + 'static>>>;
2019

2120
// If the Mutex turns out to be a problem, or FnMut turns out to be useless, we can remove the Mutex and turn FnMut into Fn
22-
static HANDLER: THandler = OnceCell::new();
21+
static HANDLER: THandler = OnceLock::new();
2322

2423
pub fn register<F: FnMut(String) + Send + 'static>(_scheme: &str, handler: F) -> Result<()> {
2524
listen(handler)?;

src/windows.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn register<F: FnMut(String) + Send + 'static>(scheme: &str, handler: F) ->
1818
let hkcu = RegKey::predef(HKEY_CURRENT_USER);
1919
let base = Path::new("Software").join("Classes").join(scheme);
2020

21-
let exe = tauri_utils::platform::current_exe()?
21+
let exe = crate::current_exe()?
2222
.display()
2323
.to_string()
2424
.replace("\\\\?\\", "");

0 commit comments

Comments
 (0)