Skip to content

Instantly share code, notes, and snippets.

@erdevasconcellos
Last active May 8, 2024 17:17
Show Gist options
  • Save erdevasconcellos/c6fee17babf53f0c3c00eb4045b41772 to your computer and use it in GitHub Desktop.
Save erdevasconcellos/c6fee17babf53f0c3c00eb4045b41772 to your computer and use it in GitHub Desktop.
WinButton() es una función @composable que simula un botón clásico de Windows 7. Alternativa al horrible Button() de Material3.
package com.vascosoft.anotadortruco.ui.composables
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Email
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
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.draw.drawBehind
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
@Preview(
showBackground = true,
name = "Prueba WinButton"
)
@Composable
fun PruebaWinButton() {
Box(
modifier = Modifier
.background(Color.White)
.padding(10.dp)
){
Column {
WinButton(
text = "Disabled",
onClick = {},
enabled = false
)
Spacer(Modifier.height(8.dp))
WinButton(
text = "Botoncito 1",
onClick = {}
)
Spacer(Modifier.height(8.dp))
WinButton(
text = "Botoncito 2",
onClick = {},
default = true,
glyph = Icons.Default.Email
)
}
}
}
@Composable
fun WinButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
text: String = "WinButton",
enabled: Boolean = true,
glyph: ImageVector? = null,
default: Boolean = false,
color: Color = Color.Black
) {
val padding = PaddingValues(start = 12.dp, top = 4.dp, end = 12.dp, bottom = 4.dp)
val shape = RoundedCornerShape(2.dp)
val borderColors: List<Color> = if (!enabled) {
listOf( Color(0xffbfc2c4), Color(0xffededed) )
} else {
if (default) {
listOf( Color(0xff3985b6), Color(0xff5bd5f2) )
} else {
listOf(Color(0xffd0d0d0), Color(0xffededed) )
}
}
val backgroundColors = listOf( Color(0xffededed), Color(0xffd0d0d0) )
Box(
modifier = modifier
.clickable(enabled) { onClick() }
.border(
width = 1.dp,
color = borderColors[0],
shape = shape
)
.clip(shape)
.drawBehind {
if (enabled) {
drawRect(
color = backgroundColors[0],
size = Size(size.width, size.height / 2)
)
drawRect(
color = backgroundColors[1],
topLeft = Offset(x = 0f, y = size.height / 2),
size = Size(size.width, size.height / 2)
)
} else {
drawRect(
color = Color(0xfff4f4f4),
size = size
)
}
}
.padding(1.dp)
.border(
width = 1.dp,
color = borderColors[1],
shape = shape
)
.padding(padding),
contentAlignment = Alignment.Center
){
Row(
verticalAlignment = Alignment.CenterVertically
) {
glyph?.let {
Icon(imageVector = it, contentDescription = null)
Spacer(modifier = Modifier.width(8.dp))
}
Text(
text = text,
fontSize = 18.sp,
color = if (enabled) color else Color(0xffb1adaa)
)
}
}
}
@erdevasconcellos
Copy link
Author

Android Studio Preview:
imagen

En funcionamiento real en app:
imagen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment