-
Notifications
You must be signed in to change notification settings - Fork 206
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
Hide title when enable the BigFont/BigDisplay in FloorMapScreen #1149
Open
kosenda
wants to merge
3
commits into
DroidKaigi:main
Choose a base branch
from
kosenda:bug-fix/floormapscreen-hide-title
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+103
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
core/ui/src/commonMain/kotlin/io/github/droidkaigi/confsched2023/ui/AutoSizableText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package io.github.droidkaigi.confsched2023.ui | ||
|
||
import androidx.compose.foundation.layout.BoxWithConstraints | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.runtime.mutableFloatStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalFontFamilyResolver | ||
import androidx.compose.ui.text.Paragraph | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.unit.TextUnit | ||
import androidx.compose.ui.unit.sp | ||
|
||
private const val CONTRACTION_RATIO = 0.95f | ||
|
||
/** | ||
* ref: https://blog.canopas.com/autosizing-textfield-in-jetpack-compose-7a80f0270853 | ||
*/ | ||
@Composable | ||
fun AutoSizableText( | ||
text: String, | ||
minFontSize: TextUnit, | ||
style: TextStyle, | ||
modifier: Modifier = Modifier, | ||
maxLines: Int = Int.MAX_VALUE, | ||
fontWeight: FontWeight? = null, | ||
) { | ||
val density = LocalDensity.current | ||
var tempFontSize by remember(text, style) { mutableFloatStateOf(style.fontSize.value) } | ||
|
||
// Calculate size before displaying | ||
BoxWithConstraints(modifier = modifier) { | ||
val calculateParagraph = @Composable { | ||
Paragraph( | ||
text = text, | ||
style = style.copy(fontSize = tempFontSize.sp), | ||
constraints = this.constraints, | ||
density = density, | ||
fontFamilyResolver = LocalFontFamilyResolver.current, | ||
) | ||
} | ||
|
||
var paragraph = calculateParagraph() | ||
while ( | ||
tempFontSize > minFontSize.value && | ||
(paragraph.height / density.density > maxHeight.value || paragraph.lineCount > maxLines) | ||
) { | ||
tempFontSize *= CONTRACTION_RATIO | ||
paragraph = calculateParagraph() | ||
} | ||
|
||
Text( | ||
text = text, | ||
maxLines = maxLines, | ||
fontWeight = fontWeight, | ||
style = style.copy(fontSize = tempFontSize.sp), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Thanks for providing the references! Is this your original implementation? Otherwise, just to clarify, could you please tell us if this code complies with our license? Thanks!
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 don't think I can say that it is 100% original, as I am sure that I am referring to an article.
However, I would like to say that there are many parts that I came up with or researched and created myself, so there is no problem.
I don't see anything in the article (Medium) or in the sample code on GitHub about licensing, and I honestly don't know what to do if the above is insufficient.