diff --git a/packages/genui/CHANGELOG.md b/packages/genui/CHANGELOG.md index c10b17b25..983c9270a 100644 --- a/packages/genui/CHANGELOG.md +++ b/packages/genui/CHANGELOG.md @@ -1,5 +1,15 @@ # [genui](https://pub.dev/packages/genui) Changelog +## (WIP) + +- **Fix**: `Image` accepts the catalog's `description` and passes it to Flutter + as `semanticLabel`. The basic catalog declares it as the accessibility text + for an image, and every other first-party renderer applies it, but genui's + schema left it out and built `Image.network` / `Image.asset` without a label, + so an image was the one component whose whole accessible name comes from the + agent and the one component that dropped it. It is a `DynamicString`, so a + data binding works the same as a literal. + ## 0.10.3 - Updated `basicCatalogId` to point to the canonical URL: `https://a2ui.org/specification/v0_9/catalogs/basic/catalog.json`. diff --git a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart index da9829cdd..a96813d5e 100644 --- a/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart +++ b/packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart @@ -17,6 +17,9 @@ Schema _schema() { description: 'Asset path (e.g. assets/...) or network URL (e.g. https://...)', ), + 'description': A2uiSchemas.stringReference( + description: 'Accessibility text for the image.', + ), 'fit': S.string( description: 'How the image should be inscribed into the box.', enumValues: BoxFit.values.map((e) => e.name).toList(), @@ -47,10 +50,20 @@ Schema _schema() { } extension type _ImageData.fromMap(JsonMap _json) { - factory _ImageData({required JsonMap url, String? fit, String? variant}) => - _ImageData.fromMap({'url': url, 'fit': fit, 'variant': variant}); + factory _ImageData({ + required JsonMap url, + Object? description, + String? fit, + String? variant, + }) => _ImageData.fromMap({ + 'url': url, + 'description': description, + 'fit': fit, + 'variant': variant, + }); Object get url => _json['url'] as Object; + Object? get description => _json['description']; BoxFit? get fit => _json['fit'] != null ? BoxFit.values.firstWhere((e) => e.name == _json['fit'] as String) : null; @@ -65,6 +78,8 @@ extension type _ImageData.fromMap(JsonMap _json) { /// /// - `url`: The URL of the image to display. Can be a network URL or a local /// asset path. +/// - `description`: Accessibility text for the image, announced by a screen +/// reader in place of the image itself. /// - `fit`: How the image should be inscribed into the box. See [BoxFit] for /// possible values. /// - `variant`: A usage hint for the image size and style. One of 'icon', @@ -89,109 +104,132 @@ final CatalogItem image = CatalogItem( widgetBuilder: (itemContext) { final imageData = _ImageData.fromMap(itemContext.data as JsonMap); + // Most images carry no description, and that path needs no binding. + final Object? description = imageData.description; + if (description == null) return _buildImage(itemContext, imageData, null); + return BoundString( dataContext: itemContext.dataContext, - value: imageData.url, - builder: (context, value) { - if (value == null || value.isEmpty) { - genUiLogger.warning( - 'Image widget created with no URL at path: ' - '${itemContext.dataContext.path}', - ); - return const SizedBox.shrink(); - } + value: description, + builder: (context, resolved) { + final String? semanticLabel = resolved == null || resolved.isEmpty + ? null + : resolved; + return _buildImage(itemContext, imageData, semanticLabel); + }, + ); + }, +); - Widget child; - if (value.startsWith('http')) { - child = Image.network( - value, - fit: imageData.fit, - frameBuilder: - ( - BuildContext context, - Widget child, - int? frame, - bool wasSynchronouslyLoaded, - ) { - if (wasSynchronouslyLoaded) { - return child; - } - return AnimatedOpacity( - opacity: frame == null ? 0 : 1, - duration: const Duration(seconds: 1), - curve: Curves.easeOut, - child: child, - ); - }, - loadingBuilder: - ( - BuildContext context, - Widget child, - ImageChunkEvent? loadingProgress, - ) { - if (loadingProgress == null) { - return child; - } - return Center( - child: CircularProgressIndicator( - value: loadingProgress.expectedTotalBytes != null - ? loadingProgress.cumulativeBytesLoaded / - loadingProgress.expectedTotalBytes! - : null, - ), - ); - }, - errorBuilder: - (BuildContext context, Object error, StackTrace? stackTrace) { - return const Icon(Icons.broken_image); - }, - ); - } else { - child = Image.asset( - value, - fit: imageData.fit, - frameBuilder: - ( - BuildContext context, - Widget child, - int? frame, - bool wasSynchronouslyLoaded, - ) { - if (wasSynchronouslyLoaded) { - return child; - } - return AnimatedOpacity( - opacity: frame == null ? 0 : 1, - duration: const Duration(seconds: 1), - curve: Curves.easeOut, - child: child, - ); - }, - errorBuilder: - (BuildContext context, Object error, StackTrace? stackTrace) { - return const Icon(Icons.broken_image); - }, - ); - } +Widget _buildImage( + CatalogItemContext itemContext, + _ImageData imageData, + String? semanticLabel, +) { + return BoundString( + dataContext: itemContext.dataContext, + value: imageData.url, + builder: (context, value) { + if (value == null || value.isEmpty) { + genUiLogger.warning( + 'Image widget created with no URL at path: ' + '${itemContext.dataContext.path}', + ); + return const SizedBox.shrink(); + } - if (imageData.variant == 'avatar') { - child = CircleAvatar(child: child); - } + Widget child; + if (value.startsWith('http')) { + child = Image.network( + value, + fit: imageData.fit, + semanticLabel: semanticLabel, + frameBuilder: + ( + BuildContext context, + Widget child, + int? frame, + bool wasSynchronouslyLoaded, + ) { + if (wasSynchronouslyLoaded) { + return child; + } + return AnimatedOpacity( + opacity: frame == null ? 0 : 1, + duration: const Duration(seconds: 1), + curve: Curves.easeOut, + child: child, + ); + }, + loadingBuilder: + ( + BuildContext context, + Widget child, + ImageChunkEvent? loadingProgress, + ) { + if (loadingProgress == null) { + return child; + } + return Center( + child: CircularProgressIndicator( + value: loadingProgress.expectedTotalBytes != null + ? loadingProgress.cumulativeBytesLoaded / + loadingProgress.expectedTotalBytes! + : null, + ), + ); + }, + errorBuilder: + (BuildContext context, Object error, StackTrace? stackTrace) { + return const Icon(Icons.broken_image); + }, + ); + } else { + child = Image.asset( + value, + fit: imageData.fit, + semanticLabel: semanticLabel, + frameBuilder: + ( + BuildContext context, + Widget child, + int? frame, + bool wasSynchronouslyLoaded, + ) { + if (wasSynchronouslyLoaded) { + return child; + } + return AnimatedOpacity( + opacity: frame == null ? 0 : 1, + duration: const Duration(seconds: 1), + curve: Curves.easeOut, + child: child, + ); + }, + errorBuilder: + (BuildContext context, Object error, StackTrace? stackTrace) { + return const Icon(Icons.broken_image); + }, + ); + } - if (imageData.variant == 'header') { - return SizedBox(width: double.infinity, child: child); - } + if (imageData.variant == 'avatar') { + child = CircleAvatar(child: child); + } - final double size = switch (imageData.variant) { - 'icon' || 'avatar' => 32.0, - 'smallFeature' => 50.0, - 'mediumFeature' => 150.0, - 'largeFeature' => 400.0, - _ => 150.0, - }; + if (imageData.variant == 'header') { + return SizedBox(width: double.infinity, child: child); + } - return SizedBox(width: size, height: size, child: child); - }, - ); - }, -); + final double size = switch (imageData.variant) { + 'icon' || 'avatar' => 32.0, + 'smallFeature' => 50.0, + 'mediumFeature' => 150.0, + 'largeFeature' => 400.0, + _ => 150.0, + }; + + return SizedBox(width: size, height: size, child: child); + }, + ); +} diff --git a/packages/genui/test/catalog/core_widgets/image_test.dart b/packages/genui/test/catalog/core_widgets/image_test.dart index f82e2bd4e..0a181e2a6 100644 --- a/packages/genui/test/catalog/core_widgets/image_test.dart +++ b/packages/genui/test/catalog/core_widgets/image_test.dart @@ -118,6 +118,120 @@ void main() { debugNetworkImageHttpClientProvider = null; } }); + + testWidgets('the catalog description names the image', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface([ + component( + id: 'root', + type: 'Image', + properties: { + 'url': 'https://example.com/image.png', + 'description': 'A golden retriever puppy', + }, + ), + ]), + ); + await tester.pumpAndSettle(); + + expect( + tester.widget(find.byType(Image)).semanticLabel, + 'A golden retriever puppy', + ); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); + + testWidgets('the description may come from the data model', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface( + [ + component( + id: 'root', + type: 'Image', + properties: { + 'url': 'https://example.com/image.png', + 'description': {'path': '/alt'}, + }, + ), + ], + data: {'alt': 'The chart for this quarter'}, + ), + ); + await tester.pumpAndSettle(); + + expect( + tester.widget(find.byType(Image)).semanticLabel, + 'The chart for this quarter', + ); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); + + testWidgets('an image with no description is left as it was', ( + WidgetTester tester, + ) async { + debugNetworkImageHttpClientProvider = _FakeSuccessHttpClient.new; + try { + await HttpOverrides.runZoned(() async { + await tester.pumpWidget( + _surface([ + component( + id: 'root', + type: 'Image', + properties: {'url': 'https://example.com/image.png'}, + ), + ]), + ); + await tester.pumpAndSettle(); + + expect(tester.widget(find.byType(Image)).semanticLabel, isNull); + }); + } finally { + debugNetworkImageHttpClientProvider = null; + } + }); +} + +/// Renders [components] on one surface built from the image catalog item. +Widget _surface(List components, {JsonMap? data}) { + final surfaceController = SurfaceController( + catalogs: [ + Catalog([BasicCatalogItems.image], catalogId: 'test_catalog'), + ], + ); + addTearDown(surfaceController.dispose); + const surfaceId = 'testSurface'; + surfaceController.handleMessage( + updateComponents(surfaceId: surfaceId, components: components), + ); + surfaceController.handleMessage( + createSurface(surfaceId: surfaceId, catalogId: 'test_catalog'), + ); + if (data != null) { + surfaceController.handleMessage( + updateDataModel(surfaceId: surfaceId, value: data), + ); + } + return MaterialApp( + home: Scaffold( + body: Surface(surfaceContext: surfaceController.contextFor(surfaceId)), + ), + ); } class _FakeSuccessHttpClient extends Fake implements HttpClient {