fixes window dragging when window was not focused

This commit is contained in:
Gellert Hegyi 2025-01-17 13:23:37 +01:00
parent 5d6878e308
commit 8695aaa477
2 changed files with 15 additions and 1 deletions

View file

@ -27,15 +27,22 @@ NSView* viewUnderneathPoint(NSView* self, NSPoint point) {
} }
NSView* swizzledHitTest(id obj, SEL sel, NSPoint point) { NSView* swizzledHitTest(id obj, SEL sel, NSPoint point) {
NSView* view = obj;
NSView* originalReturn = NSView* originalReturn =
((NSView*(*) (id, SEL, NSPoint))g_originalHitTest)(obj, sel, point); ((NSView*(*) (id, SEL, NSPoint))g_originalHitTest)(obj, sel, point);
// Do the original implementation when window is not focused, so window can be
// dragged immediately even, when not focused.
if (!view.window.isKeyWindow) {
return originalReturn;
}
objc_setAssociatedObject(obj, objc_setAssociatedObject(obj,
&kIsDraggableKey, &kIsDraggableKey,
@(originalReturn == nil), @(originalReturn == nil),
OBJC_ASSOCIATION_COPY_NONATOMIC); OBJC_ASSOCIATION_COPY_NONATOMIC);
NSView* viewUnderPoint = viewUnderneathPoint(obj, point); NSView* viewUnderPoint = viewUnderneathPoint(view, point);
return [viewUnderPoint hitTest:point]; return [viewUnderPoint hitTest:point];
} }

View file

@ -31,11 +31,14 @@
<input type="text" placeholder="type..." /> <input type="text" placeholder="type..." />
<button id="foo">cannot be clicked</button> <button id="foo">cannot be clicked</button>
<button id="dbl2">can be doubleclicked</button>
</div> </div>
<input type="text" placeholder="type..." /> <input type="text" placeholder="type..." />
<button id="bar">can be clicked</button> <button id="bar">can be clicked</button>
<button id="dbl">can be doubleclicked</button>
<script> <script>
window.onload = () => { window.onload = () => {
@ -43,6 +46,8 @@
const foo = document.getElementById('foo'); const foo = document.getElementById('foo');
const bar = document.getElementById('bar'); const bar = document.getElementById('bar');
const dbl = document.getElementById('dbl');
const dbl2 = document.getElementById('dbl2');
const span = document.querySelector('span'); const span = document.querySelector('span');
const inc = () => { const inc = () => {
@ -51,6 +56,8 @@
foo.addEventListener('click', inc); foo.addEventListener('click', inc);
bar.addEventListener('click', inc); bar.addEventListener('click', inc);
dbl.addEventListener('dblclick', inc);
dbl2.addEventListener('dblclick', inc);
} }
</script> </script>
</body> </body>