Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 14 additions & 24 deletions apps/desktop/src/lib/toastDrag.test.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
import { describe, expect, test } from "bun:test";
import {
dragExit,
dragOpacity,
dragState,
holdSelection,
isClick,
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", () => {
Expand All @@ -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);
Expand Down
58 changes: 26 additions & 32 deletions apps/desktop/src/lib/toastDrag.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,50 @@
// 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.
// 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;
/** 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. */
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 {
Expand Down
14 changes: 8 additions & 6 deletions apps/desktop/src/ui/Toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions scripts/comment-baseline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down