Skip to content

Commit 5316d16

Browse files
feature: Add inner scene
1 parent 52c9895 commit 5316d16

File tree

1 file changed

+98
-0
lines changed
  • FloconDesktop/navigation/src/commonMain/kotlin/io/github/openflocon/navigation/scene

1 file changed

+98
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package io.github.openflocon.navigation.scene
2+
3+
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.clickable
5+
import androidx.compose.foundation.interaction.MutableInteractionSource
6+
import androidx.compose.foundation.layout.Box
7+
import androidx.compose.foundation.layout.fillMaxSize
8+
import androidx.compose.foundation.layout.offset
9+
import androidx.compose.material.icons.Icons
10+
import androidx.compose.material.icons.sharp.Close
11+
import androidx.compose.runtime.Composable
12+
import androidx.compose.runtime.Immutable
13+
import androidx.compose.runtime.remember
14+
import androidx.compose.ui.Alignment
15+
import androidx.compose.ui.Modifier
16+
import androidx.compose.ui.graphics.Color
17+
import androidx.compose.ui.unit.IntOffset
18+
import androidx.compose.ui.unit.dp
19+
import androidx.navigation3.runtime.NavEntry
20+
import androidx.navigation3.scene.OverlayScene
21+
import androidx.navigation3.scene.Scene
22+
import androidx.navigation3.scene.SceneStrategy
23+
import androidx.navigation3.scene.SceneStrategyScope
24+
import io.github.openflocon.library.designsystem.FloconTheme
25+
import io.github.openflocon.library.designsystem.components.FloconIcon
26+
import io.github.openflocon.library.designsystem.components.FloconIconTonalButton
27+
import io.github.openflocon.library.designsystem.components.FloconSurface
28+
29+
@Immutable
30+
internal data class InnerWindowScene<T : Any>(
31+
override val key: Any,
32+
private val entry: NavEntry<T>,
33+
override val previousEntries: List<NavEntry<T>>,
34+
override val overlaidEntries: List<NavEntry<T>>,
35+
private val onBack: () -> Unit,
36+
) : OverlayScene<T> {
37+
38+
override val entries: List<NavEntry<T>> = listOf(entry)
39+
40+
override val content: @Composable (() -> Unit) = {
41+
Box(
42+
contentAlignment = Alignment.Center,
43+
modifier = Modifier
44+
.fillMaxSize()
45+
.background(Color.Black.copy(alpha = 0.5f))
46+
.clickable(onClick = onBack, indication = null, interactionSource = remember { MutableInteractionSource() })
47+
) {
48+
FloconSurface(
49+
shape = FloconTheme.shapes.small,
50+
modifier = Modifier.fillMaxSize(0.9f)
51+
) {
52+
entry.Content()
53+
}
54+
FloconIconTonalButton(
55+
onClick = onBack,
56+
modifier = Modifier
57+
.align(Alignment.TopEnd)
58+
.offset {
59+
IntOffset(
60+
x = -16.dp.roundToPx(),
61+
y = 16.dp.roundToPx()
62+
)
63+
}
64+
) {
65+
FloconIcon(
66+
imageVector = Icons.Sharp.Close
67+
)
68+
}
69+
}
70+
}
71+
}
72+
73+
public class InnerWindowSceneStrategy<T : Any>() : SceneStrategy<T> {
74+
75+
public override fun SceneStrategyScope<T>.calculateScene(
76+
entries: List<NavEntry<T>>
77+
): Scene<T>? {
78+
val lastEntry = entries.lastOrNull() ?: return null
79+
80+
if (lastEntry.metadata[INNER_WINDOW_KEY] == true) {
81+
return InnerWindowScene(
82+
key = lastEntry.contentKey,
83+
entry = lastEntry,
84+
previousEntries = entries.dropLast(1),
85+
overlaidEntries = entries.dropLast(1),
86+
onBack = onBack,
87+
)
88+
}
89+
90+
return null
91+
}
92+
93+
public companion object {
94+
private val INNER_WINDOW_KEY = InnerWindowScene::class.qualifiedName!!
95+
96+
public fun innerWindow(): Map<String, Any> = mapOf(INNER_WINDOW_KEY to true)
97+
}
98+
}

0 commit comments

Comments
 (0)