Corntex
0.0.3indexedFlexible context menu library enhances desktop applications with hierarchical submenus, customizable styling, right-click detection, keyboard navigation, and manual state management for advanced control.
Flexible context menu library enhances desktop applications with hierarchical submenus, customizable styling, right-click detection, keyboard navigation, and manual state management for advanced control.
A flexible and hierarchical context menu library for Compose Desktop applications that supports nested submenus, keyboard navigation, and customizable styling.
https://github.com/user-attachments/assets/c6fa77aa-65b2-4ae3-9e4d-a849c937c82f
dependencies {
implementation("io.github.thedroiddiv:corntex:<latest-version>")
}
@Composable
fun MyComponent() {
ContextMenuArea(
items = {
listOf(
ContextMenuEntry.Single(label = "Cut", onClick = { /* Handle cut */ }),
ContextMenuEntry.Single(label = "Copy", onClick = { /* Handle copy */ }),
ContextMenuEntry.Single(label = "Paste", onClick = { /* Handle paste */ })
)
}
) {
Text("Right-click me for context menu!")
}
}
For more control over menu behavior, you can manage the state manually:
@Composable
fun ManualStateExample {
contextMenuState = rememberContextMenuState()
selectedItem remember { mutableStateOf<String?>() }
ContextMenuArea(state = contextMenuState) {
ListItem(
modifier = Modifier.onClick(
enabled = ,
interactionSource = MutableInteractionSource()
) {
selectedItem =
contextMenuState.show(
position = IntOffset(, ),
items = listOf(
ContextMenuEntry.Single(
,
onClick = { }),
ContextMenuEntry.Single(
,
onClick = { })
)
)
}
) {
Text()
}
}
}
The main composable for creating context menu areas.
@Composable
fun ContextMenuArea(
items: () -> List<ContextMenuEntry>,
modifier: Modifier = Modifier,
content: @Composable () -> Unit
)
@Composable
fun ContextMenuArea(
modifier: Modifier = Modifier,
state: ,
content: @ () ->
)
State holder for managing context menu visibility and hierarchy.
class HierarchicalContextMenuState {
val openMenus: List<MenuLevel>
fun show(position: IntOffset, items: List<ContextMenuEntry>)
fun hide()
fun onItemHover
}
@Composable
fun rememberContextMenuState(): HierarchicalContextMenuState
Creates and remembers a context menu state for the current composition.
fun Modifier.contextMenuOpenDetector(
state: HierarchicalContextMenuState,
items: () -> List<ContextMenuEntry>
): Modifier
Modifier that detects right-click events and shows the context menu.
The library uses a theme system for consistent styling. You can customize colors, dimensions and typography through the ContextMenuTheme:
ContextMenuTheme(
colors = darkContextMenuColor // or provide a .copy/custom instance,
tokens = defaultContextMenuTokens // or provide a .copy/ustom instance,
typography = defaultContextMenuTypography // or provide a .copy/custom instance
) {
DesktopApp()
}
For complete customization, implement your own ContextMenuRepresentation:
class CustomContextMenuRepresentation : ContextMenuRepresentation {
@Composable override fun Representation(state: HierarchicalContextMenuState) {
// Your custom menu implementation
}
}
fun AdvancedContextMenu() {
ContextMenuArea(items = {
listOf(
ContextMenuEntry.Single(
label = "New File",
icon = painterResource("icons/file.png"),
onClick = { /* Create new file */ }
),
ContextMenuEntry.Submenu(
label = "Export As",
icon = painterResource("icons/export.png"),
submenuItems = listOf(
ContextMenuEntry.Single("PDF", onClick = { /* Export as PDF */ }),
ContextMenuEntry.Single("PNG", onClick = { /* Export as PNG */ }),
ContextMenuEntry.Divider,
ContextMenuEntry.Single("Other...", onClick = { /* Show export dialog */ })
)
),
ContextMenuEntry.Divider, ContextMenuEntry.Single(
label = "Delete",
enabled = false, // Disabled item
onClick = { /* Handle delete */ }
)
)
}
) {
// Your content here
Card(modifier = Modifier.size(200.dp)) {
Text("Right-click for advanced menu")
}
}
}
Surfaced from shared tags and platforms — no rankings paid for.