fix(webui): align 3D model viewer layout and full-width control bar with video viewer

This commit is contained in:
yiling
2026-10-08 23:47:41 +08:00
parent 2e24646c8b
commit ae6cdc88da
3 changed files with 157 additions and 123 deletions
@@ -1,5 +1,6 @@
import React from 'react'
import { Play, Pause, RotateCcw, Maximize, Minimize, Minus, Plus, Download } from 'lucide-react'
import { cn } from '@/ui/lib/utils'
import { RulerSlider } from './ruler-slider'
export interface ModelControlBarProps {
@@ -44,35 +45,45 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
}
return (
<div className="absolute inset-x-0 bottom-4 z-20 flex justify-center px-4 pointer-events-none">
<div className="pointer-events-auto flex items-center gap-3 px-4 py-2 rounded-xl bg-card/90 backdrop-blur-md border border-border/80 shadow-xl w-full max-w-4xl text-foreground">
{/* Play/Pause & Degree Readout */}
<div className="flex items-center gap-3 shrink-0">
<div
className={cn(
'transition-all duration-300 ease-in-out z-20 text-foreground',
isFullScreen
? 'absolute bottom-0 left-0 right-0 px-4 py-3 bg-card/90 backdrop-blur-md border-t border-border'
: 'relative w-full bg-card border-t border-border px-4 py-3 z-10 transition-colors duration-200',
)}
>
{/* Top: Ruler Slider across full width */}
<div className="mb-3 px-1">
<RulerSlider currentDegree={currentDegree} onChange={onSeekDegree} />
</div>
{/* Bottom: Lower Controls Row */}
<div className="flex items-center justify-between text-foreground">
{/* Left Side: Play/Pause, Degree Readout, Reset Rotation */}
<div className="flex items-center gap-4">
<button
type="button"
onClick={togglePlay}
aria-label={isPlaying ? 'Pause' : 'Play'}
className="flex items-center justify-center w-8 h-8 rounded-lg bg-primary text-primary-foreground hover:bg-primary/90 transition-colors shadow-xs"
className="hover:text-primary transition-colors"
data-testid="play-toggle"
data-playing={isPlaying}
>
{isPlaying ? <Pause className="w-4 h-4" /> : <Play className="w-4 h-4 ml-0.5" />}
{isPlaying ? (
<Pause className="w-6 h-6 fill-current" />
) : (
<Play className="w-6 h-6 fill-current" />
)}
</button>
<div
className="text-xs font-mono font-medium tabular-nums min-w-[76px]"
className="flex items-center gap-1 text-sm font-medium tabular-nums select-none min-w-[90px]"
data-testid="degree-readout"
>
{currentDegree}° / 360°
<span className="text-muted-foreground font-mono">{currentDegree}° / 360°</span>
</div>
</div>
{/* Ruler Slider */}
<div className="flex-1 min-w-[200px]">
<RulerSlider currentDegree={currentDegree} onChange={onSeekDegree} />
</div>
{/* Right Action Tools */}
<div className="flex items-center gap-2 shrink-0">
{/* Reset rotation to 0° */}
<button
type="button"
onClick={onResetRotation}
@@ -82,9 +93,12 @@ export const ModelControlBar: React.FC<ModelControlBarProps> = ({
>
<RotateCcw className="w-4 h-4" />
</button>
</div>
{/* Right Side: Zoom controls, Download, Fullscreen */}
<div className="flex items-center gap-2">
{/* Zoom controls */}
<div className="hidden sm:flex items-center gap-1 bg-muted/60 rounded-lg p-0.5 border border-border/40">
<div className="flex items-center gap-1 bg-muted/60 rounded-lg p-0.5 border border-border/40">
<button
type="button"
onClick={() => onZoomChange(Math.max(0.2, zoom - 0.2))}
@@ -160,4 +160,15 @@ describe('ModelViewer', () => {
fireEvent.click(resetBtn)
expect(onTimeUpdate).toHaveBeenCalledWith(0)
})
it('renders children alongside viewer surface when provided', () => {
const { getByTestId } = render(
<ModelViewer file={mockAsset}>
<div data-testid="test-sidebar">Sidebar</div>
</ModelViewer>,
)
expect(getByTestId('test-sidebar')).toBeDefined()
expect(getByTestId('model-viewer-surface')).toBeDefined()
})
})
@@ -27,6 +27,7 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
},
ref,
) => {
const rootRef = useRef<HTMLDivElement | null>(null)
const videoRef = useRef<HTMLVideoElement | null>(null)
const containerRef = useRef<HTMLDivElement | null>(null)
const [containerSize, setContainerSize] = useState({ width: 0, height: 0 })
@@ -214,8 +215,9 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
// Fullscreen toggle
const toggleFullScreen = () => {
if (!rootRef.current) return
if (!document.fullscreenElement) {
containerRef.current?.requestFullscreen().catch(() => {})
rootRef.current.requestFullscreen().catch(() => {})
} else {
document.exitFullscreen().catch(() => {})
}
@@ -255,116 +257,125 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
return (
<div
ref={containerRef}
ref={rootRef}
data-testid="model-viewer-container"
className="relative w-full h-full bg-background overflow-hidden select-none flex items-center justify-center"
className={cn(
'group shadow-2xl font-sans select-none flex flex-col mx-auto relative',
isFullScreen ? 'h-full w-full rounded-none bg-black' : 'w-full h-full',
)}
>
{/* Interactive Drag Rotation Surface */}
<div
data-testid="model-viewer-surface"
onPointerDown={handleViewerPointerDown}
onPointerMove={handleViewerPointerMove}
onPointerUp={handleViewerPointerUp}
onPointerCancel={handleViewerPointerUp}
className={cn(
'absolute inset-0 z-0 touch-none',
isDrawingMode
? 'cursor-crosshair'
: isDraggingRotation
? 'cursor-grabbing'
: 'cursor-grab',
)}
>
{/* Centered Scaled Media Wrapper */}
<div
className="absolute pointer-events-none"
style={{
left: 0,
top: 0,
width: mediaWidth,
height: mediaHeight,
transform: `translate(${pan.x}px, ${pan.y}px) scale(${zoom})`,
transformOrigin: '0 0',
}}
>
{videoSrc && (
<video
ref={videoRef}
src={videoSrc}
poster={posterUrl}
playsInline
loop
muted
autoPlay={autoPlay}
preload="auto"
onLoadedData={() => {
setIsLoading(false)
if (startTime && startTime > 0) {
seekToFrame(Math.floor(startTime * FPS))
}
}}
onPlay={() => {
setIsPlaying(true)
onPlay?.()
}}
onPause={() => {
setIsPlaying(false)
onPause?.()
}}
onTimeUpdate={() => {
if (videoRef.current && !isDraggingRotation) {
setCurrentTime(videoRef.current.currentTime)
onTimeUpdate?.(videoRef.current.currentTime)
}
}}
onError={() => {
setIsLoading(false)
setError('Failed to load turntable video')
}}
className="w-full h-full block object-contain pointer-events-none"
/>
)}
</div>
<div className="flex-1 flex flex-col-reverse md:flex-row min-h-0 relative">
{/* Render Carousel/Sidebar here if not fullscreen */}
{!isFullScreen && children}
{/* Drawing Canvas Overlay */}
{containerSize.width > 0 && containerSize.height > 0 && (
<DrawingCanvas
width={containerSize.width}
height={containerSize.height}
mediaDimensions={{
{/* Interactive Drag Rotation Surface & Model Area */}
<div
ref={containerRef}
data-testid="model-viewer-surface"
onPointerDown={handleViewerPointerDown}
onPointerMove={handleViewerPointerMove}
onPointerUp={handleViewerPointerUp}
onPointerCancel={handleViewerPointerUp}
className={cn(
'flex-1 bg-black relative flex items-center justify-center overflow-hidden min-h-0 touch-none',
isDrawingMode
? 'cursor-crosshair'
: isDraggingRotation
? 'cursor-grabbing'
: 'cursor-grab',
)}
>
{/* Centered Scaled Media Wrapper */}
<div
className="absolute pointer-events-none"
style={{
left: 0,
top: 0,
width: mediaWidth,
height: mediaHeight,
transform: `translate(${pan.x}px, ${pan.y}px) scale(${zoom})`,
transformOrigin: '0 0',
}}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
className="absolute inset-0 pointer-events-none"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
)}
>
{videoSrc && (
<video
ref={videoRef}
src={videoSrc}
poster={posterUrl}
playsInline
loop
muted
autoPlay={autoPlay}
preload="auto"
onLoadedData={() => {
setIsLoading(false)
if (startTime && startTime > 0) {
seekToFrame(Math.floor(startTime * FPS))
}
}}
onPlay={() => {
setIsPlaying(true)
onPlay?.()
}}
onPause={() => {
setIsPlaying(false)
onPause?.()
}}
onTimeUpdate={() => {
if (videoRef.current && !isDraggingRotation) {
setCurrentTime(videoRef.current.currentTime)
onTimeUpdate?.(videoRef.current.currentTime)
}
}}
onError={() => {
setIsLoading(false)
setError('Failed to load turntable video')
}}
className="w-full h-full block object-contain pointer-events-none"
/>
)}
</div>
{/* Drawing Canvas Overlay */}
{containerSize.width > 0 && containerSize.height > 0 && (
<DrawingCanvas
width={containerSize.width}
height={containerSize.height}
mediaDimensions={{
width: mediaWidth,
height: mediaHeight,
}}
annotations={displayAnnotations}
scale={zoom}
offset={pan}
className="absolute inset-0 pointer-events-none"
isDrawing={isDrawing}
currentTool={currentTool}
currentColor={currentColor}
onAddAnnotation={addAnnotation}
/>
)}
{/* Loading Spinner */}
{isLoading && !error && (
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
<div className="w-14 h-14 bg-background/80 backdrop-blur-md rounded-full flex items-center justify-center border border-border shadow-lg">
<Loader2 className="w-7 h-7 text-primary animate-spin" />
</div>
</div>
)}
{/* Error Display */}
{error && (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-2 text-destructive z-10 bg-background/90 p-4 text-center">
<AlertCircle className="w-10 h-10" />
<p className="text-sm font-medium">{error}</p>
</div>
)}
</div>
</div>
{/* Loading Spinner */}
{isLoading && !error && (
<div className="absolute inset-0 flex items-center justify-center bg-black/20 pointer-events-none z-10">
<div className="w-14 h-14 bg-background/80 backdrop-blur-md rounded-full flex items-center justify-center border border-border shadow-lg">
<Loader2 className="w-7 h-7 text-primary animate-spin" />
</div>
</div>
)}
{/* Error Display */}
{error && (
<div className="absolute inset-0 flex flex-col items-center justify-center gap-2 text-destructive z-10 bg-background/90 p-4 text-center">
<AlertCircle className="w-10 h-10" />
<p className="text-sm font-medium">{error}</p>
</div>
)}
{/* Floating Bottom Control Bar */}
{/* Full-width docked bottom control bar */}
<ModelControlBar
isPlaying={isPlaying}
currentDegree={currentDegree}
@@ -380,8 +391,6 @@ export const ModelViewer = React.forwardRef<MediaController, FileViewerProps>(
onZoomReset={handleZoomReset}
toggleFullScreen={toggleFullScreen}
/>
{children}
</div>
)
},