import 'dart:math' as math; import 'package:flutter/material.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:cutdex_ui/widgets.dart'; import '../adaptive/desktop_workspace_controller.dart'; import '../adaptive/workspace_layout_policy.dart'; import '../../shared/layout/workspace_layout_metrics.dart'; import '../../shared/widgets/workspace_card.dart'; import '../../shared/widgets/workspace_resize_handle.dart'; import '../../workspace/workspace_mode.dart'; typedef WorkspaceSidebarBuilder = Widget Function( DesktopWorkspaceGeometry geometry, VoidCallback expandEditor, ); /// Three independent desktop surfaces with resize gutters between them. class DesktopWorkspaceShell extends StatefulWidget { const DesktopWorkspaceShell({ super.key, required this.mode, required this.controller, required this.sidebarCollapsed, required this.sidebarWidth, required this.onSidebarCollapsedChanged, required this.onSidebarWidthChanged, required this.sidebarBuilder, required this.agentBuilder, required this.editor, required this.scaffoldKey, required this.navigationDrawer, this.projectTabsLayout = false, this.timelineHeight, this.onTimelineHeightChanged, this.onJunctionResizeEnd, this.onSidebarResizeEnd, this.sidebarCanExpand = true, }); static const gutterWidth = WorkspaceLayoutMetrics.desktopSpacing; final WorkspaceMode mode; final DesktopWorkspaceController controller; final bool sidebarCollapsed; final double? sidebarWidth; final ValueChanged onSidebarCollapsedChanged; final ValueChanged onSidebarWidthChanged; final WorkspaceSidebarBuilder sidebarBuilder; final WorkspaceSidebarBuilder agentBuilder; final Widget editor; final GlobalKey scaffoldKey; final Widget navigationDrawer; final bool projectTabsLayout; final double? timelineHeight; final ValueChanged? onTimelineHeightChanged; final VoidCallback? onJunctionResizeEnd; final VoidCallback? onSidebarResizeEnd; final bool sidebarCanExpand; @override State createState() => _DesktopWorkspaceShellState(); } class _DesktopWorkspaceShellState extends State { double? _sidebarRestoreWidth; bool _sidebarJunctionDragging = false; void _startSidebarResize(double width, {bool junction = false}) { _sidebarRestoreWidth = width; if (junction) setState(() => _sidebarJunctionDragging = true); } void _resizeSidebar(double requestedWidth, double maximum) { if (!widget.sidebarCanExpand || widget.controller.agentFloating) return; if (requestedWidth < WorkspaceLayoutMetrics.sidebarSnapThreshold) { if (!widget.sidebarCollapsed) { final restoreWidth = _sidebarRestoreWidth; if (restoreWidth != null && widget.sidebarWidth != restoreWidth) { widget.onSidebarWidthChanged(restoreWidth); } widget.onSidebarCollapsedChanged(true); } return; } if (widget.sidebarCollapsed) widget.onSidebarCollapsedChanged(false); widget.onSidebarWidthChanged( requestedWidth.clamp(WorkspaceLayoutMetrics.sidebarMinimumWidth, maximum), ); } void _finishSidebarResize() { _sidebarRestoreWidth = null; if (_sidebarJunctionDragging) { setState(() => _sidebarJunctionDragging = false); widget.onJunctionResizeEnd?.call(); } else { widget.onSidebarResizeEnd?.call(); } } @override void initState() { super.initState(); widget.controller.addListener(_changed); } void _changed() => setState(() {}); @override void didUpdateWidget(covariant DesktopWorkspaceShell oldWidget) { super.didUpdateWidget(oldWidget); if (widget.controller.agentFloating || !widget.sidebarCanExpand || widget.onTimelineHeightChanged == null) { _sidebarJunctionDragging = false; } if (oldWidget.controller != widget.controller) { oldWidget.controller.removeListener(_changed); widget.controller.addListener(_changed); } } @override void dispose() { widget.controller.removeListener(_changed); super.dispose(); } Widget _card(String name, Widget child) => WorkspaceCard(key: Key('desktop-$name-card'), child: child); Widget _gutter({ required String name, required double initialWidth, required ValueChanged onResize, double direction = 1, VoidCallback? onEnd, VoidCallback? onStart, Set highlightTargets = const {}, Offset highlightOffset = Offset.zero, }) => SizedBox( width: WorkspaceLayoutMetrics.desktopResizeHitExtent, height: double.infinity, child: WorkspaceResizeHandle( key: Key('$name-resize'), axis: Axis.horizontal, initialValue: initialWidth, direction: direction, onResize: onResize, onEnd: onEnd, onStart: onStart, highlightTargets: highlightTargets, highlightOffset: highlightOffset, ), ); @override Widget build(BuildContext context) => WorkspaceResizeHighlightScope(child: _workspace(context)); Widget _workspace(BuildContext context) => widget.projectTabsLayout ? _projectWorkspace(context) : LayoutBuilder( builder: (context, constraints) { const gap = DesktopWorkspaceShell.gutterWidth; final width = constraints.maxWidth; final requestedFloating = widget.controller.agentFloating; final showAgent = widget.mode == WorkspaceMode.agent || widget.controller.agentVisible; final navigationGap = widget.sidebarCollapsed ? 0.0 : gap; final resolved = DesktopWorkspaceGeometry.resolve( // The geometry policy accounts for a one-pixel divider; reserve gutters. width: width - navigationGap - gap + 1, mode: requestedFloating || !showAgent ? WorkspaceMode.cut : WorkspaceMode.agent, sidebarCollapsed: widget.sidebarCollapsed, sidebarWidth: widget.sidebarWidth, agentWidth: widget.controller.agentWidth, agentFullscreen: widget.controller.agentFullscreen, ); final navigationSpace = widget.sidebarCollapsed ? 0.0 : resolved.sidebarWidth; final floating = requestedFloating || (showAgent && widget.mode == WorkspaceMode.cut && resolved.editorHidden && !widget.controller.agentFullscreen); final agentHidden = floating || !showAgent || resolved.editorHidden && widget.mode == WorkspaceMode.cut && !widget.controller.agentFullscreen; final geometry = DesktopWorkspaceGeometry( sidebarWidth: resolved.sidebarWidth, agentWidth: agentHidden ? 0 : resolved.editorHidden ? width - navigationSpace - navigationGap : resolved.agentWidth, editorHidden: resolved.editorHidden && !agentHidden, ); final available = width - navigationSpace - navigationGap; void expandEditor() { final collapseNavigation = available < 220 + gap + DesktopWorkspaceGeometry.editorMinimumWidth; if (collapseNavigation) widget.onSidebarCollapsedChanged(true); widget.controller.setAgentFullscreen(false); } void resizeAgent(double requested) { final maximum = available - gap - DesktopWorkspaceGeometry.editorMinimumWidth; if (maximum < 220) return; final agentWidth = requested.clamp(220.0, maximum); widget.controller.setAgentWidth(agentWidth); } return Scaffold( key: widget.scaffoldKey, drawerEnableOpenDragGesture: false, backgroundColor: Colors.transparent, drawer: Drawer( width: 320, child: SafeArea(child: widget.navigationDrawer), ), body: Stack( key: const Key('desktop-workspace-shell'), children: [ Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (!widget.sidebarCollapsed) ...[ SizedBox( width: navigationSpace, child: _card( 'project', widget.sidebarBuilder(geometry, expandEditor), ), ), const SizedBox( key: Key('project-sidebar-divider'), width: gap, ), ], Expanded( child: Offstage( key: const Key('editor-auto-collapse'), offstage: geometry.editorHidden, child: TickerMode( enabled: !geometry.editorHidden, child: OverflowBox( alignment: Alignment.topLeft, minWidth: geometry.editorHidden ? DesktopWorkspaceGeometry.editorRestoreWidth : null, maxWidth: geometry.editorHidden ? DesktopWorkspaceGeometry.editorRestoreWidth : null, child: SizedBox( key: const Key('desktop-editor-card'), child: widget.editor, ), ), ), ), ), if (!agentHidden && !geometry.editorHidden) const SizedBox(width: gap), if (!floating) Offstage( offstage: agentHidden, child: ExcludeFocus( excluding: agentHidden, child: TickerMode( enabled: !agentHidden, child: SizedBox( key: const Key('desktop-agent-slot'), width: agentHidden ? widget.controller.agentWidth ?? 320 : geometry.agentWidth, child: _card( 'agent', widget.agentBuilder(geometry, expandEditor), ), ), ), ), ), ], ), if (floating) Positioned( key: const Key('agent-floating-window'), right: 12, bottom: 12, width: math.min(560, math.max(0, available - 24)), child: AppFloatingDialog( key: const Key('floating-agent-dialog'), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(22), ), backgroundColor: AppSurfaces.of(context) .composerBackground, child: ConstrainedBox( constraints: BoxConstraints( maxHeight: math.min( 640, math.max(0, constraints.maxHeight - 24), ), ), child: widget.agentBuilder(geometry, expandEditor), ), ), ), if (!widget.sidebarCollapsed) Positioned( left: navigationSpace - WorkspaceLayoutMetrics.desktopResizeSlop, top: 0, bottom: 0, child: _gutter( name: 'project-sidebar', initialWidth: navigationSpace, onResize: (value) => widget.onSidebarWidthChanged( value.clamp( WorkspaceLayoutMetrics.sidebarMinimumWidth, DesktopWorkspaceGeometry.sidebarMaximum( width - 2 * gap + 1, WorkspaceMode.agent, ), ), ), ), ), if (!agentHidden && !geometry.editorHidden) Positioned( left: geometry.editorHidden ? navigationSpace + navigationGap + (widget.sidebarCollapsed ? 0 : WorkspaceLayoutMetrics.desktopResizeSlop) : width - geometry.agentWidth - gap - WorkspaceLayoutMetrics.desktopResizeSlop, top: 0, bottom: 0, child: _gutter( name: 'agent-panel', initialWidth: geometry.agentWidth, direction: -1, onResize: resizeAgent, ), ), ], ), ); }, ); Widget _projectWorkspace(BuildContext context) => LayoutBuilder( builder: (context, constraints) { const gap = DesktopWorkspaceShell.gutterWidth; final width = constraints.maxWidth; final maximum = WorkspaceLayoutMetrics.sidebarMaximumWidth(width - gap); final sidebarWidth = (widget.sidebarWidth ?? WorkspaceLayoutMetrics.sidebarDefaultWidth) .clamp(WorkspaceLayoutMetrics.sidebarMinimumWidth, maximum) .toDouble(); final floating = widget.controller.agentFloating; final collapsed = widget.sidebarCollapsed || floating; double resolveTimelineHeight(double? height) => WorkspaceLayoutMetrics.resolveTimelineHeight( constraints.maxHeight, height, ); final timelineHeight = resolveTimelineHeight(widget.timelineHeight); final geometry = DesktopWorkspaceGeometry( sidebarWidth: sidebarWidth, agentWidth: 0, editorHidden: false, ); return Scaffold( key: widget.scaffoldKey, drawerEnableOpenDragGesture: false, backgroundColor: Colors.transparent, drawer: Drawer( width: 320, child: SafeArea( // The drawer and floating panel share the conversation widget. child: floating ? const SizedBox.shrink() : widget.navigationDrawer, ), ), body: Stack( key: const Key('desktop-workspace-shell'), children: [ Row( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ if (!collapsed) ...[ SizedBox( width: sidebarWidth, child: _card( 'chat', widget.sidebarBuilder(geometry, () {}), ), ), const SizedBox(width: gap), ], Expanded( child: SizedBox( key: const Key('desktop-editor-card'), child: widget.editor, ), ), ], ), if (!floating && widget.sidebarCanExpand && WorkspaceLayoutPolicy.desktopNavigationFits(width)) Positioned( left: collapsed ? 0 : sidebarWidth - WorkspaceLayoutMetrics.desktopResizeSlop, top: 0, bottom: 0, child: _gutter( name: 'project-sidebar', highlightTargets: const {WorkspaceResizeBoundary.sidebar}, highlightOffset: collapsed ? const Offset( -WorkspaceLayoutMetrics.desktopResizeHitExtent / 2, 0, ) : Offset.zero, initialWidth: collapsed ? 0 : sidebarWidth, onStart: () => _startSidebarResize(sidebarWidth), onResize: (value) => _resizeSidebar(value, maximum), onEnd: _finishSidebarResize, ), ), if (!floating && widget.sidebarCanExpand && WorkspaceLayoutPolicy.desktopNavigationFits(width) && widget.onTimelineHeightChanged != null) Positioned( left: collapsed ? 0 : sidebarWidth - WorkspaceLayoutMetrics.desktopResizeSlop, bottom: timelineHeight - WorkspaceLayoutMetrics.desktopResizeSlop, width: WorkspaceLayoutMetrics.desktopResizeHitExtent, height: WorkspaceLayoutMetrics.desktopResizeHitExtent, child: WorkspaceResizeHandle.junction( key: const Key('desktop-chat-timeline-junction'), highlightOffset: collapsed ? const Offset( -WorkspaceLayoutMetrics.desktopResizeHitExtent / 2, 0, ) : Offset.zero, highlightTargets: const { WorkspaceResizeBoundary.sidebar, WorkspaceResizeBoundary.timeline, }, initialWidth: collapsed ? 0 : sidebarWidth, initialHeight: timelineHeight, verticalDirection: -1, onStart: () => _startSidebarResize(sidebarWidth, junction: true), onWidthChanged: (value) => _resizeSidebar(value, maximum), onHeightChanged: (value) => widget.onTimelineHeightChanged!( resolveTimelineHeight(value), ), onEnd: _finishSidebarResize, ), ), if (floating) Positioned( key: const Key('agent-floating-window'), right: 12, bottom: 12, width: math.min(560, math.max(0, width - 24)), child: AppFloatingDialog( key: const Key('floating-agent-dialog'), shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(22), ), backgroundColor: AppSurfaces.of(context).composerBackground, child: ConstrainedBox( constraints: BoxConstraints( maxHeight: math.min( 640, math.max(0, constraints.maxHeight - 24), ), ), child: widget.agentBuilder(geometry, () {}), ), ), ), ], ), ); }, ); }