Skip to content

Commit

Permalink
chore: Move overlay alpha animation spec into RevealOverlayEffect int…
Browse files Browse the repository at this point in the history
…erface (#61)
  • Loading branch information
svenjacobs authored Aug 18, 2023
1 parent 4a58dc6 commit ae0c649
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ fun MainScreen(modifier: Modifier = Modifier) {
val scope = rememberCoroutineScope()

Reveal(
modifier = modifier,
revealState = revealState,
onRevealableClick = { key ->
scope.launch {
if (key == Keys.Fab) {
Expand All @@ -63,6 +61,8 @@ fun MainScreen(modifier: Modifier = Modifier) {
}
},
onOverlayClick = { scope.launch { revealState.hide() } },
modifier = modifier,
revealState = revealState,
overlayInserter = InPlaceRevealOverlayInserter(),
overlayContent = { key -> RevealOverlayContent(key) },
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public abstract class BaseRevealTest {
revealState = rememberRevealState()

Reveal(
revealState = revealState,
onRevealableClick = onRevealableClick,
onOverlayClick = onOverlayClick,
revealState = revealState,
overlayContent = { key ->
when (key) {
Keys.Key1 -> Text("Overlay1")
Expand Down
54 changes: 24 additions & 30 deletions reveal-core/src/main/kotlin/com/svenjacobs/reveal/Reveal.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.svenjacobs.reveal

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
Expand Down Expand Up @@ -31,9 +29,9 @@ import com.svenjacobs.reveal.effect.dim.DimRevealOverlayEffect
*
* When active, applies the [overlayEffect] and only reveals current revealable element.
*
* Adds a new `ComposeView` to the root content view (`android.R.id.content`) for the fullscreen
* effect. Therefore it does not matter where in the component hierarchy this composable is added.
* The effect is always rendered fullscreen.
* When [FullscreenRevealOverlayInserter] is used, adds a new `ComposeView` to the root content
* view (`android.R.id.content`) for the fullscreen effect. Therefore it does not matter where in
* the component hierarchy this composable is added. The effect is always rendered fullscreen.
*
* Elements inside the contents of this composable are registered as "revealables" via the
* [RevealScope.revealable] modifier in the scope of the [content] composable.
Expand All @@ -44,30 +42,29 @@ import com.svenjacobs.reveal.effect.dim.DimRevealOverlayEffect
* images) next to the reveal area. This content is placed above the greyed out backdrop. Elements
* in this scope can be aligned relative to the reveal area via [RevealOverlayScope.align].
*
* @param onRevealableClick Called when the revealable area was clicked, where the
* parameter `key` is the key of the current revealable item.
* @param onOverlayClick Called when the overlay is clicked somewhere outside of the
* current revealable, where the parameter `key` is the key of the
* current revealable.
* @param modifier Modifier applied to this composable.
* @param revealState State which controls the visibility of the reveal effect.
* @param overlayEffect The effect which is used for the background and reveal of
* items. Currently only [DimRevealOverlayEffect] is supported.
* @param overlayEffectAnimationSpec Animation spec for the animated alpha value of the overlay
* effect when showing or hiding.
* @param overlayInserter Strategy of how to insert the overlay into the composition.
* @param overlayContent Optional content which is placed above the overlay and where
* its elements can be aligned relative to the reveal area via
* modifiers available in the scope of this composable. The `key`
* parameter is the key of the current visible revealable item.
* @param content Actual content which is visible when the Reveal composable is
* not active. Elements are registered as revealables via
* modifiers provided in the scope of this composable.
* @param onRevealableClick Called when the revealable area was clicked, where the parameter `key`
* is the key of the current revealable item.
* @param onOverlayClick Called when the overlay is clicked somewhere outside of the current
* revealable, where the parameter `key` is the key of the current
* revealable.
* @param modifier Modifier applied to this composable.
* @param revealState State which controls the visibility of the reveal effect.
* @param overlayEffect The effect which is used for the background and reveal of items.
* Currently only [DimRevealOverlayEffect] is supported.
* @param overlayInserter Strategy of how to insert the overlay into the composition.
* @param overlayContent Optional content which is placed above the overlay and where its
* elements can be aligned relative to the reveal area via modifiers
* available in the scope of this composable. The `key` parameter is the
* key of the current visible revealable item.
* @param content Actual content which is visible when the Reveal composable is not
* active. Elements are registered as revealables via modifiers provided
* in the scope of this composable.
*
* @see RevealState
* @see RevealScope
* @see RevealOverlayScope
* @see DimRevealOverlayEffect
* @see RevealOverlayInserter
*/
@Composable
public fun Reveal(
Expand All @@ -76,14 +73,13 @@ public fun Reveal(
modifier: Modifier = Modifier,
revealState: RevealState = rememberRevealState(),
overlayEffect: RevealOverlayEffect = DimRevealOverlayEffect(),
overlayEffectAnimationSpec: AnimationSpec<Float> = tween(durationMillis = 500),
overlayInserter: RevealOverlayInserter = FullscreenRevealOverlayInserter(),
overlayContent: @Composable RevealOverlayScope.(key: Key) -> Unit = {},
content: @Composable RevealScope.() -> Unit,
overlayContent: @Composable (RevealOverlayScope.(key: Key) -> Unit) = {},
content: @Composable (RevealScope.() -> Unit),
) {
val animatedOverlayAlpha by animateFloatAsState(
targetValue = if (revealState.isVisible) 1.0f else 0.0f,
animationSpec = overlayEffectAnimationSpec,
animationSpec = overlayEffect.alphaAnimationSpec,
finishedListener = { alpha ->
if (alpha == 0.0f) {
revealState.onHideAnimationFinished()
Expand Down Expand Up @@ -178,7 +174,6 @@ public fun Reveal(
modifier: Modifier = Modifier,
revealState: RevealState = rememberRevealState(),
overlayEffect: RevealOverlayEffect = DimRevealOverlayEffect(),
overlayEffectAnimationSpec: AnimationSpec<Float> = tween(durationMillis = 500),
overlayContent: @Composable RevealOverlayScope.(key: Key) -> Unit = {},
content: @Composable RevealScope.() -> Unit,
): Unit = Reveal(
Expand All @@ -187,7 +182,6 @@ public fun Reveal(
modifier = modifier,
revealState = revealState,
overlayEffect = overlayEffect,
overlayEffectAnimationSpec = overlayEffectAnimationSpec,
overlayInserter = FullscreenRevealOverlayInserter(revealableOffset),
overlayContent = overlayContent,
content = content,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.svenjacobs.reveal.effect

import androidx.compose.animation.core.AnimationSpec
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import androidx.compose.ui.Modifier
Expand All @@ -21,4 +22,9 @@ public interface RevealOverlayEffect {
modifier: Modifier,
content: @Composable RevealOverlayScope.(key: Key) -> Unit,
)

/**
* Animation spec for the animated alpha of the overlay when this effect is shown or hidden.
*/
public val alphaAnimationSpec: AnimationSpec<Float>
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,17 @@ import com.svenjacobs.reveal.internal.rect.toIntRect
/**
* An overlay effect which dims the background as specified via [color].
*
* @param color Background color of the overlay.
* @param contentAnimationSpec Animation spec for the animated alpha value of the overlay content.
* @param alphaAnimationSpec Animation spec for the animated alpha of the overlay when this
* effect is shown or hidden.
* @param color Background color of the overlay.
* @param contentAlphaAnimationSpec Animation spec for the animated alpha value of the overlay
* content.
*/
@Immutable
public class DimRevealOverlayEffect(
override val alphaAnimationSpec: AnimationSpec<Float> = tween(durationMillis = 500),
private val color: Color = Color.Black.copy(alpha = 0.8f),
private val contentAnimationSpec: AnimationSpec<Float> = tween(durationMillis = 500),
private val contentAlphaAnimationSpec: AnimationSpec<Float> = tween(durationMillis = 500),
) : RevealOverlayEffect {

@Composable
Expand All @@ -58,7 +62,7 @@ public class DimRevealOverlayEffect(
revealable = it,
fromState = Gone,
toState = Visible,
contentAnimationSpec = contentAnimationSpec,
contentAlphaAnimationSpec = contentAlphaAnimationSpec,
)
}

Expand All @@ -67,7 +71,7 @@ public class DimRevealOverlayEffect(
revealable = it,
fromState = Visible,
toState = Gone,
contentAnimationSpec = contentAnimationSpec,
contentAlphaAnimationSpec = contentAlphaAnimationSpec,
)
}

Expand Down Expand Up @@ -136,12 +140,13 @@ private fun rememberDimItemHolder(
revealable: ActualRevealable,
fromState: DimItemState,
toState: DimItemState,
contentAnimationSpec: AnimationSpec<Float>,
contentAlphaAnimationSpec: AnimationSpec<Float>,
): DimItemHolder = key(revealable.key) {
val targetState = remember { mutableStateOf(fromState) }
val contentAlpha = animateFloatAsState(
targetValue = if (targetState.value == Visible) 1.0f else 0.0f,
animationSpec = contentAnimationSpec,
animationSpec = contentAlphaAnimationSpec,
label = "contentAlpha",
)

LaunchedEffect(Unit) {
Expand Down

0 comments on commit ae0c649

Please sign in to comment.