Skip to content

Commit

Permalink
Optimize recompositions when calculating optimal size. Update libs
Browse files Browse the repository at this point in the history
  • Loading branch information
jeketos committed Mar 19, 2024
1 parent 5e2b290 commit f7aaac5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 22 deletions.
6 changes: 3 additions & 3 deletions auto-size-text/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ android {
}

dependencies {
implementation "androidx.compose.ui:ui:1.6.0"
implementation 'androidx.compose.material3:material3:1.1.2'
implementation "androidx.compose.ui:ui:1.6.3"
implementation 'androidx.compose.material3:material3:1.2.1'
}
ext {
PUBLISH_GROUP_ID = 'com.idapgroup'
PUBLISH_VERSION = '0.2.2'
PUBLISH_VERSION = '0.3.0'
PUBLISH_ARTIFACT_ID = 'autosizetext-compose'
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import androidx.compose.ui.unit.TextUnit

/**
* future releases:
* 1. Support overflow
* 2. Support maxFontSize
* 3. Add types: Maximisation, Minimization, Balanced
* 1. Support maxFontSize
* 2. Add types: Maximisation, Minimization, Balanced
*/

/**
Expand Down Expand Up @@ -90,10 +89,15 @@ public fun AutoSizeText(
var textReadyToDraw by remember(key1 = text) {
mutableStateOf(false)
}
var decreasingStage: SizeDecreasingStage? by remember(key1 = text) {
mutableStateOf(null)
}

Text(
modifier = modifier.drawWithContent {
if (textReadyToDraw) drawContent()
if (textReadyToDraw) {
drawContent()
}
},
text = text,
color = color,
Expand All @@ -111,17 +115,28 @@ public fun AutoSizeText(
softWrap = softWrap,
overflow = if (textReadyToDraw) overflow else TextOverflow.Clip,
onTextLayout = { result ->
if (textReadyToDraw) {
onTextLayout(result)
return@Text
}
if (minFontSize == TextUnit.Unspecified || overriddenMetrics.fontSize > minFontSize) {
if (result.didOverflowHeight) {
val correctedFontSize = overriddenMetrics.fontSize.times(SIZE_DECREASER)
if (result.didOverflowHeight.not() && decreasingStage == null) {
textReadyToDraw = true
onTextLayout(result)
return@Text
}

decreasingStage = decreasingStage.next(result.didOverflowHeight)
if (decreasingStage == SizeDecreasingStage.Peace) {
textReadyToDraw = true
} else {
val correctedFontSize = overriddenMetrics.fontSize.times(decreasingStage!!.value)
val correctedLineHeight =
if (keepLineHeight) lineHeight else correctedFontSize.div(ratio)
overriddenMetrics = overriddenMetrics.copy(
fontSize = correctedFontSize,
lineHeight = correctedLineHeight
)
} else {
textReadyToDraw = true
}
} else {
if (overriddenMetrics.fontSize <= minFontSize) {
Expand All @@ -137,4 +152,3 @@ public fun AutoSizeText(
},
)
}

19 changes: 19 additions & 0 deletions auto-size-text/src/main/java/com/idapgroup/autosizetext/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,25 @@ import androidx.compose.ui.unit.TextUnit

internal const val SIZE_DECREASER = 0.9f

internal enum class SizeDecreasingStage(val value: Float) {
Offense(0.5f),
Defence(1.2f),
Diplomacy(0.95f),
Peace(Float.NaN);
}

internal fun SizeDecreasingStage?.next(didOverflowHeight: Boolean) :SizeDecreasingStage {
return when {
this == null -> SizeDecreasingStage.Offense
this == SizeDecreasingStage.Offense && didOverflowHeight -> SizeDecreasingStage.Offense
this == SizeDecreasingStage.Offense -> SizeDecreasingStage.Defence
this == SizeDecreasingStage.Defence && didOverflowHeight.not() -> SizeDecreasingStage.Defence
this == SizeDecreasingStage.Defence -> SizeDecreasingStage.Diplomacy
this == SizeDecreasingStage.Diplomacy && didOverflowHeight -> SizeDecreasingStage.Diplomacy
else -> SizeDecreasingStage.Peace
}
}

internal data class InnerMetrics(
val fontSize: TextUnit,
val lineHeight: TextUnit,
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ buildscript {
}// Top-level build file where you can add configuration options common to all sub-projects/modules.

plugins {
id 'com.android.application' version '8.2.0' apply false
id 'com.android.library' version '8.2.0' apply false
id 'com.android.application' version '8.2.2' apply false
id 'com.android.library' version '8.2.2' apply false
id 'org.jetbrains.kotlin.android' version '1.9.22' apply false
id 'io.github.gradle-nexus.publish-plugin' version "2.0.0-rc-1"
}
Expand Down
8 changes: 4 additions & 4 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ dependencies {
implementation 'androidx.core:core-ktx:1.12.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
implementation 'androidx.activity:activity-compose:1.8.2'
implementation "androidx.compose.ui:ui:1.6.0"
implementation "androidx.compose.ui:ui-tooling-preview:1.6.0"
implementation 'androidx.compose.material3:material3:1.1.2'
implementation "androidx.compose.ui:ui:1.6.3"
implementation "androidx.compose.ui:ui-tooling-preview:1.6.3"
implementation 'androidx.compose.material3:material3:1.2.1'

//Most useful dependency
implementation 'com.idapgroup:autosizetext-compose:0.2.2'
implementation 'com.idapgroup:autosizetext-compose:0.3.0'
// implementation(project(":auto-size-text"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ package com.idapgroup.autosizetext.sample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.Composable
Expand Down Expand Up @@ -65,15 +69,15 @@ fun Greeting() {
AutoSizeText(
modifier = Modifier.height(100.dp),
text = longText,
fontSize = 40.sp,
fontSize = 1000.sp,
lineHeight = 40.sp,
keepLineHeight = true,
)
Box(modifier = Modifier.height(40.dp)) {
AutoSizeText(
text = longText,
fontSize = 40.sp,
lineHeight = 42.sp,
fontSize = 1000.sp,
lineHeight = 1020.sp,
)
}

Expand Down

0 comments on commit f7aaac5

Please sign in to comment.