Context menus
Replace Compose-drawn context menus with the platform menu, NSMenu on macOS and a Fluent, Adwaita, or Breeze flyout on Windows and Linux.
Nucleus replaces Compose's context menus with the platform menu: an NSMenu on macOS, and a
flyout styled as Fluent on Windows, Adwaita on GTK desktops, or Breeze on Qt desktops. It comes
with nucleus-application.
Turn on the native menu
fun main() = nucleusApplication {
DecoratedWindow(
onCloseRequest = ::exitApplication,
title = "MyApp",
nativeContextMenu = true,
) {
// Text fields now show the platform Cut/Copy/Paste menu
}
}The flag covers ContextMenuArea, the text-field Cut/Copy/Paste menu, and
spell-check suggestions. HostedWindow and the Material and Jewel windows
take the same parameter. The style follows the platform, and on Linux the session's toolkit.
Add your own items
NucleusContextMenuItem extends Compose's ContextMenuItem with an icon and a shortcut label:
ContextMenuArea(
items = {
listOf(
NucleusContextMenuItem(
label = "Reveal in Finder",
icon = ContextMenuIcon.Folder,
onClick = { revealInFileManager(path) },
),
NucleusContextMenuDivider,
NucleusContextMenuItem(
label = "Delete",
icon = ContextMenuIcon.Delete,
shortcut = "Del",
onClick = { delete(path) },
),
)
},
) {
FileRow(path)
}NucleusContextMenuSubmenu nests a group. Its items lambda runs when the menu opens, so it can
list data that changes:
NucleusContextMenuSubmenu(label = "Open recent") { recentFiles.map { fileItem(it) } }Icons differ per platform: Adwaita shows none, and ContextMenuIcon.SfSymbol applies to macOS
only.
Show keyboard shortcuts
An item with a stock editing icon gets the platform label on its own: ⌘C on macOS, Ctrl+C
elsewhere. Pass shortcut to override it, or shortcut = "" to hide it. The macOS NSMenu does
not show shortcut labels.
How the flyout behaves on screen
Changed in 3.0
On Windows and Linux the flyout opens in its own native popup surface, even when the window keeps
nativePopupLayers = false. Your other popups still follow that flag.
The flyout can leave its window and stays within the work area of its display. A second right click while the menu is open moves it to the new spot. The flyout closes when its window loses focus.
To give one of your own popups the same treatment, wrap it:
NativePopupLayers {
DropdownMenu(expanded = open, onDismissRequest = { open = false }) { /* … */ }
}See Dialogs and popups for the full popup-layer model.
Detect the Linux toolkit
Read the value Nucleus uses to pick the Linux style, for example to match another widget:
when (LinuxUiToolkit.Current) {
LinuxUiToolkit.Qt -> // KDE Plasma, LXQt, Deepin, …
LinuxUiToolkit.Gtk -> // GNOME, Xfce, Cinnamon, MATE, …
}Unknown desktops and bare tiling window managers resolve to Gtk.
API reference
Package dev.nucleusframework.application.contextmenu.
| Symbol | Description |
|---|---|
NucleusContextMenuItem(label, enabled, icon, shortcut, onClick) | Item with an optional icon and shortcut label. |
NucleusContextMenuDivider | Separator item. |
NucleusContextMenuSubmenu(label) { items } | Nested group. items runs when the menu opens. |
ContextMenuIcon | Cut, Copy, Paste, SelectAll, Delete, Folder, SfSymbol(name). |
ContextMenuIcon.stockShortcut() | Platform shortcut label for the editing icons, else null. |
NativeContextMenuProvider(enabled) { } | Installs the native menu for a subtree without the window parameter. |
isNativeContextMenuSupported | Whether this platform can show it. |
LocalContextMenuDivider | The separator the active renderer draws, or null. |
LocalContextMenuItemInterpreter | Overrides how Compose items map to ContextMenuEntry for a subtree. |
NativePopupLayers { } | Package dev.nucleusframework.window.tao. Opens a subtree's popups in native surfaces. |
To show an NSMenu yourself, menu-macos provides popUpNativeMenu(items) with
NativePopupMenuItem entries. It blocks until the menu closes.
Call popUpNativeMenu off the UI thread. From a UI-thread callback it stops frames, animations,
Dispatchers.Main, and delay until the menu closes.
What's next
- Spell check: suggestions inside these menus.
- Dialogs and popups: native popup layers for the rest of your popups.
- macOS menu bar: the application menu, on the same native layer.
Last updated on