Fixed fast clickable for lists & added hover effect to it

This commit is contained in:
Abdelilah El Aissaoui 2022-10-10 18:01:06 +02:00
parent add5666d89
commit 4f019ca8ed
3 changed files with 22 additions and 8 deletions

View File

@ -1,7 +1,11 @@
package com.jetpackduba.gitnuro.extensions package com.jetpackduba.gitnuro.extensions
import androidx.compose.foundation.background import androidx.compose.foundation.*
import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsHoveredAsState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.ExperimentalComposeUiApi import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.awt.awtEventOrNull import androidx.compose.ui.awt.awtEventOrNull
@ -38,9 +42,11 @@ fun Modifier.handOnHover(): Modifier {
// TODO Try to restore hover that was shown with clickable modifier // TODO Try to restore hover that was shown with clickable modifier
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
fun Modifier.fastClickable(onClick: () -> Unit) = @Composable
fun Modifier.fastClickable(key: Any = Unit, onClick: () -> Unit) =
this.handOnHover() this.handOnHover()
.pointerInput(Unit) { .hoverBackground()
.pointerInput(key) {
while (true) { while (true) {
val lastMouseEvent = awaitPointerEventScope { awaitFirstDownEvent() } val lastMouseEvent = awaitPointerEventScope { awaitFirstDownEvent() }
val mouseEvent = lastMouseEvent.awtEventOrNull val mouseEvent = lastMouseEvent.awtEventOrNull
@ -51,4 +57,12 @@ fun Modifier.fastClickable(onClick: () -> Unit) =
} }
} }
} }
} }
@Composable
private fun Modifier.hoverBackground(): Modifier {
val hoverInteraction = remember { MutableInteractionSource() }
return this.hoverable(hoverInteraction)
.indication(hoverInteraction, LocalIndication.current)
}

View File

@ -861,7 +861,7 @@ fun DiffLineText(line: Line, diffEntryType: DiffEntryType, onActionTriggered: ()
contentDescription = null, contentDescription = null,
tint = Color.White, tint = Color.White,
modifier = Modifier modifier = Modifier
.fastClickable { onActionTriggered() } .fastClickable(line) { onActionTriggered() }
.size(14.dp) .size(14.dp)
.clip(RoundedCornerShape(2.dp)) .clip(RoundedCornerShape(2.dp))
.background(color), .background(color),

View File

@ -619,7 +619,7 @@ fun UncommitedChangesLine(
modifier = Modifier modifier = Modifier
.height(40.dp) .height(40.dp)
.fillMaxWidth() .fillMaxWidth()
.fastClickable { onUncommitedChangesSelected() } .handMouseClickable { onUncommitedChangesSelected() }
.padding(start = graphWidth) .padding(start = graphWidth)
.backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected) .backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected)
.padding(DIVIDER_WIDTH.dp), .padding(DIVIDER_WIDTH.dp),
@ -742,7 +742,7 @@ fun CommitLine(
) { ) {
Box( Box(
modifier = Modifier modifier = Modifier
.fastClickable { onRevCommitSelected() } .fastClickable(graphNode) { onRevCommitSelected() }
.padding(start = graphWidth) .padding(start = graphWidth)
.height(LINE_HEIGHT.dp) .height(LINE_HEIGHT.dp)
.backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected) .backgroundIf(isSelected, MaterialTheme.colors.backgroundSelected)