diff --git a/packages/genui/CHANGELOG.md b/packages/genui/CHANGELOG.md index 72880c452..78377a33d 100644 --- a/packages/genui/CHANGELOG.md +++ b/packages/genui/CHANGELOG.md @@ -7,6 +7,10 @@ - Remove invalid `'functionCall'` envelope from `checksToExpression` and support tolerant unwrapping in `_evaluateStream`. - Align truthiness evaluation in `isTruthy` across primitives, data model objects, and structured `ValidationResult` objects, while warning on malformed expressions or missing functions. - Migrate `CheckBox`, `Slider`, and `ChoicePicker` to `ValidationHelper.validateStream` to evaluate rules individually and display authored error messages. +- Fixed `Slider` deriving `divisions` from range, allowing continuous sliding + by default (`divisions: null`), preventing assertion crashes on sub-unit + ranges, fixing thumb value text formatting and literal value fallback, and + handling inverted ranges (`max < min`). ## 0.10.4 diff --git a/packages/genui/lib/src/catalog/basic_catalog_widgets/slider.dart b/packages/genui/lib/src/catalog/basic_catalog_widgets/slider.dart index 319374a33..7e5c69305 100644 --- a/packages/genui/lib/src/catalog/basic_catalog_widgets/slider.dart +++ b/packages/genui/lib/src/catalog/basic_catalog_widgets/slider.dart @@ -54,6 +54,15 @@ extension type _SliderData.fromMap(JsonMap _json) { } } +String _formatSliderValue(num val) { + if (val == val.roundToDouble()) { + return val.toInt().toString(); + } + final String fixed = val.toStringAsFixed(6); + final String trimmed = fixed.replaceAll(RegExp(r'\.?0+$'), ''); + return trimmed.isEmpty ? '0' : trimmed; +} + /// A Material Design slider. /// /// This widget allows the user to select a value from a range by sliding a @@ -89,6 +98,13 @@ final slider = CatalogItem( } } + final double min = sliderData.min; + final double max = sliderData.max < min ? min : sliderData.max; + final double currentVal = (effectiveValue ?? min).toDouble().clamp( + min, + max, + ); + final Widget sliderWidget = Padding( padding: const EdgeInsetsDirectional.only(end: 16.0), child: Row( @@ -96,18 +112,15 @@ final slider = CatalogItem( children: [ Expanded( child: Slider( - value: (effectiveValue ?? sliderData.min).toDouble(), - min: sliderData.min, - max: sliderData.max, - divisions: (sliderData.max - sliderData.min).toInt(), + value: currentVal, + min: min, + max: max, onChanged: (newValue) { itemContext.dataContext.update(DataPath(path), newValue); }, ), ), - Text( - value?.toStringAsFixed(0) ?? sliderData.min.toStringAsFixed(0), - ), + Text(_formatSliderValue(currentVal)), ], ), ); diff --git a/packages/genui/test/catalog/core_widgets/slider_test.dart b/packages/genui/test/catalog/core_widgets/slider_test.dart index 97b39136c..1b88d5c0e 100644 --- a/packages/genui/test/catalog/core_widgets/slider_test.dart +++ b/packages/genui/test/catalog/core_widgets/slider_test.dart @@ -8,44 +8,57 @@ import 'package:genui/genui.dart'; import '../../test_infra/message_builders.dart'; +Future _pumpSlider( + WidgetTester tester, { + Map properties = const {}, + double? initialModelValue, +}) async { + final surfaceController = SurfaceController( + catalogs: [ + Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'), + ], + ); + const surfaceId = 'testSurface'; + final List components = [ + component(id: 'root', type: 'Slider', properties: properties), + ]; + surfaceController.handleMessage( + updateComponents(surfaceId: surfaceId, components: components), + ); + surfaceController.handleMessage( + createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), + ); + if (initialModelValue != null && properties['value'] is Map) { + final path = (properties['value'] as Map)['path'] as String?; + if (path != null) { + surfaceController + .contextFor(surfaceId) + .dataModel + .update(DataPath(path), initialModelValue); + } + } + + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: Surface(surfaceContext: surfaceController.contextFor(surfaceId)), + ), + ), + ); + await tester.pumpAndSettle(); + return surfaceController; +} + void main() { testWidgets('Slider widget renders and handles changes', ( WidgetTester tester, ) async { - final surfaceController = SurfaceController( - catalogs: [ - Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'), - ], - ); - const surfaceId = 'testSurface'; - final List components = [ - component( - id: 'root', - type: 'Slider', - properties: { - 'value': {'path': '/myValue'}, - }, - ), - ]; - surfaceController.handleMessage( - updateComponents(surfaceId: surfaceId, components: components), - ); - surfaceController.handleMessage( - createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), - ); - surfaceController - .contextFor(surfaceId) - .dataModel - .update(DataPath('/myValue'), 0.5); - - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Surface( - surfaceContext: surfaceController.contextFor(surfaceId), - ), - ), - ), + final SurfaceController surfaceController = await _pumpSlider( + tester, + properties: { + 'value': {'path': '/myValue'}, + }, + initialModelValue: 0.5, ); final Slider slider = tester.widget(find.byType(Slider)); @@ -54,7 +67,7 @@ void main() { await tester.drag(find.byType(Slider), const Offset(100, 0)); expect( surfaceController - .contextFor(surfaceId) + .contextFor('testSurface') .dataModel .getValue(DataPath('/myValue')), greaterThan(0.5), @@ -62,41 +75,13 @@ void main() { }); testWidgets('Slider widget renders label', (WidgetTester tester) async { - final surfaceController = SurfaceController( - catalogs: [ - Catalog([BasicCatalogItems.slider], catalogId: 'test_catalog'), - ], - ); - const surfaceId = 'testSurface'; - final List components = [ - component( - id: 'root', - type: 'Slider', - properties: { - 'value': {'path': '/myValue'}, - 'label': 'Volume', - }, - ), - ]; - surfaceController.handleMessage( - updateComponents(surfaceId: surfaceId, components: components), - ); - surfaceController.handleMessage( - createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), - ); - surfaceController - .contextFor(surfaceId) - .dataModel - .update(DataPath('/myValue'), 0.5); - - await tester.pumpWidget( - MaterialApp( - home: Scaffold( - body: Surface( - surfaceContext: surfaceController.contextFor(surfaceId), - ), - ), - ), + await _pumpSlider( + tester, + properties: { + 'value': {'path': '/myValue'}, + 'label': 'Volume', + }, + initialModelValue: 0.5, ); expect(find.text('Volume'), findsOneWidget); @@ -164,4 +149,122 @@ void main() { expect(find.text('Must be at least 0.5'), findsNothing); }); + + testWidgets('Slider widget is continuous (divisions is null) by default', ( + WidgetTester tester, + ) async { + await _pumpSlider( + tester, + properties: { + 'value': {'path': '/myValue'}, + }, + initialModelValue: 0.5, + ); + + final Slider slider = tester.widget(find.byType(Slider)); + expect(slider.divisions, isNull); + expect(slider.min, 0.0); + expect(slider.max, 1.0); + expect(slider.value, 0.5); + }); + + testWidgets('Slider widget does not crash on sub-unit ranges', ( + WidgetTester tester, + ) async { + await _pumpSlider( + tester, + properties: { + 'value': {'path': '/myValue'}, + 'min': 0.0, + 'max': 0.5, + }, + initialModelValue: 0.2, + ); + + expect(tester.takeException(), isNull); + final Slider slider = tester.widget(find.byType(Slider)); + expect(slider.divisions, isNull); + expect(slider.min, 0.0); + expect(slider.max, 0.5); + expect(slider.value, 0.2); + }); + + testWidgets( + 'Slider widget displays literal value without data model binding', + (WidgetTester tester) async { + await _pumpSlider( + tester, + properties: {'value': 0.5, 'min': 0.0, 'max': 1.0}, + ); + + final Slider slider = tester.widget(find.byType(Slider)); + expect(slider.value, 0.5); + expect(find.text('0.5'), findsOneWidget); + }, + ); + + testWidgets( + 'Slider widget formats whole numbers and decimal numbers correctly', + (WidgetTester tester) async { + final SurfaceController controller = await _pumpSlider( + tester, + properties: { + 'value': {'path': '/myValue'}, + 'min': 0.0, + 'max': 100.0, + }, + initialModelValue: 10.0, + ); + + // Whole numbers should not display trailing .0 + expect(find.text('10'), findsOneWidget); + + // Decimal numbers should preserve fractional part without trailing zeroes + controller + .contextFor('testSurface') + .dataModel + .update(DataPath('/myValue'), 0.25); + await tester.pumpAndSettle(); + expect(find.text('0.25'), findsOneWidget); + + // Sub-unit / high precision values up to 6 decimals should be preserved + controller + .contextFor('testSurface') + .dataModel + .update(DataPath('/myValue'), 0.005); + await tester.pumpAndSettle(); + expect(find.text('0.005'), findsOneWidget); + }, + ); + + testWidgets( + 'Slider widget clamps label display when initial value is out of bounds', + (WidgetTester tester) async { + await _pumpSlider( + tester, + properties: {'value': -5.0, 'min': 0.0, 'max': 10.0}, + ); + + final Slider slider = tester.widget(find.byType(Slider)); + expect(slider.value, 0.0); + expect(find.text('0'), findsOneWidget); + }, + ); + + testWidgets( + 'Slider widget handles inverted range (max < min) by clamping to min', + (WidgetTester tester) async { + await _pumpSlider( + tester, + properties: {'value': 5.0, 'min': 10.0, 'max': 0.0}, + ); + + expect(tester.takeException(), isNull); + final Slider slider = tester.widget(find.byType(Slider)); + expect(slider.min, 10.0); + expect(slider.max, 10.0); + expect(slider.value, 10.0); + expect(find.text('10'), findsOneWidget); + }, + ); }