// 应用外壳:负责公共窗口框架、主题、文件流程、侧栏停靠与全局快捷键。 // // 架构位置:位于 UI 顶层(main.dart 的根组件加载),**不识别具体模块**: // - 模块清单、元数据、文档生命周期与工作区 UI 全部来自 // `lib/modules/registry.dart` + `lib/modules/module.dart`; // - 外壳只负责:工具栏公共项 / 侧栏停靠轨道 / 状态栏容器 / 文件打开保存 / // 最近列表 / 全局快捷键 / 模块快捷键合并; // - 新增模块时不要修改本文件;通过注册表与模块契约扩展。 import 'dart:convert'; import 'dart:io'; import 'dart:math' as math; import 'package:flutter/material.dart' hide Action; import 'package:flutter/services.dart'; import 'package:sequence_diagram/modules/registry.dart'; import 'package:sequence_diagram/persistence/recent_store.dart'; import 'package:sequence_diagram/persistence/android_docs.dart'; import 'package:sequence_diagram/state/app_controller.dart'; import 'package:sequence_diagram/theme/tokens.dart'; import 'package:sequence_diagram/update/update_service.dart'; import 'package:sequence_diagram/version.dart'; import 'brand_menu.dart'; import 'dialogs.dart'; import 'home_view.dart'; import 'update_dialog.dart'; import 'update_icon.dart'; /// 应用外壳组件:承载主界面布局与文件/侧栏等交互逻辑。 class AppShell extends StatefulWidget { /// 创建应用外壳,[controller] 为全局应用控制器。 const AppShell({super.key, required this.controller}); /// 全局应用控制器。 final AppController controller; /// 创建应用外壳对应的状态对象。 @override State createState() => _AppShellState(); } /// 应用外壳状态:维护左右侧栏的可见性、钉住与悬浮状态, /// 并提供文件操作、导出与快捷键处理。 class _AppShellState extends State { // ---- 侧栏状态机:可见(visible) × 钉住(pinned) × 悬停(hover) ---- /// 左侧栏是否可见。 bool _leftVisible = true; /// 左侧栏是否钉住(钉住则停靠,不随鼠标移出隐藏)。 bool _leftPinned = true; /// 鼠标是否悬停在左侧栏区域(仅未钉住时用于悬浮展开)。 bool _leftHover = false; /// 右侧栏是否可见。 bool _rightVisible = true; /// 右侧栏是否钉住。 bool _rightPinned = true; /// 鼠标是否悬停在右侧栏区域。 bool _rightHover = false; /// 控制器快捷访问器。 AppController get c => widget.controller; // ---- 由上述三态派生的侧栏呈现方式 ---- /// 左侧栏宽度:随屏幕宽度动态压缩(300 → 200),窄屏双侧栏停靠也不溢出。 double get _leftPanelWidth { final double w = MediaQuery.sizeOf(context).width; return math.min(300.0, math.max(200.0, w * 0.24)); } /// 右侧栏宽度:随屏幕宽度动态压缩(340 → 240),窄屏双侧栏停靠也不溢出。 double get _rightPanelWidth { final double w = MediaQuery.sizeOf(context).width; return math.min(340.0, math.max(240.0, w * 0.26)); } /// 工具栏整体缩放系数:触屏平台(安卓 / iOS)缩小到 80%, /// 一排即可放下全部操作且不影响点按;桌面端保持原尺寸。 double get _toolScale => (Platform.isAndroid || Platform.isIOS) ? 0.8 : 1.0; /// 工具栏字号:随 [_toolScale] 缩放并保底 9,兼顾紧凑与可读。 double _toolFont(double size) => math.max(9.0, size * _toolScale); /// 左侧栏是否处于停靠(固定)状态。 bool get _leftDocked => _leftVisible && _leftPinned; /// 右侧栏是否处于停靠(固定)状态。 bool get _rightDocked => _rightVisible && _rightPinned; /// 左侧栏是否处于悬浮展开状态。 bool get _leftFloat => _leftVisible && !_leftPinned && _leftHover; /// 右侧栏是否处于悬浮展开状态。 bool get _rightFloat => _rightVisible && !_rightPinned && _rightHover; /// 左侧栏是否收起为贴边轨道。 bool get _leftRail => !_leftDocked && !_leftFloat; /// 右侧栏是否收起为贴边轨道。 bool get _rightRail => !_rightDocked && !_rightFloat; /// 当前注册模块;首页为 null。 EdcModule? get _module => moduleById(c.activeModuleId); /// 构造模块 UI 上下文(外壳 -> 模块)。 ModuleUiContext _moduleUi(Tokens t, {bool compact = false}) => ModuleUiContext( context: context, controller: c, tokens: t, compact: compact, leftRail: _leftRail, rightRail: _rightRail, leftPanelWidth: _leftPanelWidth, rightPanelWidth: _rightPanelWidth, toolScale: _toolScale, toolFont: _toolFont, panelControls: (bool left) => _panelControls(t, left), ); /// 订阅控制器变化以便刷新界面;启动后延迟静默检查更新。 @override void initState() { super.initState(); c.addListener(_onChange); // 延迟几秒再检查,避免与启动渲染争资源。 WidgetsBinding.instance.addPostFrameCallback((_) => _autoCheckUpdate()); } // ---- 应用内更新状态 ---- /// 检测到的新版本信息;为空表示暂无更新(或尚未检查 / 检查失败)。 UpdateInfo? _update; /// 是否正在手动检查更新(防止重复点击)。 bool _checkingUpdate = false; /// 是否已完成一次按平台 / 屏幕宽度的默认布局初始化。 bool _layoutInited = false; /// 首次构建时按平台与屏幕宽度设定侧栏默认状态(动态兼容不同分辨率): /// - 触屏平台(Android / iOS):默认收起为贴边轨道,给画布让出完整宽度; /// - 桌面窄窗口(< 1500 逻辑宽):同样默认收起,避免两侧栏挤占画布。 @override void didChangeDependencies() { super.didChangeDependencies(); if (_layoutInited) return; _layoutInited = true; final bool touch = Platform.isAndroid || Platform.isIOS; final double w = MediaQuery.of(context).size.width; if (touch || w < 1500) { _leftVisible = false; _rightVisible = false; _leftPinned = false; _rightPinned = false; } } /// 取消订阅并释放资源。 @override void dispose() { c.removeListener(_onChange); super.dispose(); } /// 控制器变化回调:触发重建。 void _onChange() => setState(() {}); // ===================== 文件与导出流程 ===================== /// 当前有未保存修改时弹窗确认是否放弃;返回 true 表示可以继续。 Future _confirmDiscardIfDirty() async { if (!c.dirty) return true; final bool? ok = await showDialog( context: context, builder: (BuildContext ctx) => AlertDialog( title: const Text('放弃未保存的修改?'), content: const Text('当前项目有未保存的修改,继续将丢失这些修改。'), actions: [ TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('取消')), FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('继续')), ], ), ); return ok == true; } /// 记录「最近打开」(异步写盘,失败静默)。 void _rememberRecent(String path, String moduleId) { RecentStore.remember(path: path, moduleId: moduleId); } /// 从首页 / 品牌菜单进入某模块并**新建**该类型文档(未保存修改先确认)。 Future _startModule(EdcModule m) async { if (!m.available) return; if (c.activeModuleId == m.id) return; if (!await _confirmDiscardIfDirty()) return; m.newDocument(c); c.setModuleId(m.id, status: m.enterStatus); } /// 返回首页(未保存修改先确认)。 Future _goHome() async { if (c.isHome) return; if (!await _confirmDiscardIfDirty()) return; c.goHome(); } /// 品牌菜单命令路由(`_home` / `_settings` / 模块 id)。 Future _handleBrandCommand(String cmd) async { if (!mounted) return; if (cmd == '_home') { await _goHome(); return; } if (cmd == '_settings') { await showDialog( context: context, builder: (_) => SoftwareSettingsDialog(controller: c), ); return; } final EdcModule? m = moduleById(cmd); if (m != null) await _startModule(m); } /// 新建项目:当前模块内新建(未保存修改先确认)。 Future _newProject() async { final EdcModule? m = _module; if (m == null) { await _openDocumentChooser(); return; } if (!await _confirmDiscardIfDirty()) return; m.newDocument(c); } /// 打开文档(工具栏 / Ctrl+O):模块内直接打开;首页时先选择文档类型。 Future _open() async { final EdcModule? m = _module; if (m == null) { await _openDocumentChooser(); return; } await _openDocumentOfModule(m); } /// 「打开文档…」:先选文档类型,再走对应类型的打开流程。 Future _openDocumentChooser() async { final EdcModule? m = await showDocumentTypeDialog(context); if (m == null) return; await _openDocumentOfModule(m); } /// 打开某模块的文档:后缀与解析由模块契约提供。 Future _openDocumentOfModule(EdcModule m) async { if (!await _confirmDiscardIfDirty()) return; if (!mounted) return; final String suffix = m.suffix; // 安卓版:从公共目录打开(兼容旧目录)。 if (AndroidDocs.isAvailable) { final String? name = await showDialog( context: context, builder: (_) => AndroidFileDialog( saveMode: false, defaultName: '', suffix: suffix, ), ); if (name == null) return; try { final Uint8List? bytes = await AndroidDocs.readFile(name); if (bytes == null) { if (mounted) await showMessage(context, '打开失败', '未找到文件 $name'); return; } c.setModuleId(m.id, status: m.enterStatus); m.loadDocument(c, utf8.decode(bytes), path: null); c.setStatus('已打开 ${AndroidDocs.location()}/$name'); } catch (e) { if (mounted) await showMessage(context, '打开失败', '$e'); } return; } final String? path = await pickFile( context, save: false, initialPath: m.filePath(c), suffix: suffix, ); if (path == null) return; try { await _openDocumentAt(m, path); } catch (e) { if (mounted) await showMessage(context, '打开失败', '不是有效的${m.name}文档:$e'); } } /// 直接打开给定路径的文档(最近打开 / 文件选择共用;桌面与安卓一致)。 Future _openDocumentAt(EdcModule m, String path) async { String src; if (AndroidDocs.isAvailable) { final String name = path.split(RegExp(r'[\\/]')).last; final Uint8List? bytes = await AndroidDocs.readFile(name); if (bytes == null) throw Exception('未找到文件 $name'); src = utf8.decode(bytes); } else { src = await File(path).readAsString(); } c.setModuleId(m.id, status: m.enterStatus); m.loadDocument(c, src, path: AndroidDocs.isAvailable ? null : path); _rememberRecent(path, m.id); } /// 打开最近文件(失败时移除失效条目并提示)。 Future _openRecent(EdcModule m, RecentDoc doc) async { if (!await _confirmDiscardIfDirty()) return; try { await _openDocumentAt(m, doc.path); } catch (e) { await RecentStore.remove(doc.path); if (mounted) { await showMessage(context, '打开失败', '文件可能已被移动或删除:\n$e'); } } } /// 保存:已有路径则直接保存,否则转入“另存为”。 /// /// 时序图与架构图分别保存为 `*.seq.json` 与 `*.arch.json` 两个独立文件 /// (内容互不包含;时序图中不含架构数据)。 Future _save() async { final EdcModule? m = _module; if (m == null) return; final String? path = m.filePath(c); // 安卓版:始终走“另存为”(Documents 通道),避免把展示路径当真实路径写入。 if (AndroidDocs.isAvailable || path == null) { await _saveAs(); return; } try { await m.saveTo(c, path); _rememberRecent(path, m.id); } catch (e) { if (mounted) await showMessage(context, '保存失败', '$e'); } } /// 另存为:按当前模块选择目标路径(`*.seq.json` / `*.arch.json`),写入并生成 `.bak` 备份。 Future _saveAs() async { final EdcModule? m = _module; if (m == null) return; final String suffix = m.suffix; final String? current = m.filePath(c); // 安卓版:写入公共 Documents(MediaStore 通道)。 if (AndroidDocs.isAvailable) { final String? name = await showDialog( context: context, builder: (_) => AndroidFileDialog( saveMode: true, defaultName: '${m.documentName(c)}$suffix', suffix: suffix, ), ); if (name == null) return; try { final Uint8List data = Uint8List.fromList(utf8.encode(m.encodeDocument(c))); await AndroidDocs.saveFile(name, data, mime: 'application/json'); final String display = '${AndroidDocs.location()}/$name'; await m.markSaved(c, display); _rememberRecent(display, m.id); } catch (e) { if (mounted) await showMessage(context, '保存失败', '$e'); } return; } final String? path = await pickFile( context, save: true, initialPath: current, defaultName: current == null ? '${m.documentName(c)}$suffix' : null, suffix: suffix, ); if (path == null) return; try { await m.saveTo(c, path); _rememberRecent(path, m.id); } catch (e) { if (mounted) await showMessage(context, '保存失败', '$e'); } } // ===================== 主界面构建 ===================== /// 首页(空白启动工作台):新建文档网格 / 打开 / 最近打开。 Widget _buildHome(Tokens t) { return Scaffold( backgroundColor: t.bg, body: HomeView( controller: c, onNewModule: _startModule, onOpenDocument: _openDocumentChooser, onOpenRecent: _openRecent, onOpenSettings: () => _handleBrandCommand('_settings'), onOpenHelp: () => showDialog(context: context, builder: (_) => const HelpDialog()), onCheckUpdate: _openUpdate, ), ); } /// 构建主界面:绑定全局快捷键,按“工具栏 / 中部(侧栏 + 画布) / 状态栏” /// 三段式纵向布局组织界面。 @override Widget build(BuildContext context) { final Tokens t = context.tok; // 首页(空白启动工作台):不渲染工作区工具栏 / 画布 / 状态栏。 if (c.isHome) return _buildHome(t); final EdcModule? module = _module; if (module == null) return _buildHome(t); final ModuleUiContext ui = _moduleUi(t); // 只读视图(如时序图流程图):撤销 / 重做等编辑操作统一停用。 final bool readOnly = module.isReadOnly(c); // 全局快捷键绑定:编辑、文件、帮助、主题等。 return CallbackShortcuts( bindings: { const SingleActivator(LogicalKeyboardKey.keyZ, control: true): () { if (!readOnly) c.undo(); }, const SingleActivator(LogicalKeyboardKey.keyY, control: true): () { if (!readOnly) c.redo(); }, const SingleActivator(LogicalKeyboardKey.keyS, control: true): _save, const SingleActivator(LogicalKeyboardKey.keyO, control: true): _open, const SingleActivator(LogicalKeyboardKey.keyN, control: true): _newProject, const SingleActivator(LogicalKeyboardKey.f1): () => showDialog(context: context, builder: (_) => const HelpDialog()), const SingleActivator(LogicalKeyboardKey.f2): () => showDialog(context: context, builder: (_) => SettingsDialog(controller: c)), const SingleActivator(LogicalKeyboardKey.f3): c.toggleTheme, ...module.buildShortcuts(ui), }, child: Focus( autofocus: true, child: Scaffold( backgroundColor: t.bg, body: Column( children: [ // 顶部:系统状态栏区域与工具栏同底色,内容在系统栏下方, // 移动端实现真正的全屏(无黑边),桌面端 padding 为 0 无影响。 Container( color: t.chrome, padding: EdgeInsets.only(top: MediaQuery.paddingOf(context).top), child: _toolbar(t), ), Expanded( child: Row( // 拉伸交叉轴:保证左右侧栏始终从工具栏一直贯通到底部状态栏。 crossAxisAlignment: CrossAxisAlignment.stretch, children: [ // 左侧栏:仅在停靠状态占用布局宽度。 if (_leftDocked && module.hasLeftPanel) _moduleLeftPanel(t), Expanded( // 画布舞台:模块提供工作区,外壳只叠加侧栏轨道 / 浮动面板。 child: Stack( children: [ Positioned.fill(child: module.buildWorkspace(ui)), // 收起状态:贴边轨道(悬停可展开为悬浮面板)。 if (module.hasLeftPanel && _leftRail) Positioned(left: 0, top: 0, bottom: 0, child: _rail(t, true)), if (module.hasRightPanel && _rightRail) Positioned(right: 0, top: 0, bottom: 0, child: _rail(t, false)), // 悬浮状态:覆盖在画布上的可浮动侧栏。 if (module.hasLeftPanel && _leftFloat) Positioned(left: 0, top: 0, bottom: 0, child: _floating(t, true)), if (module.hasRightPanel && _rightFloat) Positioned(right: 0, top: 0, bottom: 0, child: _floating(t, false)), ], ), ), // 右侧栏:仅在停靠状态占用布局宽度。 if (_rightDocked && module.hasRightPanel) _moduleRightPanel(t), ], ), ), // 底部:系统导航栏区域与状态栏同底色,内容在系统栏上方。 Container( color: t.chrome, padding: EdgeInsets.only(bottom: MediaQuery.paddingOf(context).bottom), child: _moduleStatusBar(t), ), ], ), ), ), ); } // ===================== 应用内更新 ===================== /// 启动后静默检查更新:发现新版本时在工具栏按钮上显示红色角标。 Future _autoCheckUpdate() async { // 测试环境不发起网络检查(避免 pending timer 与真实请求)。 if (Platform.environment['FLUTTER_TEST'] == 'true') return; await Future.delayed(const Duration(seconds: 3)); if (!mounted || _update != null) return; try { final UpdateInfo? info = await UpdateService.check(); if (mounted && info != null) setState(() => _update = info); } catch (_) { // 静默失败:不打扰用户 } } /// 更新检查按钮:圆环 + 上箭头 + 底座(按 docs/更新.png 的设计绘制); /// 检测到新版本时叠加红色角标。 Widget _updateBtn(Tokens t) { final UpdateInfo? info = _update; final Widget btn = _iconWidgetBtn( UpdateIcon(size: 18 * _toolScale, color: t.text), _openUpdate, tooltip: info == null ? '检查更新' : '发现新版本 v${info.version}', ); if (info == null) return btn; // 红点固定放在按钮左上角(默认是右上角,更醒目也符合「有更新」的提示习惯)。 return Badge( backgroundColor: t.danger, smallSize: 8, alignment: Alignment.topLeft, child: btn, ); } /// 打开更新对话框:已有检测结果时直接弹出,否则先检查一次。 Future _openUpdate() async { if (_checkingUpdate) return; UpdateInfo? info = _update; if (info == null) { _checkingUpdate = true; c.setStatus('正在检查更新…'); try { info = await UpdateService.check(); } catch (e) { if (mounted) c.setStatus('检查更新失败:$e'); } finally { _checkingUpdate = false; } if (!mounted) return; if (info == null) { // 没有更新的说明与安装包:明确提示当前已是最新版本。 c.setStatus('已是最新版本(v$kAppVersion)'); await showMessage(context, '检查更新', '当前已是最新版本(v$kAppVersion)。'); return; } setState(() => _update = info); } if (!mounted) return; await showDialog( context: context, // 点对话框外灰色区域不关闭(下载中防误触退出)。 barrierDismissible: false, builder: (_) => UpdateDialog(controller: c, info: info!), ); } // ===================== 工具栏(自适应 / 紧凑) ===================== /// 构建顶部工具栏:左侧为品牌与文件/编辑操作,右侧为导出、缩放、 /// 单位、侧栏开关与主题切换。根据可用宽度在完整布局与紧凑布局间切换; /// 触屏平台(安卓)整体缩放 [_toolScale],一排放下全部操作。 Widget _toolbar(Tokens t) { final double s = _toolScale; final EdcModule? module = _module; if (module == null) return const SizedBox.shrink(); final bool readOnly = module.isReadOnly(c); return Container( height: 54 * s, padding: EdgeInsets.symmetric(horizontal: 6 * s), decoration: BoxDecoration( color: t.chrome, border: Border(bottom: BorderSide(color: t.line2)), ), child: LayoutBuilder( builder: (BuildContext ctx, BoxConstraints cons) { // 响应式断点:宽于 1760 用完整布局;否则用紧凑布局。 final bool compact = cons.maxWidth < 1760; final ModuleUiContext ui = _moduleUi(t, compact: compact); // 侧栏开关(模块提供侧栏时才显示)。 final Widget sideToggleLeft = _iconBtn(t, Icons.view_sidebar_outlined, () => setState(() { if (_leftVisible) { _leftVisible = false; _leftHover = false; } else { _leftVisible = true; if (!_leftPinned) _leftHover = true; } }), tooltip: '显示 / 隐藏左侧栏'); final Widget sideToggleRight = Transform( alignment: Alignment.center, transform: Matrix4.diagonal3Values(-1.0, 1.0, 1.0), child: _iconBtn(t, Icons.view_sidebar_outlined, () => setState(() { if (_rightVisible) { _rightVisible = false; _rightHover = false; } else { _rightVisible = true; if (!_rightPinned) _rightHover = true; } }), tooltip: '显示 / 隐藏右侧栏'), ); final double gap = compact ? 4 * s : 6 * s; final List left = [ BrandMenuButton( controller: c, scale: s, compact: compact, onCommand: _handleBrandCommand, ), SizedBox(width: compact ? 10 * s : 14 * s), _iconBtn(t, Icons.note_add_outlined, _newProject, tooltip: '新建 Ctrl+N'), _iconBtn(t, Icons.folder_open_outlined, _open, tooltip: '打开 Ctrl+O'), _iconBtn(t, Icons.save_outlined, _save, tooltip: '保存 Ctrl+S'), SizedBox(width: gap), _iconBtn(t, Icons.undo, !readOnly && c.canUndo ? c.undo : null, tooltip: readOnly ? '撤销(当前视图只读)' : '撤销 Ctrl+Z'), _iconBtn(t, Icons.redo, !readOnly && c.canRedo ? c.redo : null, tooltip: readOnly ? '重做(当前视图只读)' : '重做 Ctrl+Y'), SizedBox(width: gap), ...module.buildToolbarCenter(ui, compact: compact), ]; final List right = [ ...module.buildToolbarRight(ui), if (module.hasLeftPanel || module.hasRightPanel) SizedBox(width: gap), if (module.hasLeftPanel) sideToggleLeft, if (module.hasRightPanel) sideToggleRight, _iconBtn(t, Icons.help_outline, () => showDialog(context: context, builder: (_) => const HelpDialog()), tooltip: '帮助 F1'), _updateBtn(t), _themeToggle(t), ]; // 用「最小宽度 = 视口宽度」约束让内容铺满整条工具栏: // 空间足够时 Spacer 把右侧工具组顶到最右(不再留空白); // 空间不足时横向滚动,避免溢出。 return SingleChildScrollView( scrollDirection: Axis.horizontal, child: ConstrainedBox( constraints: BoxConstraints(minWidth: cons.maxWidth), child: IntrinsicWidth( child: Row(children: [...left, const Spacer(), ...right]), ), ), ); }, ), ); } /// 主题切换控件:日间 / 夜间两段式按钮。 Widget _themeToggle(Tokens t) { final bool light = c.brightness == Brightness.light; Widget half(IconData icon, bool active, VoidCallback onTap) { return InkWell( onTap: onTap, borderRadius: BorderRadius.circular(8), child: Container( width: 32 * _toolScale, height: 26 * _toolScale, alignment: Alignment.center, decoration: BoxDecoration( color: active ? t.accent.withValues(alpha: 0.18) : Colors.transparent, borderRadius: BorderRadius.circular(8), border: active ? Border.all(color: t.accent, width: 1.2) : null, ), child: Icon(icon, size: 16 * _toolScale, color: active ? t.accent : t.dim), ), ); } return Container( padding: EdgeInsets.all(4 * _toolScale), decoration: BoxDecoration( color: t.panel, borderRadius: BorderRadius.circular(10), border: Border.all(color: t.line2), ), child: Row( mainAxisSize: MainAxisSize.min, children: [ half(Icons.wb_sunny_outlined, light, () { if (light) return; c.toggleTheme(); }), half(Icons.nightlight_round, !light, () { if (!light) return; c.toggleTheme(); }), ], ), ); } /// 图标按钮,可附带 tooltip 与自定义颜色;尺寸随 [_toolScale] 缩放, /// 触屏平台收紧点按区域,避免默认 48dp 目标把工具栏撑满。 Widget _iconBtn(Tokens t, IconData icon, VoidCallback? onTap, {String? tooltip, Color? color}) { final double s = _toolScale; return IconButton( onPressed: onTap, icon: Icon(icon, size: 18 * s, color: onTap == null ? t.dim : (color ?? t.text)), tooltip: tooltip, splashRadius: 16 * s, padding: EdgeInsets.zero, constraints: BoxConstraints(minWidth: 34 * s, minHeight: 34 * s), // 新版 Flutter 的 IconButton 已移除 tapTargetSize 形参,改由 style 提供。 style: IconButton.styleFrom(tapTargetSize: s < 1 ? MaterialTapTargetSize.shrinkWrap : null), ); } /// 自定义图标组件版按钮(用于更新按钮的矢量图标): /// 尺寸与点按区域与 [_iconBtn] 完全一致。 Widget _iconWidgetBtn(Widget icon, VoidCallback? onTap, {String? tooltip}) { final double s = _toolScale; return IconButton( onPressed: onTap, icon: icon, tooltip: tooltip, splashRadius: 16 * s, padding: EdgeInsets.zero, constraints: BoxConstraints(minWidth: 34 * s, minHeight: 34 * s), style: IconButton.styleFrom(tapTargetSize: s < 1 ? MaterialTapTargetSize.shrinkWrap : null), ); } // ===================== 侧栏停靠状态机(钉住 / 悬浮 / 轨道) ===================== /// 侧栏标题栏控制按钮:切换钉住状态与隐藏侧栏。 Widget _panelControls(Tokens t, bool left) { final bool pinned = left ? _leftPinned : _rightPinned; return Row( mainAxisSize: MainAxisSize.min, children: [ _headerIcon( t, pinned ? Icons.push_pin : Icons.push_pin_outlined, pinned ? '取消钉住(改为悬浮)' : '钉住侧栏', () => setState(() { if (left) { _leftPinned = !_leftPinned; _leftHover = _leftPinned; } else { _rightPinned = !_rightPinned; _rightHover = _rightPinned; } }), active: pinned, ), _headerIcon( t, Icons.close, '隐藏侧栏', () => setState(() { if (left) { _leftVisible = false; _leftHover = false; } else { _rightVisible = false; _rightHover = false; } }), ), ], ); } /// 侧栏标题栏中的小图标按钮,[active] 控制高亮。 Widget _headerIcon(Tokens t, IconData icon, String tip, VoidCallback onTap, {bool active = false}) { return Tooltip( message: tip, child: InkWell( onTap: onTap, borderRadius: BorderRadius.circular(6), child: Padding( padding: const EdgeInsets.all(4), child: Icon(icon, size: 15, color: active ? t.accent : t.dim), ), ), ); } /// 侧栏收起时的贴边轨道:鼠标进入即展开为悬浮面板,点击亦可展开。 Widget _rail(Tokens t, bool left) { final bool visible = left ? _leftVisible : _rightVisible; final bool pinned = left ? _leftPinned : _rightPinned; return MouseRegion( cursor: SystemMouseCursors.click, onEnter: (_) { if (visible && !pinned) { setState(() { if (left) { _leftHover = true; } else { _rightHover = true; } }); } }, child: GestureDetector( onTap: () => setState(() { if (left) { _leftVisible = true; if (Platform.isAndroid || Platform.isIOS) { // 触屏平台:点把手直接停靠展开(无悬停概念)。 _leftPinned = true; } else if (!_leftPinned) { _leftHover = true; } } else { _rightVisible = true; if (Platform.isAndroid || Platform.isIOS) { _rightPinned = true; } else if (!_rightPinned) { _rightHover = true; } } }), child: Container( width: 22, decoration: BoxDecoration( color: t.panel.withValues(alpha: 0.96), border: Border( right: left ? BorderSide(color: t.line) : BorderSide.none, left: left ? BorderSide.none : BorderSide(color: t.line), ), ), alignment: Alignment.topCenter, padding: const EdgeInsets.only(top: 14), child: Icon(left ? Icons.chevron_right : Icons.chevron_left, size: 18, color: t.sub), ), ), ); } /// 悬浮侧栏:用 [MouseRegion] 跟踪悬停,鼠标移出即收回为轨道。 Widget _floating(Tokens t, bool left) { return MouseRegion( onEnter: (_) => setState(() { if (left) { _leftHover = true; } else { _rightHover = true; } }), onExit: (_) => setState(() { if (left) { _leftHover = false; } else { _rightHover = false; } }), child: left ? _moduleLeftPanel(t, floating: true) : _moduleRightPanel(t, floating: true), ); } /// 当前模块左侧栏(外壳停靠 / 轨道 / 悬浮共用)。 Widget _moduleLeftPanel(Tokens t, {bool floating = false}) { final EdcModule? m = _module; if (m == null || !m.hasLeftPanel) return const SizedBox.shrink(); return m.buildLeftPanel(_moduleUi(t), floating: floating) ?? const SizedBox.shrink(); } /// 当前模块右侧栏。 Widget _moduleRightPanel(Tokens t, {bool floating = false}) { final EdcModule? m = _module; if (m == null || !m.hasRightPanel) return const SizedBox.shrink(); return m.buildRightPanel(_moduleUi(t), floating: floating) ?? const SizedBox.shrink(); } /// 当前模块状态栏。 Widget _moduleStatusBar(Tokens t) { final EdcModule? m = _module; if (m == null) return const SizedBox.shrink(); return m.buildStatusBar(_moduleUi(t)); } }