Skip to content

Commit 8f8729d

Browse files
fix(core): allow canceling data-tauri-drag-region maximization on macOS, closes #8306 (#8312)
* fix(core): allow canceling `data-tauri-drag-region` maximization on macOS, closes #8306 * Update .changes/tauri-data-drag-region-macos-maximize.md * fix typo * cancel if mouse moves * Update tauri-data-drag-region-macos-maximize.md [skip ci] * Update core/tauri/scripts/core.js [skip ci] --------- Co-authored-by: Lucas Fernandes Nogueira <lucas@tauri.app>
1 parent 446fc99 commit 8f8729d

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'tauri': 'patch:bug'
3+
---
4+
5+
On macOS, allow cancelling maximization when doubleclick happens on `data-tauri-drag-region` by simply keeping the left moust button pressed and then moving the mouse away of the starting position of the click, which is consistent with the native behavior of macOS.

core/tauri/scripts/core.js

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,16 +142,40 @@
142142
)
143143
}
144144

145-
// drag region
145+
//-----------------------//
146+
// data-tauri-drag-region
147+
//
148+
// drag on mousedown and maximize on double click on Windows and Linux
149+
// while macOS macos maximization should be on mouseup and if the mouse
150+
// moves after the double click, it should be cancelled (see https://github.com/tauri-apps/tauri/issues/8306)
151+
//-----------------------//
152+
const TAURI_DRAG_REGION_ATTR = 'data-tauri-drag-region';
153+
let x = 0, y = 0;
146154
document.addEventListener('mousedown', (e) => {
147-
if (e.target.hasAttribute('data-tauri-drag-region') && e.button === 0) {
155+
if (
156+
// element has the magic data attribute
157+
e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
158+
// and was left mouse button
159+
e.button === 0 &&
160+
// and was normal click to drag or double click to maximize
161+
(e.detail === 1 || e.detail === 2)
162+
) {
163+
164+
// macOS maximization happens on `mouseup`,
165+
// so we save needed state and early return
166+
if (osName === 'macos' && e.detail == 2) {
167+
x = e.clientX
168+
y = e.clientY
169+
return
170+
}
171+
148172
// prevents text cursor
149173
e.preventDefault()
174+
150175
// fix #2549: double click on drag region edge causes content to maximize without window sizing change
151176
// https://github.com/tauri-apps/tauri/issues/2549#issuecomment-1250036908
152177
e.stopImmediatePropagation()
153178

154-
// start dragging if the element has a `tauri-drag-region` data attribute and maximize on double-clicking it
155179
window.__TAURI_INVOKE__('tauri', {
156180
__tauriModule: 'Window',
157181
message: {
@@ -165,6 +189,34 @@
165189
})
166190
}
167191
})
192+
// on macOS we maximze on mouseup instead, to match the system behavior where maximization can be canceled
193+
// if the mouse moves outside the data-tauri-drag-region
194+
if (osName === "macos") {
195+
document.addEventListener('mouseup', (e) => {
196+
if (
197+
// element has the magic data attribute
198+
e.target.hasAttribute(TAURI_DRAG_REGION_ATTR) &&
199+
// and was left mouse button
200+
e.button === 0 &&
201+
// and was double click
202+
e.detail === 2 &&
203+
// and the cursor hasn't moved from initial mousedown
204+
e.clientX === x && e.clientY === y
205+
) {
206+
window.__TAURI_INVOKE__('tauri', {
207+
__tauriModule: 'Window',
208+
message: {
209+
cmd: 'manage',
210+
data: {
211+
cmd: {
212+
type: '__toggleMaximize'
213+
}
214+
}
215+
}
216+
})
217+
}
218+
})
219+
}
168220

169221
let permissionSettable = false
170222
let permissionValue = 'default'

0 commit comments

Comments
 (0)