From bf39ea426bda39fb0da93a7ee89561b50d3b0f7f Mon Sep 17 00:00:00 2001 From: MM20 <15646950+MM2-0@users.noreply.github.com> Date: Mon, 19 Jun 2023 22:14:06 +0200 Subject: [PATCH] Move launcher icon click events out of icon composable --- .../ui/component/ShapedLauncherIcon.kt | 26 ++------------- .../launcher/search/common/grid/GridItem.kt | 32 ++++++++++++------- .../sheets/CustomizeSearchableSheet.kt | 5 +-- .../ui/settings/icons/IconsSettingsScreen.kt | 3 +- .../ui/settings/tags/EditTagSheet.kt | 6 ++-- 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/component/ShapedLauncherIcon.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/component/ShapedLauncherIcon.kt index 1ea5768a..9a09fdef 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/component/ShapedLauncherIcon.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/component/ShapedLauncherIcon.kt @@ -101,8 +101,6 @@ fun ShapedLauncherIcon( size: Dp, icon: () -> LauncherIcon? = { null }, badge: () -> Badge? = { null }, - onClick: (() -> Unit)? = null, - onLongClick: (() -> Unit)? = null, shape: Shape = LocalIconShape.current ) { @@ -149,17 +147,7 @@ fun ShapedLauncherIcon( ) { Box( modifier = Modifier - .fillMaxSize() - .then( - if (onClick != null || onLongClick != null) { - Modifier.pointerInput(onClick, onLongClick) { - detectTapGestures( - onLongPress = { onLongClick?.invoke() }, - onTap = { onClick?.invoke() }, - ) - } - } else Modifier - ), + .fillMaxSize(), contentAlignment = Alignment.Center ) { val bmp = currentBitmap @@ -253,17 +241,7 @@ fun ShapedLauncherIcon( tonalElevation = 1.dp, modifier = Modifier .size(size * 0.33f) - .align(Alignment.BottomEnd) - .then( - if (onClick != null || onLongClick != null) { - Modifier.pointerInput(onClick, onLongClick) { - detectTapGestures( - onLongPress = { onLongClick?.invoke() }, - onTap = { onClick?.invoke() }, - ) - } - } else Modifier - ), + .align(Alignment.BottomEnd), color = MaterialTheme.colorScheme.secondary, shape = CircleShape ) { diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/common/grid/GridItem.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/common/grid/GridItem.kt index 66c5e17e..2ecec81e 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/common/grid/GridItem.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/search/common/grid/GridItem.kt @@ -5,6 +5,8 @@ import androidx.activity.compose.BackHandler import androidx.compose.animation.core.animateFloatAsState import androidx.compose.animation.core.tween import androidx.compose.foundation.background +import androidx.compose.foundation.combinedClickable +import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.absoluteOffset @@ -87,11 +89,27 @@ fun GridItem( var showPopup by remember(item.key) { mutableStateOf(false) } var bounds by remember { mutableStateOf(Rect.Zero) } - Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) { + val launchOnPress = !item.preferDetailsOverLaunch + val hapticFeedback = LocalHapticFeedback.current + + Column( + modifier = modifier + .combinedClickable( + onClick = { + if (!launchOnPress || !viewModel.launch(context, bounds)) { + showPopup = true + }}, + onLongClick = { + hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress) + showPopup = true + }, + indication = null, + interactionSource = remember { MutableInteractionSource() }, + ), + horizontalAlignment = Alignment.CenterHorizontally) { val badge by viewModel.badge.collectAsStateWithLifecycle() val icon by viewModel.icon.collectAsStateWithLifecycle() - val launchOnPress = !item.preferDetailsOverLaunch val windowSize = LocalWindowSize.current @@ -115,7 +133,6 @@ fun GridItem( } } - val hapticFeedback = LocalHapticFeedback.current val iconShape = LocalIconShape.current Box( @@ -141,15 +158,6 @@ fun GridItem( size = LocalGridSettings.current.iconSize.dp, badge = { badge }, icon = { icon }, - onClick = { - if (!launchOnPress || !viewModel.launch(context, bounds)) { - showPopup = true - } - }, - onLongClick = { - hapticFeedback.performHapticFeedback(HapticFeedbackType.LongPress) - showPopup = true - } ) } if (showLabels) { diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/sheets/CustomizeSearchableSheet.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/sheets/CustomizeSearchableSheet.kt index 213f4e29..d8380bf8 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/sheets/CustomizeSearchableSheet.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/launcher/sheets/CustomizeSearchableSheet.kt @@ -4,6 +4,7 @@ import android.content.pm.PackageManager import android.graphics.drawable.InsetDrawable import androidx.activity.compose.BackHandler import androidx.appcompat.content.res.AppCompatResources +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize @@ -121,7 +122,7 @@ fun CustomizeSearchableSheet( icon = badgeDrawable ) }, - onClick = { + modifier = Modifier.clickable { viewModel.openIconPicker() } ) @@ -350,7 +351,7 @@ fun IconPreview( ShapedLauncherIcon( size = iconSize, icon = { item?.preview }, - onClick = onClick + modifier = Modifier.clickable(onClick = onClick), ) } } diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/icons/IconsSettingsScreen.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/icons/IconsSettingsScreen.kt index 065b61d3..e7d9c56a 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/icons/IconsSettingsScreen.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/icons/IconsSettingsScreen.kt @@ -4,6 +4,7 @@ import android.graphics.drawable.ColorDrawable import androidx.appcompat.app.AppCompatActivity import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -364,7 +365,7 @@ fun IconShapePreference( ) ) }, - onClick = { + modifier = Modifier.clickable { onValueChanged(it) showDialog = false }, diff --git a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/tags/EditTagSheet.kt b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/tags/EditTagSheet.kt index 5e0db8fc..189adef0 100644 --- a/app/ui/src/main/java/de/mm20/launcher2/ui/settings/tags/EditTagSheet.kt +++ b/app/ui/src/main/java/de/mm20/launcher2/ui/settings/tags/EditTagSheet.kt @@ -2,6 +2,7 @@ package de.mm20.launcher2.ui.settings.tags import androidx.compose.animation.AnimatedVisibility import androidx.compose.foundation.background +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues @@ -203,10 +204,9 @@ fun ListItem( ShapedLauncherIcon( icon = { icon }, size = 48.dp, - modifier = Modifier.padding(4.dp), - onClick = { + modifier = Modifier.padding(4.dp).clickable { onTagChanged(!item.isTagged) - } + }, ) if (item.isTagged) { Surface(