feat: V1.0.0 发布(呆头鹅)

- 默认浅色白底主题;字体整数化 + Microsoft YaHei UI 提升清晰度
- 相位分区:圆周/条形显示、沿弧排列、拖拽边界(虚线参考+弱吸附)、增删改(对话框/右键/双击)
- 动作:Ctrl 多选批量对齐;上下拖拽自由换行;左右端拖拽改起止(虚线+弱吸附+左右箭头光标)
- 动作编辑支持角度与时间双向换算
- 圆周顺时针箭头移至刻度环外;条形左侧标题列收窄
- 侧栏可隐藏/钉住;适应视图按内容自适应;节拍统计改为按钮弹窗
- AI 生成时序图(DeepSeek,Flash/Pro,推理强度),基于 dart:io 无插件
- 窗口标题显示文件名/未命名文件;关于信息(设计人/版本/版权)
- 全量中文注释、docs/ARCHITECTURE.md、docs/HELP.md(按任务组织)
- 构建脚本输出到 dist/,并附带 dist 生成物
This commit is contained in:
呆头鹅
2026-09-18 16:50:12 +08:00
parent e7ffa35959
commit 6cfa79507c
51 changed files with 12340 additions and 0 deletions
+549
View File
@@ -0,0 +1,549 @@
/// 条形(时间轴)视图绘制器。
///
/// 职责:把 [Project] 中的动作按“双轴”条形图渲染到 Flutter 画布上——
/// 横轴同时表达相位角度与等价的绝对时间,纵轴按工位顺序排列动作行。
///
/// 架构位置:
/// - 全部几何计算委托给 [BarLayout](`lib/layout/layout.dart`),
/// 本绘制器只负责取几何结果并落笔,不自行推导坐标。
/// - 颜色一律取自 [Tokens],因此与圆形视图及 SVG 导出保持同一视觉体系。
/// - 关键路径信息([criticalActions]、[criticalDeps])由上层分析后传入,
/// 本类不参与计算,仅负责高亮呈现。
///
/// 绘制顺序(见 [paint]):
/// 相位带 → 时间/相位标尺 → 循环边界 → 动作行 → 依赖连线。
library;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart' hide Action;
import '../layout/layout.dart';
import '../model/models.dart';
import '../model/units.dart';
import '../theme/tokens.dart';
/// 条形时间轴视图的 [CustomPainter] 实现。
///
/// 坐标约定:横轴 `x` 表示 0°–360° 相位(并线性映射到 0–cycle 时间),
/// 纵轴 `y` 表示动作行。角度到像素的换算由 [BarLayout.xFor] 完成。
class BarPainter extends CustomPainter {
/// 创建条形视图绘制器。
///
/// 参数:
/// - [project]:待绘制的工程数据(动作、工位、依赖、视图开关)。
/// - [tokens]:当前主题令牌,决定所有颜色。
/// - [criticalActions]:关键路径上的动作 id 集合,用于描边高亮。
/// - [criticalDeps]:关键路径上的依赖集合,键格式 `fromId->toId`。
/// - [selectedId]:当前选中动作 id,可为空。
///
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
BarPainter({
required this.project,
required this.tokens,
required this.criticalActions,
required this.criticalDeps,
required this.selectedIds,
this.guideAngle,
});
/// 待绘制的工程数据源。
final Project project;
/// 当前主题令牌。
final Tokens tokens;
/// 关键路径上的动作 id 集合。
final Set<String> criticalActions;
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
final Set<String> criticalDeps;
/// 当前选中的动作 id 集合(支持多选)。
final Set<String> selectedIds;
/// 拖拽相位分区边界时的参考角(度);非空时绘制一条竖直虚线参考。
final double? guideAngle;
/// 标尺横线所在的 y 坐标(画布坐标)。
static const double axisTop = 138;
/// 相位色带的顶部 y 坐标。
static const double phaseBandTop = 92;
/// 绘制整张条形视图。
///
/// 参数 [canvas]:目标画布;[size]:可用尺寸(用于构建 [BarLayout])。
/// 副作用:向 [canvas] 写入若干图元,不修改任何模型状态。
/// 图层顺序遵循“先背景后前景”:相位带、标尺、边界、动作行、依赖。
@override
void paint(Canvas canvas, Size size) {
final BarLayout layout = BarLayout(project, size);
final UnitSettings u = project.units;
final double cycle = project.cycle.durationMs;
if (project.viewState.showPhaseBands) _phaseBands(canvas, layout);
if (project.viewState.showRuler) _ruler(canvas, layout, u, cycle);
_boundaries(canvas, layout);
_rows(canvas, layout, u, cycle);
if (project.viewState.showDeps) _deps(canvas, layout, u);
// 分区拖拽参考线绘制在最上层,保证清晰可见。
if (guideAngle != null) _guide(canvas, layout, u);
}
/// 绘制相位分区拖拽时的竖直虚线参考线,并在顶部标注当前角度。
void _guide(Canvas canvas, BarLayout layout, UnitSettings u) {
final double deg = guideAngle!;
final double x = layout.xFor(deg.clamp(0, 360));
final double bottom = layout.yFor(layout.ordered.length) - 4;
final Paint p = Paint()
..color = tokens.accent
..strokeWidth = 1.4;
// 虚线:从相位带一直画到图表底部。
const double dash = 6;
const double gap = 6;
double y = phaseBandTop - 8;
while (y < bottom) {
canvas.drawLine(Offset(x, y), Offset(x, math.min(y + dash, bottom)), p);
y += dash + gap;
}
_text(canvas, '${deg.toStringAsFixed(1)}°', Offset(x, phaseBandTop - 18),
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w700);
}
/// 过滤出有效(跨度 > 0)的相位分区。
///
/// 相位分区来自 [Project.phaseZones],可在工艺参数面板中编辑;
/// 这里只做基本合法性过滤,保证异常数据不会画出负宽度色带。
List<PhaseZone> _phaseZones() {
return project.phaseZones.where((PhaseZone z) => z.endAngle > z.startAngle).toList();
}
/// 绘制相位色带。
///
/// 算法:把每个相位分区映射到横轴像素,绘制一个圆角矩形,填充强调色的
/// 低透明度版本并描边;宽度足够(> 64px)时居中标注分区名称。
/// 若分区跨越 360°(结束角 > 360),则在左侧补画从 0° 起的续段,
/// 对应“跨循环”语义。
void _phaseBands(Canvas canvas, BarLayout layout) {
for (final PhaseZone z in _phaseZones()) {
final double x0 = layout.xFor(z.startAngle);
final double x1 = layout.xFor(z.endAngle);
final Rect r = Rect.fromLTWH(x0, phaseBandTop, math.max(x1 - x0, 2), 32);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()..color = tokens.accent.withValues(alpha: 0.08),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()
..style = PaintingStyle.stroke
..color = tokens.line2,
);
// 名称居中且按色带宽度截断,保证不越出色块。
if (r.width > 26) {
_textClipped(canvas, z.name, Offset(r.center.dx, r.center.dy), r.width - 10,
color: tokens.sub, size: 11);
}
if (z.endAngle > 360) {
final double wx = layout.xFor(z.endAngle - 360) - layout.plotLeft;
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(layout.plotLeft, phaseBandTop, wx, 32), const Radius.circular(6)),
Paint()..color = tokens.accent.withValues(alpha: 0.08),
);
}
}
}
/// 绘制双轴标尺。
///
/// 每 15° 一个刻度,30° 为主刻度(更长);每 60° 标注文字:
/// 下排为相位(度,强调色),上排为等价的绝对时间(由 [tvalStr] 格式化)。
/// 参数 [u] 提供单位设置,[cycle] 为一个完整循环的毫秒数。
void _ruler(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
canvas.drawLine(Offset(layout.plotLeft, axisTop), Offset(layout.plotRight, axisTop),
Paint()..color = tokens.line2..strokeWidth = 1);
for (int d = 0; d <= 360; d += 15) {
final double x = layout.xFor(d.toDouble());
final bool major = d % 30 == 0;
canvas.drawLine(
Offset(x, axisTop),
Offset(x, axisTop + (major ? 28 : 14)),
Paint()
..color = major ? tokens.line2 : tokens.line
..strokeWidth = 1,
);
if (d % 60 == 0) {
_text(canvas, pfmt(d.toDouble(), u, dec: 0), Offset(x, axisTop + 44),
color: tokens.accent, size: 10, align: TextAlign.center);
_text(canvas, tvalStr(d / 360 * cycle, u, dec: u.timeUnit == TimeUnit.ms ? 0 : 1),
Offset(x, axisTop + 22), color: tokens.dim, size: 10, align: TextAlign.center);
}
}
_text(canvas, '时间 / ${timeUnitLabel(u.timeUnit)}', Offset(layout.plotLeft - 14, axisTop + 22),
color: tokens.dim, size: 10, align: TextAlign.right);
_text(canvas, '相位 / ${phaseUnitLabel(u.phaseUnit)}', Offset(layout.plotLeft - 14, axisTop + 44),
color: tokens.accent, size: 10, align: TextAlign.right);
}
/// 绘制循环起止边界线(0° 与 360°)。
///
/// 0° 使用 [Tokens.ok]、360° 使用 [Tokens.warn] 的虚线竖线表示,
/// 并在下方标注“循环开始 / 循环结束”。虚线由 [_dashedLine] 手工分段绘制。
void _boundaries(Canvas canvas, BarLayout layout) {
for (final double d in <double>[0, 360]) {
final double x = layout.xFor(d);
final Color col = d == 0 ? tokens.ok : tokens.warn;
final Paint p = Paint()
..color = col.withValues(alpha: 0.7)
..strokeWidth = 1.4;
_dashedLine(canvas, Offset(x, axisTop + 28), Offset(x, layout.yFor(layout.ordered.length) - 4), p);
_text(
canvas,
d == 0 ? '循环开始 0°' : '循环结束 360°',
Offset(d == 0 ? x + 8 : x - 8, axisTop + 62),
color: col,
size: 10,
align: d == 0 ? TextAlign.left : TextAlign.right,
);
}
}
/// 在两点之间绘制固定 dash/gap 的虚线。
///
/// 参数 [a]/[b]:线段端点;[p]:画笔(颜色与线宽由调用方决定)。
/// 通过沿单位方向向量步进生成短线段,不依赖 dashPathEffect。
void _dashedLine(Canvas canvas, Offset a, Offset b, Paint p) {
const double dash = 6;
const double gap = 6;
final double total = (b - a).distance;
final Offset dir = (b - a) / total;
double t = 0;
while (t < total) {
final double end = math.min(t + dash, total);
canvas.drawLine(a + dir * t, a + dir * end, p);
t = end + gap;
}
}
/// 绘制所有动作行。
///
/// 每行包含:
/// - 隔行背景(偶数行填充 [Tokens.panel2] 半透明色,便于横向阅读);
/// - 左侧序号色块,以及 `序号. id 名称`、`工位 · 驱动` 文本;
/// - 行底分隔线;
/// - 主条块(带阴影、选中描边、关键路径描边);
/// - 跨循环延续段(当 [Action.wrap] 且开启自动拆分时);
/// - 条块内或条块外的名称与时长标注。
///
/// 参数 [layout] 提供行列几何,[u]/[cycle] 用于时长文本换算。
void _rows(Canvas canvas, BarLayout layout, UnitSettings u, double cycle) {
final List<Action> ordered = layout.ordered;
for (int i = 0; i < ordered.length; i++) {
final Action a = ordered[i];
final double y = layout.yFor(i);
// 隔行背景色带,弱化行边界。
if (i.isEven) {
canvas.drawRRect(
RRect.fromRectAndRadius(
Rect.fromLTWH(20, y - 8, layout.plotRight - 20, BarLayout.rowVisual),
const Radius.circular(10),
),
Paint()..color = tokens.panel2.withValues(alpha: 0.5),
);
}
// 左侧序号徽标:动作自身颜色作底,叠加白色序号。
final Station? st = project.stationById(a.stationId);
final Color c = Color(a.color);
canvas.drawRRect(
RRect.fromRectAndRadius(Rect.fromLTWH(32, y + 8, 18, 18), const Radius.circular(5)),
Paint()..color = c,
);
_text(canvas, '${i + 1}', Offset(41, y + 17),
color: tokens.chip, size: 10, align: TextAlign.center, weight: FontWeight.w700);
// 动作标识与工位/驱动信息(两行文本)。
// 标题列文字按列宽截断,避免越界压到图表。
final double labelMax = BarLayout.leftGutter - 68;
_textMax(canvas, '${i + 1}. ${a.id} ${a.name}', Offset(60, y + 6), labelMax,
color: tokens.text, size: 12, weight: FontWeight.w600);
_textMax(canvas, '${st?.name ?? a.stationId} · ${a.driver}', Offset(60, y + 21), labelMax,
color: tokens.dim, size: 10);
// 行底分隔线,颜色较淡。
canvas.drawLine(
Offset(layout.plotLeft, y + BarLayout.rowVisual - 4),
Offset(layout.plotRight, y + BarLayout.rowVisual - 4),
Paint()
..color = tokens.line.withValues(alpha: 0.5)
..strokeWidth = 1,
);
// 动作条块:先投影,再填充,最后描边。
final Rect r = layout.rectFor(a);
final bool sel = selectedIds.contains(a.id);
final bool crit = criticalActions.contains(a.id);
canvas.drawRRect(
RRect.fromRectAndRadius(r.shift(const Offset(1.5, 2)), const Radius.circular(8)),
Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()..color = c.withValues(alpha: 0.92),
);
// 选中描边:选中时更亮,未选中时仅保留很淡的轮廓。
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = tokens.selOutline.withValues(alpha: sel ? 0.9 : 0.25),
);
// 关键路径条块额外叠加一圈加粗高亮描边。
if (crit) {
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(8)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = tokens.crit,
);
}
// 跨循环延续:把 360° 之后的部分回绕到左侧 0° 起绘制,
// 使用 [Tokens.tailOp] 透明度淡化以区别于主体。
if (a.wrap && u.autoSplitWrap) {
final double wx = layout.xFor(a.endAngle - 360) - layout.plotLeft;
final Rect tail = Rect.fromLTWH(layout.plotLeft, r.top, math.max(wx, 2), r.height);
canvas.drawRRect(
RRect.fromRectAndRadius(tail, const Radius.circular(8)),
Paint()..color = c.withValues(alpha: tokens.tailOp),
);
_text(canvas, '跨循环延续 ${(a.endAngle - 360).toStringAsFixed(0)}°',
Offset(layout.plotLeft + wx + 18, r.center.dy), color: tokens.warn, size: 10);
}
// 文字放置策略:条块足够宽时内置名称与时长;否则视右侧剩余空间
// 决定放到条块左侧还是右侧,避免文字越出绘图区。
final double durMs = a.durationAngle / 360 * cycle;
if (r.width > 150) {
_text(canvas, a.name, Offset(r.left + 10, r.center.dy - 5),
color: tokens.arcText, size: 11, weight: FontWeight.w700);
_text(canvas, tfmt(durMs, u), Offset(r.left + 10, r.center.dy + 7),
color: tokens.arcText.withValues(alpha: 0.8), size: 10);
} else if (r.right + 170 > layout.plotRight) {
_text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.left - 10, r.center.dy),
color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right);
} else {
_text(canvas, '${a.name} ${tfmt(durMs, u)}', Offset(r.right + 10, r.center.dy),
color: c, size: 11, weight: FontWeight.w600);
}
_text(canvas, 'Δ${a.durationAngle.toStringAsFixed(0)}°', Offset(layout.plotRight - 4, r.center.dy),
color: tokens.dim, size: 10, align: TextAlign.right);
}
}
/// 绘制动作依赖连线(正交折线)。
///
/// 算法要点:
/// - 建立“动作 id → 行号”索引,用于判断两行相对位置。
/// - FS 依赖从源条块右端出发;SS 依赖从源条块左端出发。
/// - 相邻行(行距 ≤ 1)走短折线;跨多行时在行区间外侧选一条“泳道”
/// 作为竖直通道,泳道位置按区间内条块的最右/最左边界外扩,
/// 并叠加 `(k % 3) * 16` 的错位,避免多条线完全重叠。
/// - 关键路径依赖先画一层加粗低透明度的“光晕”,再画主线。
/// - 末端绘制箭头与类型标签(FS 顺序 / SS +偏移角)。
///
/// 参数 [layout] 提供条块矩形;[u] 当前未直接使用,保留以对齐调用接口。
void _deps(Canvas canvas, BarLayout layout, UnitSettings u) {
final List<Action> ordered = layout.ordered;
final Map<String, int> index = <String, int>{};
for (int i = 0; i < ordered.length; i++) {
index[ordered[i].id] = i;
}
int k = 0;
for (final Action to in ordered) {
for (final Dependency d in to.deps) {
final Action? from = project.actionById(d.fromId);
if (from == null) continue;
final int ia = index[from.id] ?? 0;
final int ib = index[to.id] ?? 0;
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
final bool crit = criticalDeps.contains('${d.fromId}->${d.toId}');
final Rect ra = layout.rectFor(from);
final Rect rb = layout.rectFor(to);
final double sy = ra.center.dy;
final double ty = rb.center.dy;
final double sx = d.type == DepType.fs ? ra.right : ra.left;
final double ex = d.type == DepType.fs ? sx - 9 : sx + 9;
final double tx = rb.left + 14;
final double dir = ty > sy ? 1 : -1;
final double gapY = ty - dir * 23;
final double tipY = ty - dir * 15;
final Path p = Path();
double chipX;
final double chipY = gapY;
// 相邻行:短折线,直接从源端折向目标条块上方。
if ((ib - ia).abs() <= 1) {
p.moveTo(ex, sy + dir * 13);
p.lineTo(ex, gapY);
p.lineTo(tx, gapY);
p.lineTo(tx, tipY - dir * 12);
chipX = (ex + tx) / 2;
} else {
// 跨多行:扫描区间内所有条块边界,据此在外侧选一条竖直泳道。
double maxRight = 0;
double minLeft = double.infinity;
for (int r = math.min(ia, ib) + 1; r < math.max(ia, ib); r++) {
final Rect rr = layout.rectFor(ordered[r]);
maxRight = math.max(maxRight, rr.right);
minLeft = math.min(minLeft, rr.left);
}
double lane = math.max(math.max(sx, tx), maxRight) + 28 + (k % 3) * 16;
if (lane > layout.plotRight - 16) {
lane = math.min(math.min(sx, tx), minLeft) - 28 - (k % 3) * 16;
}
final double srcGapY = sy + dir * 27;
p.moveTo(ex, sy + dir * 13);
p.lineTo(ex, srcGapY);
p.lineTo(lane, srcGapY);
p.lineTo(lane, gapY);
p.lineTo(tx, gapY);
p.lineTo(tx, tipY - dir * 12);
chipX = (lane + tx) / 2;
}
// 关键路径依赖:先铺一层加粗低透明度光晕,再画主线。
if (crit) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 9
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.18),
);
}
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = crit ? 2.2 : 1.7
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.95),
);
_arrow(canvas, Offset(tx, tipY), Offset(0, dir), col);
final String label = d.type == DepType.fs ? 'FS 顺序' : 'SS +${d.offsetAngle.toStringAsFixed(0)}°';
_chip(canvas, Offset(chipX, chipY), label, col, crit);
k++;
}
}
}
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
///
/// 参数 [col] 为填充色;[dir] 只需提供方向(本函数不归一化,直接按分量使用)。
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
const double s = 13;
final Offset b = tip - dir * s;
final Offset n = Offset(-dir.dy, dir.dx) * s * 0.5;
final Path p = Path()
..moveTo(tip.dx, tip.dy)
..lineTo(b.dx + n.dx, b.dy + n.dy)
..lineTo(b.dx - n.dx, b.dy - n.dy)
..close();
canvas.drawPath(p, Paint()..color = col);
}
/// 绘制依赖类型标签胶囊。
///
/// 参数 [center]:胶囊中心;[label]:文字;[col]:边框与文字色;
/// [crit]:为真时在左侧追加星形关键标记,并把胶囊加宽。
void _chip(Canvas canvas, Offset center, String label, Color col, bool crit) {
final TextPainter tp = _tp(label, 10.5, col, FontWeight.w600);
final double w = tp.width + 18 + (crit ? 18 : 0);
const double h = 18;
final Rect r = Rect.fromCenter(center: center, width: w, height: h);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()..color = tokens.chip,
);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = col,
);
if (crit) {
canvas.drawCircle(Offset(r.left + 9, center.dy), 7, Paint()..color = tokens.crit);
_text(canvas, '★', Offset(r.left + 9, center.dy), color: tokens.onAccent, size: 10, align: TextAlign.center);
tp.paint(canvas, Offset(r.left + 20, center.dy - tp.height / 2));
} else {
tp.paint(canvas, Offset(r.left + 9, center.dy - tp.height / 2));
}
}
/// 构造并布局一个 [TextPainter]。
///
/// 统一使用微软雅黑字体;参数 [s] 文本、[size] 字号、[color] 颜色、
/// [weight] 字重、[align] 对齐方式。返回已完成 layout 的实例。
TextPainter _tp(String s, double size, Color color, FontWeight weight, {TextAlign align = TextAlign.left}) {
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
textAlign: align,
)..layout();
return tp;
}
/// 便捷文本绘制。
///
/// 在 [at] 处按 [align] 对齐绘制字符串 [s](默认左对齐、垂直居中)。
/// [color] 缺省时使用 [Tokens.text]。
/// 在 [at] 处绘制左对齐的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
void _textMax(Canvas canvas, String s, Offset at, double maxWidth,
{Color? color, double size = 12, FontWeight weight = FontWeight.normal}) {
if (maxWidth <= 4) return;
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
maxLines: 1,
ellipsis: '…',
)..layout(maxWidth: maxWidth);
tp.paint(canvas, Offset(at.dx, at.dy - tp.height / 2));
}
/// 在 [center] 处绘制居中的单行文字,并按 [maxWidth] 截断(超出显示省略号)。
void _textClipped(Canvas canvas, String s, Offset center, double maxWidth,
{Color? color, double size = 11, FontWeight weight = FontWeight.w600}) {
if (maxWidth <= 4) return;
final TextPainter tp = TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color ?? tokens.text, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
maxLines: 1,
ellipsis: '…',
)..layout(maxWidth: maxWidth);
tp.paint(canvas, Offset(center.dx - tp.width / 2, center.dy - tp.height / 2));
}
void _text(Canvas canvas, String s, Offset at,
{Color? color, double size = 12, FontWeight weight = FontWeight.normal, TextAlign align = TextAlign.left}) {
final TextPainter tp = _tp(s, size, color ?? tokens.text, weight, align: align);
double dx = at.dx;
if (align == TextAlign.center) dx -= tp.width / 2;
if (align == TextAlign.right) dx -= tp.width;
tp.paint(canvas, Offset(dx, at.dy - tp.height / 2));
}
/// 始终返回 `true`。
///
/// 绘制完全依赖每次重建的布局与外部状态,且本绘制器不做缓存,
/// 因此保守地要求每次重绘。
@override
bool shouldRepaint(covariant BarPainter old) => true;
}
+591
View File
@@ -0,0 +1,591 @@
/// 圆形(极坐标)视图绘制器。
///
/// 职责:把 [Project] 中的动作绘制为同心圆环——角度表示相位
/// (0° 位于 12 点钟方向,顺时针递增),半径表示动作所属层级(layer)。
///
/// 架构位置:
/// - 几何换算(圆心、各层半径、刻度半径)来自 [CircularLayout];
/// 极坐标换算复用 `layout.dart` 中的 [polar] 与 [arcPath]。
/// - 颜色取自 [Tokens],与条形视图及 SVG 导出保持同一视觉体系。
/// - 跨循环动作通过 [splitArcs] 拆成两段弧绘制,保证角度连续、视觉闭合。
library;
import 'dart:math' as math;
import 'dart:ui' as ui;
import 'package:flutter/material.dart' hide Action;
import '../layout/layout.dart';
import '../model/models.dart';
import '../theme/tokens.dart';
/// 圆形视图的 [CustomPainter] 实现。
///
/// 所有角度以“12 点钟为 0°、顺时针为正”为约定,由 [polar] 完成换算。
class CircularPainter extends CustomPainter {
/// 创建圆形视图绘制器。
///
/// 参数:
/// - [project]:待绘制的工程数据(动作、依赖、视图开关)。
/// - [tokens]:当前主题令牌,决定所有颜色。
/// - [criticalActions]:关键路径上的动作 id 集合。
/// - [criticalDeps]:关键路径上的依赖集合,键格式 `fromId->toId`。
/// - [selectedId]:当前选中动作 id,可为空。
///
/// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。
CircularPainter({
required this.project,
required this.tokens,
required this.criticalActions,
required this.criticalDeps,
required this.selectedIds,
});
/// 待绘制的工程数据源。
final Project project;
/// 当前主题令牌。
final Tokens tokens;
/// 关键路径上的动作 id 集合。
final Set<String> criticalActions;
/// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。
final Set<String> criticalDeps;
/// 当前选中的动作 id 集合(支持多选)。
final Set<String> selectedIds;
/// 绘制整个圆形视图。
///
/// 参数 [canvas]/[size] 遵循 Flutter 绘制约定。
/// 图层顺序:表盘底 → 圆环背景 → 象限线 → 层号 → 依赖 → 动作弧
/// → 起点标记 / 方向箭头 / 刻度。
@override
void paint(Canvas canvas, Size size) {
final CircularLayout layout = CircularLayout(project, size);
final UnitSettings u = project.units;
final Offset c = layout.center;
final double th = layout.th;
final double tickR = layout.tickR;
// 表盘底板:半径略大于最外圈刻度,形成一圈底色与边框。
canvas.drawCircle(c, tickR + 10, Paint()..color = tokens.dial);
canvas.drawCircle(
c,
tickR + 10,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.2
..color = tokens.line2,
);
// 圆环背景:每层用一条粗描边构成环带,奇偶层交替着色,
// 并在环带内外沿各画一条细描边作为边界。
final int layers = project.maxLayer + 1;
if (project.viewState.showRingBg) {
for (int i = 0; i < layers; i++) {
final double r = layout.radiusFor(i);
canvas.drawCircle(
c,
r,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = th
..color = i.isEven ? tokens.ringA : tokens.ringB,
);
canvas.drawCircle(
c,
r - th / 2,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = tokens.ringEdge,
);
canvas.drawCircle(
c,
r + th / 2,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = tokens.ringEdge,
);
}
}
canvas.drawCircle(c, math.max(CircularLayout.r0 - th / 2, 8), Paint()..color = tokens.dial);
// 相位分区环:在最外动作环与刻度环之间画一圈分区色带并标注名称。
if (project.viewState.showPhaseBands) _phaseRing(canvas, layout);
// 象限辅助线(0°/90°/180°/270°)。
_quadrants(canvas, c, layout);
// 层号标签:为每层挑选一个空位角度,避免压住该层的动作弧。
if (project.viewState.showLayerLabels) {
for (int i = 0; i < layers; i++) {
final double r = layout.radiusFor(i);
final double free = _freeAngle(i);
final Offset lp = polar(c, r, free);
_text(canvas, 'L${i + 1}', lp, color: tokens.ringLabel, size: 10, align: TextAlign.center, weight: FontWeight.w700);
}
}
// 依赖连线先于动作弧绘制,使其位于弧之下、不被遮挡。
if (project.viewState.showDeps) {
for (final Action to in project.actions) {
for (final Dependency d in to.deps) {
_dep(canvas, layout, d);
}
}
}
// 逐个绘制动作弧。
for (final Action a in project.actions) {
_action(canvas, layout, a, u);
}
// 起点标记、顺时针方向提示与外围刻度。
_startMarker(canvas, layout);
_cwArrow(canvas, layout);
if (project.viewState.showRuler) _ticks(canvas, layout);
}
/// 绘制相位分区环:在最外动作环与刻度环之间画一圈分区色带,标注分区名称,
/// 并在每条边界处画一条径向虚线,便于与条形视图的分区对应。
void _phaseRing(Canvas canvas, CircularLayout layout) {
final List<PhaseZone> zones =
project.phaseZones.where((PhaseZone z) => z.endAngle > z.startAngle).toList();
if (zones.isEmpty) return;
final Offset c = layout.center;
// 加宽分区带(16px);tickR 已在外层动作环之外留出更大间距,避免挤在一起。
final double rBand = layout.tickR - 16;
const double bandW = 16;
// 底环,作为分区的“轨道”。
canvas.drawCircle(
c,
rBand,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = bandW
..color = tokens.track,
);
// 分区配色(循环取用强调色系)。
const List<Color> pal = <Color>[
Color(0xFF4CC9F0),
Color(0xFF7C5CFF),
Color(0xFF34D399),
Color(0xFFFFD166),
Color(0xFFF472B6),
];
for (int i = 0; i < zones.length; i++) {
final PhaseZone z = zones[i];
final Color col = pal[i % pal.length];
// 分区色带(跨圈会拆成两段)。
for (final List<double> s in splitArcs(z.startAngle, z.endAngle)) {
canvas.drawPath(
arcPath(c, rBand, s[0], s[1]),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = bandW
..color = col.withValues(alpha: 0.85),
);
}
// 名称沿圆弧排列(切向),并按弧长截断,保证不超出色带。
final double mid = ((z.startAngle + z.endAngle) / 2) % 360;
final double span = (z.endAngle - z.startAngle).clamp(0, 360);
final double arcLen = 2 * math.pi * rBand * (span / 360);
_arcLabel(canvas, c, rBand, mid, z.name, tokens.arcText, 11, math.max(arcLen - 10, 22));
// 边界径向虚线。
for (final double b in <double>[z.startAngle, z.endAngle]) {
final Offset p1 = polar(c, rBand - bandW / 2 - 5, b);
final Offset p2 = polar(c, rBand + bandW / 2 + 5, b);
_dashed(canvas, p1, p2, col.withValues(alpha: 0.9));
}
}
}
/// 在两点之间画一条虚线(用于分区边界参考)。
void _dashed(Canvas canvas, Offset a, Offset b, Color col) {
const double dash = 3;
const double gap = 3;
final double total = (b - a).distance;
if (total <= 0) return;
final Offset dir = (b - a) / total;
double t = 0;
final Paint paint = Paint()
..color = col
..strokeWidth = 1.4
..strokeCap = StrokeCap.round;
while (t < total) {
final double end = math.min(t + dash, total);
canvas.drawLine(a + dir * t, a + dir * end, paint);
t = end + gap;
}
}
/// 沿圆弧切向绘制文字,并按 [maxWidth] 截断,保证不超出色带。
///
/// 文字位于半径 [r]、角度 [midDeg] 处,旋转到该点的切线方向;
/// 当文字位于左半圈(90°–270°)时翻转 180°,避免倒置。
void _arcLabel(Canvas canvas, Offset c, double r, double midDeg, String text, Color col, double size, double maxWidth) {
final TextPainter tp = TextPainter(
text: TextSpan(
text: text,
style: TextStyle(color: col, fontSize: size, fontWeight: FontWeight.w700, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
maxLines: 1,
ellipsis: '…',
)..layout(maxWidth: maxWidth);
final Offset pos = polar(c, r, midDeg);
final double rad = midDeg * math.pi / 180;
final bool flip = midDeg > 90 && midDeg < 270;
canvas.save();
canvas.translate(pos.dx, pos.dy);
canvas.rotate(flip ? rad + math.pi : rad);
tp.paint(canvas, Offset(-tp.width / 2, -tp.height / 2));
canvas.restore();
}
/// 为指定 [layer] 选一个尽量不被动作弧覆盖的角度,用于放置层号“L1/L2…”。
///
/// 从候选角列表(围绕正上方 270° 左右摆动)中挑选第一个与本层任何
/// 动作弧都不重叠的角度;若全部冲突则回退到 270°。
double _freeAngle(int layer) {
const List<double> cands = <double>[270, 278, 262, 286, 254, 294, 246, 302, 238, 310];
final List<Action> inLayer = project.actions.where((Action a) => a.layer == layer).toList();
for (final double cand in cands) {
bool ok = true;
for (final Action a in inLayer) {
if (_inArc(a, cand)) {
ok = false;
break;
}
}
if (ok) return cand;
}
return 270;
}
/// 判断角度 [deg] 是否落在动作 [a] 的弧范围内。
///
/// 处理两类情况:普通动作(结束角 ≤ 360)用闭区间判定;
/// 跨循环动作(结束角 > 360)用“大于起点 或 小于回绕终点”的并集判定。
/// 两侧各留 0.5° 容差以吸收浮点误差。
bool _inArc(Action a, double deg) {
final double n = deg % 360;
if (a.endAngle <= 360) return n >= a.startAngle - 0.5 && n <= a.endAngle + 0.5;
return n >= a.startAngle - 0.5 || n <= (a.endAngle - 360) + 0.5;
}
/// 绘制 0°/90°/180°/270° 四条象限辅助线(从内圈延伸到刻度环)。
void _quadrants(Canvas canvas, Offset c, CircularLayout layout) {
for (final double d in <double>[0, 90, 180, 270]) {
final Offset p1 = polar(c, 30, d);
final Offset p2 = polar(c, layout.tickR + 4, d);
canvas.drawLine(
p1,
p2,
Paint()
..color = tokens.line2.withValues(alpha: 0.7)
..strokeWidth = 1,
);
}
}
/// 绘制单个动作弧。
///
/// 算法:
/// - 通过 [splitArcs] 把可能跨循环的动作拆成一到两段,逐段绘制。
/// - 每段先画加宽阴影弧,再画本体弧;跨循环的“尾巴”段(第二段)
/// 以 [Tokens.tailOp] 淡化,并额外叠一层高透明度描边以示区分。
/// - 关键动作沿弧画 [Tokens.crit] 描边;选中动作在外侧画强调色细弧。
/// - 在弧中点附近放置 `序号. id 名称` 与起止角度文本;
/// 短弧(时长 < 38°)使用更小字号并收紧行距。
///
/// 参数 [u] 为当前单位设置(保留以对齐调用接口,当前未直接使用)。
void _action(Canvas canvas, CircularLayout layout, Action a, UnitSettings u) {
final Offset c = layout.center;
final double r = layout.radiusFor(a.layer);
final double th = layout.th;
final Color col = Color(a.color);
final bool sel = selectedIds.contains(a.id);
final bool crit = criticalActions.contains(a.id);
// 跨循环动作会被拆成 [a0,360] 与 [0,a1-360] 两段。
final List<List<double>> segs = splitArcs(a.startAngle, a.endAngle);
for (int k = 0; k < segs.length; k++) {
// 第二段且为跨循环时视为“尾巴”,需淡化处理。
final bool tail = a.wrap && k == 1;
final Path p = arcPath(c, r, segs[k][0], segs[k][1]);
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = th + 7
..strokeCap = a.wrap ? StrokeCap.butt : StrokeCap.round
..color = tokens.shadow.withValues(alpha: tokens.shadowOp),
);
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = th
..strokeCap = a.wrap ? StrokeCap.butt : StrokeCap.round
..color = col.withValues(alpha: tail ? tokens.tailOp : 1),
);
if (tail) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = th
..strokeCap = StrokeCap.butt
..color = col.withValues(alpha: 0.9),
);
}
}
if (crit) {
for (final List<double> s in segs) {
canvas.drawPath(
arcPath(c, r, s[0], s[1]),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2
..color = tokens.crit,
);
}
}
if (sel) {
for (final List<double> s in segs) {
canvas.drawPath(
arcPath(c, r + th / 2 + 8, s[0], s[1]),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1.6
..color = tokens.accent,
);
}
}
// 文字沿圆弧排列并按弧长截断,保证始终位于动作色带之内。
final double mid = (a.startAngle + a.endAngle) / 2;
final int step = project.actions.indexOf(a) + 1;
final double span = a.wrap ? (360 - a.startAngle) : a.durationAngle;
final double arcLen = 2 * math.pi * r * (span.clamp(0, 360) / 360);
_arcLabel(canvas, c, r, mid, '$step. ${a.id} ${a.name}', tokens.arcText, 11, math.max(arcLen - 12, 20));
}
/// 绘制圆形视图中的依赖连线。
///
/// 路径由四段组成:从源弧末端沿半径到中间半径 [rC] → 沿圆弧走到目标弧
/// 起点角度 → 再沿半径落到目标弧表面([rTip])→ 末端加箭头。
/// - 相邻层取两层半径均值作为 [rC];层差大于 1 时改用最外圈 `rMax+24` 绕行。
/// - [large]/[cw] 决定圆弧走大弧还是小弧、顺时针还是逆时针。
/// - 关键依赖先画低透明度粗描边,再画主线,最后放置 `A→B 类型` 胶囊。
void _dep(Canvas canvas, CircularLayout layout, Dependency d) {
final Action? A = project.actionById(d.fromId);
final Action? B = project.actionById(d.toId);
if (A == null || B == null) return;
final Offset c = layout.center;
final double rA = layout.radiusFor(A.layer);
final double rB = layout.radiusFor(B.layer);
final double aEnd = math.min(A.endAngle, 360);
final double aStart = B.startAngle;
// 层差大于 1 时从最外圈绕行,避免穿过中间层弧线。
final bool skip = (A.layer - B.layer).abs() > 1;
final double rC = skip ? layout.rMax + 24 : (rA + rB) / 2;
final bool cw = aStart >= aEnd;
final int large = (aStart - aEnd).abs() > 180 ? 1 : 0;
final Offset p0 = polar(c, rA, aEnd);
final Offset p1 = polar(c, rC, aEnd);
final Offset p2 = polar(c, rC, aStart);
final double rTip = rC < rB ? rB - layout.th / 2 - 4 : rB + layout.th / 2 + 4;
final Offset p3 = polar(c, rTip, aStart);
final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss;
final bool crit = criticalDeps.contains('${d.fromId}->${d.toId}');
final Path p = Path()
..moveTo(p0.dx, p0.dy)
..lineTo(p1.dx, p1.dy)
..arcToPoint(p2, radius: Radius.circular(rC), largeArc: large == 1, clockwise: cw)
..lineTo(p3.dx, p3.dy);
if (crit) {
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 9
..strokeCap = StrokeCap.round
..color = col.withValues(alpha: 0.2),
);
}
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = crit ? 3 : (d.type == DepType.fs ? 2.4 : 2)
..strokeCap = StrokeCap.round
..color = col,
);
_arrow(canvas, p3, p3 - p2, col);
// 标签胶囊放置在连线中段:跨层时贴最外圈,短跨度时外移避免压线。
final double theta = (aEnd + aStart) / 2;
final double span = (aStart - aEnd).abs();
final double rChip = skip ? rC : (span < 30 ? rB + layout.th / 2 + 10 : rC);
final Offset mid = polar(c, rChip, theta);
String label = d.type == DepType.fs ? 'FS' : 'SS+${d.offsetAngle.toStringAsFixed(0)}°';
if (d.type == DepType.ff) label = 'FF';
if (d.type == DepType.sf) label = 'SF';
_chip(canvas, mid, '${A.id}→${B.id} $label', col, crit);
}
/// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。
///
/// 与条形视图不同,这里会先把 [dir] 归一化;长度过短时直接返回。
void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) {
final double len = dir.distance;
if (len < 0.001) return;
final Offset u = dir / len;
const double s = 12;
final Offset b = tip - u * s;
final Offset n = Offset(-u.dy, u.dx) * s * 0.5;
final Path p = Path()
..moveTo(tip.dx, tip.dy)
..lineTo(b.dx + n.dx, b.dy + n.dy)
..lineTo(b.dx - n.dx, b.dy - n.dy)
..close();
canvas.drawPath(p, Paint()..color = col);
}
/// 绘制依赖标签胶囊。
///
/// 参数 [center]:胶囊中心;[label]:文字;[col]:边框与文字色;
/// [crit]:为真时在左侧追加星形关键标记并加宽胶囊。
void _chip(Canvas canvas, Offset center, String label, Color col, bool crit) {
final TextPainter tp = _tp(label, 10, col, FontWeight.w600);
final double w = tp.width + 16 + (crit ? 18 : 0);
final Rect r = Rect.fromCenter(center: center, width: w, height: 17);
canvas.drawRRect(RRect.fromRectAndRadius(r, const Radius.circular(6)), Paint()..color = tokens.chip);
canvas.drawRRect(
RRect.fromRectAndRadius(r, const Radius.circular(6)),
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 1
..color = col,
);
double tx = r.left + 8;
if (crit) {
canvas.drawCircle(Offset(r.left + 8, center.dy), 6.5, Paint()..color = tokens.crit);
_text(canvas, '★', Offset(r.left + 8, center.dy), color: tokens.onAccent, size: 10, align: TextAlign.center);
tx = r.left + 18;
}
tp.paint(canvas, Offset(tx, center.dy - tp.height / 2));
}
/// 绘制 0° 起点标记:顶部朝下的三角与“0° · 12 点钟”文字。
void _startMarker(Canvas canvas, CircularLayout layout) {
final Offset c = layout.center;
final double r = layout.tickR + 12;
final Offset tip = Offset(c.dx, c.dy - r - 22);
final Path p = Path()
..moveTo(tip.dx, tip.dy)
..lineTo(tip.dx - 9, tip.dy - 15)
..lineTo(tip.dx + 9, tip.dy - 15)
..close();
canvas.drawPath(p, Paint()..color = tokens.accent);
_text(canvas, '0° · 12 点钟', Offset(c.dx, tip.dy - 30),
color: tokens.accent, size: 11, align: TextAlign.center, weight: FontWeight.w600);
}
/// 绘制顺时针方向指示:一段外圈圆弧、箭头与“顺时针 CW”文字。
void _cwArrow(Canvas canvas, CircularLayout layout) {
final Offset c = layout.center;
// 放在刻度环之外,避免“冲进”视图内部。
final double r = layout.tickR + 16;
final Path p = arcPath(c, r, 20, 70);
canvas.drawPath(
p,
Paint()
..style = PaintingStyle.stroke
..strokeWidth = 2.4
..strokeCap = StrokeCap.round
..color = tokens.accent2,
);
final Offset t1 = polar(c, r, 74);
final Offset t2 = polar(c, r + 14, 68);
final Offset t3 = polar(c, r - 14, 66);
final Path a = Path()
..moveTo(t1.dx, t1.dy)
..lineTo(t2.dx, t2.dy)
..lineTo(t3.dx, t3.dy)
..close();
canvas.drawPath(a, Paint()..color = tokens.accent2);
final Offset label = polar(c, r + 30, 45);
_text(canvas, '顺时针 CW', label, color: tokens.accent2, size: 11, align: TextAlign.center, weight: FontWeight.w600);
}
/// 绘制外围角度刻度。
///
/// 每 5° 一个刻度,10° 为中刻度,30° 为主刻度并标注度数;
/// 90° 的整数倍使用强调色加粗,便于快速定位象限。
void _ticks(Canvas canvas, CircularLayout layout) {
final Offset c = layout.center;
final double r = layout.tickR;
for (int d = 0; d < 360; d += 5) {
final bool major = d % 30 == 0;
final bool mid = d % 10 == 0;
final Offset p1 = polar(c, r, d.toDouble());
final Offset p2 = polar(c, r + (major ? 20 : (mid ? 13 : 7)), d.toDouble());
canvas.drawLine(
p1,
p2,
Paint()
..strokeWidth = major ? 1.4 : 1
..color = major ? tokens.line2 : tokens.line,
);
if (major) {
final Offset lp = polar(c, r + 38, d.toDouble());
_text(canvas, '$d°', lp,
color: d % 90 == 0 ? tokens.accent : tokens.sub,
size: 10,
align: TextAlign.center,
weight: d % 90 == 0 ? FontWeight.w700 : FontWeight.normal);
}
}
}
/// 构造并布局一个 [TextPainter]。
///
/// 统一使用微软雅黑字体;返回已完成 layout 的实例。
TextPainter _tp(String s, double size, Color color, FontWeight weight) {
return TextPainter(
text: TextSpan(
text: s,
style: TextStyle(color: color, fontSize: size, fontWeight: weight, fontFamily: 'Microsoft YaHei UI'),
),
textDirection: ui.TextDirection.ltr,
)..layout();
}
/// 便捷文本绘制。
///
/// 在 [at] 处按 [align] 对齐绘制字符串 [s](默认左对齐、垂直居中)。
/// [color] 缺省时使用 [Tokens.text]。
void _text(Canvas canvas, String s, Offset at,
{Color? color, double size = 12, FontWeight weight = FontWeight.normal, TextAlign align = TextAlign.left}) {
final TextPainter tp = _tp(s, size, color ?? tokens.text, weight);
double dx = at.dx;
if (align == TextAlign.center) dx -= tp.width / 2;
if (align == TextAlign.right) dx -= tp.width;
tp.paint(canvas, Offset(dx, at.dy - tp.height / 2));
}
/// 始终返回 `true`。
///
/// 绘制完全依赖每次重建的布局与外部状态,且本绘制器不做缓存,
/// 因此保守地要求每次重绘。
@override
bool shouldRepaint(covariant CircularPainter old) => true;
}