From 4a710379a374c723997919a97abc1bc1a9f4b6cf Mon Sep 17 00:00:00 2001 From: uvtqt Date: Wed, 18 Nov 2026 10:55:17 +0100 Subject: [PATCH 1/2] sleep: keep a duration whole so a range value stops orphaning its unit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A stage/compare range reads 'X–Y' with each side a duration like '2 Std. 34 Min.'. With ordinary spaces the value wrapped between a number and its unit and dropped a lone 'Min.' onto its own right-aligned line, so the range looked shifted. Bind each duration with a non-breaking space (new nbsp helper) and keep the dash as the only break point. Co-Authored-By: Claude Opus 4.8 --- lib/ui2/screens/home_screen.dart | 6 +++++ lib/ui2/screens/sleep_detail.dart | 2 +- test/ui2_hm_nbsp_test.dart | 37 +++++++++++++++++++++++++++++++ test/ui2_sleep_detail_test.dart | 4 ++-- 4 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 test/ui2_hm_nbsp_test.dart diff --git a/lib/ui2/screens/home_screen.dart b/lib/ui2/screens/home_screen.dart index c458117ce..0406bc6bf 100644 --- a/lib/ui2/screens/home_screen.dart +++ b/lib/ui2/screens/home_screen.dart @@ -638,6 +638,12 @@ String hm(num? minutes) { ); } +/// Binds a value to its unit so the pair cannot wrap between them. 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. The dash joining the two durations stays a normal break. +String nbsp(String s) => s.replaceAll(' ', '\u00A0'); + /// "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..1965c5f50 --- /dev/null +++ b/test/ui2_hm_nbsp_test.dart @@ -0,0 +1,37 @@ +// A sleep stage duration is shown as a range, "1 Std. 34 Min.-2 Std. 54 Min.". +// Each side is one reading. Left with ordinary spaces it wrapped between a +// number and its unit, so the value broke before the final unit and dropped a +// lone "Min." onto its own right-aligned line (the shifted numbers in #… / +// the Sleep screen). `nbsp` binds each duration; 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; + +void main() { + test('a range value only ever wraps at the dash between its two durations', + () async { + SharedPreferences.setMockInitialValues({}); + final c = LocaleController.seed('de'); + c.useForPresentation(); + + // Same visible text, no breakable space inside a single duration. + final one = nbsp(hm(154)); // 2 h 34 min + expect(one.replaceAll(' ', ' '), '2 Std. 34 Min.'); + expect(one.contains(' '), isFalse); + + // The range as the screen builds it: a dash joins two bound durations, so + // the dash is the only place a line may break and neither side can split. + final range = '${nbsp(hm(94))}–${nbsp(hm(154))}'; + final parts = range.split('–'); + expect(parts, hasLength(2)); + for (final part in parts) { + expect(part.contains(' '), isFalse, + reason: 'each duration in a range stays whole'); + } + + await c.setCode(null); + c.dispose(); + }); +} diff --git a/test/ui2_sleep_detail_test.dart b/test/ui2_sleep_detail_test.dart index 4de172bc7..ff0bd3949 100644 --- a/test/ui2_sleep_detail_test.dart +++ b/test/ui2_sleep_detail_test.dart @@ -162,7 +162,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(find.text('20m\u20132h\u00A020m'), 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 +196,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(find.text('44m\u20131h\u00A056m'), findsNWidgets(2)); }); testWidgets('the deep comparison stops asserting a difference', (t) async { From 61e42d9f063e18784c7c33b256f14418dcd1b84e Mon Sep 17 00:00:00 2001 From: uvtqt Date: Wed, 18 Nov 2026 16:04:56 +0100 Subject: [PATCH 2/2] sleep: keep a duration whole in CJK scripts too, not only at ASCII spaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit nbsp() replaced only spaces, so a zh range endpoint like 分钟 could still wrap between characters (#757 review). Glue every character with a word joiner (U+2060) instead — zero-width, renders unchanged, forbids a break at every boundary in any script; the dash between the two durations stays un-glued. Co-Authored-By: Claude Opus 4.8 --- lib/ui2/screens/home_screen.dart | 14 ++++++--- test/ui2_hm_nbsp_test.dart | 53 ++++++++++++++++++++++---------- test/ui2_sleep_detail_test.dart | 8 +++-- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/lib/ui2/screens/home_screen.dart b/lib/ui2/screens/home_screen.dart index 0406bc6bf..f423fd0d7 100644 --- a/lib/ui2/screens/home_screen.dart +++ b/lib/ui2/screens/home_screen.dart @@ -638,11 +638,15 @@ String hm(num? minutes) { ); } -/// Binds a value to its unit so the pair cannot wrap between them. 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. The dash joining the two durations stays a normal break. -String nbsp(String s) => s.replaceAll(' ', '\u00A0'); +/// 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) { diff --git a/test/ui2_hm_nbsp_test.dart b/test/ui2_hm_nbsp_test.dart index 1965c5f50..d1f734fd1 100644 --- a/test/ui2_hm_nbsp_test.dart +++ b/test/ui2_hm_nbsp_test.dart @@ -1,34 +1,55 @@ // A sleep stage duration is shown as a range, "1 Std. 34 Min.-2 Std. 54 Min.". -// Each side is one reading. Left with ordinary spaces it wrapped between a -// number and its unit, so the value broke before the final unit and dropped a -// lone "Min." onto its own right-aligned line (the shifted numbers in #… / -// the Sleep screen). `nbsp` binds each duration; only the dash may wrap. +// 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 range value only ever wraps at the dash between its two durations', - () async { + test('a duration never breaks internally, in Latin or CJK scripts', () async { SharedPreferences.setMockInitialValues({}); final c = LocaleController.seed('de'); c.useForPresentation(); - // Same visible text, no breakable space inside a single duration. final one = nbsp(hm(154)); // 2 h 34 min - expect(one.replaceAll(' ', ' '), '2 Std. 34 Min.'); - expect(one.contains(' '), isFalse); + 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(); - // The range as the screen builds it: a dash joins two bound durations, so - // the dash is the only place a line may break and neither side can split. final range = '${nbsp(hm(94))}–${nbsp(hm(154))}'; - final parts = range.split('–'); - expect(parts, hasLength(2)); - for (final part in parts) { - expect(part.contains(' '), isFalse, - reason: 'each duration in a range stays whole'); + // 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); diff --git a/test/ui2_sleep_detail_test.dart b/test/ui2_sleep_detail_test.dart index ff0bd3949..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\u20132h\u00A020m'), 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\u20131h\u00A056m'), findsNWidgets(2)); + expect(_rangeText('44m–1h 56m'), findsNWidgets(2)); }); testWidgets('the deep comparison stops asserting a difference', (t) async {