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, }); 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; @override State createState() => _DesktopWorkspaceShellState(); } class _DesktopWorkspaceShellState extends State { @override void initState() { super.initState(); widget.controller.addListener(_changed); } void _changed() => setState(() {}); @override void didUpdateWidget(covariant DesktopWorkspaceShell oldWidget) { super.didUpdateWidget(oldWidget); 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, }) => SizedBox( width: WorkspaceLayoutMetrics.desktopResizeHitExtent, height: double.infinity, child: WorkspaceResizeHandle( key: Key('$name-resize'), axis: Axis.horizontal, initialValue: initialWidth, direction: direction, onResize: onResize, onEnd: onEnd, ), ); @override Widget build(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, 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; final geometry = DesktopWorkspaceGeometry( sidebarWidth: sidebarWidth, agentWidth: 0, editorHidden: false, ); return Scaffold( key: widget.scaffoldKey, 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 (!collapsed) Positioned( left: sidebarWidth - WorkspaceLayoutMetrics.desktopResizeSlop, top: 0, bottom: 0, child: _gutter( name: 'project-sidebar', initialWidth: sidebarWidth, onResize: (value) => widget.onSidebarWidthChanged( value.clamp( WorkspaceLayoutMetrics.sidebarMinimumWidth, maximum, ), ), ), ), 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, () {}), ), ), ), ], ), ); }, ); }