diff --git a/lib/ui2/screens/home_screen.dart b/lib/ui2/screens/home_screen.dart index c458117ce..f423fd0d7 100644 --- a/lib/ui2/screens/home_screen.dart +++ b/lib/ui2/screens/home_screen.dart @@ -638,6 +638,16 @@ String hm(num? minutes) { ); } +/// Binds a run so it cannot wrap internally, in any script. A duration like +/// "2 Std. 34 Min." is one reading; left breakable, a range value such as a +/// sleep stage ("1 Std. 34 Min.-2 Std. 54 Min.") dropped a lone "Min." onto its +/// own line. A word joiner (U+2060) between every character forbids a break at +/// each boundary — including the spaceless ones in CJK units like "分钟", which +/// an ASCII-space guard could not reach (#757). Zero-width, so the text renders +/// unchanged; the dash joining two durations is left un-glued and stays the one +/// place a range may wrap. +String nbsp(String s) => s.runes.map(String.fromCharCode).join('\u2060'); + /// "8h" for a whole number of hours, else [hm]: the caption's compact forms. String _compactHm(num? minutes, AppLocalizations? l) { if (minutes == null) return ''; diff --git a/lib/ui2/screens/sleep_detail.dart b/lib/ui2/screens/sleep_detail.dart index 290830d34..3b85ceea6 100644 --- a/lib/ui2/screens/sleep_detail.dart +++ b/lib/ui2/screens/sleep_detail.dart @@ -185,7 +185,7 @@ String sleepReadingText(AppLocalizations? l, num value, int places, /// A stage interval as one label. Seconds in, because that is what the analytics /// interval carries; `hm` wants minutes. String _rangeText(ana.StageInterval i) => - '${hm(i.loSec / 60)}–${hm(i.hiSec / 60)}'; + '${nbsp(hm(i.loSec / 60))}–${nbsp(hm(i.hiSec / 60))}'; String _capitalise(String s) => s.isEmpty ? s : s[0].toUpperCase() + s.substring(1); diff --git a/test/ui2_hm_nbsp_test.dart b/test/ui2_hm_nbsp_test.dart new file mode 100644 index 000000000..d1f734fd1 --- /dev/null +++ b/test/ui2_hm_nbsp_test.dart @@ -0,0 +1,58 @@ +// A sleep stage duration is shown as a range, "1 Std. 34 Min.-2 Std. 54 Min.". +// Each side is one reading and must stay whole wherever it is drawn. Binding +// only ASCII spaces kept Latin units together but left CJK units (e.g. zh +// "分钟") breakable, since those scripts wrap between characters with no space +// to guard (#757 review). `nbsp` now glues every character with a word joiner, +// so no duration breaks internally in any locale; only the dash may wrap. +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +import 'package:openstrap_edge/state/locale_controller.dart'; +import 'package:openstrap_edge/ui2/screens/home_screen.dart' show hm, nbsp; + +/// True iff a word joiner sits between every adjacent pair of code points, so +/// the run offers no internal line-break opportunity. +bool fullyGlued(String s) { + final runes = s.runes.toList(); + for (var i = 0; i + 1 < runes.length; i++) { + if (runes[i] != 0x2060 && runes[i + 1] != 0x2060) return false; + } + return true; +} + +void main() { + test('a duration never breaks internally, in Latin or CJK scripts', () async { + SharedPreferences.setMockInitialValues({}); + final c = LocaleController.seed('de'); + c.useForPresentation(); + + final one = nbsp(hm(154)); // 2 h 34 min + expect(one.replaceAll('⁠', ''), '2 Std. 34 Min.'); // unchanged text + expect(fullyGlued(one), isTrue); + + // CJK has no spaces to bind, and wraps between characters — the gap the + // space-only version missed. + expect(nbsp('分钟'), '分⁠钟'); + expect(fullyGlued(nbsp('1 小时 34 分钟')), isTrue); + + await c.setCode(null); + c.dispose(); + }); + + test('a range only ever wraps at the dash between its two durations', + () async { + SharedPreferences.setMockInitialValues({}); + final c = LocaleController.seed('de'); + c.useForPresentation(); + + final range = '${nbsp(hm(94))}–${nbsp(hm(154))}'; + // The dash is the one separator left un-glued, so it is the only break. + expect(range.replaceAll('⁠', '').split('–'), [hm(94), hm(154)]); + for (final part in range.split('–')) { + expect(fullyGlued(part), isTrue); + } + + await c.setCode(null); + c.dispose(); + }); +} diff --git a/test/ui2_sleep_detail_test.dart b/test/ui2_sleep_detail_test.dart index 4de172bc7..34a83b1fc 100644 --- a/test/ui2_sleep_detail_test.dart +++ b/test/ui2_sleep_detail_test.dart @@ -63,6 +63,10 @@ Future _pump(WidgetTester t, SleepData d, {double scale = 1}) async { await t.pumpAndSettle(); } +Finder _rangeText(String plain) => find.byWidgetPredicate((w) => + w is Text && + (w.data ?? '').replaceAll('\u2060', '').replaceAll('\u00A0', ' ') == plain); + void main() { testWidgets('a measure inside the middle half of your nights is typical', (t) async { @@ -162,7 +166,7 @@ void main() { // showed a count is the contradiction this item exists to remove. Range // totals are too wide to sit beside the lanes on a 390pt phone without // starving the plot, so the hypnogram leaves them to the table. - expect(find.text('20m–2h 20m'), findsNWidgets(2)); + expect(_rangeText('20m–2h 20m'), findsNWidgets(2)); // And the share column is gone with the count it was computed from. for (final share in const ['52%', '17%', '19%', '13%']) { expect(find.text(share), findsNothing); @@ -196,7 +200,7 @@ void main() { tstHistory: _flat(20, 420))); // Same 80 minutes, half-width 0.45x = 36m. Narrower than the 60m above, // from the night's own confidence rather than one published figure. - expect(find.text('44m–1h 56m'), findsNWidgets(2)); + expect(_rangeText('44m–1h 56m'), findsNWidgets(2)); }); testWidgets('the deep comparison stops asserting a difference', (t) async {