Files
cutdex/lib/app/shells/desktop_workspace_shell.dart
T
lingjhf 1f73765745
Gitea CI / Windows x64 and Android (pull_request) Successful in 16m21s
Gitea CI / Gitea CI passed (pull_request) Successful in 3s
refactor(desktop): reorganize workspace panels and navigation
2026-10-06 12:11:59 +08:00

250 lines
8.6 KiB
Dart

import 'package:flutter/material.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,
});
static const gutterWidth = WorkspaceLayoutMetrics.desktopSpacing;
final WorkspaceMode mode;
final DesktopWorkspaceController controller;
final bool sidebarCollapsed;
final double? sidebarWidth;
final ValueChanged<bool> onSidebarCollapsedChanged;
final ValueChanged<double> onSidebarWidthChanged;
final WorkspaceSidebarBuilder sidebarBuilder;
final WorkspaceSidebarBuilder agentBuilder;
final Widget editor;
final GlobalKey<ScaffoldState> scaffoldKey;
final Widget navigationDrawer;
@override
State<DesktopWorkspaceShell> createState() => _DesktopWorkspaceShellState();
}
class _DesktopWorkspaceShellState extends State<DesktopWorkspaceShell> {
@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<double> 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) => LayoutBuilder(
builder: (context, constraints) {
const gap = DesktopWorkspaceShell.gutterWidth;
final width = constraints.maxWidth;
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: 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 agentHidden =
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 (!agentHidden)
SizedBox(
key: const Key('desktop-agent-slot'),
width: geometry.agentWidth,
child: _card(
'agent',
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,
),
),
],
),
);
},
);
}