Files

618 lines
22 KiB
Dart
Raw Permalink Normal View History

/// 架构图模块 UI:工具栏中段 / 右侧工具、左右侧栏、状态栏。
///
/// 由 `ArchitectureModule` 通过 `lib/modules/registry.dart` 注册后由外壳调用。
library;
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';
import 'package:flutter/material.dart' hide Action;
import 'package:sequence_diagram/modules/architecture/domain/arch_dxf.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_export.dart';
import 'package:sequence_diagram/modules/architecture/domain/arch_models.dart';
import 'package:sequence_diagram/modules/architecture/sketch/sketch_models.dart';
import 'package:sequence_diagram/modules/architecture/sketch/sketch_solver.dart';
import 'package:sequence_diagram/modules/architecture/ui/arch_panel.dart';
import 'package:sequence_diagram/modules/architecture/ui/arch_view.dart';
import 'package:sequence_diagram/modules/module.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/ui/dialogs.dart';
/// 架构图模块 UI 构建器。
class ArchitectureUi {
/// 构造 UI 构建器。
const ArchitectureUi(this.ui);
/// 外壳上下文。
final ModuleUiContext ui;
AppController get c => ui.controller;
BuildContext get context => ui.context;
Tokens get t => ui.tokens;
double get _toolScale => ui.toolScale;
double _toolFont(double size) => ui.toolFont(size);
double get _leftPanelWidth => ui.leftPanelWidth;
double get _rightPanelWidth => ui.rightPanelWidth;
Widget _panelControls(Tokens t, bool left) => ui.panelControls(left);
/// 工具栏中段:选择 / 手绘 / 连线 / 橡皮 + 识别。
List<Widget> buildToolbarCenter(Tokens t, {required bool compact}) =>
<Widget>[_archTools(t, compact)];
/// 工具栏右侧:导出 / 缩放 / 适应视图。
List<Widget> buildToolbarRight(Tokens t, {required bool compact}) => <Widget>[
_exportMenu(t),
SizedBox(width: (compact ? 4 : 6) * _toolScale),
_zoomBox(t),
SizedBox(width: (compact ? 4 : 6) * _toolScale),
_iconBtn(t, Icons.crop_free, c.archFit, tooltip: '适应视图'),
];
/// 左侧栏。
Widget buildLeftPanel(Tokens t, {bool floating = false}) =>
_archLeftPanel(t, floating: floating);
/// 右侧栏。
Widget buildRightPanel(Tokens t, {bool floating = false}) =>
_archRightPanel(t, floating: floating);
/// 底部状态栏。
Widget buildStatusBar(Tokens t) => _archStatusBar(t);
/// 工作区:架构图画布。
Widget buildWorkspace() => Container(
color: t.canvas2,
child: ArchView(controller: c),
);
/// 缩放控件:显示当前百分比并提供缩小 / 放大按钮。
Widget _zoomBox(Tokens t) {
final double s = _toolScale;
final double zoom = c.arch.zoom;
final String pct = '${(zoom * 100).round()}%';
return Container(
height: 34 * s,
padding: EdgeInsets.symmetric(horizontal: 4 * s),
decoration: BoxDecoration(
color: t.panel,
borderRadius: BorderRadius.circular(9),
border: Border.all(color: t.line2),
),
child: Row(
children: <Widget>[
_miniIcon(t, Icons.remove, () => c.archZoomBy(0.9)),
SizedBox(
width: 52 * s,
child: Text(pct,
textAlign: TextAlign.center,
style: TextStyle(color: t.text, fontSize: _toolFont(11))),
),
_miniIcon(t, Icons.add, () => c.archZoomBy(1.1)),
],
),
);
}
/// 导出菜单:SVG / PNG / DXF。
Widget _exportMenu(Tokens t) {
final double s = _toolScale;
final bool licensed = c.licensed;
final Color fg = licensed ? Colors.white : t.dim;
return Padding(
padding: EdgeInsets.symmetric(horizontal: 3 * s),
child: PopupMenuButton<String>(
tooltip: '导出(SVG / PNG / DXF)· 架构图',
color: t.panel2,
position: PopupMenuPosition.under,
onSelected: (String v) {
if (v == 'svg') _exportArchSvg();
if (v == 'png') _exportArchPng();
if (v == 'dxf') _exportArchDxf();
},
itemBuilder: (BuildContext ctx) => <PopupMenuEntry<String>>[
_exportItem(t, 'svg', Icons.code, '导出 SVG',
'矢量图:架构图无损缩放 / 二次编辑', licensed),
_exportItem(t, 'png', Icons.image_outlined, '导出 PNG',
'位图(3840×2160):分享 / 汇报', licensed),
_exportItem(t, 'dxf', Icons.architecture, '导出 DXF',
'AutoCAD / DraftSight 等可编辑的 CAD 图纸', licensed),
],
child: Container(
height: 34 * s,
padding: EdgeInsets.symmetric(horizontal: 12 * s),
decoration: BoxDecoration(
color: licensed ? t.accent2 : t.panel2,
borderRadius: BorderRadius.circular(9),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.ios_share, size: 15 * s, color: fg),
SizedBox(width: 6 * s),
Text('导出',
style: TextStyle(
fontSize: _toolFont(11),
fontWeight: FontWeight.w600,
color: fg)),
Icon(Icons.arrow_drop_down, size: 18 * s, color: fg),
],
),
),
),
);
}
Widget _archTools(Tokens t, bool compact) {
// 草图模式:工具在左侧栏,这里显示状态与快捷退出。
if (c.sketchMode) {
return Container(
padding: EdgeInsets.symmetric(horizontal: 12 * _toolScale),
height: 34 * _toolScale,
decoration: BoxDecoration(
color: t.panel,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: t.accent),
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.edit_note, size: 15 * _toolScale, color: t.accent),
SizedBox(width: 6 * _toolScale),
Text(
'参数化草图 · ${sketchToolLabel(c.sketchTool)}',
style: TextStyle(
color: t.accent,
fontSize: _toolFont(11),
fontWeight: FontWeight.w600,
),
),
SizedBox(width: 10 * _toolScale),
InkWell(
onTap: () => c.setSketchMode(false),
borderRadius: BorderRadius.circular(7),
child: Container(
padding: EdgeInsets.symmetric(
horizontal: 8 * _toolScale, vertical: 3 * _toolScale),
decoration: BoxDecoration(
color: t.panel2,
borderRadius: BorderRadius.circular(7),
border: Border.all(color: t.line),
),
child: Text('退出草图',
style:
TextStyle(color: t.sub, fontSize: _toolFont(10.5))),
),
),
],
),
);
}
final ArchTool tool = c.archTool;
Widget seg(String label, IconData icon, ArchTool value, String tip) {
final bool active = tool == value;
return Tooltip(
message: tip,
child: InkWell(
onTap: () => c.setArchTool(value),
borderRadius: BorderRadius.circular(10),
child: Container(
width: (compact ? 74 : 90) * _toolScale,
height: 34 * _toolScale,
alignment: Alignment.center,
decoration: BoxDecoration(
color: active ? t.accent.withValues(alpha: 0.16) : Colors.transparent,
borderRadius: BorderRadius.circular(10),
border: active ? Border.all(color: t.accent, width: 1.4) : null,
),
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(icon, size: 15 * _toolScale, color: active ? t.accent : t.sub),
SizedBox(width: 4 * _toolScale),
Text(label,
style: TextStyle(
color: active ? t.accent : t.sub,
fontWeight: active ? FontWeight.w600 : FontWeight.normal,
fontSize: _toolFont(12),
)),
],
),
),
),
);
}
return Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Container(
padding: EdgeInsets.all(3 * _toolScale),
decoration: BoxDecoration(
color: t.panel,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: t.line2),
),
child: Row(
children: <Widget>[
seg('选择', Icons.near_me_outlined, ArchTool.select, '选择 / 移动 / 缩放'),
seg('手绘', Icons.gesture, ArchTool.pen,
c.archAutoRecognize ? '按住拖动绘制,松手自动识别' : '按住拖动绘制;画完点「识别」确认识别'),
seg('连线', Icons.arrow_right_alt, ArchTool.connect,
'从图形拖到另一个图形(横平竖直正交走线,角点可拖拽)'),
seg('橡皮', Icons.cleaning_services_outlined, ArchTool.eraser,
'擦除图形 / 墨迹 / 连线(拖动连擦,一次撤销可整体回退)'),
],
),
),
SizedBox(width: 6 * _toolScale),
// 「识别」确认按钮:手动模式下把待识别墨迹统一识别(自动模式下也可用)。
Container(
padding: EdgeInsets.all(3 * _toolScale),
decoration: BoxDecoration(
color: t.panel,
borderRadius: BorderRadius.circular(12),
border: Border.all(color: t.line2),
),
child: InkWell(
onTap: c.arch.strokes.isEmpty ? null : c.archRecognizeInk,
borderRadius: BorderRadius.circular(9),
child: Container(
height: 34 * _toolScale,
padding: EdgeInsets.symmetric(horizontal: 12 * _toolScale),
alignment: Alignment.center,
child: Row(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Icon(Icons.auto_fix_high,
size: 15 * _toolScale,
color: c.arch.strokes.isEmpty ? t.dim : t.accent),
SizedBox(width: 5 * _toolScale),
Text(
'识别(${c.arch.strokes.length})',
style: TextStyle(
color: c.arch.strokes.isEmpty ? t.dim : t.accent,
fontSize: _toolFont(11),
fontWeight: FontWeight.w600,
),
),
],
),
),
),
),
],
);
}
Widget _miniIcon(Tokens t, IconData icon, VoidCallback onTap) {
return InkWell(
onTap: onTap,
borderRadius: BorderRadius.circular(6),
child: Padding(
padding: EdgeInsets.all(6 * _toolScale),
child: Icon(icon, size: 15 * _toolScale, color: t.sub),
),
);
}
PopupMenuItem<String> _exportItem(
Tokens t, String value, IconData icon, String label, String hint, bool enabled) {
final Color main = enabled ? t.text : t.dim;
return PopupMenuItem<String>(
value: value,
enabled: enabled,
height: 46,
child: Row(
children: <Widget>[
Icon(icon, size: 17, color: enabled ? t.accent : t.dim),
const SizedBox(width: 10),
Expanded(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(label,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: main, fontSize: 12.5, fontWeight: FontWeight.w600)),
Text(hint,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: enabled ? t.dim : t.dim.withValues(alpha: 0.6), fontSize: 10)),
],
),
),
],
),
);
}
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),
);
}
Future<void> _exportArchSvg() async {
if (!c.licensed) {
if (context.mounted) {
await showMessage(context, '未授权', '未检测到有效授权(license/*.lic),无法导出 SVG。');
}
return;
}
final String svg = buildArchSvg(doc: c.arch, baseTokens: Tokens.of(c.brightness));
if (AndroidDocs.isAvailable) {
final String? name = await showDialog<String>(
context: context,
builder: (_) => AndroidFileDialog(
saveMode: true,
defaultName: '${c.project.name}-架构图.svg',
suffix: '.svg',
),
);
if (name == null) return;
try {
await AndroidDocs.saveFile(name, Uint8List.fromList(utf8.encode(svg)),
mime: 'image/svg+xml');
if (context.mounted) c.setStatus('已导出 ${AndroidDocs.location()}/$name');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
return;
}
final String? path = await pickFile(
context,
save: true,
defaultName: '${c.project.name}-架构图.svg',
suffix: '.svg',
);
if (path == null) return;
try {
await File(path).writeAsString(svg, flush: true);
if (context.mounted) c.setStatus('已导出 $path');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
}
Future<void> _exportArchPng() async {
if (!c.licensed) {
if (context.mounted) {
await showMessage(context, '未授权', '未检测到有效授权(license/*.lic),无法导出 PNG。');
}
return;
}
final Uint8List bytes;
try {
bytes = await renderArchPng(
doc: c.arch,
baseTokens: Tokens.of(c.brightness),
projectName: c.project.name,
);
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
return;
}
if (!context.mounted) return;
if (AndroidDocs.isAvailable) {
final String? name = await showDialog<String>(
context: context,
builder: (_) => AndroidFileDialog(
saveMode: true,
defaultName: '${c.project.name}-架构图.png',
suffix: '.png',
),
);
if (name == null) return;
try {
await AndroidDocs.saveFile(name, bytes, mime: 'image/png');
if (context.mounted) c.setStatus('已导出 ${AndroidDocs.location()}/$name');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
return;
}
final String? path = await pickFile(
context,
save: true,
defaultName: '${c.project.name}-架构图.png',
suffix: '.png',
);
if (path == null) return;
try {
await File(path).writeAsBytes(bytes, flush: true);
if (context.mounted) c.setStatus('已导出 $path');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
}
Future<void> _exportArchDxf() async {
if (!c.licensed) {
if (context.mounted) {
await showMessage(context, '未授权', '未检测到有效授权(license/*.lic),无法导出 DXF。');
}
return;
}
final String dxf = buildArchDxf(doc: c.arch);
if (AndroidDocs.isAvailable) {
final String? name = await showDialog<String>(
context: context,
builder: (_) => AndroidFileDialog(
saveMode: true,
defaultName: '${c.project.name}-架构图.dxf',
suffix: '.dxf',
),
);
if (name == null) return;
try {
await AndroidDocs.saveFile(name, Uint8List.fromList(utf8.encode(dxf)),
mime: 'application/dxf');
if (context.mounted) c.setStatus('已导出 ${AndroidDocs.location()}/$name');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
return;
}
final String? path = await pickFile(
context,
save: true,
defaultName: '${c.project.name}-架构图.dxf',
suffix: '.dxf',
);
if (path == null) return;
try {
await File(path).writeAsString(dxf, flush: true);
if (context.mounted) c.setStatus('已导出 $path');
} catch (e) {
if (context.mounted) await showMessage(context, '导出失败', '$e');
}
}
Widget _archLeftPanel(Tokens t, {bool floating = false}) {
return Container(
width: _leftPanelWidth,
decoration: BoxDecoration(
color: t.panel,
border: floating ? Border.all(color: t.line2) : Border(right: BorderSide(color: t.line)),
boxShadow: floating
? <BoxShadow>[BoxShadow(color: Colors.black.withValues(alpha: 0.4), blurRadius: 20, offset: const Offset(4, 0))]
: null,
),
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(12, 10, 12, 12),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Row(
children: <Widget>[
_sectionLabel(t, '架构图 · 设计工具'),
const Spacer(),
_panelControls(t, true),
],
),
const SizedBox(height: 10),
ArchLeftBody(controller: c),
],
),
),
);
}
Widget _archRightPanel(Tokens t, {bool floating = false}) {
return Container(
width: _rightPanelWidth,
decoration: BoxDecoration(
color: t.panel,
border: floating ? Border.all(color: t.line2) : Border(left: BorderSide(color: t.line)),
boxShadow: floating
? <BoxShadow>[BoxShadow(color: Colors.black.withValues(alpha: 0.4), blurRadius: 20, offset: const Offset(-4, 0))]
: null,
),
child: Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(16, 12, 10, 0),
child: Row(
children: <Widget>[
_sectionLabel(t, '属性 · 架构图'),
const Spacer(),
_panelControls(t, false),
],
),
),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.fromLTRB(14, 8, 14, 16),
child: ArchRightBody(controller: c),
),
),
],
),
);
}
Widget _sectionLabel(Tokens t, String s) => Text(
s,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: TextStyle(color: t.text2, fontSize: 11.5, letterSpacing: 1.2, fontWeight: FontWeight.w600),
);
Widget _archStatusBar(Tokens t) {
final ArchDoc doc = c.arch;
final String toolLabel = c.sketchMode
? '草图·${sketchToolLabel(c.sketchTool)}'
: switch (c.archTool) {
ArchTool.select => '选择',
ArchTool.pen => c.archAutoRecognize ? '手绘识别' : '手绘(待确认)',
ArchTool.connect => '连线',
ArchTool.eraser => '橡皮',
};
final List<String> items = <String>[
c.sketchMode ? '参数化草图' : '架构图模式',
if (c.sketchMode) ...<String>[
'实体 ${doc.sketch.entities.length}',
'约束 ${doc.sketch.constraints.where((SketchConstraint k) => !k.internal).length}',
'尺寸 ${doc.sketch.dims.length}',
'自由度 ${sketchDofCount(doc.sketch)}',
] else ...<String>[
'元件 ${doc.shapes.length}',
'连线 ${doc.connectors.length}',
'墨迹 ${doc.strokes.length}',
'图层 ${doc.layerById(doc.activeLayer).name}',
],
'工具 $toolLabel',
if (!c.sketchMode && c.archOrtho) '正交 开',
if (!c.sketchMode && c.archPolar) '极轴 ${c.archPolarDeg.round()}°',
'缩放 ${(doc.zoom * 100).round()}%',
c.status,
];
return Container(
height: 32,
padding: const EdgeInsets.symmetric(horizontal: 16),
decoration: BoxDecoration(
color: t.chrome,
border: Border(top: BorderSide(color: t.line2)),
),
child: Row(
children: <Widget>[
Expanded(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: items.map((String s) {
final bool last = s == items.last;
return Padding(
padding: const EdgeInsets.only(right: 22),
child: Text(s, style: TextStyle(color: last ? t.ok : t.sub, fontSize: 11)),
);
}).toList(),
),
),
),
if (c.dirty)
Padding(
padding: const EdgeInsets.only(right: 14),
child: Text('● 未保存', style: TextStyle(color: t.warn, fontSize: 11)),
),
Text('F1 帮助 · Ctrl+Z 撤销 · Ctrl+S 保存', style: TextStyle(color: t.dim, fontSize: 10)),
],
),
);
}
}