Add TagChip component

This commit is contained in:
MM20 2023-04-02 22:43:10 +02:00
parent 32bee8684e
commit a811113602
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
3 changed files with 81 additions and 44 deletions

View File

@ -12,13 +12,11 @@ import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.requiredHeight
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Edit
import androidx.compose.material.icons.rounded.ExpandLess
import androidx.compose.material.icons.rounded.ExpandMore
import androidx.compose.material.icons.rounded.Star
import androidx.compose.material.icons.rounded.Tag
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.Icon
@ -57,7 +55,7 @@ fun FavoritesTagSelector(
bottom = if (reverse) 4.dp else 8.dp,
end = if (editButton) 8.dp else 0.dp
)
then
then
if (editButton && expanded) Modifier.height(IntrinsicSize.Min) else Modifier,
horizontalArrangement = Arrangement.End,
verticalAlignment = Alignment.CenterVertically,
@ -85,17 +83,11 @@ fun FavoritesTagSelector(
label = { Text(stringResource(R.string.favorites)) }
)
for (tag in tags) {
FilterChip(
TagChip(
modifier = Modifier.padding(start = 8.dp),
tag = tag,
selected = selectedTag == tag.tag,
onClick = { onSelectTag(tag.tag) },
leadingIcon = {
Icon(
imageVector = Icons.Rounded.Tag,
contentDescription = null
)
},
label = { Text(tag.label) }
)
}
if (canScroll) {
@ -124,17 +116,11 @@ fun FavoritesTagSelector(
label = { Text(stringResource(R.string.favorites)) }
)
for (tag in tags) {
FilterChip(
TagChip(
modifier = Modifier.padding(end = 8.dp),
tag = tag,
selected = selectedTag == tag.tag,
onClick = { onSelectTag(tag.tag) },
leadingIcon = {
Icon(
imageVector = Icons.Rounded.Tag,
contentDescription = null
)
},
label = { Text(tag.label) }
)
}
}

View File

@ -0,0 +1,57 @@
package de.mm20.launcher2.ui.common
import androidx.compose.foundation.clickable
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Close
import androidx.compose.material.icons.rounded.Tag
import androidx.compose.material3.FilterChip
import androidx.compose.material3.FilterChipDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.SelectableChipColors
import androidx.compose.material3.SelectableChipElevation
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import de.mm20.launcher2.search.data.Tag
@Composable
fun TagChip(
modifier: Modifier = Modifier,
tag: Tag,
selected: Boolean = false,
onClick: () -> Unit = {},
clearable: Boolean = false,
onClear: (() -> Unit)? = null,
colors: SelectableChipColors = FilterChipDefaults.filterChipColors(),
elevation: SelectableChipElevation? = FilterChipDefaults.filterChipElevation(),
) {
FilterChip(
modifier = modifier,
selected = selected,
onClick = onClick,
leadingIcon = {
Icon(
imageVector = Icons.Rounded.Tag,
contentDescription = null
)
},
label = {
Text(
tag.label
)
},
colors = colors,
elevation = elevation,
trailingIcon = if (clearable) {
{
Icon(
modifier = Modifier.clickable {
onClear?.invoke()
},
imageVector = Icons.Rounded.Close,
contentDescription = null
)
}
} else null
)
}

View File

@ -91,6 +91,7 @@ import de.mm20.launcher2.badges.Badge
import de.mm20.launcher2.icons.LauncherIcon
import de.mm20.launcher2.search.SavableSearchable
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.common.TagChip
import de.mm20.launcher2.ui.component.BottomSheetDialog
import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.component.ShapedLauncherIcon
@ -101,7 +102,6 @@ import de.mm20.launcher2.ui.component.dragndrop.rememberLazyDragAndDropGridState
import de.mm20.launcher2.ui.component.dragndrop.rememberLazyDragAndDropListState
import de.mm20.launcher2.ui.ktx.toPixels
import de.mm20.launcher2.ui.locals.LocalGridSettings
import kotlinx.coroutines.currentCoroutineContext
import kotlin.math.roundToInt
@Composable
@ -532,9 +532,13 @@ fun ReorderFavoritesGrid(viewModel: EditFavoritesSheetVM, paddingValues: Padding
Icon(Icons.Rounded.Create, null)
},
contentPadding = PaddingValues(
start = MenuDefaults.DropdownMenuItemContentPadding.calculateStartPadding(LocalLayoutDirection.current),
end = MenuDefaults.DropdownMenuItemContentPadding.calculateEndPadding(LocalLayoutDirection.current),
top = if(availableTags.isNotEmpty()) 8.dp else 0.dp,
start = MenuDefaults.DropdownMenuItemContentPadding.calculateStartPadding(
LocalLayoutDirection.current
),
end = MenuDefaults.DropdownMenuItemContentPadding.calculateEndPadding(
LocalLayoutDirection.current
),
top = if (availableTags.isNotEmpty()) 8.dp else 0.dp,
),
text = {
Box {
@ -546,7 +550,9 @@ fun ReorderFavoritesGrid(viewModel: EditFavoritesSheetVM, paddingValues: Padding
}
BasicTextField(
value = newTag,
onValueChange = { newTag = it.replace(",", "") },
onValueChange = {
newTag = it.replace(",", "")
},
textStyle = LocalTextStyle.current.copy(
color = LocalContentColor.current
),
@ -573,7 +579,7 @@ fun ReorderFavoritesGrid(viewModel: EditFavoritesSheetVM, paddingValues: Padding
contentDescription = null
)
},
onClick = { }
onClick = { }
)
}
}
@ -595,34 +601,22 @@ fun ReorderFavoritesGrid(viewModel: EditFavoritesSheetVM, paddingValues: Padding
) { tag ->
DraggableItem(state = rowState, key = tag.key) { dragged ->
FilterChip(
TagChip(
modifier = Modifier
.padding(end = 12.dp)
.pointerInput(null) {
val coroutineContext =
currentCoroutineContext()
},
tag = tag,
selected = tag.tag == hoveredTag,
onClick = {},
label = { Text(tag.label) },
leadingIcon = {
Icon(Icons.Rounded.Tag, null)
},
trailingIcon = {
Icon(
modifier = Modifier.clickable {
viewModel.unpinTag(tag)
},
imageVector = Icons.Rounded.Close,
contentDescription = null
)
},
elevation = if (dragged) FilterChipDefaults.elevatedFilterChipElevation() else FilterChipDefaults.filterChipElevation(),
colors = if (dragged) FilterChipDefaults.elevatedFilterChipColors()
else FilterChipDefaults.filterChipColors(
containerColor = MaterialTheme.colorScheme.surface
)
),
clearable = true,
onClear = {
viewModel.unpinTag(tag)
}
)
}
}