Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/genui/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`.
Expand Down
240 changes: 139 additions & 101 deletions packages/genui/lib/src/catalog/basic_catalog_widgets/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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;
Expand All @@ -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',
Expand All @@ -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);
},
);
Comment on lines 111 to +120

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

When imageData.description is null (which is the case for any image without a description), wrapping the image in a BoundString is unnecessary. This introduces redundant widget builds, state allocation, and listener setup. We can optimize this by checking if imageData.description is null and directly returning the built image without the BoundString wrapper.

    final description = imageData.description;
    if (description == null) {
      return _buildImage(itemContext, imageData, null);
    }

    return BoundString(
      dataContext: itemContext.dataContext,
      value: description,
      builder: (context, resolvedDescription) {
        final String? semanticLabel =
            resolvedDescription == null || resolvedDescription.isEmpty
                ? null
                : resolvedDescription;
        return _buildImage(itemContext, imageData, semanticLabel);
      },
    );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done. Most images have no description anyway, so that path skips the wrapper now.

},
);

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);
},
);
}
Loading
Loading