Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Hold the escape click guard until the button comes up (#11568)

Michael Hanbe072ee2ff03
diff --git a/studio/frontend/src/features/chat/hooks/use-sidebar-drag.ts b/studio/frontend/src/features/chat/hooks/use-sidebar-drag.ts
index 52af3f2d130..4da87c2912b 100644
--- a/studio/frontend/src/features/chat/hooks/use-sidebar-drag.ts
+++ b/studio/frontend/src/features/chat/hooks/use-sidebar-drag.ts
@@ -254,6 +254,7 @@ export function useSidebarDrag(options: UseSidebarDragOptions): SidebarDragApi {
const pointerId = event.pointerId;
const at = { x: startX, y: startY };
let started = false;
+ let escaped = false;
let frame = 0;
const detach = () => {
@@ -304,7 +305,7 @@ export function useSidebarDrag(options: UseSidebarDragOptions): SidebarDragApi {
// Every window handler answers only the pointer that started the gesture. A second
// pointer, a finger on a touch screen above all, is not this drag.
function onMove(moved: PointerEvent) {
- if (moved.pointerId !== pointerId) return;
+ if (moved.pointerId !== pointerId || escaped) return;
at.x = moved.clientX;
at.y = moved.clientY;
if (!started) {
@@ -337,7 +338,9 @@ export function useSidebarDrag(options: UseSidebarDragOptions): SidebarDragApi {
if (released.pointerId !== pointerId) return;
detach();
if (!started) return;
+ // A drop or an escape: either way this release must not click the row it landed on.
swallowClick();
+ if (escaped) return;
const dragged = sidebarDragSource();
const aimed = aim(released.clientX, released.clientY);
clear();
@@ -346,18 +349,23 @@ export function useSidebarDrag(options: UseSidebarDragOptions): SidebarDragApi {
}
}
- function onCancel(cancelled: PointerEvent) {
- if (cancelled.pointerId !== pointerId) return;
+ function onCancel(aborted: PointerEvent) {
+ if (aborted.pointerId !== pointerId) return;
detach();
if (started) clear();
}
function onKey(pressed: KeyboardEvent) {
- if (pressed.key !== "Escape") return;
- detach();
- if (!started) return;
+ if (pressed.key !== "Escape" || escaped) return;
+ if (!started) {
+ // Nothing was lifted, so the press is abandoned and a click after it is the row's.
+ detach();
+ return;
+ }
pressed.preventDefault();
- swallowClick();
+ // The button is still down. The gesture keeps its listeners so the release, whenever
+ // it comes, is still ours to swallow: a guard armed now would be long gone by then.
+ escaped = true;
clear();
}
diff --git a/studio/frontend/tests/sidebar-row-drag.test.ts b/studio/frontend/tests/sidebar-row-drag.test.ts
index 5787ad837e4..50cff29d8b6 100644
--- a/studio/frontend/tests/sidebar-row-drag.test.ts
+++ b/studio/frontend/tests/sidebar-row-drag.test.ts
@@ -993,9 +993,9 @@ test("the edge keeps scrolling while the pointer rests on it", () => {
// could drive, and drop, a drag the mouse started, which is exactly what touch is kept out of.
test("only the pointer that started the drag drives it", () => {
for (const handler of [
- /function onMove\(moved: PointerEvent\) \{\n\s*if \(moved\.pointerId !== pointerId\) return;/,
+ /function onMove\(moved: PointerEvent\) \{\n\s*if \(moved\.pointerId !== pointerId \|\| escaped\) return;/,
/function onUp\(released: PointerEvent\) \{\n\s*if \(released\.pointerId !== pointerId\) return;/,
- /function onCancel\(cancelled: PointerEvent\) \{\n\s*if \(cancelled\.pointerId !== pointerId\) return;/,
+ /function onCancel\(aborted: PointerEvent\) \{\n\s*if \(aborted\.pointerId !== pointerId\) return;/,
]) {
assert.match(HOOK, handler);
}
@@ -1006,3 +1006,29 @@ test("only the pointer that started the drag drives it", () => {
assert.match(HOOK, /end: \(\) => \{\n\s*detach\(\);\n\s*if \(started\) clear\(\);/);
assert.match(HOOK, /if \(press\.current === self\) press\.current = null;/);
});
+
+// Escape cancels while the button is still down, and the release comes later. A click guard
+// armed at the keypress is torn down on the next tick, long before that release, so the row
+// under the pointer would open the chat the drag was cancelled out of.
+test("escape keeps the click guard until the button is released", () => {
+ assert.match(
+ HOOK,
+ /if \(pressed\.key !== "Escape" \|\| escaped\) return;\n\s*if \(!started\) \{[^]*?detach\(\);\n\s*return;\n\s*\}/,
+ );
+ // The gesture keeps its listeners: only the release detaches and swallows.
+ const onKey = HOOK.slice(
+ HOOK.indexOf("function onKey(pressed: KeyboardEvent)"),
+ HOOK.indexOf("window.addEventListener(\"pointermove\", onMove);"),
+ );
+ assert.ok(
+ !/escaped = true;[^]*?detach\(\);/.test(onKey),
+ "escape must not detach while the button is still down",
+ );
+ assert.match(onKey, /escaped = true;\n\s*clear\(\);/);
+ assert.match(
+ HOOK,
+ /swallowClick\(\);\n\s*if \(escaped\) return;/,
+ );
+ // And a cancelled drag does not come back to life on the next move.
+ assert.match(HOOK, /if \(moved\.pointerId !== pointerId \|\| escaped\) return;/);
+});