feat: add init scripts (#361)

This commit is contained in:
Lucas Fernandes Nogueira
2023-05-17 18:25:56 -03:00
committed by GitHub
parent d87b569643
commit 7ae7167fbe
13 changed files with 140 additions and 13 deletions
+1
View File
@@ -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>,
+11
View File
@@ -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)]
{
+17
View File
@@ -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)
}
})
+7
View File
@@ -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)
}
})()