feat: add init scripts (#361)

pull/367/head
Lucas Fernandes Nogueira 2 years ago committed by GitHub
parent d87b569643
commit 7ae7167fbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

14
Cargo.lock generated

@ -4960,7 +4960,7 @@ dependencies = [
[[package]]
name = "tauri"
version = "2.0.0-alpha.8"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"anyhow",
"bytes 1.4.0",
@ -5010,7 +5010,7 @@ dependencies = [
[[package]]
name = "tauri-build"
version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"anyhow",
"cargo_toml",
@ -5030,7 +5030,7 @@ dependencies = [
[[package]]
name = "tauri-codegen"
version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"base64 0.21.0",
"brotli",
@ -5055,7 +5055,7 @@ dependencies = [
[[package]]
name = "tauri-macros"
version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"heck 0.4.1",
"proc-macro2",
@ -5449,7 +5449,7 @@ dependencies = [
[[package]]
name = "tauri-runtime"
version = "0.13.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"gtk",
"http",
@ -5469,7 +5469,7 @@ dependencies = [
[[package]]
name = "tauri-runtime-wry"
version = "0.13.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"cocoa",
"gtk",
@ -5489,7 +5489,7 @@ dependencies = [
[[package]]
name = "tauri-utils"
version = "2.0.0-alpha.4"
source = "git+https://github.com/tauri-apps/tauri?branch=next#59db76af4c88645ee03b9f87c0f787fbc0040905"
source = "git+https://github.com/tauri-apps/tauri?branch=next#994e4fd6d9e649e0d76124cd6fcd18443ac585b0"
dependencies = [
"aes-gcm 0.10.1",
"brotli",

@ -27,7 +27,7 @@ tauri-plugin-notification = { path = "../../../plugins/notification", features =
tauri-plugin-os = { path = "../../../plugins/os" }
tauri-plugin-process = { path = "../../../plugins/process" }
tauri-plugin-shell = { path = "../../../plugins/shell" }
tauri-plugin-window = { path = "../../../plugins/window" }
tauri-plugin-window = { path = "../../../plugins/window", features = ["devtools", "icon-ico", "icon-png"] }
[dependencies.tauri]
workspace = true

@ -271,8 +271,8 @@ pub(crate) async fn ask<R: Runtime>(
title,
message,
type_,
ok_button_label,
cancel_button_label,
Some(ok_button_label.unwrap_or_else(|| "Yes".into())),
Some(cancel_button_label.unwrap_or_else(|| "No".into())),
))
}
@ -292,7 +292,7 @@ pub(crate) async fn confirm<R: Runtime>(
title,
message,
type_,
ok_button_label,
cancel_button_label,
Some(ok_button_label.unwrap_or_else(|| "Ok".into())),
Some(cancel_button_label.unwrap_or_else(|| "Cancel".into())),
))
}

@ -0,0 +1,15 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
window.alert = function (message) {
window.__TAURI_INVOKE__('plugin:dialog|message', {
message: message.toString()
})
}
window.confirm = function (message) {
return window.__TAURI_INVOKE__('plugin:dialog|confirm', {
message: message.toString()
})
}

@ -69,7 +69,15 @@ impl<R: Runtime> Dialog<R> {
/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("dialog")
let mut builder = Builder::new("dialog");
// Dialogs are implemented natively on Android
#[cfg(not(target_os = "android"))]
{
builder = builder.js_init_script(include_str!("init.js").to_string());
}
builder
.invoke_handler(tauri::generate_handler![
commands::open,
commands::save,

@ -0,0 +1,40 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
;(function () {
// open <a href="..."> links with the API
function openLinks() {
document.querySelector('body').addEventListener(
'click',
function (e) {
var target = e.target
while (target != null) {
if (target.matches('a')) {
if (
target.href &&
(['http://', 'https://', 'mailto:', 'tel:'].some(v => target.href.startsWith(v))) &&
target.target === '_blank'
) {
window.__TAURI_INVOKE__('plugin:shell|open', {
path: target.href
})
e.preventDefault()
}
break
}
target = target.parentElement
}
}
)
}
if (
document.readyState === 'complete' ||
document.readyState === 'interactive'
) {
openLinks()
} else {
window.addEventListener('DOMContentLoaded', openLinks, true)
}
})()

@ -64,6 +64,7 @@ impl<R: Runtime, T: Manager<R>> ShellExt<R> for T {
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<Config>> {
Builder::<R, Option<Config>>::new("shell")
.js_init_script(include_str!("init.js").to_string())
.invoke_handler(tauri::generate_handler![
commands::execute,
commands::stdin_write,

@ -13,3 +13,4 @@ thiserror.workspace = true
[features]
icon-png = ["tauri/icon-png"]
icon-ico = ["tauri/icon-ico"]
devtools = []

@ -182,6 +182,7 @@ pub fn internal_toggle_maximize<R: Runtime>(
Ok(())
}
#[cfg(any(debug_assertions, feature = "devtools"))]
#[tauri::command]
pub fn internal_toggle_devtools<R: Runtime>(
window: Window<R>,

@ -7,7 +7,18 @@ use tauri::{
mod desktop_commands;
pub fn init<R: Runtime>() -> TauriPlugin<R> {
let mut init_js = String::new();
// window.print works on Linux/Windows; need to use the API on macOS
#[cfg(any(target_os = "macos", target_os = "ios"))]
{
init_js.push_str(include_str!("./scripts/print.js"));
}
init_js.push_str(include_str!("./scripts/drag.js"));
#[cfg(any(debug_assertions, feature = "devtools"))]
init_js.push_str(include_str!("./scripts/toggle-devtools.js"));
Builder::new("window")
.js_init_script(init_js)
.invoke_handler(|invoke| {
#[cfg(desktop)]
{

@ -0,0 +1,17 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
document.addEventListener('mousedown', (e) => {
if (e.target.hasAttribute('data-tauri-drag-region') && e.buttons === 1) {
// prevents text cursor
e.preventDefault()
// fix #2549: double click on drag region edge causes content to maximize without window sizing change
// https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
e.stopImmediatePropagation()
// start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
const cmd = e.detail === 2 ? 'internal_toggle_maximize' : 'start_dragging'
window.__TAURI_INVOKE__('plugin:window|' + cmd)
}
})

@ -0,0 +1,7 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
window.print = function () {
return window.__TAURI_INVOKE__('plugin:window|print')
}

@ -0,0 +1,26 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
;(function () {
function toggleDevtoolsHotkey() {
const isHotkey = navigator.appVersion.includes('Mac')
? event => event.metaKey && event.altKey && event.key === 'I'
: event => event.ctrlKey && event.shiftKey && event.key === 'I'
document.addEventListener('keydown', event => {
if (isHotkey(event)) {
window.__TAURI_INVOKE__('plugin:window|internal_toggle_devtools');
}
})
}
if (
document.readyState === 'complete' ||
document.readyState === 'interactive'
) {
toggleDevtoolsHotkey()
} else {
window.addEventListener('DOMContentLoaded', toggleDevtoolsHotkey, true)
}
})()
Loading…
Cancel
Save