feat: Support URL Scheme for Linux

This commit is contained in:
MystiPanda 2024-01-10 13:03:34 +08:00
parent 3c6d6b90bc
commit 3a2a7a1476
3 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,6 @@
[Desktop Entry]
Type=Application
Name=Clash Verge Scheme Handler
Exec=clash-verge %u
StartupNotify=false
MimeType=x-scheme-handler/clash;

View File

@ -92,6 +92,14 @@ pub fn clash_pid_path() -> Result<PathBuf> {
Ok(app_home_dir()?.join("clash.pid"))
}
#[cfg(linux)]
pub fn local_applications_dir() -> Result<PathBuf> {
use tauri::api::path::home_dir;
Ok(home_dir()
.ok_or(anyhow::anyhow!("failed to get home dir"))?
.join(".local/share/applications"))
}
#[cfg(windows)]
pub fn service_dir() -> Result<PathBuf> {
Ok(app_home_dir()?.join("service"))

View File

@ -325,6 +325,28 @@ pub fn init_scheme() -> Result<()> {
}
#[cfg(target_os = "linux")]
pub fn init_scheme() -> Result<()> {
use std::io::Write;
let desktop_file_content = include_str!("./desktop/clash-verge-scheme.desktop");
let desktop_dir = dirs::local_applications_dir()?;
if !desktop_dir.exists() {
let _ = std::fs::create_dir_all(&desktop_dir);
}
let mut desktop_file =
std::fs::File::create(dirs::local_applications_dir()?.join("clash-verge-scheme.desktop"))?;
desktop_file.write_all(desktop_file_content.as_bytes())?;
let output = std::process::Command::new("xdg-mime")
.arg("default")
.arg("clash-verge-scheme.desktop")
.arg("x-scheme-handler/clash")
.output()?;
if !output.status.success() {
return Err(anyhow::anyhow!(
"failed to set clash scheme, {}",
String::from_utf8_lossy(&output.stderr)
));
}
Ok(())
}
#[cfg(target_os = "macos")]