Commonize minor UI utility functions (#5945)

This commit is contained in:
David Perez 2025-09-26 15:34:25 -05:00 committed by GitHub
parent eab2c17614
commit fd555e92d3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 30 additions and 141 deletions

View File

@ -39,8 +39,8 @@ import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.manager.IntentManager
import com.bitwarden.ui.platform.resource.BitwardenDrawable
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.platform.util.displayLabel
import com.x8bit.bitwarden.ui.platform.feature.settings.appearance.model.AppLanguage
import com.x8bit.bitwarden.ui.platform.util.displayLabel
import kotlinx.collections.immutable.toImmutableList
/**

View File

@ -1,16 +0,0 @@
package com.x8bit.bitwarden.ui.platform.util
import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.util.Text
import com.bitwarden.ui.util.asText
/**
* Returns a human-readable display label for the given [AppTheme].
*/
val AppTheme.displayLabel: Text
get() = when (this) {
AppTheme.DEFAULT -> BitwardenString.default_system.asText()
AppTheme.DARK -> BitwardenString.dark.asText()
AppTheme.LIGHT -> BitwardenString.light.asText()
}

View File

@ -27,8 +27,8 @@ import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.bitwarden.authenticator.ui.platform.components.button.AuthenticatorTextButton
import com.bitwarden.authenticator.ui.platform.components.toggle.BitwardenWideSwitch
import com.bitwarden.authenticator.ui.platform.components.util.maxDialogHeight
import com.bitwarden.authenticator.ui.platform.components.util.maxDialogWidth
import com.bitwarden.ui.platform.components.dialog.util.maxDialogHeight
import com.bitwarden.ui.platform.components.dialog.util.maxDialogWidth
import com.bitwarden.ui.platform.resource.BitwardenString
/**

View File

@ -23,7 +23,7 @@ import androidx.compose.ui.semantics.testTagsAsResourceId
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import com.bitwarden.authenticator.ui.platform.components.button.AuthenticatorTextButton
import com.bitwarden.authenticator.ui.platform.components.util.maxDialogHeight
import com.bitwarden.ui.platform.components.dialog.util.maxDialogHeight
import com.bitwarden.ui.platform.resource.BitwardenString
/**

View File

@ -28,7 +28,7 @@ import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.tooling.preview.Preview
import com.bitwarden.authenticator.ui.platform.components.util.nonLetterColorVisualTransformation
import com.bitwarden.ui.platform.components.util.nonLetterColorVisualTransformation
import com.bitwarden.ui.platform.resource.BitwardenDrawable
import com.bitwarden.ui.platform.resource.BitwardenString
@ -80,7 +80,11 @@ fun BitwardenPasswordField(
onValueChange = onValueChange,
visualTransformation = when {
!showPassword -> PasswordVisualTransformation()
readOnly -> nonLetterColorVisualTransformation()
readOnly -> nonLetterColorVisualTransformation(
digitColor = MaterialTheme.colorScheme.primary,
specialCharacterColor = MaterialTheme.colorScheme.error,
)
else -> VisualTransformation.None
},
singleLine = singleLine,

View File

@ -1,35 +0,0 @@
package com.bitwarden.authenticator.ui.platform.components.util
import android.content.res.Configuration
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
/**
* Provides the maximum height [Dp] common for all dialogs with a given [Configuration].
*/
val Configuration.maxDialogHeight: Dp
get() = when (orientation) {
Configuration.ORIENTATION_LANDSCAPE -> 312.dp
Configuration.ORIENTATION_PORTRAIT -> 542.dp
Configuration.ORIENTATION_UNDEFINED -> Dp.Unspecified
@Suppress("DEPRECATION")
Configuration.ORIENTATION_SQUARE,
-> Dp.Unspecified
else -> Dp.Unspecified
}
/**
* Provides the maximum width [Dp] common for all dialogs with a given [Configuration].
*/
val Configuration.maxDialogWidth: Dp
get() = when (orientation) {
Configuration.ORIENTATION_LANDSCAPE -> 542.dp
Configuration.ORIENTATION_PORTRAIT -> 312.dp
Configuration.ORIENTATION_UNDEFINED -> Dp.Unspecified
@Suppress("DEPRECATION")
Configuration.ORIENTATION_SQUARE,
-> Dp.Unspecified
else -> Dp.Unspecified
}

View File

@ -1,62 +0,0 @@
package com.bitwarden.authenticator.ui.platform.components.util
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.input.OffsetMapping
import androidx.compose.ui.text.input.TransformedText
import androidx.compose.ui.text.input.VisualTransformation
import androidx.compose.ui.text.withStyle
/**
* Returns the [VisualTransformation] that alters the output of the text in an input field by
* applying different colors to the digits and special characters, letters will remain unaffected.
*/
@Composable
fun nonLetterColorVisualTransformation(): VisualTransformation {
val digitColor = MaterialTheme.colorScheme.primary
val specialCharacterColor = MaterialTheme.colorScheme.error
return remember(digitColor, specialCharacterColor) {
NonLetterColorVisualTransformation(
digitColor = digitColor,
specialCharacterColor = specialCharacterColor,
)
}
}
/**
* Alters the visual output of the text in an input field.
*
* All numbers in the text will have the [digitColor] applied to it and special characters will
* have the [specialCharacterColor] applied.
*/
private class NonLetterColorVisualTransformation(
private val digitColor: Color,
private val specialCharacterColor: Color,
) : VisualTransformation {
override fun filter(text: AnnotatedString): TransformedText =
TransformedText(
buildTransformedAnnotatedString(text.toString()),
OffsetMapping.Identity,
)
private fun buildTransformedAnnotatedString(text: String): AnnotatedString {
val builder = AnnotatedString.Builder()
text.toCharArray().forEach { char ->
when {
char.isDigit() -> builder.withStyle(SpanStyle(color = digitColor)) { append(char) }
!char.isLetter() -> {
builder.withStyle(SpanStyle(color = specialCharacterColor)) { append(char) }
}
else -> builder.append(char)
}
}
return builder.toAnnotatedString()
}
}

View File

@ -71,6 +71,7 @@ import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.manager.IntentManager
import com.bitwarden.ui.platform.resource.BitwardenDrawable
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.platform.util.displayLabel
import com.bitwarden.ui.util.Text
import com.bitwarden.ui.util.asText

View File

@ -1,16 +0,0 @@
package com.bitwarden.authenticator.ui.platform.util
import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.util.Text
import com.bitwarden.ui.util.asText
/**
* Returns a human-readable display label for the given [AppTheme].
*/
val AppTheme.displayLabel: Text
get() = when (this) {
AppTheme.DEFAULT -> BitwardenString.default_system.asText()
AppTheme.DARK -> BitwardenString.dark.asText()
AppTheme.LIGHT -> BitwardenString.light.asText()
}

View File

@ -16,16 +16,16 @@ import com.bitwarden.ui.platform.theme.BitwardenTheme
* applying different colors to the digits and special characters, letters will remain unaffected.
*/
@Composable
fun nonLetterColorVisualTransformation(): VisualTransformation {
val digitColor = BitwardenTheme.colorScheme.text.codeBlue
val specialCharacterColor = BitwardenTheme.colorScheme.text.codePink
return remember(digitColor, specialCharacterColor) {
fun nonLetterColorVisualTransformation(
digitColor: Color = BitwardenTheme.colorScheme.text.codeBlue,
specialCharacterColor: Color = BitwardenTheme.colorScheme.text.codePink,
): VisualTransformation =
remember(digitColor, specialCharacterColor) {
NonLetterColorVisualTransformation(
digitColor = digitColor,
specialCharacterColor = specialCharacterColor,
)
}
}
/**
* Alters the visual output of the text in an input field.

View File

@ -1,6 +1,19 @@
package com.bitwarden.ui.platform.util
import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.resource.BitwardenString
import com.bitwarden.ui.util.Text
import com.bitwarden.ui.util.asText
/**
* Returns a human-readable display label for the given [AppTheme].
*/
val AppTheme.displayLabel: Text
get() = when (this) {
AppTheme.DEFAULT -> BitwardenString.default_system.asText()
AppTheme.DARK -> BitwardenString.dark.asText()
AppTheme.LIGHT -> BitwardenString.light.asText()
}
/**
* Returns `true` if the app is currently using dark mode.

View File

@ -1,4 +1,4 @@
package com.x8bit.bitwarden.ui.platform.util
package com.bitwarden.ui.platform.util
import com.bitwarden.ui.platform.feature.settings.appearance.model.AppTheme
import com.bitwarden.ui.platform.resource.BitwardenString