Files

152 lines
5.2 KiB
Dart
Raw Permalink Normal View History

2026-09-18 16:50:12 +08:00
/// 单位换算与格式化工具层。
///
/// 本文件集中处理“毫秒/角度”与用户可读文本之间的转换,供 UI 与控制器复用:
/// - 时间单位与相位单位的显示标签;
/// - 时间/角度数值格式化(含小数位策略);
/// - 用户输入文本的解析(`120ms`、`1.5s`、`30%`、`45°` 等);
/// - 数值钳制、吸附与角度归一化等通用数学工具。
library;
import 'models.dart';
/// 各时间单位相对毫秒的换算系数:`时间值 = 毫秒 / kTimeDiv[unit]`。
const Map<TimeUnit, double> kTimeDiv = <TimeUnit, double>{
TimeUnit.ms: 1,
TimeUnit.s: 1000,
TimeUnit.min: 60000,
};
/// 各时间单位的默认小数位数,用于自动精度模式。
const Map<TimeUnit, int> kTimeDec = <TimeUnit, int>{
TimeUnit.ms: 0,
TimeUnit.s: 3,
TimeUnit.min: 4,
};
/// 返回时间单位的显示标签(`ms` / `s` / `min`)。
String timeUnitLabel(TimeUnit u) {
switch (u) {
case TimeUnit.ms:
return 'ms';
case TimeUnit.s:
return 's';
case TimeUnit.min:
return 'min';
}
}
/// 返回相位单位的显示标签(角度 `°` 或百分比 `%`)。
String phaseUnitLabel(PhaseUnit u) => u == PhaseUnit.deg ? '°' : '%';
/// 将毫秒换算为指定时间单位的数值。
double timeValue(double ms, TimeUnit u) => ms / kTimeDiv[u]!;
/// 计算应保留的小数位数。
///
/// 优先级:[override](调用方显式指定)> [UnitSettings.precision](>=0 时)
/// > 该时间单位的默认精度。
int _dec(UnitSettings u, int? override) {
if (override != null) return override;
if (u.precision >= 0) return u.precision;
return kTimeDec[u.timeUnit]!;
}
/// 将毫秒格式化为带单位后缀的显示文本,如 `1.500 s`。
///
/// 参数 [ms]:毫秒值;[u]:单位设置;[dec]:可选的小数位覆盖。
String tfmt(double ms, UnitSettings u, {int? dec}) {
return '${timeValue(ms, u.timeUnit).toStringAsFixed(_dec(u, dec))} ${timeUnitLabel(u.timeUnit)}';
}
/// 将毫秒格式化为仅含数值、不含单位的字符串,用于输入框回填。
String tvalStr(double ms, UnitSettings u, {int dec = 1}) {
return timeValue(ms, u.timeUnit).toStringAsFixed(dec);
}
/// 自动选择合适单位的毫秒格式化(不依赖 [UnitSettings])。
///
/// 规则:<100ms 用毫秒(1 位小数),<1000ms 用整数毫秒,
/// <60s 用秒(<10s 保留 3 位、否则 2 位),其余用分钟(3 位)。
String tfmtAuto(double ms) {
if (ms < 100) return '${ms.toStringAsFixed(1)} ms';
if (ms < 1000) return '${ms.round()} ms';
if (ms < 60000) return '${(ms / 1000).toStringAsFixed(ms < 10000 ? 3 : 2)} s';
return '${(ms / 60000).toStringAsFixed(3)} min';
}
/// 将角度格式化为显示文本。
///
/// 当单位为百分比时按 `deg / 3.6` 换算并保留 2 位小数;否则按角度显示。
String pfmt(double deg, UnitSettings u, {int dec = 1}) {
if (u.phaseUnit == PhaseUnit.percent) {
return '${(deg / 3.6).toStringAsFixed(2)}%';
}
return '${deg.toStringAsFixed(dec)}°';
}
/// 将相位角换算为时间并格式化为文本。
///
/// 参数 [deg]:角度;[u]:单位设置;[cycleMs]:整圈时长(毫秒);
/// [dec]:可选小数位覆盖。
String deg2msStr(double deg, UnitSettings u, double cycleMs, {int? dec}) {
return tfmt(deg / 360 * cycleMs, u, dec: dec);
}
/// 解析用户输入的时长文本,返回毫秒;无法解析时返回 null。
///
/// 支持后缀 `ms` / `s` / `min`(不区分大小写,可省略则按 [u] 的当前时间单位);
/// 例如 `120ms`、`1.5s`、`2 min`。
double? parseDuration(String text, UnitSettings u) {
final String s = text.trim().toLowerCase();
if (s.isEmpty) return null;
double factor = kTimeDiv[u.timeUnit]!;
String numPart = s;
// 依次识别时间后缀并确定换算系数;注意先匹配 `ms`/`min` 再匹配 `s`。
if (s.endsWith('ms')) {
factor = 1;
numPart = s.substring(0, s.length - 2);
} else if (s.endsWith('min')) {
factor = 60000;
numPart = s.substring(0, s.length - 3);
} else if (s.endsWith('s')) {
factor = 1000;
numPart = s.substring(0, s.length - 1);
}
final double? v = double.tryParse(numPart.trim());
if (v == null) return null;
return v * factor;
}
/// 解析用户输入的相位文本,返回角度;无法解析时返回 null。
///
/// 支持 `%`(按 `v * 3.6` 换算为角度)与 `°` 后缀,无后缀时按角度解析。
double? parsePhase(String text) {
final String s = text.trim();
if (s.isEmpty) return null;
if (s.endsWith('%')) {
final double? v = double.tryParse(s.substring(0, s.length - 1).trim());
if (v == null) return null;
return v * 3.6;
}
final String num = s.endsWith('°') ? s.substring(0, s.length - 1).trim() : s;
return double.tryParse(num);
}
/// 将 [v] 限制在闭区间 `[lo, hi]` 内。
double clamp(double v, double lo, double hi) => v < lo ? lo : (v > hi ? hi : v);
/// 将 [value] 吸附到 [step] 的整数倍。
///
/// 当 [step] <= 0 时表示禁用吸附,直接返回原值。
double snapTo(double value, double step) {
if (step <= 0) return value;
return (value / step).roundToDouble() * step;
}
/// 将角度归一化到 `[0, 360)` 区间。
double normalizeAngle(double deg) {
double a = deg % 360;
if (a < 0) a += 360;
return a;
}