Skip to content

Conversation

@LongCatIsLooong
Copy link
Contributor

@LongCatIsLooong LongCatIsLooong commented Apr 27, 2023

I'd like to find out the fontSize of a PlaceholderSpan, and currently there doesn't seem to be a way to do TextStyle cascading inheritance in the framework:

InlineSpan.visitChildren traverses the entire InlineSpan tree using a preorder traversal, and nodes that don't have "content" will be skipped (https://master-api.flutter.dev/flutter/painting/InlineSpan/visitChildren.html):

Walks this InlineSpan and any descendants in pre-order and calls visitor for each span that has content.

which makes it impossible to do TextStyle inheritance in the framework:

  • InlineSpans with a non-null TextStyle but has no content will be skipped
  • visitChildren doesn't directly expose the hierarchy, it only gives information about the flattened tree.

This doesn't look like a breaking change, most internal customers are extending WidgetSpan which has a concrete implementation of the new method.

Alternatively I could create a fake ui.ParagraphBuilder and record the ui.TextStyle at the top of the stack when addPlaceholder is called. But ui.TextStyle properties are not exposed to the framework.

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, including Features we expect every widget to implement.
  • I signed the CLA.
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

@flutter-dashboard flutter-dashboard bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. labels Apr 27, 2023
@LongCatIsLooong LongCatIsLooong marked this pull request as ready for review April 28, 2023 02:01
Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

It's too bad that the semantics of InlineSpan.visitChildren is different from most other visitChildren, which do not automatically traverse... Oh well.

/// `visitor` callback returns `false` on an immediate child. This method
/// itself returns a `bool` indicating whether the visitor callback returned
/// `true` on all immediate children.
bool visitDirectChildren(InlineSpanVisitor visitor);
Copy link
Member

Choose a reason for hiding this comment

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

Can you link the docs of visitDirectChildren and visitChildren which a one-sentence summary of what the difference is?

return 1.0;
}
}
return switch ((textAlign, textDirection)) {
Copy link
Member

Choose a reason for hiding this comment

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

nice usage of new language features!

builder.pushStyle(style!.getTextStyle(textScaleFactor: textScaleFactor));
}
if (text != null) {
if (text case final String text) {
Copy link
Member

Choose a reason for hiding this comment

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

IMO, text != null was much clearer here before and easier to read (even if it means to do an extra null check, if you really want to avoid that, I would go with a local variable instead)

Copy link
Member

Choose a reason for hiding this comment

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

Same below in other places in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wonder what makes the optional binding form harder to read? Was it the lack of syntax highlighting on github?

or would it be cleaner if it's with a ? or when the type annotation gets omitted?

  if (text case final text?) {

or

  if (text case final nonNullText?) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh the keywords are highlighted.

Comment on lines 313 to 314
return (text == null || visitor(this))
&& !(children?.any((InlineSpan descendant) => !descendant.visitChildren(visitor)) ?? false);
Copy link
Member

Choose a reason for hiding this comment

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

The old code is way more readable here and it also made the intend clearer.

Copy link
Member

Choose a reason for hiding this comment

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

(the first two ifs in the old code could probably be merged, though)

Comment on lines 318 to 319
bool visitDirectChildren(InlineSpanVisitor visitor) => !(children?.any((InlineSpan child) => !visitor(child)) ?? false);

Copy link
Member

Choose a reason for hiding this comment

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

Same, this doesn't seem to be the most readable way of expressing the logic.

@LongCatIsLooong LongCatIsLooong force-pushed the TextSpanVisitChildren-fix branch from 5ad4d0b to a662a8c Compare May 3, 2023 00:32
Copy link
Member

@goderbauer goderbauer left a comment

Choose a reason for hiding this comment

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

LGTM

@LongCatIsLooong LongCatIsLooong added the autosubmit Merge PR when tree becomes green via auto submit App label May 3, 2023
@auto-submit auto-submit bot merged commit f704c68 into flutter:master May 3, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
@LongCatIsLooong LongCatIsLooong deleted the TextSpanVisitChildren-fix branch May 4, 2023 04:14
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 16, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems autosubmit Merge PR when tree becomes green via auto submit App framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants