feat(menu): add @Composable tray menu DSL#403
Open
kdroidFilter wants to merge 1 commit into
Open
Conversation
Lets users call painterResource() and other @composable functions directly inside Tray { … } and SubMenu { … } without hoisting them above application { … }. - ComposableTrayMenuScope: new public interface, every method is @composable and submenu lambdas are @composable too. - RecordingComposableScope: captures DSL calls during composition into a flat MenuOp tree, then replays them into the existing TrayMenuBuilder consumed by the native (Windows/Linux/Mac/Awt) impls. - New Tray() composable overloads for Painter / ImageVector / DrawableResource / iconContent / polymorphic Windows-vs-mac-linux, all taking @composable ComposableTrayMenuScope.() -> Unit. - @LowPriorityInOverloadResolution on the existing Tray() composables so existing demos auto-resolve to the new API transparently — the new scope is a strict superset (dispose() no-op, DrawableResource overloads, legacy CheckableItem(onToggle)). - Replace PainterResourceWorkaroundDemo with DemoComposableMenu; the workaround it taught is no longer needed.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lets users call
painterResource()and other@Composablefunctions directly insideTray { … }andSubMenu { … }— no more hoistingval icon = painterResource(...)aboveapplication { … }.application { Tray(icon = painterResource(Res.drawable.icon), tooltip = "App") { SubMenu(label = "Advanced", icon = painterResource(Res.drawable.advanced)) { Item(label = "Reload", icon = painterResource(Res.drawable.reload)) { /* … */ } } } }How it works
ComposableTrayMenuScope— new public interface. Every method is@Composableand submenu lambdas are@Composabletoo, so composable calls work at every level.RecordingComposableScope— captures the DSL calls during composition into a flatMenuOptree, then replays them into the existingTrayMenuBuilderconsumed by the native (Windows / Linux / macOS / AWT) impls. The native side is unchanged.Tray()composable overloads forPainter,ImageVector,DrawableResource,iconContent, and the polymorphic Windows-vs-mac-linux variants — all accepting@Composable ComposableTrayMenuScope.() -> Unit.@LowPriorityInOverloadResolutionon the existingTray()composables. The new scope is a strict superset ofTrayMenuBuilder(adds a no-opdispose(),DrawableResourceoverloads, legacyCheckableItem(onToggle)), so existing demos auto-resolve to the new API without source changes.PainterResourceWorkaroundDemo→DemoComposableMenu— the workaround the old demo taught is no longer needed.Notes
kotlin.internal.LowPriorityInOverloadResolutionwith@file:Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER")— internal stdlib annotation, widely used in practice but officially unsupported.Test plan
./gradlew buildpasses (main + demo modules + ktlint)DemoComposableMenuon Linux — menu opens, icons render in items and submenus, click handlers fireDemoComposableMenuon Windows — sameDemoComposableMenuon macOS — sameDemoWithDrawableResources) — confirms backward-compat resolution to new API still renders icons correctlyCheckableItemand verify the native menu reflects the new state on next open