diff --git a/src/renderer/src/assets/main.css b/src/renderer/src/assets/main.css index 1769888762f..25db24d1815 100644 --- a/src/renderer/src/assets/main.css +++ b/src/renderer/src/assets/main.css @@ -1946,7 +1946,9 @@ html.native-shell .app-layout { content: ''; position: absolute; top: -7px; - left: 0.8125rem; + /* Why: centred on the phone icon, measured by use-intro-arrow. */ + inset-inline-start: calc(var(--intro-arrow-x, 1.25rem) - 7px); + display: var(--intro-arrow-display, block); border-style: solid; border-width: 0 7px 7px; /* Why: use foreground + border to create a subtle shadow that adapts to theme */ @@ -1958,7 +1960,8 @@ html.native-shell .app-layout { content: ''; position: absolute; top: -6px; - left: 0.8125rem; + inset-inline-start: calc(var(--intro-arrow-x, 1.25rem) - 6px); + display: var(--intro-arrow-display, block); border-style: solid; border-width: 0 6px 6px; border-color: transparent transparent var(--muted) transparent; diff --git a/src/renderer/src/components/agent/AgentLaunchPickerMenu.tsx b/src/renderer/src/components/agent/AgentLaunchPickerMenu.tsx index 6d2b6f94b4e..7e125447748 100644 --- a/src/renderer/src/components/agent/AgentLaunchPickerMenu.tsx +++ b/src/renderer/src/components/agent/AgentLaunchPickerMenu.tsx @@ -9,6 +9,7 @@ import { Input } from '@/components/ui/input' import { Terminal } from 'lucide-react' import { agentPickerBlankTerminalMatches } from '@/lib/agent-catalog/agent-picker-search' import { AgentDefaultContextMenu } from './agent-combobox-items' +import { AgentPickerResultsScroller } from './AgentPickerResultsScroller' import { AgentPickerMenuRow, type AgentPickerMenuLaunch } from './AgentLaunchPickerMenuRow' import { DropdownMenuItem } from '@/components/ui/dropdown-menu' import { choiceMatches, type AgentPickerChoice } from './agent-picker-search-rows' @@ -210,7 +211,7 @@ export function AgentLaunchPickerMenu({ /> ) : null} -
+
{showBlankTerminal ? ( ) : null}
-
+ ) } diff --git a/src/renderer/src/components/agent/AgentPickerResultsScroller.tsx b/src/renderer/src/components/agent/AgentPickerResultsScroller.tsx new file mode 100644 index 00000000000..a3f5b2d350b --- /dev/null +++ b/src/renderer/src/components/agent/AgentPickerResultsScroller.tsx @@ -0,0 +1,64 @@ +import { createContext, useContext, useLayoutEffect, useState, type ReactNode } from 'react' + +/** How a host menu sizes the agent list; absent everywhere but menus that fit it to the screen. */ +export type AgentPickerListFit = { + registerList: (node: HTMLDivElement | null) => void + /** null: no cap of its own, the host menu scrolls. */ + maxHeight: number | null + scrollPaddingBottom: number | null + fadeVisible: boolean +} + +export const AgentPickerListFitContext = createContext(null) + +// Its own component so fit updates (e.g. the fade on scroll) never re-render the rows. +export function AgentPickerResultsScroller({ children }: { children: ReactNode }) { + const fit = useContext(AgentPickerListFitContext) + if (!fit) { + return ( +
+ {children} +
+ ) + } + return {children} +} + +function FittedResultsScroller({ + fit: { registerList, maxHeight, scrollPaddingBottom, fadeVisible }, + children +}: { + fit: AgentPickerListFit + children: ReactNode +}) { + const [node, setNode] = useState(null) + useLayoutEffect(() => { + if (!node) { + return + } + registerList(node) + return () => registerList(null) + }, [registerList, node]) + return ( +
+
+ {children} +
+ {fadeVisible ? ( +
+ ) : null} +
+ ) +} diff --git a/src/renderer/src/components/emulator-pane/tab-intro/MobileEmulatorTabIntroCallout.tsx b/src/renderer/src/components/emulator-pane/tab-intro/MobileEmulatorTabIntroCallout.tsx index 08f4fffaf92..1ea9030d5e7 100644 --- a/src/renderer/src/components/emulator-pane/tab-intro/MobileEmulatorTabIntroCallout.tsx +++ b/src/renderer/src/components/emulator-pane/tab-intro/MobileEmulatorTabIntroCallout.tsx @@ -1,15 +1,25 @@ +import { useState } from 'react' import { X } from 'lucide-react' import { Button } from '@/components/ui/button' import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip' import { useTranslate } from '@/i18n/use-translate' import { useMobileEmulatorTabIntroActions } from './use-mobile-emulator-tab-intro-actions' +import { CREATE_ROW_MOBILE_EMULATOR_ID } from '../../tab-bar/create-menu/tab-bar-create-row-item' +import { useIntroArrow, type IntroArrowAnchor } from './use-intro-arrow' -export function MobileEmulatorTabIntroCallout(): React.JSX.Element { +export function MobileEmulatorTabIntroCallout({ + anchor +}: { + anchor: IntroArrowAnchor +}): React.JSX.Element { const t = useTranslate() + const [callout, setCallout] = useState(null) + useIntroArrow(callout, anchor, CREATE_ROW_MOBILE_EMULATOR_ID) const { keepIntro, hideIntro, dismissIntro } = useMobileEmulatorTabIntroActions() return (
event.preventDefault()} diff --git a/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.test.ts b/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.test.ts new file mode 100644 index 00000000000..29f8b697fdf --- /dev/null +++ b/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.test.ts @@ -0,0 +1,67 @@ +import { describe, expect, it } from 'vitest' +import { introArrowInset, revealScrollDelta } from './intro-arrow-geometry' + +// A 280px callout at x=104 with 1px borders under a row of 36px icons (2px gaps) from x=110. +const callout = { left: 104, right: 384 } +const view = { left: 110, right: 360 } +const base = { callout, borderStart: 1, width: 278, view, scale: 1, rtl: false } + +describe('introArrowInset', () => { + it('centres the arrow under the phone wherever it sits in the row', () => { + expect(introArrowInset({ ...base, iconCenter: 128 })).toEqual({ inset: 23, visible: true }) + expect(introArrowInset({ ...base, iconCenter: 242 })).toEqual({ inset: 137, visible: true }) + expect(introArrowInset({ ...base, iconCenter: 280 })).toEqual({ inset: 175, visible: true }) + }) + + it('gives the settled position while the menu is still scaling open', () => { + // Scaled 0.98 about the callout's left edge: viewport distances shrink, local ones must not. + const scaled = { left: 104, right: 104 + 280 * 0.98 } + expect( + introArrowInset({ ...base, callout: scaled, iconCenter: 104 + 176 * 0.98, scale: 0.98 }).inset + ).toBeCloseTo(175) + }) + + it('measures from the right edge in right-to-left layouts', () => { + expect(introArrowInset({ ...base, rtl: true, iconCenter: 360 })).toEqual({ + inset: 23, + visible: true + }) + }) + + it('keeps the arrow off the rounded corners', () => { + expect(introArrowInset({ ...base, iconCenter: 106 }).inset).toBe(12) + expect(introArrowInset({ ...base, iconCenter: 390 }).inset).toBe(266) + }) + + it('hides the arrow once the phone is scrolled out of the row', () => { + expect(introArrowInset({ ...base, iconCenter: 380 }).visible).toBe(false) + expect(introArrowInset({ ...base, iconCenter: 100 }).visible).toBe(false) + }) +}) + +describe('revealScrollDelta', () => { + const row = { view, margin: 24, scale: 1 } + + it('scrolls an icon under the end fade into view', () => { + expect(revealScrollDelta({ ...row, item: { left: 350, right: 386 } })).toBe(50) + }) + + it('scrolls an icon under the start fade into view', () => { + expect(revealScrollDelta({ ...row, item: { left: 90, right: 126 } })).toBe(-44) + }) + + it('leaves a clear icon alone', () => { + expect(revealScrollDelta({ ...row, item: { left: 200, right: 236 } })).toBe(0) + }) + + it('returns local px while the menu is scaled', () => { + expect( + revealScrollDelta({ + item: { left: 190, right: 208 }, + view: { left: 0, right: 200 }, + margin: 24, + scale: 0.5 + }) + ).toBe(40) + }) +}) diff --git a/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.ts b/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.ts new file mode 100644 index 00000000000..ba039c9123e --- /dev/null +++ b/src/renderer/src/components/emulator-pane/tab-intro/intro-arrow-geometry.ts @@ -0,0 +1,62 @@ +type Span = { left: number; right: number } + +/** Keeps the 7px arrow clear of the callout's rounded corners. */ +const MIN_ARROW_INSET = 12 + +/** + * Where the tip's arrow centre sits, in the callout's local px from its inline-start padding edge. + * Rects are viewport px, so they are divided by the menu's open-animation scale. + */ +export function introArrowInset({ + callout, + borderStart, + width, + iconCenter, + view, + scale, + rtl +}: { + /** Callout border box, viewport px. */ + callout: Span + /** Inline-start border width, local px. */ + borderStart: number + /** Callout padding-box width, local px. */ + width: number + /** Phone icon centre x, viewport px. */ + iconCenter: number + /** The icon row's visible box, viewport px. */ + view: Span + scale: number + rtl: boolean +}): { inset: number; visible: boolean } { + const fromStart = rtl ? callout.right - iconCenter : iconCenter - callout.left + const inset = fromStart / scale - borderStart + return { + inset: Math.min(Math.max(inset, MIN_ARROW_INSET), width - MIN_ARROW_INSET), + visible: iconCenter >= view.left && iconCenter <= view.right + } +} + +/** Physical scrollLeft change that brings the icon clear of the row's edge fades; 0 if it already is. */ +export function revealScrollDelta({ + item, + view, + margin, + scale +}: { + item: Span + view: Span + /** The row's edge-fade width, local px. */ + margin: number + scale: number +}): number { + const start = view.left + margin * scale + const end = view.right - margin * scale + if (item.left < start) { + return (item.left - start) / scale + } + if (item.right > end) { + return (item.right - end) / scale + } + return 0 +} diff --git a/src/renderer/src/components/emulator-pane/tab-intro/use-intro-arrow.ts b/src/renderer/src/components/emulator-pane/tab-intro/use-intro-arrow.ts new file mode 100644 index 00000000000..dbac878c547 --- /dev/null +++ b/src/renderer/src/components/emulator-pane/tab-intro/use-intro-arrow.ts @@ -0,0 +1,86 @@ +import { useLayoutEffect } from 'react' +import { introArrowInset, revealScrollDelta } from './intro-arrow-geometry' + +/** The create row's edge-fade width (main.css .terminal-tab-strip--fade-*). */ +const ROW_FADE_PX = 24 + +export type IntroArrowAnchor = { + /** The create row's horizontally scrolling icon strip. */ + scroller: HTMLElement | null + /** Changes when the icons are reordered, shown or hidden. */ + order: string +} + +/** Points the callout's arrow at an icon in the create row, revealing the icon first. */ +export function useIntroArrow( + callout: HTMLElement | null, + anchor: IntroArrowAnchor, + itemId: string +): void { + const { scroller, order } = anchor + useLayoutEffect(() => { + if (!callout || !scroller) { + return + } + const icon = scroller.querySelector( + `[data-create-row-item="${CSS.escape(itemId)}"]` + ) + if (!icon) { + return + } + const scaleOf = (): number => { + const width = callout.getBoundingClientRect().width + return callout.offsetWidth > 0 && width > 0 ? width / callout.offsetWidth : 1 + } + const delta = revealScrollDelta({ + item: icon.getBoundingClientRect(), + view: scroller.getBoundingClientRect(), + margin: ROW_FADE_PX, + scale: scaleOf() + }) + if (delta !== 0) { + scroller.scrollBy({ left: delta, behavior: 'instant' }) + } + const measure = (): void => { + if (callout.offsetWidth === 0) { + return + } + const box = callout.getBoundingClientRect() + const iconBox = icon.getBoundingClientRect() + const style = getComputedStyle(callout) + const { inset, visible } = introArrowInset({ + callout: box, + borderStart: Number.parseFloat(style.borderInlineStartWidth) || 0, + width: callout.clientWidth, + iconCenter: (iconBox.left + iconBox.right) / 2, + view: scroller.getBoundingClientRect(), + scale: box.width / callout.offsetWidth, + rtl: style.direction === 'rtl' + }) + callout.style.setProperty('--intro-arrow-x', `${inset}px`) + callout.style.setProperty('--intro-arrow-display', visible ? 'block' : 'none') + } + measure() + let frame: number | null = null + const schedule = (): void => { + if (frame === null) { + frame = requestAnimationFrame(() => { + frame = null + measure() + }) + } + } + const resize = new ResizeObserver(schedule) + resize.observe(callout) + resize.observe(scroller) + scroller.addEventListener('scroll', schedule, { passive: true }) + return () => { + resize.disconnect() + scroller.removeEventListener('scroll', schedule) + if (frame !== null) { + cancelAnimationFrame(frame) + } + } + // `order` re-runs this after customizing moves the icon. + }, [callout, scroller, order, itemId]) +} diff --git a/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRow.tsx b/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRow.tsx index eaeaab79932..42bb540ab64 100644 --- a/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRow.tsx +++ b/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRow.tsx @@ -25,6 +25,7 @@ import type { TabBarCreateRowItem } from './tab-bar-create-row-item' import { TabBarCreateRowCustomizer } from './TabBarCreateRowCustomizer' import { CREATE_ROW_HIGHLIGHT_CLASS, TabBarCreateRowIcons } from './TabBarCreateRowIcons' import { useCreateRowScroller } from './use-create-row-scroller' +import type { IntroArrowAnchor } from '../../emulator-pane/tab-intro/use-intro-arrow' const EMPTY_LAYOUT: NewTabMenuLayout = { order: [], hidden: [] } @@ -43,7 +44,7 @@ export function TabBarCreateRow({ customizable: boolean layoutSetting: keyof NewTabMenuSettings /** Content tied to shown items, e.g. an intro for an icon the user has not hidden. */ - renderBelow?: (shownIds: ReadonlySet) => React.ReactNode + renderBelow?: (shownIds: ReadonlySet, anchor: IntroArrowAnchor) => React.ReactNode }): React.JSX.Element { const t = useTranslate() const closeMenu = useDropdownMenuClose() @@ -63,12 +64,13 @@ export function TabBarCreateRow({ const [keyboardNav, setKeyboardNav] = useState(false) const [hoveredIndex, setHoveredIndex] = useState(null) const pointerInside = useRef(false) - const { scrollerRef, attachScroller, overflow, revealIcon } = useCreateRowScroller() + const { scrollerRef, scroller, attachScroller, overflow, revealIcon } = useCreateRowScroller() const [recordingActionId, setRecordingActionId] = useState(null) const customizeIndex = visible.length const active = Math.min(activeIndex, customizeIndex) const keyboardActive = rowFocused && keyboardNav - const below = renderBelow?.(new Set(visible.map((item) => item.id))) + const visibleIds = visible.map((item) => item.id) + const below = renderBelow?.(new Set(visibleIds), { scroller, order: visibleIds.join(' ') }) useShortcutRecorderFocus(recordingActionId !== null) diff --git a/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRowIcons.tsx b/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRowIcons.tsx index 70928486120..349365824d1 100644 --- a/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRowIcons.tsx +++ b/src/renderer/src/components/tab-bar/create-menu/TabBarCreateRowIcons.tsx @@ -65,6 +65,7 @@ export function TabBarCreateRowIcons({ type="button" tabIndex={-1} aria-label={item.label} + data-create-row-item={item.id} className={cn( 'flex h-7 w-9 shrink-0 items-center justify-center rounded-[7px] text-muted-foreground outline-hidden', highlighted && CREATE_ROW_HIGHLIGHT_CLASS diff --git a/src/renderer/src/components/tab-bar/create-menu/tab-bar-static-create-menu.tsx b/src/renderer/src/components/tab-bar/create-menu/tab-bar-static-create-menu.tsx index d9dc74fd837..eddb48c422d 100644 --- a/src/renderer/src/components/tab-bar/create-menu/tab-bar-static-create-menu.tsx +++ b/src/renderer/src/components/tab-bar/create-menu/tab-bar-static-create-menu.tsx @@ -2,6 +2,7 @@ import React from 'react' import { FilePlus, FileText, Globe, Smartphone, TerminalSquare } from 'lucide-react' import { useTranslate } from '@/i18n/use-translate' import { MobileEmulatorTabIntroCallout } from '../../emulator-pane/tab-intro/MobileEmulatorTabIntroCallout' +import { NewTabMenuTipSlot } from '../menu-fit/NewTabMenuTipSlot' import { ShellIcon } from '../../terminal/shells/ShellIcon' import { useAppStore } from '@/store/index' import { usePluginNewFiles } from '@/store/plugins/file-creation/plugin-new-files' @@ -203,11 +204,15 @@ export function TabBarStaticCreateMenu({ items={items} customizable={!terminalOnly} layoutSetting={NEW_TAB_MENU_LAYOUT_SETTING[newTabMenuSurface]} - renderBelow={(shownIds) => - showMobileEmulatorIntroCallout && isMacOs && shownIds.has(CREATE_ROW_MOBILE_EMULATOR_ID) ? ( - - ) : null - } + renderBelow={(shownIds, anchor) => ( + + + + )} /> ) } diff --git a/src/renderer/src/components/tab-bar/create-menu/use-create-row-scroller.ts b/src/renderer/src/components/tab-bar/create-menu/use-create-row-scroller.ts index f836cce6b97..0d2d5bc4d9d 100644 --- a/src/renderer/src/components/tab-bar/create-menu/use-create-row-scroller.ts +++ b/src/renderer/src/components/tab-bar/create-menu/use-create-row-scroller.ts @@ -42,6 +42,7 @@ export function useCreateRowScroller() { return { scrollerRef, + scroller, overflow, attachScroller: (node: HTMLDivElement | null): void => { scrollerRef.current = node diff --git a/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuContent.tsx b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuContent.tsx new file mode 100644 index 00000000000..fc50ec56845 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuContent.tsx @@ -0,0 +1,26 @@ +import React, { useCallback, useState } from 'react' +import { DropdownMenuContent } from '@/components/ui/dropdown-menu' +import { cn } from '@/lib/class-names' +import { NewTabMenuFitProvider } from './NewTabMenuFitProvider' + +/** The + menu's content: fits its agent list to the screen for the session it is open. */ +export function NewTabMenuContent({ + className, + children, + ...props +}: React.ComponentProps): React.JSX.Element { + // Why: Radix re-prefers bottom on every update, so an upward menu would jump below "+" as a search shrinks it. + const [preferTop, setPreferTop] = useState(false) + const onPlacedSide = useCallback((side: string | null) => setPreferTop(side === 'top'), []) + return ( + [data-new-tab-menu-fit=menu]]:scroll-pb-[34px]', className)} + > + {children} + + ) +} diff --git a/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuFitProvider.tsx b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuFitProvider.tsx new file mode 100644 index 00000000000..2fb982a75e0 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuFitProvider.tsx @@ -0,0 +1,188 @@ +import React, { useEffect, useEffectEvent, useMemo, useRef, useState } from 'react' +import { + AgentPickerListFitContext, + type AgentPickerListFit +} from '@/components/agent/AgentPickerResultsScroller' +import { NewTabMenuTipRoomContext, type NewTabMenuTipRoom } from './new-tab-menu-tip-room' +import { + NEW_TAB_MENU_LIST_MAX_ROWS, + fadeScrollPadding, + planNewTabMenuFit, + sameNewTabMenuFitPlan, + type NewTabMenuFitMode, + type NewTabMenuFitPlan +} from './new-tab-menu-fit-plan' + +const FALLBACK_ROW = 36 + +type FitLayout = { plan: NewTabMenuFitPlan; row: number } + +function sameLayout(a: FitLayout | null, b: FitLayout): boolean { + return a !== null && a.row === b.row && sameNewTabMenuFitPlan(a.plan, b.plan) +} + +function hasMoreBelow(scroller: HTMLElement): boolean { + return scroller.scrollTop + scroller.clientHeight < scroller.scrollHeight - 1 +} + +function verticalFrame(element: HTMLElement): number { + const style = getComputedStyle(element) + return ( + Number.parseFloat(style.paddingTop) + + Number.parseFloat(style.paddingBottom) + + Number.parseFloat(style.borderTopWidth) + + Number.parseFloat(style.borderBottomWidth) + ) +} + +/** The list scrolls in peek mode, the whole menu content in menu mode. */ +function scrollerFor( + mode: NewTabMenuFitMode | null, + box: HTMLElement | null, + list: HTMLElement | null +): HTMLElement | null { + return mode === 'peek' ? list : mode === 'menu' ? (box?.parentElement ?? null) : null +} + +/** + * Lives inside the + menu's content, so it mounts on open and unmounts on the real close: + * everything it learns (tip height, placed side) lasts one menu session. + */ +export function NewTabMenuFitProvider({ + onPlacedSide, + children +}: { + /** The side Radix first placed the menu on; null once the session ends. Must be stable. */ + onPlacedSide: (side: string | null) => void + children: React.ReactNode +}): React.JSX.Element { + const [box, setBox] = useState(null) + const [list, setList] = useState(null) + const [tip, setTip] = useState(null) + // Once the tip gives its room to the list it stays hidden until the menu next opens. + const [tipYielded, setTipYielded] = useState(false) + const [layout, setLayout] = useState(null) + const [fadeVisible, setFadeVisible] = useState(false) + const placedSide = useRef(null) + const mode = layout?.plan.mode ?? null + + const measure = useEffectEvent(() => { + const content = box?.parentElement + const wrapper = content?.parentElement + if (!box || !content || !wrapper) { + return + } + if (placedSide.current === null && !wrapper.style.transform.includes('-200%')) { + placedSide.current = content.dataset.side ?? 'bottom' + onPlacedSide(placedSide.current) + } + let planMode = mode + const available = Number.parseFloat( + wrapper.style.getPropertyValue('--radix-popper-available-height') + ) + if (list && Number.isFinite(available)) { + // The slot is a flow-root box, so its height includes the tip's own margin. + const tipHeight = tip?.isConnected ? tip.offsetHeight : 0 + const row = list.querySelector('[role="menuitem"]')?.offsetHeight || FALLBACK_ROW + const chrome = box.offsetHeight + verticalFrame(content) - list.offsetHeight - tipHeight + const next: FitLayout = { + row, + plan: planNewTabMenuFit({ + // Why floor and -1: offsetHeight rounds, and a 1px overshoot would make the menu scroll too. + available: Math.floor(available) - 1, + chrome, + tip: tipHeight, + natural: list.scrollHeight, + row + }) + } + planMode = next.plan.mode + if (next.plan.tipHidden && tipHeight > 0) { + setTipYielded(true) + } + setLayout((current) => (sameLayout(current, next) ? current : next)) + } + // Content can grow or shrink without a scroll event, so the fade is re-checked on every pass. + const scroller = scrollerFor(planMode, box, list) + const more = scroller ? hasMoreBelow(scroller) : false + setFadeVisible((current) => (current === more ? current : more)) + }) + + // Observers follow the nodes' real lifetime: the list and tip unmount while a search shows results. + useEffect(() => { + const wrapper = box?.parentElement?.parentElement + if (!box || !wrapper) { + return + } + let frame: number | null = null + const schedule = (): void => { + if (frame === null) { + frame = requestAnimationFrame(() => { + frame = null + measure() + }) + } + } + const resize = new ResizeObserver(schedule) + for (const node of [box, list, list?.firstElementChild, tip]) { + if (node) { + resize.observe(node) + } + } + // Radix rewrites the wrapper style only when it repositions (resize, flip). + const reposition = new MutationObserver(schedule) + reposition.observe(wrapper, { attributes: true, attributeFilter: ['style'] }) + schedule() + return () => { + resize.disconnect() + reposition.disconnect() + if (frame !== null) { + cancelAnimationFrame(frame) + } + } + }, [box, list, tip]) + + useEffect(() => { + const scroller = scrollerFor(mode, box, list) + if (!scroller) { + return + } + const onScroll = (): void => { + const more = hasMoreBelow(scroller) + setFadeVisible((current) => (current === more ? current : more)) + } + scroller.addEventListener('scroll', onScroll, { passive: true }) + return () => scroller.removeEventListener('scroll', onScroll) + }, [mode, box, list]) + + useEffect(() => () => onPlacedSide(null), [onPlacedSide]) + + const listFit = useMemo( + () => ({ + registerList: setList, + maxHeight: layout ? layout.plan.listMaxHeight : NEW_TAB_MENU_LIST_MAX_ROWS * FALLBACK_ROW, + scrollPaddingBottom: layout?.plan.mode === 'peek' ? fadeScrollPadding(layout.row) : null, + fadeVisible: layout?.plan.mode === 'peek' && fadeVisible + }), + [layout, fadeVisible] + ) + const tipRoom = useMemo( + () => ({ tipHidden: tipYielded, registerTip: setTip }), + [tipYielded] + ) + + return ( +
+ + {children} + + {mode === 'menu' && fadeVisible ? ( +
+ ) : null} +
+ ) +} diff --git a/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuTipSlot.tsx b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuTipSlot.tsx new file mode 100644 index 00000000000..ebc776856d4 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/NewTabMenuTipSlot.tsx @@ -0,0 +1,33 @@ +import React, { useLayoutEffect, useState } from 'react' +import { useNewTabMenuTipRoom } from './new-tab-menu-tip-room' + +/** Shows an eligible tip unless the + menu gave its room to the agent list. */ +export function NewTabMenuTipSlot({ + eligible, + children +}: { + eligible: boolean + children: React.ReactNode +}): React.ReactNode { + const room = useNewTabMenuTipRoom() + const registerTip = room?.registerTip + const [node, setNode] = useState(null) + useLayoutEffect(() => { + if (!registerTip || !node) { + return + } + registerTip(node) + return () => registerTip(null) + }, [registerTip, node]) + if (!eligible || room?.tipHidden) { + return null + } + // flow-root keeps the tip's top margin inside the measured box. + return room ? ( +
+ {children} +
+ ) : ( + children + ) +} diff --git a/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.test.ts b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.test.ts new file mode 100644 index 00000000000..615a6fe8ff0 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.test.ts @@ -0,0 +1,79 @@ +import { describe, expect, it } from 'vitest' +import { planNewTabMenuFit } from './new-tab-menu-fit-plan' + +const ROW = 36 +const rows = (count: number): number => count * ROW +// The list alone gets `space` px: no chrome, no tip. +const listIn = (space: number, agents: number, row = ROW) => + planNewTabMenuFit({ available: space, chrome: 0, tip: 0, natural: agents * row, row }) + +describe('planNewTabMenuFit', () => { + it('shows a short list whole with no peek', () => { + expect(listIn(700, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 432 }) + expect(listIn(200, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 200 }) + expect(listIn(180, 5)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 180 }) + }) + + it('peeks half of the last visible row once the list overflows', () => { + expect(listIn(700, 16)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 414 }) + expect(listIn(400, 16)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 378 }) + expect(listIn(300, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 270 }) + expect(listIn(250, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 198 }) + }) + + it('caps the list at 12 rows even with more room', () => { + expect(listIn(2000, 12)).toEqual({ tipHidden: false, mode: 'fit', listMaxHeight: 432 }) + expect(listIn(2000, 13)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 414 }) + }) + + it('keeps a 2.5-row peek down to the last budget that fits it, then the menu scrolls', () => { + expect(listIn(144, 10).listMaxHeight).toBe(126) + expect(listIn(143, 10).listMaxHeight).toBe(90) + expect(listIn(126, 10).listMaxHeight).toBe(90) + expect(listIn(108, 10).listMaxHeight).toBe(90) + expect(listIn(107, 10).listMaxHeight).toBe(90) + expect(listIn(90, 10)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 90 }) + expect(listIn(89, 10)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null }) + }) + + it('scales the peek floor with the row height', () => { + expect(listIn(88, 10, 35)).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 87.5 }) + expect(listIn(87, 10, 35)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null }) + }) + + it('hands scrolling to the menu when there is no room at all', () => { + for (const space of [54, 40, 0, -20]) { + expect(listIn(space, 3)).toEqual({ tipHidden: true, mode: 'menu', listMaxHeight: null }) + } + }) + + it('subtracts the menu chrome from the room', () => { + expect( + planNewTabMenuFit({ available: 500, chrome: 140, tip: 0, natural: rows(16), row: ROW }) + ).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 342 }) + }) + + it('keeps the tip while the list still gets a 2.5-row peek beside it', () => { + expect( + planNewTabMenuFit({ available: 270, chrome: 120, tip: 60, natural: rows(16), row: ROW }) + ).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 90 }) + }) + + it('hides the tip before the list drops under 2.5 rows', () => { + expect( + planNewTabMenuFit({ available: 269, chrome: 120, tip: 60, natural: rows(16), row: ROW }) + ).toEqual({ tipHidden: true, mode: 'peek', listMaxHeight: 126 }) + }) + + it('hides the tip when that lets the whole list fit', () => { + expect( + planNewTabMenuFit({ available: 240, chrome: 120, tip: 60, natural: rows(3), row: ROW }) + ).toEqual({ tipHidden: true, mode: 'fit', listMaxHeight: 120 }) + }) + + it('never yields a tip that has no footprint', () => { + expect( + planNewTabMenuFit({ available: 269, chrome: 120, tip: 0, natural: rows(16), row: ROW }) + ).toEqual({ tipHidden: false, mode: 'peek', listMaxHeight: 126 }) + }) +}) diff --git a/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.ts b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.ts new file mode 100644 index 00000000000..8d5375e98d6 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-fit-plan.ts @@ -0,0 +1,64 @@ +export type NewTabMenuFitMode = 'fit' | 'peek' | 'menu' + +export type NewTabMenuFitPlan = { + tipHidden: boolean + /** fit: the list shows whole; peek: the list scrolls with a half row showing; menu: the whole menu scrolls. */ + mode: NewTabMenuFitMode + listMaxHeight: number | null +} + +export const NEW_TAB_MENU_LIST_MAX_ROWS = 12 +const MIN_PEEK_ROWS = 2.5 + +/** + * Sizes the + menu's agent list to the room Radix gives the menu. Yield order: hide the tip, + * shrink the list to a 2.5-row peek, then hand scrolling to the whole menu. + */ +export function planNewTabMenuFit({ + available, + chrome, + tip, + natural, + row, + maxRows = NEW_TAB_MENU_LIST_MAX_ROWS +}: { + /** Height Radix allows the menu on the side it opened. */ + available: number + /** Everything in the menu but the list and the tip. */ + chrome: number + /** The tip's footprint; 0 when there is no tip to show. */ + tip: number + /** The list's full content height. */ + natural: number + row: number + maxRows?: number +}): NewTabMenuFitPlan { + const minPeek = MIN_PEEK_ROWS * row + const cap = (space: number): number => Math.min(space, maxRows * row) + const peek = (space: number): number => + Math.max(minPeek, (Math.floor(cap(space) / row) - 0.5) * row) + const withTip = available - chrome - tip + if (natural <= cap(withTip)) { + return { tipHidden: false, mode: 'fit', listMaxHeight: cap(withTip) } + } + if (cap(withTip) >= minPeek) { + return { tipHidden: false, mode: 'peek', listMaxHeight: peek(withTip) } + } + const space = available - chrome + if (natural <= cap(space)) { + return { tipHidden: true, mode: 'fit', listMaxHeight: cap(space) } + } + if (cap(space) >= minPeek) { + return { tipHidden: true, mode: 'peek', listMaxHeight: peek(space) } + } + return { tipHidden: true, mode: 'menu', listMaxHeight: null } +} + +export function sameNewTabMenuFitPlan(a: NewTabMenuFitPlan, b: NewTabMenuFitPlan): boolean { + return a.tipHidden === b.tipHidden && a.mode === b.mode && a.listMaxHeight === b.listMaxHeight +} + +/** Bottom scroll padding so keyboard focus never rests under the 16px fade. */ +export function fadeScrollPadding(row: number): number { + return 16 + row / 2 +} diff --git a/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-tip-room.ts b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-tip-room.ts new file mode 100644 index 00000000000..4f2dbbd5c63 --- /dev/null +++ b/src/renderer/src/components/tab-bar/menu-fit/new-tab-menu-tip-room.ts @@ -0,0 +1,13 @@ +import { createContext, useContext } from 'react' + +export type NewTabMenuTipRoom = { + /** Gave its room to the agent list; stays hidden until the menu next opens. */ + tipHidden: boolean + registerTip: (node: HTMLElement | null) => void +} + +export const NewTabMenuTipRoomContext = createContext(null) + +export function useNewTabMenuTipRoom(): NewTabMenuTipRoom | null { + return useContext(NewTabMenuTipRoomContext) +} diff --git a/src/renderer/src/components/tab-bar/strip/tab-bar-surface.tsx b/src/renderer/src/components/tab-bar/strip/tab-bar-surface.tsx index d841fdb70bc..1e5cacf0b93 100644 --- a/src/renderer/src/components/tab-bar/strip/tab-bar-surface.tsx +++ b/src/renderer/src/components/tab-bar/strip/tab-bar-surface.tsx @@ -6,7 +6,6 @@ import type { Translate } from '@/i18n/use-translate' import { Button } from '@/components/ui/button' import { DropdownMenu, - DropdownMenuContent, DropdownMenuSeparator, DropdownMenuTrigger } from '@/components/ui/dropdown-menu' @@ -25,6 +24,7 @@ import type { TabBarItemProjection } from '../model/use-tab-bar-item-projection' import type { TabBarItemActions } from '../model/use-tab-bar-item-actions' import { renderTabBarItems } from '../model/tab-bar-item-surface' import { TabBarStaticCreateMenu } from '../create-menu/tab-bar-static-create-menu' +import { NewTabMenuContent } from '../menu-fit/NewTabMenuContent' import ClientHostedBrowserTabRows from '../browser-tabs/ClientHostedBrowserTabRows' import type { ClientHostedBrowserRow } from '../../../../../shared/browser/client-hosting/client-hosted-browser-rows' import { advanceAgentSessionsTourPastNewTab } from '@/components/contextual-tours/agent-sessions-tour-new-tab' @@ -228,7 +228,7 @@ export function renderTabBarSurface( - ) : null} - +
)