Repository navigation
feat: Add maxLines to TextBoxConfig - #4214
Conversation
ufrshubham
left a comment
There was a problem hiding this comment.
Thanks, this is nicely done. Truncating after line breaking is the right approach: the layout doesn't change, so the kept lines are exactly the first lines of the full wrap. I checked this with Chinese, Japanese and Korean text too, and wrapping is unchanged.
There's one thing to fix: ScrollTextBoxComponent ignores maxLines. It rebuilds its TextBoxConfig field by field, so the new option never reaches the inner text box. With maxLines: 2, I still got 21 lines. Since this file isn't in the diff, here's the change for packages/flame/lib/src/components/scroll_text_box_component.dart:
resolvedBoxConfig = TextBoxConfig(
timePerChar: resolvedBoxConfig.timePerChar,
dismissDelay: resolvedBoxConfig.dismissDelay,
growingBox: resolvedBoxConfig.growingBox,
maxLines: resolvedBoxConfig.maxLines,
maxWidth: size.x,
// ...
);Could you also add this test to scroll_text_box_component_test.dart? It fails without the change and passes with it:
testWithFlameGame('respects maxLines', (game) async {
final component = ScrollTextBoxComponent(
size: Vector2(100, 200),
text: 'The quick brown fox jumps over the lazy dog.',
boxConfig: const TextBoxConfig(maxLines: 2),
);
await game.ensureAdd(component);
await game.ready();
final inner = component
.descendants()
.whereType<TextBoxComponent>()
.single;
expect(inner.lines.length, 2);
});The failing analyze checks aren't caused by your PR. They come from a riverpod deprecation that we'll fix separately, so you'll just need to update your branch once that's merged.
ScrollTextBoxComponent rebuilds its TextBoxConfig field by field, so the new maxLines option never reached the inner text box.
|
Done in 37185d9, with your test added as written. It fails without the change and passes with it. Thanks for checking the CJK cases. |
Description
Adds an optional
maxLinestoTextBoxConfig, as discussed in #2279. Text that would take up more lines is cut off after the last allowed line. The default isnull, so existing boxes behave as before.TextPaintand renderers that use the space-based fallback, and hard line breaks count as lines.sizekeeps its size.maxLinesthroughboxConfigreflows the text, asmaxWidthdoes.totalCharTimeused the length of the full text, so a truncated box would keep waiting after its last visible character, andfinishedandonCompletewould come late. It now uses the length of the remaining lines when the text is truncated. Boxes that aren't truncated use the same calculation as before.maxLinesmust be greater than zero, because a box with no lines has nothing forcurrentLineand_recomputeSizeto work with.This only cuts the text off. It doesn't add an ellipsis, which seemed better kept to a separate change if you want one.
Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Closes #2279