feat: add scripts

pull/361/head
Lucas Nogueira 2 years ago
parent 702b7b36bd
commit ba24207e49
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1

@ -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,44 @@
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
// 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',
function () {
__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>,

@ -6,7 +6,28 @@ use tauri::{
mod 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"))]
{
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<R: Runtime>() -> TauriPlugin<R> {
commands::set_icon,
commands::toggle_maximize,
commands::internal_toggle_maximize,
#[cfg(any(debug_assertions, feature = "devtools"))]
commands::internal_toggle_devtools,
])
.build()

@ -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)
}
})

File diff suppressed because one or more lines are too long

@ -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,3 @@
window.hotkeys('__SHORTCUT__', () => {
window.__TAURI_INVOKE__('plugin:tauri|internal_toggle_devtools');
});
Loading…
Cancel
Save