diff --git a/apps/desktop/layer/renderer/src/modules/entry-column/AIEntryLayout.tsx b/apps/desktop/layer/renderer/src/modules/entry-column/AIEntryLayout.tsx index 69a5a22c0..e63935222 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-column/AIEntryLayout.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-column/AIEntryLayout.tsx @@ -1,9 +1,10 @@ import { Spring } from "@follow/components/constants/spring.js" +import { Button } from "@follow/components/ui/button/index.js" import { PanelSplitter } from "@follow/components/ui/divider/index.js" import { defaultUISettings } from "@follow/shared/settings/defaults" import { cn } from "@follow/utils" import { AnimatePresence } from "motion/react" -import { useMemo, useRef } from "react" +import { useCallback, useEffect, useMemo, useRef, useState } from "react" import { useResizable } from "react-resizable-layout" import { useParams } from "react-router" @@ -11,6 +12,7 @@ import { AIChatPanelStyle, useAIChatPanelStyle } from "~/atoms/settings/ai" import { getUISettings, setUISetting } from "~/atoms/settings/ui" import { m } from "~/components/common/Motion" import { ROUTE_ENTRY_PENDING } from "~/constants" +import { useNavigateEntry } from "~/hooks/biz/useNavigateEntry" import { AIChatLayout } from "~/modules/app-layout/ai/AIChatLayout" import { EntryContent } from "~/modules/entry-content/components/entry-content" import { AppLayoutGridContainerProvider } from "~/providers/app-grid-layout-container-provider" @@ -20,11 +22,95 @@ import { EntryColumn } from "./index" const AIEntryLayoutImpl = () => { const { entryId } = useParams() - + const navigate = useNavigateEntry() const panelStyle = useAIChatPanelStyle() const realEntryId = entryId === ROUTE_ENTRY_PENDING ? "" : entryId + // Swipe/scroll to close functionality + const entryContentRef = useRef(null) + const accumulatedDelta = useRef(0) + const isScrollingAtTop = useRef(false) + const [showScrollHint, setShowScrollHint] = useState(false) + + const handleCloseGesture = useCallback(() => { + navigate({ entryId: null }) + }, [navigate]) + + const handleWheel = useCallback( + (e: WheelEvent) => { + if (!realEntryId || !entryContentRef.current) return + + // Find the actual scroll viewport element with correct Radix UI attribute + const entryContentElement = entryContentRef.current.querySelector( + "[data-radix-scroll-area-viewport]", + ) as HTMLElement + const scrollElement = entryContentElement || entryContentRef.current + + // Check if we're at the top of the content + const scrollTop = scrollElement?.scrollTop || 0 + isScrollingAtTop.current = scrollTop === 0 + setShowScrollHint(scrollTop === 0) + + // Handle trackpad/mouse wheel: upward scroll (deltaY < 0) or downward swipe gesture + // On macOS trackpad, natural scrolling makes upward finger movement negative deltaY + if (e.deltaY < 0 && isScrollingAtTop.current) { + e.preventDefault() + accumulatedDelta.current += Math.abs(e.deltaY) + + // Close when accumulated scroll exceeds threshold (150px for trackpad sensitivity) + if (accumulatedDelta.current > 1000) { + handleCloseGesture() + accumulatedDelta.current = 0 + } + } else { + // Reset accumulation when scrolling down or not at top + accumulatedDelta.current = 0 + } + }, + [realEntryId, handleCloseGesture], + ) + + useEffect(() => { + if (!realEntryId || !entryContentRef.current) return + + const element = entryContentRef.current + + // Find the scroll area viewport element with correct Radix UI attribute + const scrollViewport = element.querySelector("[data-radix-scroll-area-viewport]") as HTMLElement + + // Add wheel event listener to both the main container and scroll viewport + // This ensures the gesture works in both header area and scrollable content + const elementsToListen: HTMLElement[] = [element] + if (scrollViewport) { + elementsToListen.push(scrollViewport) + } + + elementsToListen.forEach((el) => { + el.addEventListener("wheel", handleWheel, { passive: false }) + }) + + // Initial scroll position check for hint visibility + const initialCheckScrollPosition = () => { + const scrollTop = scrollViewport?.scrollTop || element.scrollTop || 0 + setShowScrollHint(scrollTop === 0) + } + + // Check initial position + initialCheckScrollPosition() + + // Add scroll listener for hint visibility + const scrollElement = scrollViewport || element + scrollElement.addEventListener("scroll", initialCheckScrollPosition, { passive: true }) + + return () => { + elementsToListen.forEach((el) => { + el.removeEventListener("wheel", handleWheel) + }) + scrollElement.removeEventListener("scroll", initialCheckScrollPosition) + } + }, [realEntryId, handleWheel]) + // AI chat resizable panel configuration const aiColWidth = useMemo(() => getUISettings().aiColWidth, []) const startDragPosition = useRef(0) @@ -54,17 +140,39 @@ const AIEntryLayoutImpl = () => { {/* Entry content overlay with exit animation */} - + {realEntryId && ( - + {/* Scroll hint indicator */} +
+ +
+
)}
diff --git a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx index 451d707ec..5ee76327b 100644 --- a/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx +++ b/apps/desktop/layer/renderer/src/modules/entry-content/components/entry-content/EntryScrollingAndNavigationHandler.tsx @@ -30,12 +30,7 @@ export const EntryScrollingAndNavigationHandler = ({ scrollAnimationRef: React.RefObject | null> }) => { const isAlreadyScrolledBottomRef = useRef(false) - const isAlreadyScrolledTopRef = useRef(false) - const lastTouchYRef = useRef(null) - const wheelEndTimerRef = useRef(null) - const wheelGestureCompleteRef = useRef(false) const [showKeepScrollingPanel, setShowKeepScrollingPanel] = useState(false) - const [showBackToTimelinePanel, setShowBackToTimelinePanel] = useState(false) const when = useGlobalFocusableScopeSelector(FocusablePresets.isEntryRender) @@ -80,180 +75,52 @@ export const EntryScrollingAndNavigationHandler = ({ { enabled: when && useBackHandler }, ) - const navigateBackToTimeline = useEventCallback(() => { - navigateToTimeline({ entryId: null }) - setShowBackToTimelinePanel(false) - isAlreadyScrolledTopRef.current = false - wheelGestureCompleteRef.current = false - if (wheelEndTimerRef.current) { - clearTimeout(wheelEndTimerRef.current) - wheelEndTimerRef.current = null - } - }) - const { highlightBoundary } = useFocusActions() const smoothScrollTo = useSmoothScroll() const navigateToNext = useEventCallback(() => { EventBus.dispatch(COMMAND_ID.timeline.switchToNext) setShowKeepScrollingPanel(false) - setShowBackToTimelinePanel(false) isAlreadyScrolledBottomRef.current = false - isAlreadyScrolledTopRef.current = false - wheelGestureCompleteRef.current = false - if (wheelEndTimerRef.current) { - clearTimeout(wheelEndTimerRef.current) - wheelEndTimerRef.current = null - } if (scrollerRef.current) { smoothScrollTo(0, scrollerRef.current) } }) useEffect(() => { - // Only check bottom scroll for keyboard navigation - const checkBottomScrollOnly = ($scroller: HTMLDivElement) => { + const checkScrollBottom = ($scroller: HTMLDivElement) => { const currentScroll = $scroller.scrollTop const { scrollHeight, clientHeight } = $scroller - // Check if already scrolled to bottom and user continues scrolling down if (isAlreadyScrolledBottomRef.current) { navigateToNext() return } if (scrollHeight && clientHeight) { - // Check bottom scroll - const isAtBottom = Math.abs(currentScroll + clientHeight - scrollHeight) < 2 - isAlreadyScrolledBottomRef.current = isAtBottom - setShowKeepScrollingPanel(isAtBottom) + isAlreadyScrolledBottomRef.current = + Math.abs(currentScroll + clientHeight - scrollHeight) < 2 + setShowKeepScrollingPanel(isAlreadyScrolledBottomRef.current) } } - const handleWheelEvent = (event: WheelEvent) => { - const $scroller = scrollerRef.current - if (!$scroller) return - - // Clear existing timer - if (wheelEndTimerRef.current) { - clearTimeout(wheelEndTimerRef.current) - } - - // If scrolled to top and user tries to scroll up more - if ($scroller.scrollTop <= 2 && event.deltaY < 0) { - if ( - isAlreadyScrolledTopRef.current && - showBackToTimelinePanel && - wheelGestureCompleteRef.current - ) { - // User completed a previous wheel gesture and is starting a new one - navigateBackToTimeline() - return - } - - // First time reaching top in this wheel gesture, just show the panel - if (!isAlreadyScrolledTopRef.current) { - isAlreadyScrolledTopRef.current = true - setShowBackToTimelinePanel(true) - wheelGestureCompleteRef.current = false - } - } else if ($scroller.scrollTop > 2) { - // Reset states when user scrolls away from top - isAlreadyScrolledTopRef.current = false - setShowBackToTimelinePanel(false) - wheelGestureCompleteRef.current = false - } - - // Set timer to detect end of wheel gesture - wheelEndTimerRef.current = setTimeout(() => { - wheelGestureCompleteRef.current = true - }, 150) // 150ms after last wheel event - - // Reset bottom state when wheel is used - if (event.deltaY !== 0) { - isAlreadyScrolledBottomRef.current = false - setShowKeepScrollingPanel(false) - } - } - - const handleTouchStart = (event: TouchEvent) => { - // Reset all scroll states when touch starts + const checkScrollBottomByWheel = () => { isAlreadyScrolledBottomRef.current = false - isAlreadyScrolledTopRef.current = false - wheelGestureCompleteRef.current = false setShowKeepScrollingPanel(false) - setShowBackToTimelinePanel(false) - // Clear wheel timer - if (wheelEndTimerRef.current) { - clearTimeout(wheelEndTimerRef.current) - wheelEndTimerRef.current = null - } - // Store initial touch position - if (event.touches[0]) { - lastTouchYRef.current = event.touches[0].clientY - } } - - const handleTouchMove = (event: TouchEvent) => { - const $scroller = scrollerRef.current - if (!$scroller || !event.touches[0]) return - - const { scrollTop } = $scroller - // Check if at top and trying to scroll up via touch - if (scrollTop <= 2) { - const { clientY } = event.touches[0] - if ( - isAlreadyScrolledTopRef.current && - showBackToTimelinePanel && - lastTouchYRef.current !== null - ) { - // Additional upward touch gesture at top when panel is already showing - const deltaY = clientY - lastTouchYRef.current - if (deltaY > 20) { - // Threshold for upward swipe - navigateBackToTimeline() - return - } - } - isAlreadyScrolledTopRef.current = true - setShowBackToTimelinePanel(true) - lastTouchYRef.current = clientY - } else { - isAlreadyScrolledTopRef.current = false - setShowBackToTimelinePanel(false) - } - } - - scrollerRef.current?.addEventListener("wheel", handleWheelEvent) - scrollerRef.current?.addEventListener("touchstart", handleTouchStart) - scrollerRef.current?.addEventListener("touchmove", handleTouchMove) + scrollerRef.current?.addEventListener("wheel", checkScrollBottomByWheel) const cleanupScrollAnimation = () => { scrollAnimationRef.current?.stop() scrollAnimationRef.current = null } - - const cleanupTimers = () => { - if (wheelEndTimerRef.current) { - clearTimeout(wheelEndTimerRef.current) - wheelEndTimerRef.current = null - } - } return combineCleanupFunctions( () => { - scrollerRef.current?.removeEventListener("wheel", handleWheelEvent) - scrollerRef.current?.removeEventListener("touchstart", handleTouchStart) - scrollerRef.current?.removeEventListener("touchmove", handleTouchMove) + scrollerRef.current?.removeEventListener("wheel", checkScrollBottomByWheel) }, cleanupScrollAnimation, - cleanupTimers, EventBus.subscribe(COMMAND_ID.entryRender.scrollUp, () => { const $scroller = scrollerRef.current if (!$scroller) return - // Reset top scroll states for keyboard navigation - isAlreadyScrolledTopRef.current = false - setShowBackToTimelinePanel(false) - wheelGestureCompleteRef.current = false - const currentScroll = $scroller.scrollTop // Smart scroll distance: larger viewports get larger scroll distances // But cap it at a reasonable maximum for very large screens @@ -263,18 +130,13 @@ export const EntryScrollingAndNavigationHandler = ({ cleanupScrollAnimation() const targetScroll = Math.max(0, currentScroll - delta) smoothScrollTo(targetScroll, $scroller) - checkBottomScrollOnly($scroller) + checkScrollBottom($scroller) }), EventBus.subscribe(COMMAND_ID.entryRender.scrollDown, () => { const $scroller = scrollerRef.current if (!$scroller) return - // Reset top scroll states for keyboard navigation - isAlreadyScrolledTopRef.current = false - setShowBackToTimelinePanel(false) - wheelGestureCompleteRef.current = false - const currentScroll = $scroller.scrollTop // Smart scroll distance: larger viewports get larger scroll distances // But cap it at a reasonable maximum for very large screens @@ -287,7 +149,7 @@ export const EntryScrollingAndNavigationHandler = ({ currentScroll + delta, ) smoothScrollTo(targetScroll, $scroller) - checkBottomScrollOnly($scroller) + checkScrollBottom($scroller) }), EventBus.subscribe( COMMAND_ID.layout.focusToEntryRender, @@ -304,15 +166,7 @@ export const EntryScrollingAndNavigationHandler = ({ }, ), ) - }, [ - highlightBoundary, - navigateToNext, - navigateBackToTimeline, - scrollAnimationRef, - scrollerRef, - smoothScrollTo, - showBackToTimelinePanel, - ]) + }, [highlightBoundary, navigateToNext, scrollAnimationRef, scrollerRef, smoothScrollTo]) return ( @@ -344,34 +198,6 @@ export const EntryScrollingAndNavigationHandler = ({ )} - {showBackToTimelinePanel && ( - - - - )} ) }