From 0a5956d428a45ccad35f9882bb8f41df1640b1f8 Mon Sep 17 00:00:00 2001 From: "Jack Boswell (boswelja)" Date: Mon, 8 Jan 2024 23:18:18 +1300 Subject: [PATCH 1/2] Create a MenuHost with new behaviors --- .../boswelja/menuprovider/DefaultMenuHost.kt | 29 -------- .../boswelja/menuprovider/MenuHostImpls.kt | 72 +++++++++++++++++++ .../boswelja/menuprovider/ProvideMenuHost.kt | 18 +++++ 3 files changed, 90 insertions(+), 29 deletions(-) delete mode 100644 core/src/commonMain/kotlin/com/boswelja/menuprovider/DefaultMenuHost.kt create mode 100644 core/src/commonMain/kotlin/com/boswelja/menuprovider/MenuHostImpls.kt diff --git a/core/src/commonMain/kotlin/com/boswelja/menuprovider/DefaultMenuHost.kt b/core/src/commonMain/kotlin/com/boswelja/menuprovider/DefaultMenuHost.kt deleted file mode 100644 index 16e6075..0000000 --- a/core/src/commonMain/kotlin/com/boswelja/menuprovider/DefaultMenuHost.kt +++ /dev/null @@ -1,29 +0,0 @@ -package com.boswelja.menuprovider - -import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateListOf -import androidx.compose.runtime.remember - -internal class DefaultMenuHost : MenuHost { - private val _menuItems = mutableStateListOf() - - override val menuItems: List = _menuItems - - override fun addItems(vararg newItems: MenuItem) { - _menuItems.addAll(newItems) - } - - override fun removeItems(vararg items: MenuItem) { - _menuItems.removeAll(items.toSet()) - } -} - -/** - * Creates a new [MenuHost] for use in Composition. This should be provided via [LocalMenuHost]. - */ -@Composable -public fun rememberMenuHost(): MenuHost { - return remember { - DefaultMenuHost() - } -} diff --git a/core/src/commonMain/kotlin/com/boswelja/menuprovider/MenuHostImpls.kt b/core/src/commonMain/kotlin/com/boswelja/menuprovider/MenuHostImpls.kt new file mode 100644 index 0000000..072e86d --- /dev/null +++ b/core/src/commonMain/kotlin/com/boswelja/menuprovider/MenuHostImpls.kt @@ -0,0 +1,72 @@ +package com.boswelja.menuprovider + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateListOf +import androidx.compose.runtime.remember + +internal class CumulativeMenuHost : MenuHost { + private val _menuItems = mutableStateListOf() + + override val menuItems: List = _menuItems + + override fun addItems(vararg newItems: MenuItem) { + _menuItems.addAll(newItems) + } + + override fun removeItems(vararg items: MenuItem) { + _menuItems.removeAll(items.toSet()) + } +} + +internal class SingleTopMenuHost : MenuHost { + private val _menuItems = mutableStateListOf>() + + override val menuItems: List = _menuItems.lastOrNull().orEmpty() + + override fun addItems(vararg newItems: MenuItem) { + _menuItems.add(newItems.toList()) + } + + override fun removeItems(vararg items: MenuItem) { + _menuItems.remove(items.toList()) + } +} + +/** + * Creates a new [MenuHost] for use in Composition. This should be provided via [LocalMenuHost]. + */ +@Composable +@Deprecated( + "There are MenuHosts with different behaviors out-of-the-box.\n" + + "For a MenuHost that behaves the same, please switch to rememberCumulativeMenuHost", + replaceWith = ReplaceWith( + expression = "rememberCumulativeMenuHost()" + ) +) +public fun rememberMenuHost(): MenuHost { + return rememberCumulativeMenuHost() +} + +/** + * Remembers a new [MenuHost] that "accumulates" all menu items provided from direct descendants. In + * other words, every MenuItem provided to this MenuHost will be exposed to [MenuHost.menuItems] at + * once. + */ +@Composable +public fun rememberCumulativeMenuHost(): MenuHost { + return remember { + CumulativeMenuHost() + } +} + +/** + * Remembers a new [MenuHost] that only exposes the last set of MenuItems to [MenuHost.menuItems]. + * Inx other words, only the last group of MenuItems that were provided will be returned by + * [MenuHost.menuItems]. + */ +@Composable +public fun rememberSingleTopMenuHost(): MenuHost { + return remember { + SingleTopMenuHost() + } +} diff --git a/core/src/commonMain/kotlin/com/boswelja/menuprovider/ProvideMenuHost.kt b/core/src/commonMain/kotlin/com/boswelja/menuprovider/ProvideMenuHost.kt index 44345e5..4d8646c 100644 --- a/core/src/commonMain/kotlin/com/boswelja/menuprovider/ProvideMenuHost.kt +++ b/core/src/commonMain/kotlin/com/boswelja/menuprovider/ProvideMenuHost.kt @@ -16,3 +16,21 @@ public fun ProvideMenuHost( content = content, ) } + +/** + * A convenience function for providing a cumulative MenuHost. See [rememberCumulativeMenuHost] for + * details. To provide your own MenuHost, use [ProvideMenuHost]. + */ +@Composable +public fun ProvideCumulativeMenuHost(content: @Composable () -> Unit) { + ProvideMenuHost(menuHost = rememberCumulativeMenuHost(), content = content) +} + +/** + * A convenience function for providing a single-top MenuHost. See [rememberCumulativeMenuHost] for + * details. To provide your own MenuHost, use [ProvideMenuHost]. + */ +@Composable +public fun ProvideSingleTopMenuHost(content: @Composable () -> Unit) { + ProvideMenuHost(menuHost = rememberSingleTopMenuHost(), content = content) +} From e04f5a1e2b50b483d590bc5e569dab3250886b64 Mon Sep 17 00:00:00 2001 From: "Jack Boswell (boswelja)" Date: Mon, 8 Jan 2024 23:18:47 +1300 Subject: [PATCH 2/2] Update docs --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d5f6c2e..f6ff9aa 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ dependencies { ```kotlin @Composable fun MyComposable() { - ProvideMenuHost { + ProvideCumulativeMenuHost { Scaffold( topBar = { TopAppBar(