From 25bae5f3376cd4351e3197e61ef67aa1156dd7ed Mon Sep 17 00:00:00 2001 From: sonulen Date: Thu, 18 Apr 2024 23:24:48 +0300 Subject: [PATCH] gears: added package with set of gears for compose --- config/detekt/detekt.yml | 1 + gears/build.gradle.kts | 3 ++ gears/compose/CHANGELOG.md | 7 +++ gears/compose/README.md | 44 +++++++++++++++++++ gears/compose/build.gradle.kts | 29 ++++++++++++ .../main/kotlin/FixedFontScaleContainer.kt | 43 ++++++++++++++++++ gears/gradle.properties | 1 + settings.gradle.kts | 1 + 8 files changed, 129 insertions(+) create mode 100644 gears/build.gradle.kts create mode 100644 gears/compose/CHANGELOG.md create mode 100644 gears/compose/README.md create mode 100644 gears/compose/build.gradle.kts create mode 100644 gears/compose/src/main/kotlin/FixedFontScaleContainer.kt create mode 100644 gears/gradle.properties diff --git a/config/detekt/detekt.yml b/config/detekt/detekt.yml index 2cfad52..d6ec68b 100644 --- a/config/detekt/detekt.yml +++ b/config/detekt/detekt.yml @@ -433,6 +433,7 @@ naming: active: true excludes: ['**/test/**', '**/androidTest/**', '**/commonTest/**', '**/jvmTest/**', '**/jsTest/**', '**/iosTest/**'] functionPattern: '([a-z][a-zA-Z0-9]*)|(`.*`)' + ignoreAnnotated: ['Composable'] excludeClassPattern: '$^' FunctionParameterNaming: active: true diff --git a/gears/build.gradle.kts b/gears/build.gradle.kts new file mode 100644 index 0000000..0673c51 --- /dev/null +++ b/gears/build.gradle.kts @@ -0,0 +1,3 @@ +// For some reason gradle.properties in this project doesn't affect its subprojects +val gearsGroup = group +subprojects { group = gearsGroup } diff --git a/gears/compose/CHANGELOG.md b/gears/compose/CHANGELOG.md new file mode 100644 index 0000000..1daa5cd --- /dev/null +++ b/gears/compose/CHANGELOG.md @@ -0,0 +1,7 @@ +## Unreleased + +### Changed + +- Create gears.compose + added: + - `FixedFontScaleContainer` - A container that fixes the font scale, ignoring values, that are set in the phone's system settings diff --git a/gears/compose/README.md b/gears/compose/README.md new file mode 100644 index 0000000..ccbe907 --- /dev/null +++ b/gears/compose/README.md @@ -0,0 +1,44 @@ +# compose-ktx +[![Version](https://img.shields.io/maven-central/v/com.redmadrobot.extensions/compose-ktx?style=flat-square)][mavenCentral] +[![License](https://img.shields.io/github/license/RedMadRobot/redmadrobot-android-ktx?style=flat-square)][license] + +--- + + + +- [Installation](#installation) +- [Usage](#usage) +- [Contributing](#contributing) + + + +A set of gears for compose. + +## Installation + +Add the dependency: +```groovy +repositories { + mavenCentral() + google() +} + +dependencies { + implementation("com.redmadrobot.gears:compose:") +} +``` + +## Usage + +| Gear | Description | +|:--------------------|:-----------| +| `FixedFontScaleContainer` | A container that fixes the font scale, ignoring values, that are set in the phone's system settings | + +## Contributing + +Merge requests are welcome. +For major changes, please open an issue first to discuss what you would like to change. + + +[mavenCentral]: https://search.maven.org/artifact/com.redmadrobot.extensions/compose-ktx +[license]: ../LICENSE diff --git a/gears/compose/build.gradle.kts b/gears/compose/build.gradle.kts new file mode 100644 index 0000000..bd25962 --- /dev/null +++ b/gears/compose/build.gradle.kts @@ -0,0 +1,29 @@ +plugins { + id("com.redmadrobot.android-library") + id("com.redmadrobot.publish") + convention.library.android +} + +version = "1.6.6-0" +description = "A set of gears for compose" + +redmadrobot { + android.minSdk = 21 +} + +android { + namespace = "$group.compose" + + buildFeatures { + compose = true + } + + composeOptions { + kotlinCompilerExtensionVersion = androidx.versions.compose.compiler.get() + } +} + +dependencies { + api(androidx.compose.ui) + api(androidx.compose.runtime) +} diff --git a/gears/compose/src/main/kotlin/FixedFontScaleContainer.kt b/gears/compose/src/main/kotlin/FixedFontScaleContainer.kt new file mode 100644 index 0000000..95acc20 --- /dev/null +++ b/gears/compose/src/main/kotlin/FixedFontScaleContainer.kt @@ -0,0 +1,43 @@ +package com.redmadrobot.extensions.compose + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.platform.LocalDensity +import androidx.compose.ui.unit.Density + +/** + * A container that fixes the font scale, ignoring values, + * that are set in the phone's system settings + */ +@Composable +public fun FixedFontScaleContainer( + content: @Composable () -> Unit, +) { + val fixedFontScaleDensity = Density(LocalDensity.current.density) + CompositionLocalProvider( + LocalDensity provides fixedFontScaleDensity, + content = content, + ) +} + +/** + * A container that restricts the font scale, ignoring values, + * that are set in the phone's system settings + * + * @param limit - the upper limit of font enlargement + */ +@Composable +public fun LimitedFontScaleContainer( + limit: Float, + content: @Composable () -> Unit, +) { + val fontScale = LocalDensity.current.fontScale.coerceAtMost(limit) + val fixedFontScaleDensity = Density( + density = LocalDensity.current.density, + fontScale = fontScale, + ) + CompositionLocalProvider( + LocalDensity provides fixedFontScaleDensity, + content = content, + ) +} diff --git a/gears/gradle.properties b/gears/gradle.properties new file mode 100644 index 0000000..4f66e6a --- /dev/null +++ b/gears/gradle.properties @@ -0,0 +1 @@ +group=com.redmadrobot.gears diff --git a/settings.gradle.kts b/settings.gradle.kts index ea870e8..fae2ad2 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -34,4 +34,5 @@ include( ":ktx:lifecycle-livedata-ktx", ":ktx:resources-ktx", ":ktx:viewbinding-ktx", + ":gears:compose", )