/// 条形(时间轴)视图绘制器。 /// /// 职责:把 [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/dep_routes.dart'; 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,可为空。 /// - [selectedDep]:当前选中的依赖连线键(`from->to`),用于高亮。 /// /// 无副作用;实例不缓存布局,每次 [paint] 都重新计算。 BarPainter({ required this.project, required this.tokens, required this.criticalActions, required this.criticalDeps, required this.selectedIds, this.selectedDep, this.guideAngle, this.dropLane, this.dropValid = true, }); /// 待绘制的工程数据源。 final Project project; /// 当前主题令牌。 final Tokens tokens; /// 关键路径上的动作 id 集合。 final Set criticalActions; /// 关键路径上的依赖边集合,元素格式为 `fromId->toId`。 final Set criticalDeps; /// 当前选中的动作 id 集合(支持多选)。 final Set selectedIds; /// 当前选中的依赖连线键(`from->to`);非空时该走线以强调色高亮。 final String? selectedDep; /// 拖拽相位分区边界时的参考角(度);非空时绘制一条竖直虚线参考。 final double? guideAngle; /// 上下拖拽换泳道时,光标所在的目标泳道号;非空时高亮该泳道。 final int? dropLane; /// 目标泳道是否可放置(时间不重叠);false 时用警示色高亮。 final bool dropValid; /// 标尺横线所在的 y 坐标(画布坐标)。 static const double axisTop = 164; /// 相位色带的顶部 y 坐标。 static const double phaseBandTop = 92; /// 相位刻度值所在行的 y(分区带与刻度线之间,上排)。 static const double phaseValY = 136; /// 时间刻度值所在行的 y(分区带与刻度线之间,下排)。 static const double timeValY = 150; /// 绘制整张条形视图。 /// /// 参数 [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 (dropLane != null && layout.lanes.contains(dropLane)) { final double y = layout.yForLane(dropLane!); final double h = layout.slotsOf(dropLane!) * layout.pitch; final Color c = dropValid ? tokens.accent : tokens.danger; canvas.drawRRect( RRect.fromRectAndRadius( Rect.fromLTWH(20, y - 8, layout.plotRight - 20, h), const Radius.circular(10), ), Paint()..color = c.withValues(alpha: 0.12), ); canvas.drawRRect( RRect.fromRectAndRadius( Rect.fromLTWH(20, y - 8, layout.plotRight - 20, h), const Radius.circular(10), ), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1.4 ..color = c.withValues(alpha: 0.8), ); } 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.plotBottom - 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 _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, phaseValY), color: tokens.accent, size: 10, align: TextAlign.center); _text(canvas, tvalStr(d / 360 * cycle, u, dec: u.timeUnit == TimeUnit.ms ? 0 : 1), Offset(x, timeValY), color: tokens.sub, size: 10, align: TextAlign.center); } } // 轴单位图例:与对应刻度值同排,置于最左侧。 _text(canvas, '相位 / ${phaseUnitLabel(u.phaseUnit)}', Offset(layout.plotLeft - 12, phaseValY), color: tokens.accent, size: 10, align: TextAlign.right); _text(canvas, '时间 / ${timeUnitLabel(u.timeUnit)}', Offset(layout.plotLeft - 12, timeValY), color: tokens.sub, 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 [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.plotBottom - 4), p); } } /// 在两点之间绘制固定 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 lanes = layout.lanes; for (int li = 0; li < lanes.length; li++) { final int lane = lanes[li]; final double y = layout.yForLane(lane); final int slots = layout.slotsOf(lane); final double laneH = slots * layout.pitch; final List acts = layout.actionsInLane(lane); // 单槽泳道保持与旧版完全一致的背景带 / 底线几何;多槽位覆盖整个泳道带。 final double bandH = slots == 1 ? BarLayout.rowVisual : laneH; // 隔泳道背景色带(覆盖该泳道全部槽位),弱化边界。 if (li.isEven) { canvas.drawRRect( RRect.fromRectAndRadius( Rect.fromLTWH(20, y - 8, layout.plotRight - 20, bandH), const Radius.circular(10), ), Paint()..color = tokens.panel2.withValues(alpha: 0.5), ); } // 多槽位泳道:槽位之间用浅色虚线分隔,提示并列关系。 if (slots > 1) { for (int s = 1; s < slots; s++) { final double sy = y + s * layout.pitch - 8; _dashedLine( canvas, Offset(layout.plotLeft, sy), Offset(layout.plotRight, sy), Paint() ..color = tokens.line2.withValues(alpha: 0.5) ..strokeWidth = 1, ); } } // 泳道标题(合并显示;单槽最多两行,多槽位按泳道高度持续换行)。 _laneHeader(canvas, layout, li, acts, slots == 1 ? y : y + laneH / 2 - BarLayout.rowVisual / 2, slots, laneH); // 泳道底分隔线(单槽时与旧版一致)。 canvas.drawLine( Offset(layout.plotLeft, slots == 1 ? y + BarLayout.rowVisual - 4 : y + laneH - 4), Offset(layout.plotRight, slots == 1 ? y + BarLayout.rowVisual - 4 : y + laneH - 4), Paint() ..color = tokens.line2.withValues(alpha: 0.6) ..strokeWidth = 1, ); // 该泳道内的所有动作条(按槽位分行放置)。 for (final Action a in acts) { _bar(canvas, layout, a, u, cycle); } } } /// 绘制一条泳道的标题:序号徽标 + 合并动作名 + 副标题。 /// /// 合并动作名在宽度 [BarLayout.leftGutter]-68 内自动换行:单槽泳道最多两行 /// (保持旧版外观);多槽位泳道按泳道高度**持续换行**直到放不下为止 /// (副标题跟随实际行数下移)。 void _laneHeader(Canvas canvas, BarLayout layout, int li, List acts, double y, int slots, double laneH) { final double labelMax = BarLayout.leftGutter - 68; final Color c0 = acts.isEmpty ? tokens.accent : Color(acts.first.color); canvas.drawRRect( RRect.fromRectAndRadius(Rect.fromLTWH(32, y + 8, 18, 18), const Radius.circular(5)), Paint()..color = c0, ); _text(canvas, '${li + 1}', Offset(41, y + 17), color: tokens.chip, size: 10, align: TextAlign.center, weight: FontWeight.w700); if (acts.isEmpty) return; // 先按足够大的行数测量“真实行高”(字体回退下行高可能大于字号, // 之前按固定 13px 估算导致标题换行多行后压住灰色副标题)。 final String joined = acts.map((Action a) => '${a.id} ${a.name}').join(' / '); final TextPainter tp = TextPainter( text: TextSpan( text: joined, style: TextStyle( color: tokens.text, fontSize: 11, fontWeight: FontWeight.w600, fontFamily: 'Microsoft YaHei UI', ), ), textDirection: ui.TextDirection.ltr, maxLines: 40, ellipsis: '…', )..layout(maxWidth: labelMax); final int totalLines = tp.computeLineMetrics().length; final double lineH = totalLines == 0 ? 14 : tp.height / totalLines; // 行数上限:单槽保持两行(旧版外观);多槽位按泳道高度换算并给副标题留一行。 final bool single = slots <= 1; final int maxLines = single ? 2 : ((laneH - lineH - 8) / lineH).floor().clamp(2, 40); if (tp.computeLineMetrics().length != maxLines && maxLines < totalLines) { tp.maxLines = maxLines; tp.ellipsis = '…'; tp.layout(maxWidth: labelMax); } tp.paint(canvas, Offset(60, y + 2)); final Station? st = project.stationById(acts.first.stationId); final String sub = '${st?.name ?? acts.first.stationId} · ${acts.first.driver}' '${acts.length > 1 ? ' · 共 ${acts.length} 个' : ''}'; // 副标题紧随实际标题高度;单槽保持旧版下边距(y+32)但绝不高于标题底部, // 多槽位下若剩余高度不足则不再绘制,避免压住标题文字。 final double subY = single ? math.max(y + 32, y + 2 + tp.height + 2) : y + 2 + tp.height + 2; if (single || subY + 11 <= y + laneH) { _textMax(canvas, sub, Offset(60, subY), labelMax, color: tokens.dim, size: 10); } } /// 绘制单个动作条块(投影 / 填充 / 描边 / 跨圈尾段 / 文字标注)。 void _bar(Canvas canvas, BarLayout layout, Action a, UnitSettings u, double cycle) { final Color c = Color(a.color); 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(3)), Paint()..color = tokens.shadow.withValues(alpha: tokens.shadowOp), ); canvas.drawRRect( RRect.fromRectAndRadius(r, const Radius.circular(3)), Paint()..color = c.withValues(alpha: 0.92), ); canvas.drawRRect( RRect.fromRectAndRadius(r, const Radius.circular(3)), 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(3)), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2 ..color = tokens.crit, ); } // 选中时在两端绘制把手:提示可拖拽端点改时长(触屏目标更大、更好抓)。 if (sel) { _handle(canvas, Offset(r.left, r.center.dy)); _handle(canvas, Offset(r.right, r.center.dy)); } 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(3)), 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; final String full = '${a.name} ${tfmt(durMs, u)}'; // 默认「名称 + 时长」一起写在条块内、紧跟名称之后(单行,条块更苗条); // 仅当条块太短、放不下时才移到条块外侧。 final double fullW = _tp(full, 11, tokens.arcText, FontWeight.w700).width; if (r.width >= fullW + 20) { _text(canvas, full, Offset(r.left + 10, r.center.dy), color: tokens.arcText, size: 11, weight: FontWeight.w700); } else if (r.right + 190 > layout.plotRight) { _text(canvas, full, Offset(r.left - 10, r.center.dy), color: c, size: 11, weight: FontWeight.w600, align: TextAlign.right); } else { _text(canvas, full, 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); } /// 绘制端点把手:白边圆点,提示可拖拽端点改时长。 void _handle(Canvas canvas, Offset o) { canvas.drawCircle(o, 6, Paint()..color = tokens.accent); canvas.drawCircle( o, 6, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 2 ..color = Colors.white, ); } /// 绘制动作依赖连线(正交折线)。 /// /// 算法要点: /// - 建立“动作 id → 行号”索引,用于判断两行相对位置。 /// - FS 依赖从源条块右端出发;SS 依赖从源条块左端出发。 /// - 相邻行(行距 ≤ 1)走短折线;跨多行时在行区间外侧选一条“泳道” /// 作为竖直通道,泳道位置按区间内条块的最右/最左边界外扩, /// 并叠加 `(k % 3) * 16` 的错位,避免多条线完全重叠。 /// - 关键路径依赖先画一层加粗低透明度的“光晕”,再画主线。 /// - 末端绘制箭头与类型标签(FS 顺序 / SS +偏移角)。 /// /// 参数 [layout] 提供条块矩形;[u] 当前未直接使用,保留以对齐调用接口。 void _deps(Canvas canvas, BarLayout layout, UnitSettings u) { // 走线统一由 [buildBarDepRoutes] 计算:同组自动错开 + 手动偏移, // 与画布的命中测试 / 拖拽调整共用同一份几何。 final List routes = buildBarDepRoutes(project, layout); // 依赖标签先收集、后统一避让放置(见方法末尾 _placeChips)。 final List<_ChipSpec> chips = <_ChipSpec>[]; for (final BarDepRoute r in routes) { final Dependency d = r.dep; final Color col = d.type == DepType.fs ? tokens.fs : tokens.ss; final bool crit = criticalDeps.contains(r.key); final bool sel = selectedDep == r.key; final Color draw = sel ? tokens.accent : col; final Path p = Path() ..moveTo(r.points.first.dx, r.points.first.dy); for (int i = 1; i < r.points.length; i++) { p.lineTo(r.points[i].dx, r.points[i].dy); } // 关键路径依赖:先铺一层加粗低透明度光晕,再画主线。 if (crit) { canvas.drawPath( p, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 5 ..strokeCap = StrokeCap.round ..color = col.withValues(alpha: 0.18), ); } canvas.drawPath( p, Paint() ..style = PaintingStyle.stroke ..strokeWidth = sel ? 2.0 : (crit ? 1.1 : 0.85) ..strokeCap = StrokeCap.round ..color = sel ? draw : draw.withValues(alpha: 0.95), ); _arrow(canvas, r.arrowTip, r.arrowDir, draw); final String label = switch (d.type) { DepType.fs => 'FS', DepType.ss => 'SS+${d.offsetAngle.toStringAsFixed(0)}°', DepType.ff => 'FF', DepType.sf => 'SF', }; // 行程太短的连线:标签移到箭头外侧(宽度按真实字号精确测量)。 final double chipW = _tp(label, 8.5, draw, FontWeight.w600).width + 12; final Offset chipCenter = r.chipRun < chipW + r.chipRunMargin ? Offset(r.chipAltAnchor.dx + chipW / 2, r.chipAltAnchor.dy) : r.chipCenter; // 先记录标签位置,所有依赖线画完后统一避让放置(避免标签互相重叠)。 chips.add(_ChipSpec(chipCenter, label, draw)); } _placeChips(canvas, chips); } /// 在 [tip] 处绘制指向 [dir] 方向的实心三角箭头。 /// /// 参数 [col] 为填充色;[dir] 只需提供方向(本函数不归一化,直接按分量使用)。 void _arrow(Canvas canvas, Offset tip, Offset dir, Color col) { const double s = 6.5; 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); } /// 统一放置并绘制依赖标签,避免标签之间相互重叠。 /// /// 对每个标签依次尝试:原位 → 上下错开 → 上下再错开 → 左右错开 → 斜向错开, /// 取第一个不与已放置标签相交的位置,实现“智能避让”。标签内不再绘制五角星 /// (关键路径改用线条光晕表示),字号与色块同时收小,画面更干净。 void _placeChips(Canvas canvas, List<_ChipSpec> chips) { final List placed = []; for (final _ChipSpec chip in chips) { final TextPainter tp = _tp(chip.label, 8.5, chip.color, FontWeight.w600); final double w = tp.width + 12; const double h = 14; final List trials = [ Offset.zero, Offset(0, -(h + 3)), Offset(0, h + 3), Offset(0, -2 * (h + 3)), Offset(0, 2 * (h + 3)), Offset(0, -3 * (h + 3)), Offset(0, 3 * (h + 3)), Offset(w + 6, 0), Offset(-w - 6, 0), Offset(w + 6, -(h + 3)), Offset(-w - 6, -(h + 3)), Offset(w + 6, h + 3), Offset(-w - 6, h + 3), ]; Rect r = Rect.fromCenter(center: chip.center, width: w, height: h); for (final Offset t in trials) { final Rect cand = r.shift(t); if (!placed.any((Rect o) => o.inflate(1.5).overlaps(cand))) { r = cand; break; } } placed.add(r); _chipRect(canvas, r, tp, chip.color); } } /// 按给定矩形绘制一颗标签胶囊(无星标)。 void _chipRect(Canvas canvas, Rect r, TextPainter tp, Color col) { canvas.drawRRect( RRect.fromRectAndRadius(r, const Radius.circular(5)), Paint()..color = tokens.chip, ); canvas.drawRRect( RRect.fromRectAndRadius(r, const Radius.circular(5)), Paint() ..style = PaintingStyle.stroke ..strokeWidth = 1 ..color = col, ); tp.paint(canvas, Offset(r.left + 6, r.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; } /// 依赖标签的待放置描述:中心点、文本与颜色。 /// /// 在依赖遍历中收集,随后由 `BarPainter._placeChips` 统一做避让放置。 class _ChipSpec { const _ChipSpec(this.center, this.label, this.color); /// 期望的标签中心(逻辑坐标)。 final Offset center; /// 标签文本(如 `FS` / `SS+30°`)。 final String label; /// 边框与文字颜色(按依赖类型区分)。 final Color color; }