Skip to content
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

Introduce auto sizing for all app bar title components #891

Conversation

mannodermaus
Copy link
Contributor

@mannodermaus mannodermaus commented Aug 30, 2024

Issue

Overview (Required)

  • Introduce AutoSizeText, a component that automatically finds the correct size to fit a text into a box
    • The maximum font size is defined by the style of the text, but it will shrink if necessary
    • It uses a binary search to find the biggest size that fits in a given number of lines
  • Apply it to all titles in every top app bar of the app
    • The custom app bar of TimetableScreen
    • AnimatedTextTopAppBar (fix for the main tabs on the home screen)
    • AnimatedMediumTopAppBar (fix for contributor, sponsor, staff, settings screens)

Movie (Optional)

Before

Tabs (Large Font) Tabs (Normal Font) Contributor Screen
before_1725061103.mp4
before2_1725061142.mp4
before3_1725061201.mp4

After

Tabs (Large Font) Tabs (Normal Font) Contributor Screen
after_1725060593.mp4
after2_1725060638.mp4
after3_1725060970.mp4

@mannodermaus mannodermaus force-pushed the fix/top-bar-text-break-in-large-font-size branch from f0f32a0 to f15a996 Compare August 30, 2024 23:50
Copy link

Detekt check failed. Please run ./gradlew detekt --auto-correct to fix the issues.

@github-actions github-actions bot temporarily deployed to deploygate-distribution August 30, 2024 23:54 Inactive
@mannodermaus mannodermaus force-pushed the fix/top-bar-text-break-in-large-font-size branch from f15a996 to 596faa8 Compare August 30, 2024 23:55
@github-actions github-actions bot temporarily deployed to deploygate-distribution August 30, 2024 23:59 Inactive
@mannodermaus mannodermaus marked this pull request as ready for review August 31, 2024 00:12
* of the text size, use [TextUnit.Unspecified] for that parameter.)
*
* Adapted from Alchitry on Stack Overflow, licensed under CC BY-SA 4.0:
* https://stackoverflow.com/a/71416530
Copy link
Member

@takahirom takahirom Sep 1, 2024

Choose a reason for hiding this comment

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

It's tough to know if this is safe. I think we might have to publish our app code under CC BY-SA 4.0 as well, which makes using our app's code difficult.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. I can reimplement the same solution by hand, but with a different API. Would you prefer that, or shall we skip the adjustments for bigger font sizes?

Copy link
Member

Choose a reason for hiding this comment

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

We might need to reimplement this. I wish we had a library for it. 😅
If there isn't a licensing issue, I'll leave it up to you to implement this, as you are reliable.

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 reimplemented AutoSizeText with the same idea, but without コピペ, in 24a66c0

@takahirom
Copy link
Member

We introduced the MediumTopAppBar. Does it affect this PR?
#753

@github-actions github-actions bot temporarily deployed to deploygate-distribution September 2, 2024 11:58 Inactive
@mannodermaus
Copy link
Contributor Author

Yes, I believe it does. Let me rebase and do another pass on this!

@mannodermaus
Copy link
Contributor Author

@takahirom Adjusted the MediumTopAppBar in 0eae2c3.

Normal (no change) Large
Before
After

@github-actions github-actions bot temporarily deployed to deploygate-distribution September 2, 2024 12:15 Inactive
* with the upper bound being defined by the font size of its [style].
*/
@Composable
fun AutoSizeText(
Copy link
Member

Choose a reason for hiding this comment

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

Looks great!


// Keep decreasing the font size until the text fits in the box without exceeding the max lines
while (paragraph.didExceedMaxLines || maxHeight < paragraph.height.toDp() || maxWidth < paragraph.minIntrinsicWidth.toDp()) {
targetFontSize *= 0.95
Copy link
Member

Choose a reason for hiding this comment

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

If you are interested, we might be able to use binary search for this. 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fun idea! Added in 612af1c

@takahirom
Copy link
Member

Thank you for your comprehensive screenshots.
Could we also have a test using setFontScale?
https://github.com/DroidKaigi/conference-app-2024/blob/main/core/testing/src/main/java/io/github/droidkaigi/confsched/testing/robot/MiniRobots.kt#L158

@mannodermaus mannodermaus force-pushed the fix/top-bar-text-break-in-large-font-size branch from 0eae2c3 to b0817f9 Compare September 3, 2024 02:37
@github-actions github-actions bot temporarily deployed to deploygate-distribution September 3, 2024 02:41 Inactive
@mannodermaus mannodermaus force-pushed the fix/top-bar-text-break-in-large-font-size branch from b0817f9 to e0b7d8b Compare September 3, 2024 11:51
@mannodermaus
Copy link
Contributor Author

It took a bit to get the line count assertion for text nodes just right, but I managed to pull it off in e0b7d8b. From now on, we can assert the line count of any text based on expectations!

@github-actions github-actions bot temporarily deployed to deploygate-distribution September 3, 2024 11:58 Inactive
Copy link

github-actions bot commented Sep 3, 2024

@takahirom takahirom added the awesome Label for project-applicable insights or remarkable technologies. label Sep 3, 2024
Text(
text = text,
color = color,
fontSize = calculateFontSize(text, style, color, textAlign, maxLines),
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure, but could we consider using remember{} caching for this? If so, it might lead to a open source library-level optimization. 👍

Copy link
Member

Choose a reason for hiding this comment

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

Sorry, I forgot that we can't use Composable function in remember lambda.👀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It should be possible to rearrange this to work. Let me think about this tonight!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out that Paragraph() does not require any composable context. 👀 be532bb

@mannodermaus mannodermaus force-pushed the fix/top-bar-text-break-in-large-font-size branch from 285de21 to be532bb Compare September 4, 2024 10:24
@github-actions github-actions bot temporarily deployed to deploygate-distribution September 4, 2024 10:28 Inactive
@takahirom
Copy link
Member

Looks awesome! I want to share this, and for the people who want to use this implementation, could you update the pull request content to match the current code?

…onfsched/designsystem/component/AutoSizeText.kt

Co-authored-by: Takahiro Menju <[email protected]>
@mannodermaus
Copy link
Contributor Author

I updated the description to reference the new implementation. @takahirom please let me know if you'd like me to change something else about the PR description. 🙇‍♂️

@github-actions github-actions bot temporarily deployed to deploygate-distribution September 4, 2024 22:54 Inactive
Copy link
Member

@takahirom takahirom left a comment

Choose a reason for hiding this comment

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

Thank you for your valuable contribution!

@takahirom takahirom merged commit e2b3d10 into DroidKaigi:main Sep 5, 2024
6 checks passed
@mannodermaus mannodermaus deleted the fix/top-bar-text-break-in-large-font-size branch September 5, 2024 01:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awesome Label for project-applicable insights or remarkable technologies.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

When the text and display size is increased, the text in AnimatedTextTopAppBar may be partially hidden.
2 participants