From ba24207e493888dc80dd12ee381c349f4773331a Mon Sep 17 00:00:00 2001 From: Lucas Nogueira Date: Mon, 15 May 2023 21:14:30 -0300 Subject: [PATCH] feat: add scripts --- plugins/dialog/src/init.js | 15 +++++++ plugins/dialog/src/lib.rs | 10 ++++- plugins/shell/src/init.js | 44 +++++++++++++++++++ plugins/shell/src/lib.rs | 1 + plugins/window/Cargo.toml | 1 + plugins/window/src/commands.rs | 1 + plugins/window/src/lib.rs | 22 ++++++++++ plugins/window/src/scripts/drag.js | 17 +++++++ plugins/window/src/scripts/hotkey.js | 6 +++ plugins/window/src/scripts/print.js | 7 +++ plugins/window/src/scripts/toggle-devtools.js | 3 ++ 11 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 plugins/dialog/src/init.js create mode 100644 plugins/shell/src/init.js create mode 100644 plugins/window/src/scripts/drag.js create mode 100644 plugins/window/src/scripts/hotkey.js create mode 100644 plugins/window/src/scripts/print.js create mode 100644 plugins/window/src/scripts/toggle-devtools.js diff --git a/plugins/dialog/src/init.js b/plugins/dialog/src/init.js new file mode 100644 index 00000000..79c9fd8b --- /dev/null +++ b/plugins/dialog/src/init.js @@ -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() + }) +} diff --git a/plugins/dialog/src/lib.rs b/plugins/dialog/src/lib.rs index 85518fef..3349965b 100644 --- a/plugins/dialog/src/lib.rs +++ b/plugins/dialog/src/lib.rs @@ -69,7 +69,15 @@ impl Dialog { /// Initializes the plugin. pub fn init() -> TauriPlugin { - 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, diff --git a/plugins/shell/src/init.js b/plugins/shell/src/init.js new file mode 100644 index 00000000..295af57c --- /dev/null +++ b/plugins/shell/src/init.js @@ -0,0 +1,44 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +// open 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', + function () { + __openLinks() + }, + true + ) +} \ No newline at end of file diff --git a/plugins/shell/src/lib.rs b/plugins/shell/src/lib.rs index f9ffd1e7..5453b15b 100644 --- a/plugins/shell/src/lib.rs +++ b/plugins/shell/src/lib.rs @@ -64,6 +64,7 @@ impl> ShellExt for T { pub fn init() -> TauriPlugin> { Builder::>::new("shell") + .js_init_script(include_str!("init.js").to_string()) .invoke_handler(tauri::generate_handler![ commands::execute, commands::stdin_write, diff --git a/plugins/window/Cargo.toml b/plugins/window/Cargo.toml index 6fda6b38..60500a09 100644 --- a/plugins/window/Cargo.toml +++ b/plugins/window/Cargo.toml @@ -13,3 +13,4 @@ thiserror.workspace = true [features] icon-png = ["tauri/icon-png"] icon-ico = ["tauri/icon-ico"] +devtools = [] diff --git a/plugins/window/src/commands.rs b/plugins/window/src/commands.rs index 453c1f98..63ef037b 100644 --- a/plugins/window/src/commands.rs +++ b/plugins/window/src/commands.rs @@ -182,6 +182,7 @@ pub fn internal_toggle_maximize( Ok(()) } +#[cfg(any(debug_assertions, feature = "devtools"))] #[tauri::command] pub fn internal_toggle_devtools( window: Window, diff --git a/plugins/window/src/lib.rs b/plugins/window/src/lib.rs index 513a5d32..f58cd500 100644 --- a/plugins/window/src/lib.rs +++ b/plugins/window/src/lib.rs @@ -6,7 +6,28 @@ use tauri::{ mod commands; pub fn init() -> TauriPlugin { + 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"))] + { + let shortcut = if cfg!(target_os = "macos") { + "command+option+i" + } else { + "ctrl+shift+i" + }; + init_js.push_str(include_str!("./scripts/hotkey.js")); + init_js.push_str( + &include_str!("./scripts/toggle-devtools.js").replace("__SHORTCUT__", shortcut), + ); + } + Builder::new("window") + .js_init_script(init_js) .invoke_handler(tauri::generate_handler![ commands::create, // getters @@ -59,6 +80,7 @@ pub fn init() -> TauriPlugin { commands::set_icon, commands::toggle_maximize, commands::internal_toggle_maximize, + #[cfg(any(debug_assertions, feature = "devtools"))] commands::internal_toggle_devtools, ]) .build() diff --git a/plugins/window/src/scripts/drag.js b/plugins/window/src/scripts/drag.js new file mode 100644 index 00000000..b8abeb2c --- /dev/null +++ b/plugins/window/src/scripts/drag.js @@ -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) + } +}) diff --git a/plugins/window/src/scripts/hotkey.js b/plugins/window/src/scripts/hotkey.js new file mode 100644 index 00000000..0bf6f6d7 --- /dev/null +++ b/plugins/window/src/scripts/hotkey.js @@ -0,0 +1,6 @@ +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy +// SPDX-License-Identifier: Apache-2.0 +// SPDX-License-Identifier: MIT + +/*! hotkeys-js v3.8.7 | MIT (c) 2021 kenny wong | http://jaywcjlove.github.io/hotkeys */ +!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).hotkeys=t()}(this,function(){"use strict";var e="undefined"!=typeof navigator&&0 { + window.__TAURI_INVOKE__('plugin:tauri|internal_toggle_devtools'); +});