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
39 changes: 36 additions & 3 deletions apps/desktop/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -1251,8 +1251,14 @@
0 0 0 1px var(--rc-separator);
transition: background-color 160ms ease-out;
}
/* A toast is a small sibling of the composer: its surface, its edge and a lift that reads over the
panel or transcript under it. */
.rc-toast {
background: var(--rc-glass-bg-strong);
background: var(--rc-composer-bg);
box-shadow:
0 3px 12px -4px var(--rc-shadow-soft),
0 10px 28px -14px var(--rc-shadow-soft),
0 0 0 1px var(--rc-separator);
}
/* The composer and the question panel over it share the sidebar surface, glass, shadow and corner. */
.rc-composer-card,
Expand Down Expand Up @@ -1405,7 +1411,7 @@
height: 56px;
background: linear-gradient(to bottom, transparent 0, var(--rc-bg) 56px);
}
/* A toast rises into the window's bottom-right corner and drops away. */
/* A toast rises into its corner and drops away. */
@keyframes rc-toast-in {
from {
opacity: 0;
Expand All @@ -1424,13 +1430,31 @@
.rc-toast-out {
animation: rc-toast-out 140ms ease-in forwards;
}
/* At a top position it drops in from above and lifts away. */
@keyframes rc-toast-in-top {
from {
opacity: 0;
transform: translateY(-10px) scale(0.98);
}
}
@keyframes rc-toast-out-top {
to {
opacity: 0;
transform: translateY(-8px);
}
}
.rc-toast-in-top {
animation: rc-toast-in-top 220ms var(--ease-snappy);
}
.rc-toast-out-top {
animation: rc-toast-out-top 140ms ease-in forwards;
}
/*
* The stack: only the front card is in flow, so the stack is one card tall; the cards behind
* it are absolute, 6px higher and a touch smaller each, so they peek over its top edge and
* fan up over each other when the pointer or the keyboard is on the stack.
*/
.rc-toast-slot {
transform-origin: bottom center;
transition: transform 220ms var(--ease-snappy);
}

Expand Down Expand Up @@ -1667,6 +1691,12 @@
transform: translateY(10px);
}
}
@keyframes rc-toast-in-top-flat {
from {
opacity: 0;
transform: translateY(-10px);
}
}
/* The decorative blurs are macOS's alone: on Windows and Linux the reveal fades without its 4px. */
:root:is([data-platform="windows"], [data-platform="linux"]) .rc-reveal {
animation-name: rc-fade;
Expand All @@ -1678,6 +1708,9 @@
:root[data-platform="linux"] .rc-toast-in {
animation-name: rc-toast-in-flat;
}
:root[data-platform="linux"] .rc-toast-in-top {
animation-name: rc-toast-in-top-flat;
}
:root[data-platform="linux"] [role="dialog"].rc-whatsnew[data-state="open"] {
animation-name: rc-fade;
}
Expand Down
8 changes: 8 additions & 0 deletions apps/desktop/src/lib/toastDrag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ describe("toast drag", () => {
expect(dragState(0, -60).dy).toBe(-10);
});

test("in the left corner the card swipes left instead", () => {
expect(dragState(-(TOAST_DISMISS_PX + 1), 0, Number.POSITIVE_INFINITY, -1).dismiss).toBe(true);
expect(dragState(TOAST_DISMISS_PX + 1, 0, Number.POSITIVE_INFINITY, -1).dismiss).toBe(false);
expect(dragState(-30, 0, 40, -1).dismiss).toBe(true);
expect(dragState(-40, 0, Number.POSITIVE_INFINITY, -1)).toMatchObject({ dx: -40, away: 40 });
expect(dragState(60, 0, Number.POSITIVE_INFINITY, -1)).toMatchObject({ dx: 10, away: 0 });
});

test("the card fades as it travels but never below a fifth", () => {
expect(dragOpacity(0)).toBe(1);
expect(dragOpacity(100)).toBe(0.5);
Expand Down
37 changes: 24 additions & 13 deletions apps/desktop/src/lib/toastDrag.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.
// Pure drag math and one DOM guard (ui/Toaster.tsx): the card slides toward its own edge only
// (left in the bottom-left corner) and rubber-bands any other way; past 48px or a quick flick that
// way 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;
Expand All @@ -26,15 +26,24 @@ export interface DragState {
dismiss: boolean;
}

/** `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;
/**
* `ms` is how long the drag has lasted, for a flick; omit it while the pointer moves. `sign` is
* the dismissing side: 1 for right, -1 for left.
*/
export function dragState(
dx: number,
dy: number,
ms = Number.POSITIVE_INFINITY,
sign: 1 | -1 = 1,
): DragState {
const toward = dx * sign;
const followX = toward > 0 ? dx : dx / RESIST;
const flick = toward >= TOAST_FLICK_PX && toward / Math.max(ms, 1) > TOAST_FLICK_SPEED;
return {
dx: followX,
dy: dy / RESIST,
away: Math.max(0, dx),
dismiss: dx > TOAST_DISMISS_PX || flick,
away: Math.max(0, toward),
dismiss: toward > TOAST_DISMISS_PX || flick,
};
}

Expand All @@ -43,8 +52,10 @@ export function dragOpacity(away: number): number {
return Math.max(0.2, 1 - away / 200);
}

/** Where a dismissed card flies to: off the window's right edge. */
export const TOAST_EXIT = "translateX(120%)";
/** Where a dismissed card flies to: off the window's edge on its side. */
export function toastExit(sign: 1 | -1): string {
return `translateX(${sign * 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
23 changes: 23 additions & 0 deletions apps/desktop/src/state/toastPosition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { describe, expect, test } from "bun:test";
import { DEFAULT_TOAST_POSITION, toastFromTop, toastPosition, toastSwipeSign } from "./toastPosition";

describe("toast position", () => {
test("reads a stored position leniently", () => {
expect(toastPosition("top-center")).toBe("top-center");
expect(toastPosition("bottom-left")).toBe("bottom-left");
expect(toastPosition("middle")).toBe(DEFAULT_TOAST_POSITION);
expect(toastPosition(undefined)).toBe("bottom-right");
});

test("top positions stack downward", () => {
expect(toastFromTop("top-right")).toBe(true);
expect(toastFromTop("top-center")).toBe(true);
expect(toastFromTop("bottom-right")).toBe(false);
});

test("a card swipes toward its own edge", () => {
expect(toastSwipeSign("bottom-left")).toBe(-1);
expect(toastSwipeSign("bottom-right")).toBe(1);
expect(toastSwipeSign("top-right")).toBe(1);
});
});
34 changes: 34 additions & 0 deletions apps/desktop/src/state/toastPosition.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { usePref } from "./sidebarPrefs";

export const TOAST_POSITION_PREF = "rustcode.toasts.position";

export type ToastPosition = "bottom-right" | "bottom-left" | "top-right" | "top-center";

export const TOAST_POSITIONS: readonly { id: ToastPosition; label: string }[] = [
{ id: "bottom-right", label: "Bottom right" },
{ id: "bottom-left", label: "Bottom left" },
{ id: "top-right", label: "Top right" },
{ id: "top-center", label: "Top center" },
];

export const DEFAULT_TOAST_POSITION: ToastPosition = "bottom-right";

/** A stored position, read leniently: anything unknown is the default. */
export function toastPosition(raw: unknown): ToastPosition {
return TOAST_POSITIONS.some((p) => p.id === raw) ? (raw as ToastPosition) : DEFAULT_TOAST_POSITION;
}

/** Top positions stack downward and enter from above. */
export function toastFromTop(position: ToastPosition): boolean {
return position.startsWith("top");
}

/** The side a card is swiped toward to dismiss it: left in the left corner, right elsewhere. */
export function toastSwipeSign(position: ToastPosition): 1 | -1 {
return position === "bottom-left" ? -1 : 1;
}

export function useToastPosition() {
const [raw, set] = usePref<string>(TOAST_POSITION_PREF, DEFAULT_TOAST_POSITION);
return [toastPosition(raw), set as (next: ToastPosition) => void] as const;
}
2 changes: 2 additions & 0 deletions apps/desktop/src/ui/SettingsAppearance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { LiveBackgroundGroup } from "./LiveBackgroundGroup";
import { cx, noTextHelpers, Switch } from "./primitives";
import { SettleAfterSetting, SidebarDensitySetting, SidebarToolbarSetting } from "./SidebarDensitySetting";
import { Action, Choice, fieldClass, Group, Row, SectionTitle, Slider } from "./settingsKit";
import { ToastPositionSetting } from "./ToastPositionSetting";

// hidden and inert, so closing Settings shows the thread exactly where it was.
// A grouped nav on the left searches every row's title and keywords
Expand Down Expand Up @@ -295,6 +296,7 @@ export function AppearancePage() {
<SidebarDensitySetting />
<SidebarToolbarSetting />
<SettleAfterSetting />
<ToastPositionSetting />
</Group>
<Group
title="Theme"
Expand Down
30 changes: 30 additions & 0 deletions apps/desktop/src/ui/ToastPositionSetting.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { toast } from "../state/store";
import {
DEFAULT_TOAST_POSITION,
TOAST_POSITIONS,
type ToastPosition,
useToastPosition,
} from "../state/toastPosition";
import { Choice, Row } from "./settingsKit";

export function ToastPositionSetting() {
const [position, setPosition] = useToastPosition();
const pick = (next: ToastPosition) => {
setPosition(next);
toast("Toasts show here", "info", { id: "toast-position-sample" });
};
return (
<Row
title="Toast position"
reset={position !== DEFAULT_TOAST_POSITION ? () => pick(DEFAULT_TOAST_POSITION) : undefined}
subtitle="Where short notices like Copied or Pull request opened appear."
>
<Choice
label="Toast position"
value={position}
onChange={pick}
options={TOAST_POSITIONS.map((p) => ({ value: p.id, label: p.label }))}
/>
</Row>
);
}
Loading
Loading