diff --git a/Cargo.lock b/Cargo.lock index cfe81a35..6a12b8d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/examples/api/src-tauri/Cargo.toml b/examples/api/src-tauri/Cargo.toml index 6448c4e0..9620af9a 100644 --- a/examples/api/src-tauri/Cargo.toml +++ b/examples/api/src-tauri/Cargo.toml @@ -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 diff --git a/plugins/dialog/src/commands.rs b/plugins/dialog/src/commands.rs index 905c01ca..8232eedf 100644 --- a/plugins/dialog/src/commands.rs +++ b/plugins/dialog/src/commands.rs @@ -271,8 +271,8 @@ pub(crate) async fn ask( 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( 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())), )) } 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..c43f7cdf --- /dev/null +++ b/plugins/shell/src/init.js @@ -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 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) + } +})() 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/desktop_commands.rs b/plugins/window/src/desktop_commands.rs index 453c1f98..63ef037b 100644 --- a/plugins/window/src/desktop_commands.rs +++ b/plugins/window/src/desktop_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 a9673b6d..0f4c2dda 100644 --- a/plugins/window/src/lib.rs +++ b/plugins/window/src/lib.rs @@ -7,7 +7,18 @@ use tauri::{ mod desktop_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"))] + init_js.push_str(include_str!("./scripts/toggle-devtools.js")); + Builder::new("window") + .js_init_script(init_js) .invoke_handler(|invoke| { #[cfg(desktop)] { 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/print.js b/plugins/window/src/scripts/print.js new file mode 100644 index 00000000..2be2e491 --- /dev/null +++ b/plugins/window/src/scripts/print.js @@ -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') +} diff --git a/plugins/window/src/scripts/toggle-devtools.js b/plugins/window/src/scripts/toggle-devtools.js new file mode 100644 index 00000000..2bff9d12 --- /dev/null +++ b/plugins/window/src/scripts/toggle-devtools.js @@ -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) + } +})()