diff --git a/packages/app/e2e/browser/workspace-archive-undo.spec.ts b/packages/app/e2e/browser/workspace-archive-undo.spec.ts index 68cc16166e..219871d781 100644 --- a/packages/app/e2e/browser/workspace-archive-undo.spec.ts +++ b/packages/app/e2e/browser/workspace-archive-undo.spec.ts @@ -23,6 +23,8 @@ import { outlastUndoWindow, pressArchiveShortcut, pressUndoShortcut, + readSidebarListLayout, + recordLastRowBottomPerFrame, resizeSidebarToMinimum, rowArchiveButton, rowKebab, @@ -121,7 +123,7 @@ test.describe("Workspace archive undo", () => { expect(shortcutBox.x - (labelBox.x + labelBox.width)).toBeLessThanOrEqual(8.5); }); - test("a long list ends just below its last row, and the toast over it moves no row", async ({ + test("a long list scrolls its last row clear of the toast, and settles back smoothly after it", async ({ page, }) => { await page.clock.install({ time: Date.now() }); @@ -136,18 +138,35 @@ test.describe("Workspace archive undo", () => { const worktree = await seedWorktree(page, "archive-toast-long-list"); const resting = await scrollSidebarListToEnd(page); - expect(resting.listBottom - resting.rowBottoms.at(-1)!).toBeLessThan(resting.rowHeight); + const restingEndGap = resting.listBottom - resting.rowBottoms.at(-1)!; + expect(restingEndGap).toBeLessThan(resting.rowHeight); await archiveFromRowButton(page, worktree.workspaceId); await page.mouse.move(0, 0); - const withToast = await scrollSidebarListToEnd(page); const toastBox = await archivedToast(page).boundingBox(); if (!toastBox) throw new Error("Toast is not laid out"); - expect(toastBox.y + toastBox.height).toBeLessThanOrEqual(withToast.listBottom); + // The list keeps its usual end gap, measured from the toast's top instead of its own bottom. + await expect + .poll(async () => { + const layout = await scrollSidebarListToEnd(page); + return Math.round(toastBox.y - layout.rowBottoms.at(-1)!); + }) + .toBe(Math.round(restingEndGap)); + const frames = recordLastRowBottomPerFrame(page, 40); await outlastUndoWindow(page); + const bottoms = await frames; await expect(archivedToast(page)).toHaveCount(0); - expect(await scrollSidebarListToEnd(page)).toEqual(withToast); + + // Without scrolling again, the list ends just below its last row once more. + const settled = await readSidebarListLayout(page); + expect(settled.listBottom - settled.rowBottoms.at(-1)!).toBeCloseTo(restingEndGap, 0); + // The rows travel down by the toast's height over several frames, never in one jump. + const travel = bottoms.at(-1)! - bottoms[0]!; + expect(travel).toBeGreaterThan(toastBox.height); + const steps = bottoms.slice(1).map((bottom, index) => bottom - bottoms[index]!); + expect(Math.min(...steps)).toBeGreaterThanOrEqual(0); + expect(Math.max(...steps)).toBeLessThan(travel / 2); } finally { for (const projectId of fillerProjectIds) { await client.removeProject(projectId).catch(() => undefined); diff --git a/packages/app/e2e/support/helpers/workspace-archive-undo.ts b/packages/app/e2e/support/helpers/workspace-archive-undo.ts index c446b163c2..126b1406c0 100644 --- a/packages/app/e2e/support/helpers/workspace-archive-undo.ts +++ b/packages/app/e2e/support/helpers/workspace-archive-undo.ts @@ -175,13 +175,19 @@ export interface SidebarListLayout { rowHeight: number; } +const SIDEBAR_LIST = "sidebar-project-workspace-list-scroll"; + /** Scrolls the sidebar's workspace list to its end and reads where its rows sit. */ export async function scrollSidebarListToEnd(page: Page): Promise { - const list = page.getByTestId("sidebar-project-workspace-list-scroll"); - await list.evaluate((element) => { + await page.getByTestId(SIDEBAR_LIST).evaluate((element) => { element.scrollTop = element.scrollHeight; }); - return list.evaluate((element) => { + return readSidebarListLayout(page); +} + +/** Reads where the sidebar list's rows sit, without scrolling it. */ +export async function readSidebarListLayout(page: Page): Promise { + return page.getByTestId(SIDEBAR_LIST).evaluate((element) => { const visible = element.getBoundingClientRect(); const rows = Array.from( element.querySelectorAll( @@ -197,3 +203,26 @@ export async function scrollSidebarListToEnd(page: Page): Promise { + return page.getByTestId(SIDEBAR_LIST).evaluate( + (element, count) => + new Promise((resolve) => { + const bottoms: number[] = []; + const sample = () => { + const rows = element.querySelectorAll( + '[data-testid^="sidebar-workspace-row-"],[data-testid^="sidebar-project-row-"]', + ); + bottoms.push(Math.max(...Array.from(rows, (row) => row.getBoundingClientRect().bottom))); + if (bottoms.length >= count) resolve(bottoms); + else requestAnimationFrame(sample); + }; + requestAnimationFrame(sample); + }), + frameCount, + ); +} diff --git a/packages/app/src/components/sidebar-workspace-list.tsx b/packages/app/src/components/sidebar-workspace-list.tsx index 8b7489f5f0..d3857d24a2 100644 --- a/packages/app/src/components/sidebar-workspace-list.tsx +++ b/packages/app/src/components/sidebar-workspace-list.tsx @@ -86,6 +86,7 @@ import { } from "@/components/ui/dropdown-menu"; import { ProjectLeadingVisual } from "@/components/sidebar/project-leading-visual"; import { useToast } from "@/contexts/toast-context"; +import { SidebarToastClearance } from "@/components/sidebar/sidebar-toast-region"; import { getForgePresentation, normalizeForge } from "@/git/forge"; import { hasVisibleOrderChanged, mergeWithRemainder } from "@/utils/sidebar-reorder"; import { confirmDialog } from "@/utils/confirm-dialog"; @@ -2551,6 +2552,7 @@ function ProjectModeList({ : null} {sidebarFilterEmpty ? : projectBody} {listFooterComponent} + ); @@ -2592,8 +2594,7 @@ const styles = StyleSheet.create((theme) => ({ // Optical inset: aligns the visible Pinned/Workspaces glyph edge with the // Schedules icon across the divider; their layout boxes have different insets. paddingTop: 2, - // The sidebar toast floats over the list's end. Keep this padding the same whether or not - // a toast shows: changing it moves the rows when the toast leaves. + // Room for the sidebar toast comes from SidebarToastClearance at the end of the content. paddingBottom: theme.spacing[4], }, projectListContainer: { diff --git a/packages/app/src/components/sidebar/sidebar-status-list.tsx b/packages/app/src/components/sidebar/sidebar-status-list.tsx index 38c8981011..c1b892988f 100644 --- a/packages/app/src/components/sidebar/sidebar-status-list.tsx +++ b/packages/app/src/components/sidebar/sidebar-status-list.tsx @@ -34,6 +34,7 @@ import { CircleX, } from "lucide-react-native"; import { useToast } from "@/contexts/toast-context"; +import { SidebarToastClearance } from "@/components/sidebar/sidebar-toast-region"; import { useSidebarWorkspaceRename } from "@/workspace-rename"; import { useWorkspaceClipboardActions } from "@/hooks/use-workspace-clipboard-actions"; import { redirectIfArchivingActiveWorkspace } from "@/utils/sidebar-workspace-archive-redirect"; @@ -270,6 +271,7 @@ export function SidebarStatusWorkspaceList({ onToggleWorkspacePin={onToggleWorkspacePin} /> )} + ); @@ -1089,8 +1091,7 @@ const styles = StyleSheet.create((theme) => ({ paddingHorizontal: theme.spacing[2], // Keep status mode's Pinned/Workspaces boundary identical to project mode. paddingTop: 2, - // The sidebar toast floats over the list's end. Keep this padding the same whether or not - // a toast shows: changing it moves the rows when the toast leaves. + // Room for the sidebar toast comes from SidebarToastClearance at the end of the content. paddingBottom: theme.spacing[4], }, pinnedSection: { diff --git a/packages/app/src/components/sidebar/sidebar-toast-region.tsx b/packages/app/src/components/sidebar/sidebar-toast-region.tsx index 852ce7302d..002bf2f3f3 100644 --- a/packages/app/src/components/sidebar/sidebar-toast-region.tsx +++ b/packages/app/src/components/sidebar/sidebar-toast-region.tsx @@ -1,9 +1,31 @@ -import { useEffect, useMemo, useRef, type ReactNode } from "react"; +import { + createContext, + useCallback, + useContext, + useEffect, + useMemo, + useRef, + type ReactNode, +} from "react"; import { View } from "react-native"; +import Animated, { + useAnimatedStyle, + useSharedValue, + withTiming, + type SharedValue, +} from "react-native-reanimated"; import { StyleSheet } from "react-native-unistyles"; -import { ToastViewport, useToastHost, type ToastApi } from "@/components/toast-host"; +import { TOAST_FADE_MS, ToastViewport, useToastHost, type ToastApi } from "@/components/toast-host"; import { ToastApiProvider, useToast } from "@/contexts/toast-context"; +interface SidebarToastCoverage { + toastVisible: boolean; + /** Distance from the region's bottom edge to the toast's top edge, as last laid out. */ + height: SharedValue; +} + +const SidebarToastCoverageContext = createContext(null); + /** * Shows toasts raised by the sidebar's list over its bottom edge, just above the footer. * @@ -15,6 +37,8 @@ export function SidebarToastRegion({ active, children }: { active: boolean; chil const appToast = useToast(); const { api: sidebarToast, toast, dismiss } = useToastHost(); const activeRef = useRef(active); + const coverageHeight = useSharedValue(0); + const toastVisible = toast !== null; useEffect(() => { activeRef.current = active; @@ -35,14 +59,58 @@ export function SidebarToastRegion({ active, children }: { active: boolean; chil }; }, [appToast, sidebarToast]); + const coverage = useMemo( + () => ({ toastVisible, height: coverageHeight }), + [toastVisible, coverageHeight], + ); + const handleCoverageChange = useCallback( + (height: number) => { + coverageHeight.value = height; + }, + [coverageHeight], + ); + return ( - {children} - + + {children} + + ); } +/** + * Space at the end of a sidebar list that matches the toast covering it, so the last row can + * scroll clear of the toast. It sits below every row, so growing it moves nothing. It shrinks + * with the toast's fade, so a list scrolled to its end settles back with the toast instead of + * jumping. + */ +export function SidebarToastClearance() { + const coverage = useContext(SidebarToastCoverageContext); + return coverage ? : null; +} + +function AnimatedClearance({ coverage }: { coverage: SidebarToastCoverage }) { + const { toastVisible, height } = coverage; + const style = useAnimatedStyle(() => ({ + height: withTiming(toastVisible ? height.value : 0, { duration: TOAST_FADE_MS }), + })); + return ( + + ); +} + const styles = StyleSheet.create({ region: { position: "relative", diff --git a/packages/app/src/components/toast-host.tsx b/packages/app/src/components/toast-host.tsx index bb9a04707d..6c907e3962 100644 --- a/packages/app/src/components/toast-host.tsx +++ b/packages/app/src/components/toast-host.tsx @@ -4,7 +4,14 @@ import { } from "@/components/floating-header"; import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { createPortal } from "react-dom"; -import { Platform, StyleSheet as NativeStyleSheet, Text, ToastAndroid, View } from "react-native"; +import { + Platform, + StyleSheet as NativeStyleSheet, + Text, + ToastAndroid, + View, + type LayoutChangeEvent, +} from "react-native"; import Animated, { FadeIn, FadeOut } from "react-native-reanimated"; import { useSafeAreaInsets } from "react-native-safe-area-context"; import { StyleSheet, withUnistyles } from "react-native-unistyles"; @@ -57,8 +64,11 @@ type ToastViewportPlacement = "app-shell" | "panel" | "sidebar"; const DEFAULT_DURATION_MS = 2200; const noopDismiss: DismissToast = () => {}; const TOAST_MAX_WIDTH = 480; -const toastEntering = FadeIn.duration(140); -const toastExiting = FadeOut.duration(140); +/** How long a toast takes to fade in or out. */ +export const TOAST_FADE_MS = 140; +const SIDEBAR_TOAST_INSET = SPACING[2]; +const toastEntering = FadeIn.duration(TOAST_FADE_MS); +const toastExiting = FadeOut.duration(TOAST_FADE_MS); const ThemedCheckCircle = withUnistyles(CheckCircle2); const ThemedInfo = withUnistyles(Info); const ThemedWarning = withUnistyles(AlertTriangle); @@ -148,10 +158,13 @@ export function ToastViewport({ toast, onDismiss, placement = "app-shell", + onBottomCoverageChange, }: { toast: ToastState | null; onDismiss: () => void; placement?: ToastViewportPlacement; + /** Sidebar placement: reports the distance from the region's bottom edge to the toast's top. */ + onBottomCoverageChange?: (coverage: number) => void; }) { const insets = useSafeAreaInsets(); const isMobile = useIsCompactFormFactor(); @@ -206,6 +219,13 @@ export function ToastViewport({ return clearTimer; }, [clearTimer, durationMs, scheduleDismiss, toastId]); + const handleSidebarLayout = useCallback( + (event: LayoutChangeEvent) => { + onBottomCoverageChange?.(event.nativeEvent.layout.height + SIDEBAR_TOAST_INSET); + }, + [onBottomCoverageChange], + ); + const panelClearance = useFloatingHeaderOverlayClearance(); const headerHeight = isMobile ? HEADER_INNER_HEIGHT_MOBILE : HEADER_INNER_HEIGHT; const headerTopPadding = isMobile ? HEADER_TOP_PADDING_MOBILE : 0; @@ -227,6 +247,7 @@ export function ToastViewport({ } pointerEvents="box-none" collapsable={false} + onLayout={placement === "sidebar" ? handleSidebarLayout : undefined} >