Skip to content

Commit

Permalink
feat: support protocol (deep-link) for macOS
Browse files Browse the repository at this point in the history
  • Loading branch information
meowtec committed Jun 28, 2022
1 parent fb5626d commit 025ccfc
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 11 deletions.
16 changes: 16 additions & 0 deletions core/tauri-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,17 @@ pub struct FileAssociation {
/// Linux-only. The mime-type. e.g. 'image/png'
pub mime_type: Option<Vec<String>>,
}
/// URL protocol scheme
#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
pub struct Protocol {
/// The schemas.
pub schemes: Vec<String>,
/// The name.
pub name: String,
/// macOS-only The app’s role with respect to the type.
pub role: Option<BundleTypeRole>,
}

/// Configuration for tauri-bundler.
#[skip_serializing_none]
Expand Down Expand Up @@ -531,6 +542,8 @@ pub struct BundleConfig {
pub category: Option<String>,
/// File associations to application.
pub file_associations: Option<Vec<FileAssociation>>,
/// URL protocol schemes.
pub protocols: Option<Vec<Protocol>>,
/// A short description of your application.
pub short_description: Option<String>,
/// A longer, multi-line description of the application.
Expand Down Expand Up @@ -2943,6 +2956,7 @@ mod build {
let copyright = quote!(None);
let category = quote!(None);
let file_associations = quote!(None);
let protocols = quote!(None);
let short_description = quote!(None);
let long_description = quote!(None);
let appimage = quote!(Default::default());
Expand All @@ -2962,6 +2976,7 @@ mod build {
copyright,
category,
file_associations,
protocols,
short_description,
long_description,
appimage,
Expand Down Expand Up @@ -3366,6 +3381,7 @@ mod test {
copyright: None,
category: None,
file_associations: None,
protocols: None,
short_description: None,
long_description: None,
appimage: Default::default(),
Expand Down
43 changes: 33 additions & 10 deletions examples/file-associations/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@

use std::env;
use tauri::{api::dialog::MessageDialogBuilder, Manager};
use url::Url;

fn handle_open_files(files: &[String]) {
MessageDialogBuilder::new("Files open", format!("You opened: {:?}", files)).show(|_| {});
}

fn handle_open_urls(urls: &[&Url]) {
MessageDialogBuilder::new(
"URLs open",
format!(
"You opened: {:?}",
urls.iter().map(|url| url.to_string()).collect::<Vec<_>>()
),
)
.show(|_| {});
}

fn main() {
tauri::Builder::default()
.setup(|app| {
Expand All @@ -22,19 +34,30 @@ fn main() {
let urls: Vec<_> = serde_json::from_str::<Vec<String>>(f.payload().unwrap())
.unwrap()
.iter()
.map(|s| url::Url::parse(s).unwrap())
.map(|s| Url::parse(s).unwrap())
.collect();

// filter out non-file:// urls, you may need to handle them by another method
let file_paths: Vec<_> = urls.iter().filter_map(|url| {
if url.scheme() == "file" {
Some(url.path().into())
} else {
None
}
}).collect();

handle_open_files(&file_paths);
let file_paths: Vec<_> = urls
.iter()
.filter_map(|url| {
if url.scheme() == "file" {
Some(url.path().into())
} else {
None
}
})
.collect();

let non_file_urls: Vec<_> = urls.iter().filter(|url| url.scheme() != "file").collect();

if !file_paths.is_empty() {
handle_open_files(&file_paths);
}

if !non_file_urls.is_empty() {
handle_open_urls(&non_file_urls);
}
});

// Windows and Linux
Expand Down
7 changes: 7 additions & 0 deletions examples/file-associations/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@
"mime_type": ["image/png", "image/jpg", "image/jpeg", "image/gif", "image/bmp", "image/webp", "image/svg+xml"]
}
],
"protocols": [
{
"name": "Tauri Protocal Demo",
"schemes": ["tauri-protocal-demo"],
"role": "Editor"
}
],
"shortDescription": "",
"longDescription": "",
"deb": {
Expand Down
33 changes: 33 additions & 0 deletions tooling/bundler/src/bundle/macos/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,39 @@ fn create_info_plist(
);
}

if let Some(protocols) = settings.protocols() {
plist.insert(
"CFBundleURLTypes".into(),
plist::Value::Array(protocols.iter().map(|protocol| {
let mut dict = plist::Dictionary::new();
dict.insert(
"CFBundleURLName".into(),
protocol.name.to_string().into(),
);
dict.insert(
"CFBundleURLSchemes".into(),
plist::Value::Array(
protocol
.schemes
.iter()
.map(|scheme| scheme.to_string().into())
.collect(),
),
);
dict.insert(
"CFBundleTypeRole".into(),
protocol
.role
.as_ref()
.unwrap_or(&BundleTypeRole::Editor)
.to_string()
.into(),
);
plist::Value::Dictionary(dict)
}).collect()),
);
}

plist.insert("LSRequiresCarbon".into(), true.into());
plist.insert("NSHighResolutionCapable".into(), true.into());
if let Some(copyright) = settings.copyright_string() {
Expand Down
9 changes: 8 additions & 1 deletion tooling/bundler/src/bundle/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use super::category::AppCategory;
use crate::bundle::{common, platform::target_triple};
use tauri_utils::{
config::{BundleType, FileAssociation},
config::{BundleType, FileAssociation, Protocol},
resources::{external_binaries, ResourcePaths},
};

Expand Down Expand Up @@ -298,6 +298,8 @@ pub struct BundleSettings {
pub category: Option<AppCategory>,
/// the file associations
pub file_associations: Option<Vec<FileAssociation>>,
/// the URL protocol schemes
pub protocols: Option<Vec<Protocol>>,
/// the app's short description.
pub short_description: Option<String>,
/// the app's long description.
Expand Down Expand Up @@ -696,6 +698,11 @@ impl Settings {
&self.bundle_settings.file_associations
}

/// Return file associations.
pub fn protocols(&self) -> &Option<Vec<Protocol>> {
&self.bundle_settings.protocols
}

/// Returns the app's short description.
pub fn short_description(&self) -> &str {
self
Expand Down
42 changes: 42 additions & 0 deletions tooling/cli/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,16 @@
"$ref": "#/definitions/FileAssociation"
}
},
"protocols": {
"description": "URL protocol schemes.",
"type": [
"array",
"null"
],
"items": {
"$ref": "#/definitions/Protocol"
}
},
"shortDescription": {
"description": "A short description of your application.",
"type": [
Expand Down Expand Up @@ -1127,6 +1137,38 @@
"None"
]
},
"Protocol": {
"description": "URL protocol scheme",
"type": "object",
"required": [
"name",
"schemes"
],
"properties": {
"schemes": {
"description": "The schemas.",
"type": "array",
"items": {
"type": "string"
}
},
"name": {
"description": "The name.",
"type": "string"
},
"role": {
"description": "macOS-only The app’s role with respect to the type.",
"anyOf": [
{
"$ref": "#/definitions/BundleTypeRole"
},
{
"type": "null"
}
]
}
}
},
"AppImageConfig": {
"description": "Configuration for AppImage bundles.",
"type": "object",
Expand Down
1 change: 1 addition & 0 deletions tooling/cli/src/interface/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,7 @@ fn tauri_config_to_bundle_settings(
None => None,
},
file_associations: config.file_associations,
protocols: config.protocols,
short_description: config.short_description,
long_description: config.long_description,
external_bin: config.external_bin,
Expand Down

0 comments on commit 025ccfc

Please sign in to comment.