fix(macos): allow canceling data-tauri-drag-region maximization #8312 (#8487)

This commit is contained in:
Lucas Fernandes Nogueira 2023-12-28 10:03:14 -03:00 committed by GitHub
parent 3e00116e9f
commit af565c2a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 74 additions and 14 deletions

View File

@ -215,18 +215,32 @@ mod desktop_commands {
/// Initializes the plugin. /// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> { pub fn init<R: Runtime>() -> TauriPlugin<R> {
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
let mut init_script = String::new(); let mut init_script = String::new();
// window.print works on Linux/Windows; need to use the API on macOS // window.print works on Linux/Windows; need to use the API on macOS
#[cfg(any(target_os = "macos", target_os = "ios"))] #[cfg(any(target_os = "macos", target_os = "ios"))]
{ {
init_script.push_str(include_str!("./scripts/print.js")); init_script.push_str(include_str!("./scripts/print.js"));
} }
init_script.push_str(include_str!("./scripts/drag.js"));
#[derive(Template)]
#[default_template("./scripts/drag.js")]
struct Drag<'a> {
os_name: &'a str,
}
init_script.push_str(
&Drag {
os_name: std::env::consts::OS,
}
.render_default(&Default::default())
.unwrap()
.into_string(),
);
#[cfg(any(debug_assertions, feature = "devtools"))] #[cfg(any(debug_assertions, feature = "devtools"))]
{ {
use serialize_to_javascript::{default_template, DefaultTemplate, Template};
#[derive(Template)] #[derive(Template)]
#[default_template("./scripts/toggle-devtools.js")] #[default_template("./scripts/toggle-devtools.js")]
struct Devtools<'a> { struct Devtools<'a> {

View File

@ -2,16 +2,62 @@
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
document.addEventListener('mousedown', (e) => { ;(function () {
if (e.target.hasAttribute('data-tauri-drag-region') && e.button === 0) { const osName = __TEMPLATE_os_name__
// 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' // drag on mousedown and maximize on double click on Windows and Linux
window.__TAURI_INTERNALS__.invoke('plugin:window|' + cmd) // while macOS macos maximization should be on mouseup and if the mouse
// moves after the double click, it should be cancelled (see https://github.com/tauri-apps/tauri/issues/8306)
//-----------------------//
const TAURI_DRAG_REGION_ATTR = 'data-tauri-drag-region';
let x = 0, y = 0;
document.addEventListener('mousedown', (e) => {
if (
// element has the magic data attribute
e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
// and was left mouse button
e.button === 0 &&
// and was normal click to drag or double click to maximize
(e.detail === 1 || e.detail === 2)
) {
// macOS maximization happens on `mouseup`,
// so we save needed state and early return
if (osName === 'macos' && e.detail == 2) {
x = e.clientX
y = e.clientY
return
}
// 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_INTERNALS__.invoke('plugin:window|' + cmd)
}
})
// on macOS we maximze on mouseup instead, to match the system behavior where maximization can be canceled
// if the mouse moves outside the data-tauri-drag-region
if (osName === "macos") {
document.addEventListener('mouseup', (e) => {
if (
// element has the magic data attribute
e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
// and was left mouse button
e.button === 0 &&
// and was double click
e.detail === 2 &&
// and the cursor hasn't moved from initial mousedown
e.clientX === x && e.clientY === y
) {
window.__TAURI_INTERNALS__.invoke('plugin:window|internal_toggle_maximize')
}
})
} }
}) })()