mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-19 00:03:13 +03:00
fix(app): end pane tab drags on unmount, session switch, and second pointer (#61787)
This commit is contained in:
committed by
GitHub
Unverified
parent
f12abcaf39
commit
5ff7cfb679
@@ -0,0 +1,217 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
DISPOSED_PANE_DRAG_STATE,
|
||||
INITIAL_PANE_DRAG_STATE,
|
||||
selectDraggingKey,
|
||||
selectDragLayer,
|
||||
selectMoveKey,
|
||||
transitionPaneDrag,
|
||||
type PaneDragEvent,
|
||||
type PaneDragState,
|
||||
type PaneDropTarget,
|
||||
} from "./pane-drag-machine"
|
||||
|
||||
const strip: PaneDropTarget = {
|
||||
pane: "side",
|
||||
before: "file://b.ts",
|
||||
strip: true,
|
||||
rect: { left: 10, top: 2, width: 2, height: 20 },
|
||||
}
|
||||
|
||||
const bottom: PaneDropTarget = {
|
||||
pane: "bottom",
|
||||
strip: false,
|
||||
rect: { left: 0, top: 400, width: 800, height: 200 },
|
||||
}
|
||||
|
||||
const down = (pointerId = 1, key = "file://a.ts", button = 0): PaneDragEvent => ({
|
||||
type: "pointer.down",
|
||||
pointerId,
|
||||
button,
|
||||
x: 0,
|
||||
y: 0,
|
||||
key,
|
||||
title: key.slice(7),
|
||||
})
|
||||
|
||||
const move = (x: number, y: number, target: PaneDropTarget | undefined, pointerId = 1): PaneDragEvent => ({
|
||||
type: "pointer.move",
|
||||
pointerId,
|
||||
x,
|
||||
y,
|
||||
target,
|
||||
})
|
||||
|
||||
function replay(events: PaneDragEvent[], state: PaneDragState = INITIAL_PANE_DRAG_STATE) {
|
||||
return events.reduce<{ state: PaneDragState; effects: unknown[] }>(
|
||||
(current, event) => {
|
||||
const next = transitionPaneDrag(current.state, event)
|
||||
|
||||
return { state: next.state, effects: [...current.effects, ...next.effects] }
|
||||
},
|
||||
{ state, effects: [] },
|
||||
)
|
||||
}
|
||||
|
||||
const pressedState = replay([down()]).state
|
||||
|
||||
const draggingState = replay([down(), move(4, 0, strip)]).state
|
||||
|
||||
describe("disposal", () => {
|
||||
test("mid-drag releases the gesture and unlocks the body without dropping", () => {
|
||||
const next = transitionPaneDrag(draggingState, { type: "disposed" })
|
||||
|
||||
expect(next.state).toBe(DISPOSED_PANE_DRAG_STATE)
|
||||
expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }, { type: "click-suppression.clear" }])
|
||||
|
||||
const late = replay([move(40, 0, bottom), { type: "pointer.up", pointerId: 1 }, down(2)], next.state)
|
||||
|
||||
expect(late.state).toBe(DISPOSED_PANE_DRAG_STATE)
|
||||
expect(late.effects).toEqual([])
|
||||
})
|
||||
|
||||
test("mid-press releases the gesture, and a later move starts no drag", () => {
|
||||
const next = transitionPaneDrag(pressedState, { type: "disposed" })
|
||||
|
||||
expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }, { type: "click-suppression.clear" }])
|
||||
expect(replay([move(40, 0, strip)], next.state).state).toBe(DISPOSED_PANE_DRAG_STATE)
|
||||
})
|
||||
|
||||
test("after a release clears the click swallower the release armed", () => {
|
||||
const released = transitionPaneDrag(draggingState, { type: "pointer.up", pointerId: 1 })
|
||||
|
||||
expect(released.effects).toContainEqual({ type: "click-suppression.arm" })
|
||||
expect(transitionPaneDrag(released.state, { type: "disposed" }).effects).toEqual([
|
||||
{ type: "click-suppression.clear" },
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("a second pointer", () => {
|
||||
test("neither starts a gesture nor moves or ends the first one", () => {
|
||||
const pressedSecond = transitionPaneDrag(pressedState, down(2, "file://b.ts"))
|
||||
|
||||
expect(pressedSecond.state).toBe(pressedState)
|
||||
expect(pressedSecond.effects).toEqual([])
|
||||
|
||||
const events: PaneDragEvent[] = [
|
||||
down(2, "file://b.ts"),
|
||||
move(90, 90, bottom, 2),
|
||||
{ type: "pointer.up", pointerId: 2 },
|
||||
{ type: "pointer.cancel", pointerId: 2 },
|
||||
]
|
||||
|
||||
events.forEach((event) => {
|
||||
const next = transitionPaneDrag(draggingState, event)
|
||||
|
||||
expect(next.state).toBe(draggingState)
|
||||
expect(next.effects).toEqual([])
|
||||
})
|
||||
expect(selectMoveKey(draggingState, 2, 90, 90)).toBeUndefined()
|
||||
expect(selectMoveKey(pressedState, 2, 90, 90)).toBeUndefined()
|
||||
|
||||
expect(transitionPaneDrag(draggingState, { type: "pointer.up", pointerId: 1 }).effects).toContainEqual({
|
||||
type: "drop",
|
||||
key: "file://a.ts",
|
||||
target: strip,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("cancelling", () => {
|
||||
test.each([
|
||||
{ type: "session.changed" },
|
||||
{ type: "blur" },
|
||||
{ type: "escape" },
|
||||
{ type: "pointer.cancel", pointerId: 1 },
|
||||
] satisfies PaneDragEvent[])("$type mid-drag returns to idle without dropping", (event) => {
|
||||
const next = transitionPaneDrag(draggingState, event)
|
||||
|
||||
expect(next.state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }])
|
||||
expect(transitionPaneDrag(next.state, { type: "pointer.up", pointerId: 1 }).effects).toEqual([])
|
||||
})
|
||||
|
||||
test.each([
|
||||
{ type: "session.changed" },
|
||||
{ type: "blur" },
|
||||
{ type: "escape" },
|
||||
{ type: "pointer.cancel", pointerId: 1 },
|
||||
] satisfies PaneDragEvent[])("$type mid-press returns to idle without touching the body", (event) => {
|
||||
const next = transitionPaneDrag(pressedState, event)
|
||||
|
||||
expect(next.state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(next.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }])
|
||||
})
|
||||
})
|
||||
|
||||
describe("activation", () => {
|
||||
test("only the primary button presses", () => {
|
||||
expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down(1, "file://a.ts", 1)).state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down(1, "file://a.ts", 2)).state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(transitionPaneDrag(INITIAL_PANE_DRAG_STATE, down()).effects).toEqual([{ type: "gesture.attach" }])
|
||||
})
|
||||
|
||||
test("a move under the threshold stays a press, and its release is a click", () => {
|
||||
expect(selectMoveKey(pressedState, 1, 3, 2)).toBeUndefined()
|
||||
expect(transitionPaneDrag(pressedState, move(3, 2, strip)).state).toBe(pressedState)
|
||||
expect(selectDraggingKey(pressedState)).toBeUndefined()
|
||||
|
||||
const released = transitionPaneDrag(pressedState, { type: "pointer.up", pointerId: 1 })
|
||||
|
||||
expect(released.state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(released.effects).toEqual([{ type: "gesture.cleanup", unlockBody: false }])
|
||||
})
|
||||
|
||||
test("crossing the threshold drags and locks the body", () => {
|
||||
expect(selectMoveKey(pressedState, 1, 4, 0)).toBe("file://a.ts")
|
||||
|
||||
const over = transitionPaneDrag(pressedState, move(4, 0, strip))
|
||||
|
||||
expect(selectDraggingKey(over.state)).toBe("file://a.ts")
|
||||
expect(selectDragLayer(over.state)).toMatchObject({ x: 4, y: 0, target: strip, title: "a.ts" })
|
||||
expect(over.effects).toEqual([{ type: "body.lock", cursor: "grabbing" }])
|
||||
expect(transitionPaneDrag(pressedState, move(4, 0, undefined)).effects).toEqual([{ type: "body.lock", cursor: "" }])
|
||||
})
|
||||
})
|
||||
|
||||
describe("dragging", () => {
|
||||
test("an unchanged move returns the same state", () => {
|
||||
const next = transitionPaneDrag(draggingState, move(4, 0, { ...strip, rect: { ...strip.rect } }))
|
||||
|
||||
expect(next.state).toBe(draggingState)
|
||||
expect(next.effects).toEqual([])
|
||||
})
|
||||
|
||||
test("a move over an equal target keeps the target object", () => {
|
||||
const next = transitionPaneDrag(draggingState, move(20, 0, { ...strip, rect: { ...strip.rect } }))
|
||||
|
||||
expect(selectDragLayer(next.state)?.target).toBe(strip)
|
||||
expect(next.effects).toEqual([])
|
||||
})
|
||||
|
||||
test("the cursor follows whether a drop zone is under the pointer", () => {
|
||||
const away = transitionPaneDrag(draggingState, move(20, 0, undefined))
|
||||
|
||||
expect(away.effects).toEqual([{ type: "body.cursor", cursor: "" }])
|
||||
expect(transitionPaneDrag(away.state, move(30, 0, bottom)).effects).toEqual([
|
||||
{ type: "body.cursor", cursor: "grabbing" },
|
||||
])
|
||||
|
||||
const released = transitionPaneDrag(away.state, { type: "pointer.up", pointerId: 1 })
|
||||
|
||||
expect(released.state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(released.effects).toEqual([{ type: "gesture.cleanup", unlockBody: true }, { type: "click-suppression.arm" }])
|
||||
})
|
||||
|
||||
test("a release over a drop zone drops the tab there", () => {
|
||||
const released = replay([move(30, 0, bottom), { type: "pointer.up", pointerId: 1 }], draggingState)
|
||||
|
||||
expect(released.state).toBe(INITIAL_PANE_DRAG_STATE)
|
||||
expect(released.effects).toEqual([
|
||||
{ type: "gesture.cleanup", unlockBody: true },
|
||||
{ type: "click-suppression.arm" },
|
||||
{ type: "drop", key: "file://a.ts", target: bottom },
|
||||
])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,263 @@
|
||||
import type { SessionPane } from "@/shell/state/session-tabs"
|
||||
|
||||
/** How far the pointer moves from the press before the tab drags; a shorter press is a click. */
|
||||
export const DRAG_THRESHOLD_PX = 4
|
||||
|
||||
export type Rect = { readonly left: number; readonly top: number; readonly width: number; readonly height: number }
|
||||
|
||||
/** Where a dragged tab lands: before a tab in a pane's strip (its end without `before`), or in a pane. */
|
||||
export type PaneDropTarget = {
|
||||
readonly pane: SessionPane
|
||||
readonly before?: string
|
||||
readonly strip: boolean
|
||||
readonly rect: Rect
|
||||
}
|
||||
|
||||
export type PaneDragState =
|
||||
| { readonly status: "idle" }
|
||||
/** The primary button is down on a tab, but has not moved far enough to drag it. */
|
||||
| {
|
||||
readonly status: "pressed"
|
||||
readonly pointerId: number
|
||||
readonly key: string
|
||||
readonly title: string
|
||||
readonly originX: number
|
||||
readonly originY: number
|
||||
}
|
||||
| {
|
||||
readonly status: "dragging"
|
||||
readonly pointerId: number
|
||||
readonly key: string
|
||||
readonly title: string
|
||||
readonly x: number
|
||||
readonly y: number
|
||||
/** Undefined away from every drop zone, where a release cancels. */
|
||||
readonly target: PaneDropTarget | undefined
|
||||
}
|
||||
| { readonly status: "disposed" }
|
||||
|
||||
export type DraggingPaneState = Extract<PaneDragState, { readonly status: "dragging" }>
|
||||
|
||||
export type PaneDragEvent =
|
||||
| {
|
||||
readonly type: "pointer.down"
|
||||
readonly pointerId: number
|
||||
readonly button: number
|
||||
readonly x: number
|
||||
readonly y: number
|
||||
readonly key: string
|
||||
readonly title: string
|
||||
}
|
||||
| {
|
||||
readonly type: "pointer.move"
|
||||
readonly pointerId: number
|
||||
readonly x: number
|
||||
readonly y: number
|
||||
readonly target: PaneDropTarget | undefined
|
||||
}
|
||||
| { readonly type: "pointer.up"; readonly pointerId: number }
|
||||
| { readonly type: "pointer.cancel"; readonly pointerId: number }
|
||||
| { readonly type: "escape" }
|
||||
| { readonly type: "blur" }
|
||||
/** The screen now shows another session, whose tabs the release must not move. */
|
||||
| { readonly type: "session.changed" }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type PaneDragCursor = "grabbing" | ""
|
||||
|
||||
export type PaneDragEffect =
|
||||
/** Listens on the window for the gesture's pointer, Escape and blur. */
|
||||
| { readonly type: "gesture.attach" }
|
||||
/** Stops listening, and restores the body's selection and cursor once a drag locked them. */
|
||||
| { readonly type: "gesture.cleanup"; readonly unlockBody: boolean }
|
||||
| { readonly type: "body.lock"; readonly cursor: PaneDragCursor }
|
||||
| { readonly type: "body.cursor"; readonly cursor: PaneDragCursor }
|
||||
/** Swallows clicks until the next task: the release over a strip would click the tab under it. */
|
||||
| { readonly type: "click-suppression.arm" }
|
||||
| { readonly type: "click-suppression.clear" }
|
||||
| { readonly type: "drop"; readonly key: string; readonly target: PaneDropTarget }
|
||||
|
||||
export type PaneDragTransition = {
|
||||
readonly state: PaneDragState
|
||||
readonly effects: readonly PaneDragEffect[]
|
||||
}
|
||||
|
||||
export const INITIAL_PANE_DRAG_STATE: PaneDragState = Object.freeze({ status: "idle" })
|
||||
|
||||
export const DISPOSED_PANE_DRAG_STATE: PaneDragState = Object.freeze({ status: "disposed" })
|
||||
|
||||
const EMPTY_EFFECTS: readonly PaneDragEffect[] = Object.freeze([])
|
||||
|
||||
const ATTACH: readonly PaneDragEffect[] = Object.freeze([{ type: "gesture.attach" }])
|
||||
|
||||
const CLEANUP_PRESSED: readonly PaneDragEffect[] = Object.freeze([{ type: "gesture.cleanup", unlockBody: false }])
|
||||
|
||||
const CLEANUP_DRAGGING: readonly PaneDragEffect[] = Object.freeze([{ type: "gesture.cleanup", unlockBody: true }])
|
||||
|
||||
const RELEASE: readonly PaneDragEffect[] = Object.freeze([...CLEANUP_DRAGGING, { type: "click-suppression.arm" }])
|
||||
|
||||
const LOCK_GRABBING: readonly PaneDragEffect[] = Object.freeze([{ type: "body.lock", cursor: "grabbing" }])
|
||||
|
||||
const LOCK_DEFAULT: readonly PaneDragEffect[] = Object.freeze([{ type: "body.lock", cursor: "" }])
|
||||
|
||||
const CURSOR_GRABBING: readonly PaneDragEffect[] = Object.freeze([{ type: "body.cursor", cursor: "grabbing" }])
|
||||
|
||||
const CURSOR_DEFAULT: readonly PaneDragEffect[] = Object.freeze([{ type: "body.cursor", cursor: "" }])
|
||||
|
||||
// The click swallower outlives the gesture by one task, so every disposal clears it.
|
||||
const DISPOSE_IDLE: readonly PaneDragEffect[] = Object.freeze([{ type: "click-suppression.clear" }])
|
||||
|
||||
const DISPOSE_PRESSED: readonly PaneDragEffect[] = Object.freeze([...CLEANUP_PRESSED, ...DISPOSE_IDLE])
|
||||
|
||||
const DISPOSE_DRAGGING: readonly PaneDragEffect[] = Object.freeze([...CLEANUP_DRAGGING, ...DISPOSE_IDLE])
|
||||
|
||||
export function transitionPaneDrag(state: PaneDragState, event: PaneDragEvent): PaneDragTransition {
|
||||
if (state.status === "disposed") return unchanged(state)
|
||||
|
||||
if (event.type === "disposed") return disposed(state)
|
||||
|
||||
if (state.status === "idle") return idle(state, event)
|
||||
|
||||
if (state.status === "pressed") return pressed(state, event)
|
||||
|
||||
return dragging(state, event)
|
||||
}
|
||||
|
||||
/** The dragged tab's key while a drag shows; undefined while idle, pressed or disposed. */
|
||||
export function selectDraggingKey(state: PaneDragState) {
|
||||
return state.status === "dragging" ? state.key : undefined
|
||||
}
|
||||
|
||||
/** The drag the floating layer draws, or undefined when nothing shows. */
|
||||
export function selectDragLayer(state: PaneDragState) {
|
||||
return state.status === "dragging" ? state : undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* The dragged tab's key when a move of `pointerId` to `(x, y)` changes the gesture: it is the gesture's pointer, and it
|
||||
* drags or crosses the threshold. The driver hit-tests drop zones only then.
|
||||
*/
|
||||
export function selectMoveKey(state: PaneDragState, pointerId: number, x: number, y: number) {
|
||||
if (state.status === "dragging") return state.pointerId === pointerId ? state.key : undefined
|
||||
|
||||
if (state.status !== "pressed" || state.pointerId !== pointerId) return undefined
|
||||
|
||||
return Math.hypot(x - state.originX, y - state.originY) >= DRAG_THRESHOLD_PX ? state.key : undefined
|
||||
}
|
||||
|
||||
function idle(state: Extract<PaneDragState, { status: "idle" }>, event: PaneDragEvent): PaneDragTransition {
|
||||
if (event.type !== "pointer.down" || event.button !== 0) return unchanged(state)
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "pressed",
|
||||
pointerId: event.pointerId,
|
||||
key: event.key,
|
||||
title: event.title,
|
||||
originX: event.x,
|
||||
originY: event.y,
|
||||
},
|
||||
effects: ATTACH,
|
||||
}
|
||||
}
|
||||
|
||||
// A second pointer (another finger, or a pen) neither starts a parallel gesture nor moves or ends this one.
|
||||
function pressed(state: Extract<PaneDragState, { status: "pressed" }>, event: PaneDragEvent): PaneDragTransition {
|
||||
if (event.type === "pointer.move") {
|
||||
if (selectMoveKey(state, event.pointerId, event.x, event.y) === undefined) return unchanged(state)
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "dragging",
|
||||
pointerId: state.pointerId,
|
||||
key: state.key,
|
||||
title: state.title,
|
||||
x: event.x,
|
||||
y: event.y,
|
||||
target: event.target,
|
||||
},
|
||||
effects: event.target ? LOCK_GRABBING : LOCK_DEFAULT,
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "pointer.up" || event.type === "pointer.cancel") {
|
||||
if (event.pointerId !== state.pointerId) return unchanged(state)
|
||||
|
||||
return { state: INITIAL_PANE_DRAG_STATE, effects: CLEANUP_PRESSED }
|
||||
}
|
||||
|
||||
if (event.type === "escape" || event.type === "blur" || event.type === "session.changed")
|
||||
return { state: INITIAL_PANE_DRAG_STATE, effects: CLEANUP_PRESSED }
|
||||
|
||||
return unchanged(state)
|
||||
}
|
||||
|
||||
function dragging(state: DraggingPaneState, event: PaneDragEvent): PaneDragTransition {
|
||||
if (event.type === "pointer.move") {
|
||||
if (event.pointerId !== state.pointerId) return unchanged(state)
|
||||
|
||||
const same = sameTarget(state.target, event.target)
|
||||
|
||||
if (same && event.x === state.x && event.y === state.y) return unchanged(state)
|
||||
|
||||
return {
|
||||
state: { ...state, x: event.x, y: event.y, target: same ? state.target : event.target },
|
||||
effects: cursor(state.target, event.target),
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "pointer.up") {
|
||||
if (event.pointerId !== state.pointerId) return unchanged(state)
|
||||
|
||||
return {
|
||||
state: INITIAL_PANE_DRAG_STATE,
|
||||
effects: state.target ? [...RELEASE, { type: "drop", key: state.key, target: state.target }] : RELEASE,
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "pointer.cancel") {
|
||||
if (event.pointerId !== state.pointerId) return unchanged(state)
|
||||
|
||||
return { state: INITIAL_PANE_DRAG_STATE, effects: CLEANUP_DRAGGING }
|
||||
}
|
||||
|
||||
if (event.type === "escape" || event.type === "blur" || event.type === "session.changed")
|
||||
return { state: INITIAL_PANE_DRAG_STATE, effects: CLEANUP_DRAGGING }
|
||||
|
||||
return unchanged(state)
|
||||
}
|
||||
|
||||
function disposed(state: PaneDragState): PaneDragTransition {
|
||||
if (state.status === "pressed") return { state: DISPOSED_PANE_DRAG_STATE, effects: DISPOSE_PRESSED }
|
||||
|
||||
if (state.status === "dragging") return { state: DISPOSED_PANE_DRAG_STATE, effects: DISPOSE_DRAGGING }
|
||||
|
||||
return { state: DISPOSED_PANE_DRAG_STATE, effects: DISPOSE_IDLE }
|
||||
}
|
||||
|
||||
// Away from every drop zone the release cancels, so the cursor stays normal there.
|
||||
function cursor(previous: PaneDropTarget | undefined, next: PaneDropTarget | undefined) {
|
||||
if (Boolean(previous) === Boolean(next)) return EMPTY_EFFECTS
|
||||
|
||||
return next ? CURSOR_GRABBING : CURSOR_DEFAULT
|
||||
}
|
||||
|
||||
function sameTarget(a: PaneDropTarget | undefined, b: PaneDropTarget | undefined) {
|
||||
if (a === b) return true
|
||||
|
||||
if (!a || !b) return false
|
||||
|
||||
return (
|
||||
a.pane === b.pane &&
|
||||
a.before === b.before &&
|
||||
a.strip === b.strip &&
|
||||
a.rect.left === b.rect.left &&
|
||||
a.rect.top === b.rect.top &&
|
||||
a.rect.width === b.rect.width &&
|
||||
a.rect.height === b.rect.height
|
||||
)
|
||||
}
|
||||
|
||||
function unchanged(state: PaneDragState): PaneDragTransition {
|
||||
return { state, effects: EMPTY_EFFECTS }
|
||||
}
|
||||
@@ -1,12 +1,18 @@
|
||||
import { Show } from "solid-js"
|
||||
import { createStore } from "solid-js/store"
|
||||
import { createEffect, createMemo, createSignal, on, onCleanup, Show, untrack, type Accessor } from "solid-js"
|
||||
import { Portal } from "solid-js/web"
|
||||
import { SESSION_PANES, type SessionPane } from "@/shell/state/session-tabs"
|
||||
|
||||
type Rect = { left: number; top: number; width: number; height: number }
|
||||
|
||||
/** Where a dragged tab lands: before a tab in a pane's strip (its end without `before`), or in a pane. */
|
||||
export type PaneDropTarget = { pane: SessionPane; before?: string; strip: boolean; rect: Rect }
|
||||
import {
|
||||
INITIAL_PANE_DRAG_STATE,
|
||||
selectDraggingKey,
|
||||
selectDragLayer,
|
||||
selectMoveKey,
|
||||
transitionPaneDrag,
|
||||
type PaneDragEffect,
|
||||
type PaneDragEvent,
|
||||
type PaneDragState,
|
||||
type PaneDropTarget,
|
||||
type Rect,
|
||||
} from "./pane-drag-machine"
|
||||
|
||||
/**
|
||||
* Drags side and bottom panel tabs between panes. The strips and pane bodies mark themselves with `data-pane-strip`,
|
||||
@@ -15,17 +21,17 @@ export type PaneDropTarget = { pane: SessionPane; before?: string; strip: boolea
|
||||
* bottom edge of the session opens the bottom pane.
|
||||
*/
|
||||
export function createPaneDrag(input: {
|
||||
/** The session whose tabs drag; a change cancels the gesture, so a release cannot move a tab in another session. */
|
||||
identity: Accessor<unknown>
|
||||
/** Panes that hold tabs; a pane without them is created by dropping on its zone. */
|
||||
shown: (pane: SessionPane) => boolean
|
||||
drop: (key: string, target: PaneDropTarget) => void
|
||||
}) {
|
||||
const [state, setState] = createStore<{
|
||||
key?: string
|
||||
title: string
|
||||
x: number
|
||||
y: number
|
||||
target?: PaneDropTarget
|
||||
}>({ title: "", x: 0, y: 0 })
|
||||
const [paneDrag, setPaneDrag] = createSignal<PaneDragState>(INITIAL_PANE_DRAG_STATE)
|
||||
const dragging = createMemo(() => selectDraggingKey(paneDrag()))
|
||||
const layer = createMemo(() => selectDragLayer(paneDrag()))
|
||||
|
||||
let clearSuppression: (() => void) | undefined
|
||||
|
||||
const hit = (x: number, y: number, key: string): PaneDropTarget | undefined => {
|
||||
const inside = (rect: DOMRect) =>
|
||||
@@ -99,96 +105,154 @@ export function createPaneDrag(input: {
|
||||
}
|
||||
}
|
||||
|
||||
const start = (event: PointerEvent, key: string, title: string) => {
|
||||
if (event.button !== 0) return
|
||||
const origin = { x: event.clientX, y: event.clientY }
|
||||
const drag = { active: false }
|
||||
const swallow = (click: MouseEvent) => click.stopPropagation()
|
||||
const dispatch = (event: PaneDragEvent) => {
|
||||
const previous = untrack(paneDrag)
|
||||
const next = transitionPaneDrag(previous, event)
|
||||
|
||||
const move = (moved: PointerEvent) => {
|
||||
if (!drag.active && Math.hypot(moved.clientX - origin.x, moved.clientY - origin.y) < 4) return
|
||||
if (next.state === previous) return false
|
||||
setPaneDrag(next.state)
|
||||
next.effects.forEach(run)
|
||||
|
||||
if (!drag.active) {
|
||||
drag.active = true
|
||||
document.body.style.userSelect = "none"
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
const target = hit(moved.clientX, moved.clientY, key)
|
||||
// Away from any drop target the release cancels, so the cursor stays normal.
|
||||
document.body.style.cursor = target ? "grabbing" : ""
|
||||
setState({ key, title, x: moved.clientX, y: moved.clientY, target })
|
||||
const move = (event: PointerEvent) => {
|
||||
// Only the gesture's pointer, once it drags, hit-tests the drop zones.
|
||||
const key = selectMoveKey(untrack(paneDrag), event.pointerId, event.clientX, event.clientY)
|
||||
|
||||
if (key === undefined) return
|
||||
dispatch({
|
||||
type: "pointer.move",
|
||||
pointerId: event.pointerId,
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
target: hit(event.clientX, event.clientY, key),
|
||||
})
|
||||
}
|
||||
|
||||
const up = (event: PointerEvent) => dispatch({ type: "pointer.up", pointerId: event.pointerId })
|
||||
const cancel = (event: PointerEvent) => dispatch({ type: "pointer.cancel", pointerId: event.pointerId })
|
||||
// Leaving the window loses the release, which would leave the drag stuck.
|
||||
const blur = () => dispatch({ type: "blur" })
|
||||
|
||||
const escape = (keyboard: KeyboardEvent) => {
|
||||
if (keyboard.key !== "Escape" || !dispatch({ type: "escape" })) return
|
||||
keyboard.preventDefault()
|
||||
keyboard.stopPropagation()
|
||||
}
|
||||
|
||||
const swallow = (click: MouseEvent) => click.stopPropagation()
|
||||
|
||||
const run = (effect: PaneDragEffect) => {
|
||||
if (effect.type === "gesture.attach") {
|
||||
window.addEventListener("pointermove", move)
|
||||
window.addEventListener("pointerup", up)
|
||||
window.addEventListener("pointercancel", cancel)
|
||||
window.addEventListener("blur", blur)
|
||||
window.addEventListener("keydown", escape, true)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const finish = (commit: boolean) => {
|
||||
if (effect.type === "gesture.cleanup") {
|
||||
window.removeEventListener("pointermove", move)
|
||||
window.removeEventListener("pointerup", up)
|
||||
window.removeEventListener("pointercancel", cancel)
|
||||
window.removeEventListener("blur", blur)
|
||||
window.removeEventListener("keydown", escape, true)
|
||||
|
||||
if (!drag.active) return
|
||||
const target = state.target
|
||||
if (!effect.unlockBody) return
|
||||
document.body.style.userSelect = ""
|
||||
document.body.style.cursor = ""
|
||||
setState({ key: undefined, target: undefined })
|
||||
// The release over the strip would click whatever tab is under it.
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (effect.type === "body.lock") {
|
||||
document.body.style.userSelect = "none"
|
||||
document.body.style.cursor = effect.cursor
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (effect.type === "body.cursor") {
|
||||
document.body.style.cursor = effect.cursor
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (effect.type === "click-suppression.arm") {
|
||||
clearSuppression?.()
|
||||
window.addEventListener("click", swallow, true)
|
||||
setTimeout(() => window.removeEventListener("click", swallow, true))
|
||||
const timer = setTimeout(() => clearSuppression?.())
|
||||
clearSuppression = () => {
|
||||
clearTimeout(timer)
|
||||
window.removeEventListener("click", swallow, true)
|
||||
clearSuppression = undefined
|
||||
}
|
||||
|
||||
if (commit && target) input.drop(key, target)
|
||||
return
|
||||
}
|
||||
|
||||
const up = () => finish(true)
|
||||
const cancel = () => finish(false)
|
||||
if (effect.type === "click-suppression.clear") {
|
||||
clearSuppression?.()
|
||||
|
||||
const escape = (keyboard: KeyboardEvent) => {
|
||||
if (keyboard.key !== "Escape") return
|
||||
keyboard.preventDefault()
|
||||
keyboard.stopPropagation()
|
||||
finish(false)
|
||||
return
|
||||
}
|
||||
|
||||
window.addEventListener("pointermove", move)
|
||||
window.addEventListener("pointerup", up)
|
||||
window.addEventListener("pointercancel", cancel)
|
||||
window.addEventListener("keydown", escape, true)
|
||||
input.drop(effect.key, effect.target)
|
||||
}
|
||||
|
||||
createEffect(on(input.identity, () => void dispatch({ type: "session.changed" }), { defer: true }))
|
||||
onCleanup(() => void dispatch({ type: "disposed" }))
|
||||
|
||||
return {
|
||||
/** The dragged tab's key while a drag runs. */
|
||||
dragging: () => state.key,
|
||||
start,
|
||||
dragging,
|
||||
start: (event: PointerEvent, key: string, title: string) =>
|
||||
void dispatch({
|
||||
type: "pointer.down",
|
||||
pointerId: event.pointerId,
|
||||
button: event.button,
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
key,
|
||||
title,
|
||||
}),
|
||||
Layer: () => (
|
||||
<Show when={state.key}>
|
||||
<Portal>
|
||||
<div class="fixed inset-0 z-[100] pointer-events-none" data-component="pane-drag-layer">
|
||||
<Show when={state.target}>
|
||||
{(target) => (
|
||||
<div
|
||||
data-slot={target().strip ? "pane-drop-indicator" : "pane-drop-zone"}
|
||||
data-pane={target().pane}
|
||||
class="absolute"
|
||||
classList={{
|
||||
"bg-v2-icon-icon-accent rounded-full": target().strip,
|
||||
"rounded-[10px] border border-v2-icon-icon-accent bg-[color-mix(in_srgb,var(--v2-icon-icon-accent)_12%,transparent)]":
|
||||
!target().strip,
|
||||
}}
|
||||
style={{
|
||||
left: `${target().rect.left}px`,
|
||||
top: `${target().rect.top}px`,
|
||||
width: `${target().rect.width}px`,
|
||||
height: `${target().rect.height}px`,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Show>
|
||||
<div
|
||||
class="absolute max-w-60 h-7 px-2.5 flex items-center rounded-md bg-v2-background-bg-base text-13-regular text-v2-text-text-base truncate shadow-[var(--v2-elevation-raised)] opacity-90"
|
||||
style={{ left: `${state.x + 12}px`, top: `${state.y + 12}px` }}
|
||||
>
|
||||
{state.title}
|
||||
<Show when={layer()}>
|
||||
{(current) => (
|
||||
<Portal>
|
||||
<div class="fixed inset-0 z-[100] pointer-events-none" data-component="pane-drag-layer">
|
||||
<Show when={current().target}>
|
||||
{(target) => (
|
||||
<div
|
||||
data-slot={target().strip ? "pane-drop-indicator" : "pane-drop-zone"}
|
||||
data-pane={target().pane}
|
||||
class="absolute"
|
||||
classList={{
|
||||
"bg-v2-icon-icon-accent rounded-full": target().strip,
|
||||
"rounded-[10px] border border-v2-icon-icon-accent bg-[color-mix(in_srgb,var(--v2-icon-icon-accent)_12%,transparent)]":
|
||||
!target().strip,
|
||||
}}
|
||||
style={{
|
||||
left: `${target().rect.left}px`,
|
||||
top: `${target().rect.top}px`,
|
||||
width: `${target().rect.width}px`,
|
||||
height: `${target().rect.height}px`,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Show>
|
||||
<div
|
||||
class="absolute max-w-60 h-7 px-2.5 flex items-center rounded-md bg-v2-background-bg-base text-13-regular text-v2-text-text-base truncate shadow-[var(--v2-elevation-raised)] opacity-90"
|
||||
style={{ left: `${current().x + 12}px`, top: `${current().y + 12}px` }}
|
||||
>
|
||||
{current().title}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Portal>
|
||||
</Portal>
|
||||
)}
|
||||
</Show>
|
||||
),
|
||||
}
|
||||
|
||||
@@ -110,6 +110,7 @@ function SessionScreenContent(props: {
|
||||
const bottomOpen = createMemo(() => bottomTabs() && session.layout.view().bottom.opened())
|
||||
|
||||
const drag = createPaneDrag({
|
||||
identity: session.layout.tabKey,
|
||||
shown: (pane) => (pane === "bottom" ? bottomOpen() : region.pane(pane).keys().length > 0),
|
||||
drop: (key, target) =>
|
||||
batch(() => {
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
import { afterEach, expect, test } from "bun:test"
|
||||
import { createRoot, createSignal } from "solid-js"
|
||||
import { createPaneDrag } from "@/runtime/plugin/pane-drag"
|
||||
import type { PaneDropTarget } from "@/runtime/plugin/pane-drag-machine"
|
||||
|
||||
const cleanups: (() => void)[] = []
|
||||
|
||||
afterEach(() => {
|
||||
cleanups.splice(0).forEach((cleanup) => cleanup())
|
||||
document.body.innerHTML = ""
|
||||
document.body.removeAttribute("style")
|
||||
document.documentElement.removeAttribute("style")
|
||||
})
|
||||
|
||||
// happy-dom lays nothing out and computes no default overflow, so the page states what a browser would.
|
||||
function mountStrip() {
|
||||
for (const element of [document.documentElement, document.body]) {
|
||||
element.style.overflowX = "visible"
|
||||
element.style.overflowY = "visible"
|
||||
}
|
||||
|
||||
const strip = document.createElement("div")
|
||||
strip.dataset.paneStrip = "side"
|
||||
strip.getBoundingClientRect = () => new DOMRect(0, 0, 200, 30)
|
||||
const tab = document.createElement("div")
|
||||
tab.dataset.paneTab = "file://b.ts"
|
||||
tab.getBoundingClientRect = () => new DOMRect(100, 0, 80, 30)
|
||||
strip.append(tab)
|
||||
document.body.append(strip)
|
||||
|
||||
return tab
|
||||
}
|
||||
|
||||
function setup() {
|
||||
const [identity, setIdentity] = createSignal("session-1")
|
||||
const drops: { key: string; target: PaneDropTarget }[] = []
|
||||
|
||||
const screen = createRoot((dispose) => ({
|
||||
dispose,
|
||||
drag: createPaneDrag({
|
||||
identity,
|
||||
shown: () => true,
|
||||
drop: (key, target) => drops.push({ key, target }),
|
||||
}),
|
||||
}))
|
||||
|
||||
cleanups.push(screen.dispose)
|
||||
|
||||
return { ...screen, drops, setIdentity }
|
||||
}
|
||||
|
||||
const pointer = (type: string, x: number, pointerId = 1) =>
|
||||
new PointerEvent(type, { pointerId, clientX: x, clientY: 10, button: 0 })
|
||||
|
||||
function startDrag(fixture: ReturnType<typeof setup>) {
|
||||
fixture.drag.start(pointer("pointerdown", 0), "file://a.ts", "a.ts")
|
||||
window.dispatchEvent(pointer("pointermove", 20))
|
||||
}
|
||||
|
||||
test("releasing over a strip drops the tab and swallows the release's click for one task", async () => {
|
||||
const tab = mountStrip()
|
||||
const fixture = setup()
|
||||
const clicks: string[] = []
|
||||
tab.addEventListener("click", () => clicks.push("click"))
|
||||
|
||||
startDrag(fixture)
|
||||
|
||||
expect(fixture.drag.dragging()).toBe("file://a.ts")
|
||||
expect(document.body.style.cursor).toBe("grabbing")
|
||||
|
||||
window.dispatchEvent(pointer("pointerup", 20))
|
||||
tab.click()
|
||||
|
||||
expect(fixture.drag.dragging()).toBeUndefined()
|
||||
expect(fixture.drops).toEqual([
|
||||
{ key: "file://a.ts", target: expect.objectContaining({ pane: "side", before: "file://b.ts", strip: true }) },
|
||||
])
|
||||
expect(clicks).toEqual([])
|
||||
|
||||
await new Promise((resolve) => setTimeout(resolve))
|
||||
tab.click()
|
||||
|
||||
expect(clicks).toEqual(["click"])
|
||||
})
|
||||
|
||||
test("disposing the screen mid-drag unlocks the body, and a later release drops nothing", () => {
|
||||
mountStrip()
|
||||
const fixture = setup()
|
||||
|
||||
startDrag(fixture)
|
||||
|
||||
expect(document.body.style.userSelect).toBe("none")
|
||||
|
||||
fixture.dispose()
|
||||
|
||||
expect(document.body.style.userSelect).toBe("")
|
||||
expect(document.body.style.cursor).toBe("")
|
||||
|
||||
window.dispatchEvent(pointer("pointerup", 20))
|
||||
|
||||
expect(fixture.drops).toEqual([])
|
||||
})
|
||||
|
||||
test("switching session mid-drag cancels it, and the release drops nothing", () => {
|
||||
mountStrip()
|
||||
const fixture = setup()
|
||||
|
||||
startDrag(fixture)
|
||||
fixture.setIdentity("session-2")
|
||||
|
||||
expect(fixture.drag.dragging()).toBeUndefined()
|
||||
expect(document.body.style.userSelect).toBe("")
|
||||
|
||||
window.dispatchEvent(pointer("pointerup", 20))
|
||||
|
||||
expect(fixture.drops).toEqual([])
|
||||
})
|
||||
|
||||
test("a second pointer neither moves nor ends the first pointer's drag", () => {
|
||||
mountStrip()
|
||||
const fixture = setup()
|
||||
|
||||
startDrag(fixture)
|
||||
fixture.drag.start(pointer("pointerdown", 150, 2), "file://c.ts", "c.ts")
|
||||
window.dispatchEvent(pointer("pointermove", 190, 2))
|
||||
window.dispatchEvent(pointer("pointerup", 190, 2))
|
||||
|
||||
expect(fixture.drag.dragging()).toBe("file://a.ts")
|
||||
expect(fixture.drops).toEqual([])
|
||||
|
||||
window.dispatchEvent(pointer("pointerup", 20))
|
||||
|
||||
expect(fixture.drops).toEqual([
|
||||
{ key: "file://a.ts", target: expect.objectContaining({ pane: "side", before: "file://b.ts" }) },
|
||||
])
|
||||
})
|
||||
Reference in New Issue
Block a user