2026-09-18 16:50:12 +08:00
|
|
|
// 应用入口:负责初始化持久化存储、启动 Flutter 应用,并承载全局的
|
|
|
|
|
// 应用控制器(AppController)与应用生命周期管理。
|
|
|
|
|
//
|
|
|
|
|
// 架构位置:本文件位于最外层,向下装配 AppShell(UI 外壳)与
|
|
|
|
|
// Storage(本地持久化),并通过 AppController 向整个 UI 层提供状态与
|
|
|
|
|
// 业务动作。启动后还会检查是否存在异常退出遗留的自动快照,并引导用户
|
|
|
|
|
// 选择恢复方式。
|
2026-10-06 21:04:38 +08:00
|
|
|
import 'dart:io' show Platform;
|
|
|
|
|
|
2026-09-18 16:50:12 +08:00
|
|
|
import 'dart:ui' show AppExitResponse;
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
2026-09-19 11:08:39 +08:00
|
|
|
import 'license/license_service.dart';
|
2026-09-18 16:50:12 +08:00
|
|
|
import 'model/models.dart';
|
2026-11-22 10:02:48 +08:00
|
|
|
import 'modules/registry.dart';
|
2026-10-11 10:19:01 +08:00
|
|
|
import 'persistence/app_paths.dart';
|
2026-09-18 16:50:12 +08:00
|
|
|
import 'persistence/storage.dart';
|
|
|
|
|
import 'state/app_controller.dart';
|
|
|
|
|
import 'theme/tokens.dart';
|
|
|
|
|
import 'ui/app_shell.dart';
|
|
|
|
|
|
|
|
|
|
// ===================== 应用启动与生命周期 =====================
|
|
|
|
|
|
|
|
|
|
/// 应用引导入口:确保 Flutter 绑定初始化完成、加载本地存储,
|
|
|
|
|
/// 然后启动根组件 [SequenceDiagramApp]。
|
|
|
|
|
Future<void> main() async {
|
|
|
|
|
WidgetsFlutterBinding.ensureInitialized();
|
2026-10-06 22:13:58 +08:00
|
|
|
// 移动端:沉浸式(edge-to-edge)显示,系统栏区域由应用工具栏/状态栏底色填充。
|
2026-10-06 21:04:38 +08:00
|
|
|
if (Platform.isAndroid || Platform.isIOS) {
|
|
|
|
|
await SystemChrome.setEnabledSystemUIMode(SystemUiMode.edgeToEdge);
|
|
|
|
|
}
|
2026-09-18 16:50:12 +08:00
|
|
|
await Storage.instance.init();
|
2026-11-22 10:02:48 +08:00
|
|
|
await initModuleRegistry();
|
2026-09-18 16:50:12 +08:00
|
|
|
runApp(const SequenceDiagramApp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 应用根组件:创建并持有全局 [AppController],挂接应用生命周期监听,
|
|
|
|
|
/// 并在首帧渲染完成后触发未保存会话的恢复检查。
|
|
|
|
|
class SequenceDiagramApp extends StatefulWidget {
|
|
|
|
|
/// 创建应用根组件。
|
|
|
|
|
const SequenceDiagramApp({super.key});
|
|
|
|
|
|
|
|
|
|
/// 创建根组件对应的状态对象。
|
|
|
|
|
@override
|
|
|
|
|
State<SequenceDiagramApp> createState() => _SequenceDiagramAppState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 根组件状态:负责控制器的创建/释放、退出前的快照标记,
|
|
|
|
|
/// 以及启动阶段的恢复流程。
|
|
|
|
|
class _SequenceDiagramAppState extends State<SequenceDiagramApp> {
|
|
|
|
|
/// 全局应用控制器,负责项目状态与所有业务操作。
|
|
|
|
|
late final AppController _controller;
|
|
|
|
|
|
|
|
|
|
/// 应用生命周期监听器,用于在系统请求退出时先把快照标记为干净。
|
|
|
|
|
AppLifecycleListener? _lifecycle;
|
|
|
|
|
|
|
|
|
|
/// 初始化控制器、注册退出回调,并在首帧后检查恢复快照。
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_controller = AppController()..start();
|
2026-10-11 10:19:01 +08:00
|
|
|
// 空白启动:先进入首页,由用户选择文档类型再进入模块工作区。
|
|
|
|
|
_controller.goHome();
|
|
|
|
|
// 素材库目录(library/symbols|images|templates):确保存在(失败静默)。
|
|
|
|
|
AppPaths.ensureLibrary();
|
2026-09-18 16:50:12 +08:00
|
|
|
_controller.addListener(_updateWindowTitle);
|
2026-09-19 11:08:39 +08:00
|
|
|
// 启动即校验授权(读取可执行文件同级 license/*.lic)。
|
|
|
|
|
LicenseService.instance.init().then((LicenseInfo info) {
|
|
|
|
|
_controller.setLicensed(info.valid);
|
|
|
|
|
});
|
2026-09-18 16:50:12 +08:00
|
|
|
_lifecycle = AppLifecycleListener(
|
|
|
|
|
onExitRequested: () async {
|
|
|
|
|
await _controller.markCleanOnExit();
|
|
|
|
|
return AppExitResponse.exit;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
_updateWindowTitle();
|
|
|
|
|
_checkRecovery();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 原生窗口标题通道(在 Windows runner 中实现 SetWindowText)。
|
|
|
|
|
static const MethodChannel _windowChannel = MethodChannel('seqstudio/window');
|
|
|
|
|
|
2026-10-11 10:19:01 +08:00
|
|
|
/// 更新原生窗口标题为「模块名 - <文件名 | 未命名文件>」,
|
2026-09-18 16:50:12 +08:00
|
|
|
/// 有未保存修改时追加 ` *`。
|
|
|
|
|
void _updateWindowTitle() {
|
2026-11-22 10:02:48 +08:00
|
|
|
// 标题走模块注册表:文件路径也由当前模块提供,新增模块无需改这里。
|
|
|
|
|
if (_controller.isHome) {
|
2026-10-11 10:19:01 +08:00
|
|
|
_windowChannel
|
2026-11-22 10:02:48 +08:00
|
|
|
.invokeMethod<void>('setTitle', kBrandName)
|
2026-10-11 10:19:01 +08:00
|
|
|
.catchError((Object _) {});
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-11-22 10:02:48 +08:00
|
|
|
final EdcModule? module = moduleById(_controller.activeModuleId);
|
|
|
|
|
final String? path = module?.filePath(_controller) ?? _controller.currentFilePath;
|
|
|
|
|
final String name =
|
|
|
|
|
path == null ? '未命名文件' : path.split(RegExp(r'[\\/]')).last;
|
|
|
|
|
final String dirty = _controller.dirty ? ' *' : '';
|
|
|
|
|
final String brand = moduleTitle(_controller.activeModuleId);
|
2026-09-18 16:50:12 +08:00
|
|
|
_windowChannel
|
2026-10-11 10:19:01 +08:00
|
|
|
.invokeMethod<void>('setTitle', '$brand - $name$dirty')
|
2026-09-18 16:50:12 +08:00
|
|
|
.catchError((Object _) {});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 释放生命周期监听器与控制器资源。
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
_controller.removeListener(_updateWindowTitle);
|
|
|
|
|
_lifecycle?.dispose();
|
|
|
|
|
_controller.dispose();
|
|
|
|
|
super.dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 检查上次运行遗留的自动快照,弹出对话框让用户选择:
|
|
|
|
|
/// 恢复自动快照、从 .bak 备份恢复,或忽略并删除快照。
|
|
|
|
|
Future<void> _checkRecovery() async {
|
|
|
|
|
final RecoveryInfo? info = await Storage.instance.checkRecovery();
|
|
|
|
|
if (info == null || !mounted) return;
|
|
|
|
|
final int? choice = await showDialog<int>(
|
|
|
|
|
context: context,
|
|
|
|
|
barrierDismissible: false,
|
|
|
|
|
builder: (BuildContext ctx) {
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: const Text('检测到未正常退出的会话'),
|
|
|
|
|
content: SizedBox(
|
|
|
|
|
width: 460,
|
|
|
|
|
child: Column(
|
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: <Widget>[
|
|
|
|
|
const Text('上次运行没有正常退出,已找到自动保存的快照。'),
|
|
|
|
|
const SizedBox(height: 8),
|
|
|
|
|
Text('快照时间:${info.updatedAt.toLocal().toString().substring(0, 19)}'),
|
|
|
|
|
Text('原始文件:${info.projectPath ?? '(未保存到文件)'}'),
|
|
|
|
|
if (info.bakContent != null)
|
|
|
|
|
Text('.bak 备份:${info.bakPath}', style: const TextStyle(fontSize: 11)),
|
|
|
|
|
const SizedBox(height: 12),
|
|
|
|
|
const Text('请选择要恢复的内容:'),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: <Widget>[
|
|
|
|
|
TextButton(onPressed: () => Navigator.pop(ctx, 2), child: const Text('忽略并删除快照')),
|
|
|
|
|
if (info.bakContent != null)
|
|
|
|
|
OutlinedButton(onPressed: () => Navigator.pop(ctx, 1), child: const Text('从 .bak 恢复')),
|
|
|
|
|
FilledButton(onPressed: () => Navigator.pop(ctx, 0), child: const Text('恢复自动快照')),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
// 用户取消或选择忽略:直接清除快照,不恢复任何内容。
|
|
|
|
|
if (choice == null || choice == 2) {
|
|
|
|
|
await Storage.instance.clearRecovery();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// 恢复流程:choice == 1 走 .bak 备份,否则走自动快照;解码失败时静默忽略。
|
2026-10-11 10:19:01 +08:00
|
|
|
// 当前快照仅为时序图文档:恢复前先切到对应模块(避免停留在首页看不到内容)。
|
2026-09-18 16:50:12 +08:00
|
|
|
try {
|
2026-11-22 10:02:48 +08:00
|
|
|
_controller.setModuleId('sequence', status: '时序图设计');
|
2026-09-18 16:50:12 +08:00
|
|
|
if (choice == 1 && info.bakContent != null) {
|
|
|
|
|
_controller.loadProject(Project.decode(info.bakContent!), path: info.projectPath, dirty: true);
|
|
|
|
|
} else {
|
|
|
|
|
_controller.loadProject(Project.decode(info.content), path: info.projectPath, dirty: true);
|
|
|
|
|
}
|
|
|
|
|
_controller.setStatus('已恢复未保存的会话');
|
|
|
|
|
} catch (_) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// 构建 [MaterialApp]:监听控制器变化,按当前主题亮度装配配色,
|
|
|
|
|
/// 统一收紧文本缩放比例后加载 [AppShell]。
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return ListenableBuilder(
|
|
|
|
|
listenable: _controller,
|
|
|
|
|
builder: (BuildContext context, Widget? child) {
|
|
|
|
|
final Tokens tokens = Tokens.of(_controller.brightness);
|
|
|
|
|
return MaterialApp(
|
2026-11-22 10:02:48 +08:00
|
|
|
title: 'PDS流程设计套件 · Process Design Suite',
|
2026-09-18 16:50:12 +08:00
|
|
|
debugShowCheckedModeBanner: false,
|
|
|
|
|
theme: buildTheme(tokens),
|
2026-10-06 22:13:58 +08:00
|
|
|
// 固定文本缩放;系统栏图标随主题亮度自动切换(透明系统栏,
|
|
|
|
|
// 状态栏 / 导航栏区域由 AppShell 的工具栏 / 状态栏底色填满)。
|
2026-10-06 21:04:38 +08:00
|
|
|
builder: (BuildContext context, Widget? child) {
|
2026-10-06 22:13:58 +08:00
|
|
|
final bool dark = Theme.of(context).brightness == Brightness.dark;
|
|
|
|
|
return AnnotatedRegion<SystemUiOverlayStyle>(
|
|
|
|
|
value: SystemUiOverlayStyle(
|
|
|
|
|
statusBarColor: Colors.transparent,
|
|
|
|
|
statusBarIconBrightness: dark ? Brightness.light : Brightness.dark,
|
|
|
|
|
statusBarBrightness: dark ? Brightness.dark : Brightness.light,
|
|
|
|
|
systemNavigationBarColor: Colors.transparent,
|
|
|
|
|
systemNavigationBarIconBrightness: dark ? Brightness.light : Brightness.dark,
|
|
|
|
|
systemNavigationBarDividerColor: Colors.transparent,
|
|
|
|
|
),
|
|
|
|
|
child: MediaQuery(
|
|
|
|
|
data: MediaQuery.of(context).copyWith(
|
|
|
|
|
textScaler: const TextScaler.linear(1.0),
|
|
|
|
|
),
|
|
|
|
|
child: child ?? const SizedBox.shrink(),
|
2026-10-06 21:04:38 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2026-09-18 16:50:12 +08:00
|
|
|
home: AppShell(controller: _controller),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|