-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
717 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/AnimatedSmallFABWithLabel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import androidx.compose.animation.* | ||
import androidx.compose.animation.core.animateFloat | ||
import androidx.compose.animation.core.animateFloatAsState | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.foundation.layout.Row | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.alpha | ||
import androidx.compose.ui.draw.scale | ||
|
||
@Composable | ||
fun AnimatedSmallFABWithLabel( | ||
state: SpeedDialFABState, | ||
showLabel: Boolean, | ||
modifier: Modifier = Modifier, | ||
labelContent: @Composable () -> Unit = { }, | ||
fabContent: @Composable () -> Unit, | ||
) { | ||
val alpha = state.transition?.animateFloat( | ||
transitionSpec = { | ||
tween(durationMillis = 50) | ||
}, | ||
targetValueByState = { | ||
if (it == SpeedDialState.Expanded) 1F else 0F | ||
} | ||
) | ||
val scale = state.transition?.animateFloat( | ||
targetValueByState = { | ||
if (it == SpeedDialState.Expanded) 1F else 0F | ||
} | ||
) | ||
|
||
Row( | ||
modifier = modifier | ||
.alpha(animateFloatAsState((alpha?.value ?: 0F)).value) | ||
.scale(animateFloatAsState(scale?.value ?: 0F).value), | ||
verticalAlignment = Alignment.CenterVertically, | ||
) { | ||
AnimatedVisibility( | ||
visible = showLabel, | ||
enter = slideInHorizontally { it / 2 } + fadeIn(), | ||
exit = slideOutHorizontally { it / 2 } + fadeOut() | ||
) { | ||
labelContent.invoke() | ||
} | ||
fabContent.invoke() | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/FABItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import androidx.compose.foundation.layout.size | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.painter.Painter | ||
import androidx.compose.ui.graphics.vector.ImageVector | ||
import androidx.compose.ui.unit.dp | ||
|
||
open class FABItem( | ||
val icon: ImageVector? = null, | ||
val painter: Painter? = null, | ||
val tint: Boolean = false, | ||
val modifier: Modifier = if (painter == null) Modifier else Modifier.size(24.dp), | ||
val label: String, | ||
val onClick: () -> Unit, | ||
) |
72 changes: 72 additions & 0 deletions
72
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/SpeedDialFAB.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import androidx.compose.animation.core.Spring | ||
import androidx.compose.animation.core.animateFloat | ||
import androidx.compose.animation.core.spring | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.material3.ExtendedFloatingActionButton | ||
import androidx.compose.material3.FloatingActionButtonDefaults | ||
import androidx.compose.material3.FloatingActionButtonElevation | ||
import androidx.compose.material3.contentColorFor | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.rotate | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.graphics.Shape | ||
|
||
@Composable | ||
fun SpeedDialFAB( | ||
modifier: Modifier = Modifier, | ||
state: SpeedDialFABState, | ||
onClick: (SpeedDialFABState) -> Unit = { it.changeState() }, | ||
iconRotation: Float = 45F, | ||
expanded: Boolean = false, | ||
shape: Shape = FloatingActionButtonDefaults.extendedFabShape, | ||
containerColor: Color = FloatingActionButtonDefaults.containerColor, | ||
contentColor: Color = contentColorFor(containerColor), | ||
elevation: FloatingActionButtonElevation = FloatingActionButtonDefaults.elevation(), | ||
text: @Composable () -> Unit = { }, | ||
icon: @Composable () -> Unit, | ||
) { | ||
val rotation = if (iconRotation > 0F) { | ||
state.transition?.animateFloat( | ||
transitionSpec = { | ||
if (targetState == SpeedDialState.Expanded) { | ||
spring(stiffness = Spring.StiffnessLow) | ||
} else { | ||
spring(stiffness = Spring.StiffnessMedium) | ||
} | ||
}, | ||
label = "", | ||
targetValueByState = { | ||
if (it == SpeedDialState.Expanded) iconRotation else 0F | ||
} | ||
) | ||
} else { | ||
null | ||
} | ||
|
||
ExtendedFloatingActionButton( | ||
modifier = modifier, | ||
onClick = { | ||
onClick(state) | ||
}, | ||
expanded = expanded, | ||
shape = shape, | ||
containerColor = containerColor, | ||
contentColor = contentColor, | ||
elevation = elevation, | ||
icon = { | ||
Box( | ||
modifier = Modifier.rotate(rotation?.value ?: 0F), | ||
contentAlignment = Alignment.Center | ||
) { | ||
icon() | ||
} | ||
}, | ||
text = { | ||
text() | ||
} | ||
) | ||
} |
37 changes: 37 additions & 0 deletions
37
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/SpeedDialFABState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import androidx.compose.animation.core.Transition | ||
import androidx.compose.animation.core.updateTransition | ||
import androidx.compose.runtime.* | ||
|
||
@Composable | ||
fun rememberSpeedDialState(): SpeedDialFABState { | ||
val state = remember { SpeedDialFABState() } | ||
|
||
state.transition = updateTransition(targetState = state.currentState) | ||
|
||
return state | ||
} | ||
|
||
class SpeedDialFABState { | ||
|
||
var currentState by mutableStateOf<SpeedDialState>(SpeedDialState.Collapsed) | ||
var transition: Transition<SpeedDialState>? = null | ||
|
||
fun changeState() { | ||
currentState = if (transition?.currentState == SpeedDialState.Expanded | ||
|| (transition?.isRunning == true && transition?.targetState == SpeedDialState.Expanded)) { | ||
SpeedDialState.Collapsed | ||
} else { | ||
SpeedDialState.Expanded | ||
} | ||
} | ||
|
||
fun collapse() { | ||
currentState = SpeedDialState.Collapsed | ||
} | ||
|
||
fun expand() { | ||
currentState = SpeedDialState.Expanded | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/SpeedDialState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
sealed interface SpeedDialState { | ||
@Serializable | ||
data object Collapsed : SpeedDialState | ||
|
||
@Serializable | ||
data object Expanded : SpeedDialState | ||
} |
75 changes: 75 additions & 0 deletions
75
composeApp/src/commonMain/kotlin/dev/datlag/aniflow/ui/custom/speeddial/SubSpeedDialFABs.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package dev.datlag.aniflow.ui.custom.speeddial | ||
|
||
import androidx.compose.foundation.Image | ||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.material3.* | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.ColorFilter | ||
import androidx.compose.ui.unit.dp | ||
|
||
@Composable | ||
fun <T: FABItem> SubSpeedDialFABs( | ||
state: SpeedDialFABState, | ||
items: List<T>, | ||
showLabels: Boolean = true, | ||
verticalArrangement: Arrangement.Vertical = Arrangement.Top, | ||
labelContent: @Composable (T) -> Unit = { | ||
val backgroundColor = FloatingActionButtonDefaults.containerColor | ||
|
||
Surface( | ||
color = backgroundColor, | ||
shape = MaterialTheme.shapes.small, | ||
shadowElevation = 2.dp, | ||
onClick = { it.onClick() } | ||
) { | ||
Text( | ||
text = it.label, | ||
color = contentColorFor(backgroundColor), | ||
modifier = Modifier.padding(horizontal = 6.dp, vertical = 4.dp) | ||
) | ||
} | ||
}, | ||
fabContent: @Composable (T) -> Unit = { | ||
SmallFloatingActionButton( | ||
modifier = Modifier.padding(4.dp), | ||
onClick = { it.onClick() }, | ||
elevation = FloatingActionButtonDefaults.elevation( | ||
defaultElevation = 4.dp, | ||
hoveredElevation = 4.dp | ||
) | ||
) { | ||
if (it.icon != null) { | ||
Icon( | ||
modifier = it.modifier, | ||
imageVector = it.icon, | ||
contentDescription = it.label | ||
) | ||
} else if (it.painter != null) { | ||
Image( | ||
modifier = it.modifier, | ||
painter = it.painter, | ||
contentDescription = it.label, | ||
colorFilter = if (it.tint) ColorFilter.tint(LocalContentColor.current) else null | ||
) | ||
} | ||
} | ||
} | ||
) { | ||
Column( | ||
horizontalAlignment = Alignment.End, | ||
verticalArrangement = verticalArrangement, | ||
) { | ||
items.forEach { item -> | ||
AnimatedSmallFABWithLabel( | ||
state = state, | ||
showLabel = showLabels, | ||
labelContent = { labelContent(item) }, | ||
fabContent = { fabContent(item) } | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.