feat: use brush for user colour

to support gradient role colours

Signed-off-by: Infi <wingit@geist.ga>
This commit is contained in:
Infi 2023-06-17 04:12:46 +02:00
parent 166512fb4f
commit 5a365e8bd2
5 changed files with 50 additions and 21 deletions

View File

@ -4,32 +4,41 @@ import android.util.Log
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
// color is spelled american because Color from compose is spelled american
fun Brush.Companion.solidColor(colour: Color) = linearGradient(
colorStops = arrayOf(
0f to colour,
1f to colour
)
)
object WebCompat {
@Composable
fun parseColour(colour: String): Color {
fun parseColour(colour: String): Brush {
if (colour.startsWith("var(")) {
Log.d(
"WebCompat",
"Parsing colour $colour. ${colour.substringAfter("var(").substringBefore(")")}"
)
return when (colour.substringAfter("var(").substringBefore(")")) {
"--accent" -> MaterialTheme.colorScheme.primary
"--foreground" -> MaterialTheme.colorScheme.onBackground
"--background" -> MaterialTheme.colorScheme.background
"--error" -> MaterialTheme.colorScheme.error
else -> LocalContentColor.current
"--accent" -> Brush.solidColor(MaterialTheme.colorScheme.primary)
"--foreground" -> Brush.solidColor(MaterialTheme.colorScheme.onBackground)
"--background" -> Brush.solidColor(MaterialTheme.colorScheme.background)
"--error" -> Brush.solidColor(MaterialTheme.colorScheme.error)
else -> Brush.solidColor(LocalContentColor.current)
}
} else {
try {
return Color(android.graphics.Color.parseColor(colour))
return Brush.solidColor(Color(android.graphics.Color.parseColor(colour)))
} catch (e: IllegalArgumentException) {
Log.d(
"WebCompat",
"Failed to parse colour $colour, falling back to LocalContentColor.current"
)
return LocalContentColor.current
return Brush.solidColor(LocalContentColor.current)
}
}
}

View File

@ -20,11 +20,13 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.internals.WebCompat
import chat.revolt.api.internals.solidColor
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.schemas.Embed
import chat.revolt.components.generic.RemoteImage
import chat.revolt.components.generic.UIMarkdown
@ -50,7 +52,7 @@ fun RegularEmbed(
.fillMaxHeight()
.background(
embed.colour?.let { WebCompat.parseColour(it) }
?: MaterialTheme.colorScheme.primary
?: Brush.solidColor(MaterialTheme.colorScheme.primary)
)
)

View File

@ -9,10 +9,12 @@ import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontFamily
import androidx.compose.ui.text.font.FontStyle
@ -22,6 +24,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import chat.revolt.R
import chat.revolt.api.RevoltAPI
import chat.revolt.api.internals.solidColor
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.schemas.User
import chat.revolt.components.generic.UserAvatar
@ -41,7 +44,7 @@ fun InReplyTo(
?: stringResource(id = R.string.unknown)
val contentColor = LocalContentColor.current
val usernameColor = message?.let { authorColour(it) } ?: contentColor
val usernameColor = message?.let { authorColour(it) } ?: Brush.solidColor(contentColor)
Box(
modifier = modifier
@ -73,9 +76,11 @@ fun InReplyTo(
} else {
stringResource(id = R.string.unknown)
},
fontWeight = FontWeight.Bold,
fontSize = 12.sp,
color = usernameColor,
style = LocalTextStyle.current.copy(
fontWeight = FontWeight.Bold,
fontSize = 12.sp,
brush = usernameColor
),
maxLines = 1,
overflow = TextOverflow.Ellipsis,
modifier = Modifier.padding(horizontal = 4.dp)

View File

@ -28,11 +28,13 @@ import androidx.compose.material.icons.filled.Edit
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
@ -50,6 +52,7 @@ import chat.revolt.api.REVOLT_FILES
import chat.revolt.api.RevoltAPI
import chat.revolt.api.internals.ULID
import chat.revolt.api.internals.WebCompat
import chat.revolt.api.internals.solidColor
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.schemas.AutumnResource
import chat.revolt.api.schemas.User
@ -58,11 +61,11 @@ import chat.revolt.components.generic.UserAvatarWidthPlaceholder
import chat.revolt.api.schemas.Message as MessageSchema
@Composable
fun authorColour(message: MessageSchema): Color {
fun authorColour(message: MessageSchema): Brush {
return if (message.masquerade?.colour != null) {
WebCompat.parseColour(message.masquerade.colour)
} else {
LocalContentColor.current
Brush.solidColor(LocalContentColor.current)
}
}
@ -212,8 +215,15 @@ fun Message(
Row(verticalAlignment = Alignment.CenterVertically) {
Text(
text = authorName(message),
fontWeight = FontWeight.Bold,
color = authorColour(message),
style = LocalTextStyle.current.copy(
fontWeight = FontWeight.Bold,
brush = if (message.author == RevoltAPI.selfId) Brush.horizontalGradient(
listOf(
Color.Magenta,
Color.Cyan,
),
) else authorColour(message),
),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)

View File

@ -14,6 +14,7 @@ import androidx.compose.foundation.rememberScrollState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material3.Icon
import androidx.compose.material3.LocalTextStyle
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.surfaceColorAtElevation
@ -88,14 +89,16 @@ fun ManageableReply(
Text(
text = authorName(message = replyMessage),
fontSize = 12.sp,
modifier = Modifier
.clickable {
onToggleMention()
}
.padding(4.dp),
color = authorColour(message = replyMessage),
fontWeight = FontWeight.Bold,
style = LocalTextStyle.current.copy(
brush = authorColour(message = replyMessage),
fontWeight = FontWeight.Bold,
fontSize = 12.sp,
)
)
Text(