-
-
Notifications
You must be signed in to change notification settings - Fork 899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: Use TextElements within the TextComponent #1802
Conversation
|
||
@internal | ||
void updateBounds() { | ||
final expectedSize = textRenderer.measureText(_text); | ||
size.setValues(expectedSize.x, expectedSize.y); | ||
if (_textRenderer is FormatterTextRenderer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we put this logic inside FormatterTextRenderer
somehow?
It doens't seems right to put logic of a specific renderer on the TextComponent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have 3 TextRenderer
implementations right now, and I had an idea for a fourth, but all of them are FormatterTextRenderer
s. Ideally, we would merge FormatterTextRenderer
API into the TextRenderer
, but that would be a breaking change for anyone who implements their own TextRenderer
.
I would argue in favor of merging those two classes anyways, if the team agrees. But that ought to be in a separate PR, at which point we can simply remove the "else" part of this if-statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I vote for merging them too, I don't think there are many (if anyone at all) that has made their own TextRenderer
implementation.
|
||
String get text => _text; | ||
|
||
String _text; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
String _text;
String get text => _text;
Normally we have the private at the top and the getter/setter below it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't that style become weird when adding dartdocs to the getter though? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I can see, there is no Dart or Flutter official recommendations about this. The style that I'm currently using is taken from the example here: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#perform-dirty-checks-in-setters.
In practice, Flutter repo uses several styles: either
/// The parent of this node in the tree.
AbstractNode? get parent => _parent;
AbstractNode? _parent;
/// The current value stored in this notifier.
///
/// When the value is replaced with something that is not equal to the old
/// value as evaluated by the equality operator ==, this class notifies its
/// listeners.
@override
T get value => _value;
T _value;
set value(T newValue) {
if (_value == newValue)
return;
_value = newValue;
notifyListeners();
}
or
/// Current code point.
///
/// If the iterator has hit either end, the [_currentCodePoint] is -1
/// and `_position == _nextPosition`.
int _currentCodePoint = -1;
...
/// The rune (integer Unicode code point) starting at the current position in
/// the string.
///
/// The value is -1 if there is no current code point.
int get current => _currentCodePoint;
I guess we just need to decide which style we like more, and put this into our own style guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the first one, since we usually don't add dartdocs to both the public facing getter and the underlying value.
Description
TextElement
is a class that caches the layout of a particular string of text. When drawing any text on the screen, it is important to convert it to text-elements and then cache those, especially if there are many text fragments being displayed simultaneously.This PR changes the
TextComponent
class so that it computes and stores aTextElement
instead of usingTextRenderer.render()
directly. This improves the performance of a game, as it avoids costly recalculations of the text layout on every game tick.Checklist
fix:
,feat:
,docs:
etc).docs
and added dartdoc comments with///
.examples
.Breaking Change
Related Issues
Closes #1801