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

Hide title when enable the BigFont/BigDisplay in FloorMapScreen #1149

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

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!

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 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.

*/
@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),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.windowsizeclass.WindowSizeClass
Expand Down Expand Up @@ -63,6 +62,7 @@ import io.github.droidkaigi.confsched2023.model.FloorLevel
import io.github.droidkaigi.confsched2023.model.FloorLevel.Basement
import io.github.droidkaigi.confsched2023.model.FloorLevel.Ground
import io.github.droidkaigi.confsched2023.model.SideEvents
import io.github.droidkaigi.confsched2023.ui.AutoSizableText
import io.github.droidkaigi.confsched2023.ui.SnackbarMessageEffect
import kotlinx.collections.immutable.toImmutableList

Expand Down Expand Up @@ -152,16 +152,20 @@ private fun FloorMapScreen(
TopAppBar(
title = {
if (scrollBehavior.state.overlappedFraction == 0f) {
Text(
AutoSizableText(
text = FloorMapStrings.Title.asString(),
style = MaterialTheme.typography.headlineLarge,
minFontSize = MaterialTheme.typography.bodySmall.fontSize,
maxLines = 1,
fontWeight = FontWeight.Medium,
style = MaterialTheme.typography.headlineLarge,
)
} else {
Text(
AutoSizableText(
text = FloorMapStrings.Title.asString(),
style = MaterialTheme.typography.titleLarge,
modifier = Modifier.alpha(scrollBehavior.state.overlappedFraction),
minFontSize = MaterialTheme.typography.bodySmall.fontSize,
maxLines = 1,
style = MaterialTheme.typography.titleLarge,
)
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,34 @@ class FloorMapScreenTest {
checkScreenCapture()
}
}

@Test
@Category(ScreenshotTests::class)
@Config(fontScale = 0.5f)
fun smallFontScaleShot() {
floorMapScreenRobot {
setupScreenContent()
checkScreenCapture()
}
}

@Test
@Category(ScreenshotTests::class)
@Config(fontScale = 1.5f)
fun largeFontScaleShot() {
floorMapScreenRobot {
setupScreenContent()
checkScreenCapture()
}
}

@Test
@Category(ScreenshotTests::class)
@Config(fontScale = 2.0f)
fun hugeFontScaleShot() {
floorMapScreenRobot {
setupScreenContent()
checkScreenCapture()
}
}
}