From 4dc7225b55952fa9378dc5351409c3ba202e0c49 Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Sun, 22 Nov 2026 20:02:57 +0100 Subject: [PATCH 1/2] fix(desktop): let a toast slide one way only, right The card followed the pointer left, right and down, so a drag wobbled diagonally. It now slides right toward the window's edge, rubber-bands at a sixth any other way, and dismisses past 48px or on a quick flick right. --- apps/desktop/src/lib/toastDrag.test.ts | 38 +++++++---------- apps/desktop/src/lib/toastDrag.ts | 58 ++++++++++++-------------- apps/desktop/src/ui/Toaster.tsx | 14 ++++--- docs/design.md | 10 ++--- 4 files changed, 54 insertions(+), 66 deletions(-) diff --git a/apps/desktop/src/lib/toastDrag.test.ts b/apps/desktop/src/lib/toastDrag.test.ts index f744949e4..e745bfbbe 100644 --- a/apps/desktop/src/lib/toastDrag.test.ts +++ b/apps/desktop/src/lib/toastDrag.test.ts @@ -1,6 +1,5 @@ import { describe, expect, test } from "bun:test"; import { - dragExit, dragOpacity, dragState, holdSelection, @@ -8,33 +7,30 @@ import { type SelectionDoc, startsDrag, TOAST_DISMISS_PX, + TOAST_FLICK_PX, } from "./toastDrag"; describe("toast drag", () => { - test("a small move is a snap back, not a dismissal", () => { + test("only a drag right past 48px dismisses", () => { expect(dragState(TOAST_DISMISS_PX, 0).dismiss).toBe(false); - expect(dragState(-TOAST_DISMISS_PX, 0).dismiss).toBe(false); - expect(dragState(0, TOAST_DISMISS_PX / 2 - 1).dismiss).toBe(false); - }); - - test("past 48px sideways or 24px down it dismisses, the way it is going", () => { expect(dragState(TOAST_DISMISS_PX + 1, 0).dismiss).toBe(true); - expect(dragState(TOAST_DISMISS_PX + 1, 0).direction).toBe("right"); - expect(dragState(-TOAST_DISMISS_PX - 1, 0).direction).toBe("left"); - expect(dragState(0, TOAST_DISMISS_PX / 2).direction).toBe("down"); - // Down wins when the drag is more vertical than sideways. - expect(dragState(10, 40).direction).toBe("down"); - expect(dragState(60, 20).direction).toBe("right"); - // Upward never dismisses: it resists. + expect(dragState(-200, 0).dismiss).toBe(false); + expect(dragState(0, 200).dismiss).toBe(false); expect(dragState(0, -200).dismiss).toBe(false); }); - test("sideways follows fully, upward resists at a sixth", () => { + test("a quick flick right dismisses short of 48px, a slow or tiny one does not", () => { + expect(dragState(30, 0, 40).dismiss).toBe(true); + expect(dragState(30, 0, 400).dismiss).toBe(false); + expect(dragState(TOAST_FLICK_PX - 1, 0, 5).dismiss).toBe(false); + expect(dragState(-30, 0, 40).dismiss).toBe(false); + }); + + test("right follows fully, every other way resists at a sixth", () => { expect(dragState(40, 0)).toMatchObject({ dx: 40, dy: 0, away: 40 }); + expect(dragState(-60, 0)).toMatchObject({ dx: -10, away: 0 }); + expect(dragState(0, 60).dy).toBe(10); expect(dragState(0, -60).dy).toBe(-10); - expect(dragState(0, 60).dy).toBe(60); - // An upward drag resists, so it never travels far enough to fade. - expect(dragState(0, -60).away).toBe(0); }); test("the card fades as it travels but never below a fifth", () => { @@ -43,12 +39,6 @@ describe("toast drag", () => { expect(dragOpacity(1000)).toBe(0.2); }); - test("a dismissed card flies off the way it was dragged", () => { - expect(dragExit("left", -60)).toBe("translateX(-120%)"); - expect(dragExit("right", 60)).toBe("translateX(120%)"); - expect(dragExit("down", 4)).toBe("translateY(140%)"); - }); - test("a press that barely moves is a click", () => { expect(isClick(0, 0)).toBe(true); expect(isClick(2, 2)).toBe(true); diff --git a/apps/desktop/src/lib/toastDrag.ts b/apps/desktop/src/lib/toastDrag.ts index 84d5e55ee..a99d302cb 100644 --- a/apps/desktop/src/lib/toastDrag.ts +++ b/apps/desktop/src/lib/toastDrag.ts @@ -1,44 +1,43 @@ // What a drag on a toast does, as pure arithmetic and one DOM guard -// (ui/Toaster.tsx): the card follows sideways fully and upward at a sixth, -// drops away downward, and dismisses past 48px sideways or 24px down. A press -// that barely moves is a click, which dismisses too. A drag never selects text -// anywhere: the guard below turns selection off on the document and blocks -// `selectstart` until it is released, and every way a drag ends releases it. +// (ui/Toaster.tsx): the card slides one way only, right toward the window's +// edge. Any other way it rubber-bands at a sixth and snaps back. It dismisses +// past 48px, or on a quick flick right. A press that barely moves is a click, +// which dismisses too. A drag never selects text anywhere: the guard below +// turns selection off on the document and blocks `selectstart` until it is +// released, and every way a drag ends releases it. /** How far a drag must travel to dismiss the toast. */ export const TOAST_DISMISS_PX = 48; -/** Upward drags resist: the stack sits in the window's bottom corner and leaves downward. */ -const UP_RESIST = 6; +/** A flick faster than this, in px/ms over the whole drag, dismisses short of 48px. */ +export const TOAST_FLICK_SPEED = 0.5; +/** ...but only once it has gone this far, so a fast twitch is not a dismissal. */ +export const TOAST_FLICK_PX = 12; +/** Every way but right resists. */ +const RESIST = 6; /** Less travel than this, end to end, is a click on the card rather than a drag. */ export const TOAST_CLICK_PX = 4; export const TOAST_DRAG_MS = 160; -export type SwipeDirection = "left" | "right" | "down"; - export interface DragState { - /** Sideways movement, unmodified. */ + /** Sideways offset: full to the right, resisted to the left. */ dx: number; - /** Vertical movement: full downward, resisted upward. */ + /** Vertical offset, resisted both ways. */ dy: number; - /** How far it has travelled from its place, for the fade. */ + /** How far it has travelled toward the edge, for the fade. */ away: number; dismiss: boolean; - /** Which way it leaves when dismissed; null while it snaps back. */ - direction: SwipeDirection | null; } -export function dragState(dx: number, dy: number): DragState { - const followY = dy > 0 ? dy : dy / UP_RESIST; - const away = Math.max(Math.abs(dx), followY); - const dismiss = Math.abs(dx) > TOAST_DISMISS_PX || dy >= TOAST_DISMISS_PX / 2; - const direction: SwipeDirection | null = !dismiss - ? null - : Math.abs(dx) > dy - ? dx < 0 - ? "left" - : "right" - : "down"; - return { dx, dy: followY, away, dismiss, direction }; +/** `ms` is how long the drag has lasted, for a flick; omit it while the pointer moves. */ +export function dragState(dx: number, dy: number, ms = Number.POSITIVE_INFINITY): DragState { + const followX = dx > 0 ? dx : dx / RESIST; + const flick = dx >= TOAST_FLICK_PX && dx / Math.max(ms, 1) > TOAST_FLICK_SPEED; + return { + dx: followX, + dy: dy / RESIST, + away: Math.max(0, dx), + dismiss: dx > TOAST_DISMISS_PX || flick, + }; } /** Opacity while dragging: it fades as it travels, never below 0.2. */ @@ -46,11 +45,8 @@ export function dragOpacity(away: number): number { return Math.max(0.2, 1 - away / 200); } -/** Where a dismissed card flies to. */ -export function dragExit(direction: SwipeDirection, dx: number): string { - if (direction === "down") return "translateY(140%)"; - return `translateX(${Math.sign(dx) * 120}%)`; -} +/** Where a dismissed card flies to: off the window's right edge. */ +export const TOAST_EXIT = "translateX(120%)"; /** A press and release this close together is a click on the card: it dismisses. */ export function isClick(dx: number, dy: number): boolean { diff --git a/apps/desktop/src/ui/Toaster.tsx b/apps/desktop/src/ui/Toaster.tsx index 667798f47..ffe5c9112 100644 --- a/apps/desktop/src/ui/Toaster.tsx +++ b/apps/desktop/src/ui/Toaster.tsx @@ -4,19 +4,19 @@ // height and hide their text, so only their edges peek above it. Info and // success leave after 2.5 s, errors after 5 s. // The pointer or focus on the stack, a drag, or the window in the background -// stops the clock. A click anywhere on the card, ×, Escape, or a drag past 48px -// sideways or 24px down dismisses; a drag never selects text anywhere. +// stops the clock. A click anywhere on the card, ×, Escape, or a drag right past +// 48px (or a quick flick right) dismisses; a drag never selects text anywhere. import { type CSSProperties, type ReactNode, useCallback, useEffect, useRef, useState } from "react"; import { observeSize } from "../lib/resize"; import { - dragExit, dragOpacity, dragState, holdSelection, isClick, startsDrag, TOAST_DRAG_MS, + TOAST_EXIT, } from "../lib/toastDrag"; import { dismissToast, expireToasts, setToastsPaused, type Toast, useApp } from "../state/store"; import { nextExpiry } from "../state/toasts"; @@ -54,6 +54,8 @@ interface DragSession { pointerId: number; x: number; y: number; + /** When the press began, for a flick. */ + at: number; dx: number; dy: number; /** Puts the document back; safe to call twice. */ @@ -89,9 +91,8 @@ function ToastCard({ el.releasePointerCapture(session.pointerId); } if (flyOut && session && el) { - const { dx, direction } = dragState(session.dx, session.dy); el.style.transition = `transform ${TOAST_DRAG_MS}ms var(--ease-snappy), opacity ${TOAST_DRAG_MS}ms ease-out`; - el.style.transform = dragExit(direction ?? "down", dx); + el.style.transform = TOAST_EXIT; el.style.opacity = "0"; setFlung(true); dismissToast(toast.id); @@ -126,6 +127,7 @@ function ToastCard({ pointerId: e.pointerId, x: e.clientX, y: e.clientY, + at: e.timeStamp, dx: 0, dy: 0, release: holdSelection(document), @@ -157,7 +159,7 @@ function ToastCard({ dismissToast(toast.id); return; } - end(dragState(session.dx, session.dy).dismiss); + end(dragState(session.dx, session.dy, e.timeStamp - session.at).dismiss); }; const onPointerCancel = () => end(false); diff --git a/docs/design.md b/docs/design.md index 98ea5a644..72e9c0710 100644 --- a/docs/design.md +++ b/docs/design.md @@ -450,8 +450,8 @@ that change use `tabular`. `transform`, `opacity` and those two; never `filter` or `box-shadow` on something that moves every frame. - Toasts rise in with `rc-toast-in` (220ms, from 10px below) and drop away - in 140ms `ease-in`; a drag follows the pointer and snaps back or flies off - in 160ms, and a card shown meanwhile takes the top of the stack. At rest only + in 140ms `ease-in`; a drag slides it right only (other ways rubber-band at + a sixth) and it snaps back or flies off the right edge in 160ms, and a card shown meanwhile takes the top of the stack. At rest only the front card is in flow, the ones behind peeking 6px above it; hovering or focusing the stack fans them out 8px apart (`transform` only). - A new transcript row rises in with `rc-row-in` (260ms, fade + 6px), once: @@ -721,9 +721,9 @@ a raw element. `?catalog` in a dev build shows them all on one page. Info and success leave after 2.5s, errors after 5s. The pointer or focus on the stack, a drag, and a window in the background stop every clock, which resumes with what was left. A click anywhere on the card, ×, Escape (after a - draft or an open overlay has taken it), or a drag past 48px sideways or 24px - down dismisses; a drag follows the - pointer and snaps back in 160ms, and never selects text. Use a + draft or an open overlay has taken it), or a drag right past 48px or a quick + flick right dismisses; the card slides right only, rubber-bands any other + way, snaps back in 160ms, and never selects text. Use a toast for what is not tied to a message (copied, engine events, errors of an action), not to repeat what the screen already shows (no pin, archive, theme, harness-switch or steering notice); an error about a thread's turn From 4872b2cef22042d011bfde364a810faf81d28155 Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Sun, 22 Nov 2026 20:22:40 +0100 Subject: [PATCH 2/2] fix(desktop): keep toastDrag comments under the ratchet --- apps/desktop/src/lib/toastDrag.ts | 12 +++++------- scripts/comment-baseline.txt | 4 ++-- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/apps/desktop/src/lib/toastDrag.ts b/apps/desktop/src/lib/toastDrag.ts index a99d302cb..8559372cb 100644 --- a/apps/desktop/src/lib/toastDrag.ts +++ b/apps/desktop/src/lib/toastDrag.ts @@ -1,10 +1,8 @@ -// What a drag on a toast does, as pure arithmetic and one DOM guard -// (ui/Toaster.tsx): the card slides one way only, right toward the window's -// edge. Any other way it rubber-bands at a sixth and snaps back. It dismisses -// past 48px, or on a quick flick right. A press that barely moves is a click, -// which dismisses too. A drag never selects text anywhere: the guard below -// turns selection off on the document and blocks `selectstart` until it is -// released, and every way a drag ends releases it. +// Pure drag math and one DOM guard (ui/Toaster.tsx): the card slides right +// only and rubber-bands any other way; past 48px or a quick flick right it +// dismisses, and a barely-moving press is a click. A drag never selects text: +// the guard turns selection off and blocks `selectstart` until released, and +// every way a drag ends releases it. /** How far a drag must travel to dismiss the toast. */ export const TOAST_DISMISS_PX = 48; diff --git a/scripts/comment-baseline.txt b/scripts/comment-baseline.txt index 6841de3a7..ba8f72a00 100644 --- a/scripts/comment-baseline.txt +++ b/scripts/comment-baseline.txt @@ -160,8 +160,8 @@ 9 apps/desktop/src/lib/themes.ts 4 apps/desktop/src/lib/title.test.ts 1 apps/desktop/src/lib/title.ts -5 apps/desktop/src/lib/toastDrag.test.ts -6 apps/desktop/src/lib/toastDrag.ts +2 apps/desktop/src/lib/toastDrag.test.ts +5 apps/desktop/src/lib/toastDrag.ts 5 apps/desktop/src/lib/toolBody.ts 4 apps/desktop/src/lib/toolOutput.test.ts 10 apps/desktop/src/lib/toolOutput.ts