Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Embedder API] Add semantic string attributes #44616

Merged
merged 4 commits into from
Aug 18, 2023

Conversation

loic-sharma
Copy link
Member

@loic-sharma loic-sharma commented Aug 11, 2023

Flutter's SemanticNodes use StringAttributes to provide additional information on text values for assistive technologies. This exposes the string attributes on the embedder API so that embedders can apply string attributes to their semantics trees.

Addresses flutter/flutter#119970
Part of flutter/flutter#98948

Previous pull request: #44553

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the Flutter Style Guide and the C++, Objective-C, Java style guides.
  • I listed at least one issue that this PR fixes in the description above.
  • I added new tests to check the change I am making or feature I am adding, or Hixie said the PR is test-exempt. See testing the engine for instructions on writing and running engine tests.
  • I updated/added relevant documentation (doc comments with ///).
  • I signed the CLA.
  • All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@loic-sharma loic-sharma force-pushed the embedder_string_attributes branch from a8229be to 80c9626 Compare August 11, 2023 00:44
@loic-sharma loic-sharma changed the title [Embedder] Refactor how semantic updates are mapped [Embedder API] Add semantic string attributes Aug 11, 2023
@loic-sharma loic-sharma force-pushed the embedder_string_attributes branch 2 times, most recently from f9e176a to 824339e Compare August 11, 2023 00:55
auto-submit bot pushed a commit that referenced this pull request Aug 11, 2023
This refactors how engine semantic updates are mapped to embedder semantic updates. There are no behavioral changes.

Part of flutter/flutter#119970, flutter/flutter#98948

Next PR: #44616

## Background
For flutter/flutter#119970, we will need to update the embedder API to add string attributes to semantic nodes' text values. There are multiple kinds of string attributes, and each text value can have multiple string attributes. This requires gnarly mapping code that's best kept out of `embedder.cc`.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
@loic-sharma loic-sharma force-pushed the embedder_string_attributes branch from f67d58b to 82743fe Compare August 11, 2023 16:25
@loic-sharma loic-sharma marked this pull request as ready for review August 11, 2023 16:26
// Indicates the assistive technology should announce out the string character
// by character.
//
// See dart:ui's SpellOutStringAttribute.
Copy link
Member Author

Choose a reason for hiding this comment

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

Mirrors:

/// A string attribute that causes the assistive technologies, e.g. VoiceOver,
/// to spell out the string character by character.
///
/// See also:
///
/// * [AttributedString], where the string attributes are used.
/// * [LocaleStringAttribute], which causes the assistive technologies to
/// treat the string in the specific language.
base class SpellOutStringAttribute extends StringAttribute {

// Indicates the assistive technology should announce the string using the
// specified locale.
//
// See dart:ui's LocaleStringAttribute.
Copy link
Member Author

Choose a reason for hiding this comment

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

Mirrors:

/// A string attribute that causes the assistive technologies, e.g. VoiceOver,
/// to treat string as a certain language.
///
/// See also:
///
/// * [AttributedString], where the string attributes are used.
/// * [SpellOutStringAttribute], which causes the assistive technologies to
/// spell out the string character by character when announcing the string.
base class LocaleStringAttribute extends StringAttribute {

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

@yaakovschectman yaakovschectman left a comment

Choose a reason for hiding this comment

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

Looks good now that the first PR is in

@loic-sharma loic-sharma force-pushed the embedder_string_attributes branch from f69621a to dc9dc1a Compare August 14, 2023 19:08
@@ -58,13 +58,23 @@ class EmbedderSemanticsUpdate2 {
std::vector<FlutterSemanticsCustomAction2> actions_;
std::vector<FlutterSemanticsCustomAction2*> action_pointers_;

std::vector<std::unique_ptr<std::vector<FlutterStringAttribute*>>>
Copy link
Member

Choose a reason for hiding this comment

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

I am no longer super familiar with this bit of the codebase. But can you conform that the underlying nodes array is never modified after the vector of pointers is constructed? This will invalidate the old pointers.

Copy link
Member Author

@loic-sharma loic-sharma Aug 14, 2023

Choose a reason for hiding this comment

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

But can you conform that the underlying nodes array is never modified after the vector of pointers is constructed?

Yup, confirmed. Pointers to T are captured only if:

  1. T is owned by a std::unique_ptr<T>, or...
  2. T is owned by a std::vector<T> that is fully constructed, or...
  3. T is owned by a std::vector<std::unique_ptr<T>>. The std::unique_ptr makes the pointer to T stable even if the vector's data is reallocated.

I mirrored the approach taken for the array of layers:

std::vector<std::unique_ptr<FlutterPlatformView>> platform_views_referenced_;
std::vector<std::unique_ptr<FlutterPlatformViewMutation>>
mutations_referenced_;
std::vector<std::unique_ptr<std::vector<const FlutterPlatformViewMutation*>>>
mutations_arrays_referenced_;
std::vector<FlutterLayer> presented_layers_;

Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

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

Not done reviewing yet -- still thinking about the CreateStringAttributes method.

Copy link
Member

@cbracken cbracken left a comment

Choose a reason for hiding this comment

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

lgtm - just a few comments/suggestions.

node_string_attributes_;
std::vector<std::unique_ptr<FlutterStringAttribute>> string_attributes_;
std::vector<std::unique_ptr<FlutterLocaleStringAttribute>> locale_attributes_;
std::unique_ptr<FlutterSpellOutStringAttribute> spell_out_attribute_;
Copy link
Member

Choose a reason for hiding this comment

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

I think it's worth adding comments to these for the benefit of future us.

Basically something to the effect that these are buffers for holding temporary embedder-specific objects we create from their internal representation and pass back to the embedder in the semantics update callback in order to manage their lifetimes such that the objects are valid for the duration of the callback, and are automatically cleaned up when the update itself is dealloc'ed after the callback finishes.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added this comment to both the struct declaration and these field declarations.

switch (attribute->type) {
case StringAttributeType::kLocale: {
std::shared_ptr<flutter::LocaleStringAttribute> locale_attribute =
std::static_pointer_cast<flutter::LocaleStringAttribute>(attribute);
Copy link
Member

Choose a reason for hiding this comment

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

Alright - looking at string_attribute.h:15-16 and string_attribute.h:49-51, I see why where the shared_ptrs are coming from. I haven't looked into that code do see why they're necessary, but I'll take it on faith that they are :)

auto embedder_locale = std::make_unique<FlutterLocaleStringAttribute>();
embedder_locale->struct_size = sizeof(FlutterLocaleStringAttribute);
embedder_locale->locale = locale_attribute->locale.c_str();
locale_attributes_.push_back(std::move(embedder_locale));
Copy link
Member

Choose a reason for hiding this comment

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

Can we write this as:

locale_attributes_.emplace_back({fields, in, declaration, order});

That said, I'd have to look it up; struct initializer support without an explicit constructor may only have been added in C++20. I feel like I may have had the same discussion with knopp recently.

Similar below.

Copy link
Member Author

@loic-sharma loic-sharma Aug 18, 2023

Choose a reason for hiding this comment

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

Sadly, our compiler doesn't compile this without an explicit constructor on the struct :(

@loic-sharma loic-sharma added the autosubmit Merge PR when tree becomes green via auto submit App label Aug 18, 2023
@auto-submit auto-submit bot merged commit 7ba90e6 into flutter:main Aug 18, 2023
@loic-sharma loic-sharma deleted the embedder_string_attributes branch August 18, 2023 22:57
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Aug 20, 2023
fluttermirroringbot pushed a commit to flutter/flutter that referenced this pull request Aug 20, 2023
…132914)

flutter/engine@f4bffdc...2dcfb6c

2023-08-20 [email protected] Roll Skia from e9cf3f1740eb to 4f6b9d08b6d1 (2 revisions) (flutter/engine#44868)
2023-08-20 [email protected] Revert "Implementing TextScaler for nonlinear text scaling" (flutter/engine#44882)
2023-08-18 [email protected] [Embedder API] Add semantic string attributes (flutter/engine#44616)
2023-08-18 [email protected] Implementing TextScaler for nonlinear text scaling (flutter/engine#42062)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-engine-flutter-autoroll
Please CC [email protected],[email protected],[email protected] on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://bugs.chromium.org/p/skia/issues/entry?template=Autoroller+Bug

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
gaaclarke pushed a commit to gaaclarke/engine that referenced this pull request Aug 30, 2023
This refactors how engine semantic updates are mapped to embedder semantic updates. There are no behavioral changes.

Part of flutter/flutter#119970, flutter/flutter#98948

Next PR: flutter#44616

## Background
For flutter/flutter#119970, we will need to update the embedder API to add string attributes to semantic nodes' text values. There are multiple kinds of string attributes, and each text value can have multiple string attributes. This requires gnarly mapping code that's best kept out of `embedder.cc`.

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
gaaclarke pushed a commit to gaaclarke/engine that referenced this pull request Aug 30, 2023
Flutter's `SemanticNode`s use [`StringAttribute`](https://api.flutter.dev/flutter/dart-ui/StringAttribute-class.html)s to provide additional information on text values for assistive technologies. This exposes the string attributes on the embedder API so that embedders can apply string attributes to their semantics trees.

Addresses flutter/flutter#119970
Part of flutter/flutter#98948

Previous pull request: flutter#44553

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
autosubmit Merge PR when tree becomes green via auto submit App
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants