Skip to content

Commit

Permalink
Changed modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
spacecowboy committed May 30, 2024
1 parent 4aec286 commit ffa8a02
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.input.nestedscroll.NestedScrollConnection
import androidx.compose.ui.input.nestedscroll.NestedScrollSource
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Velocity
Expand Down Expand Up @@ -278,11 +276,13 @@ internal open class SwipeableState<T>(
to = currentValue
fraction = 1f
}

1 -> {
from = anchors.getValue(bounds[0])
to = anchors.getValue(bounds[0])
fraction = 1f
}

else -> {
val (a, b) =
if (direction > 0f) {
Expand Down Expand Up @@ -574,6 +574,7 @@ internal fun <T : Any> rememberSwipeableStateFor(
* in order to animate to the next state, even if the positional [thresholds] have not been reached.
*/
@ExperimentalMaterial3Api
@Composable
internal fun <T> Modifier.swipeable(
state: SwipeableState<T>,
anchors: Map<Float, T>,
Expand All @@ -584,27 +585,14 @@ internal fun <T> Modifier.swipeable(
thresholds: (from: T, to: T) -> ThresholdConfig = { _, _ -> FixedThreshold(56.dp) },
resistance: ResistanceConfig? = resistanceConfig(anchors.keys),
velocityThreshold: Dp = VelocityThreshold,
) = composed(
inspectorInfo =
debugInspectorInfo {
name = "swipeable"
properties["state"] = state
properties["anchors"] = anchors
properties["orientation"] = orientation
properties["enabled"] = enabled
properties["reverseDirection"] = reverseDirection
properties["interactionSource"] = interactionSource
properties["thresholds"] = thresholds
properties["resistance"] = resistance
properties["velocityThreshold"] = velocityThreshold
},
) {
): Modifier {
require(anchors.isNotEmpty()) {
"You must have at least one anchor."
}
require(anchors.values.distinct().count() == anchors.size) {
"You cannot have two anchors mapped to the same state."
}
val thresholdsCallback by rememberUpdatedState(thresholds)
val density = LocalDensity.current
state.ensureInit(anchors)
LaunchedEffect(anchors, state) {
Expand All @@ -614,22 +602,24 @@ internal fun <T> Modifier.swipeable(
state.thresholds = { a, b ->
val from = anchors.getValue(a)
val to = anchors.getValue(b)
with(thresholds(from, to)) { density.computeThreshold(a, b) }
with(thresholdsCallback(from, to)) { density.computeThreshold(a, b) }
}
with(density) {
state.velocityThreshold = velocityThreshold.toPx()
}
state.processNewAnchors(oldAnchors, anchors)
}

Modifier.draggable(
orientation = orientation,
enabled = enabled,
reverseDirection = reverseDirection,
interactionSource = interactionSource,
startDragImmediately = state.isAnimationRunning,
onDragStopped = { velocity -> launch { state.performFling(velocity) } },
state = state.draggableState,
return this.then(
Modifier.draggable(
orientation = orientation,
enabled = enabled,
reverseDirection = reverseDirection,
interactionSource = interactionSource,
startDragImmediately = state.isAnimationRunning,
onDragStopped = { velocity -> launch { state.performFling(velocity) } },
state = state.draggableState,
),
)
}

Expand Down Expand Up @@ -767,14 +757,17 @@ private fun findBounds(
a == null ->
// case 1 or 3
listOfNotNull(b)

b == null ->
// case 4
listOf(a)

a == b ->
// case 2
// Can't return offset itself here since it might not be exactly equal
// to the anchor, despite being considered an exact match.
listOf(a)

else ->
// case 5
listOf(a, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.geometry.Offset
import androidx.compose.ui.geometry.Rect
import androidx.compose.ui.geometry.center
Expand All @@ -33,7 +32,6 @@ import androidx.compose.ui.graphics.drawscope.Stroke
import androidx.compose.ui.graphics.drawscope.rotate
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
Expand Down Expand Up @@ -203,31 +201,27 @@ private val ArrowWidth = 10.dp
private val ArrowHeight = 5.dp
private val Elevation = 6.dp

@Composable
fun Modifier.pullRefreshIndicatorTransform(
state: PullRefreshState,
scale: Boolean = false,
) = composed(
inspectorInfo =
debugInspectorInfo {
name = "pullRefreshIndicatorTransform"
properties["state"] = state
properties["scale"] = scale
},
) {
): Modifier {
var height by remember { mutableIntStateOf(0) }

Modifier
.onSizeChanged { height = it.height }
.graphicsLayer {
translationY = state.position - height

if (scale && !state.refreshing) {
val scaleFraction =
LinearOutSlowInEasing
.transform(state.position / state.threshold)
.coerceIn(0f, 1f)
scaleX = scaleFraction
scaleY = scaleFraction
}
}
return this.then(
Modifier
.onSizeChanged { height = it.height }
.graphicsLayer {
translationY = state.position - height

if (scale && !state.refreshing) {
val scaleFraction =
LinearOutSlowInEasing
.transform(state.position / state.threshold)
.coerceIn(0f, 1f)
scaleX = scaleFraction
scaleY = scaleFraction
}
},
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,38 @@ package com.nononsenseapps.feeder.ui.compose.utils

import androidx.compose.foundation.focusable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.focus.focusProperties
import androidx.compose.ui.input.InputMode
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.key.key
import androidx.compose.ui.input.key.onKeyEvent
import androidx.compose.ui.platform.LocalInputModeManager
import androidx.compose.ui.platform.debugInspectorInfo

@OptIn(ExperimentalComposeUiApi::class)
fun Modifier.onKeyEventLikeEscape(action: () -> Unit) =
composed(
inspectorInfo =
debugInspectorInfo {
name = "onEscapeLikeKeyPress"
properties["action"] = action
},
) {
onKeyEvent {
when (it.key) {
Key.Escape, Key.Back, Key.NavigateOut -> {
action()
true
}

else -> false
@Composable
fun Modifier.onKeyEventLikeEscape(action: () -> Unit): Modifier {
return this.onKeyEvent {
when (it.key) {
Key.Escape, Key.Back, Key.NavigateOut -> {
action()
true
}

else -> false
}
}
}

@Composable
fun Modifier.focusableInNonTouchMode(
enabled: Boolean = true,
interactionSource: MutableInteractionSource? = null,
) = composed(
inspectorInfo =
debugInspectorInfo {
name = "focusableInNonTouchMode"
properties["enabled"] = enabled
properties["interactionSource"] = interactionSource
},
) {
): Modifier {
val inputModeManager = LocalInputModeManager.current
Modifier
.focusProperties { canFocus = inputModeManager.inputMode != InputMode.Touch }
.focusable(enabled, interactionSource)
return this.then(
Modifier
.focusProperties { canFocus = inputModeManager.inputMode != InputMode.Touch }
.focusable(enabled, interactionSource),
)
}

0 comments on commit ffa8a02

Please sign in to comment.