Skip to content

Conversation

@m-sasha
Copy link

@m-sasha m-sasha commented Dec 3, 2025

TextContextMenu can now create disabled menu items via ContextMenuItemWithEnabledState and they will be shown as such in the context menu.

The default TextContextMenu implementation (TextContextMenu.Default) utilizes this to disable, rather than remove irrelevant menu items. For example, when there is no selection in a text field, the "Copy" menu item will be disabled rather than hidden.

To get the previous behavior where the irrelevant item is hidden, use TextContextMenu.HideDisabledMenuItems like so:

CompositionLocalProvider(LocalTextContextMenu provides TextContextMenu.HideDisabledMenuItems) {
    TextField(
        state = rememberTextFieldState("Hello, World!"),
        modifier = Modifier.width(200.dp)
    )
}

Note that this is all relevant only for the old text context menus (when ComposeFoundationFlags.isNewContextMenuEnabled is false).

Fixes https://youtrack.jetbrains.com/issue/CMP-9329
Fixes https://youtrack.jetbrains.com/issue/CMP-9342

Testing

Added unit tests and also tested manually

This should be tested by QA

Release Notes

Fixes - Desktop

  • When there is no selection in SelectionContainer, the "Copy" menu item will be disabled.

Features - Desktop

  • Menu items in the context menu for text fields and SelectionContainer can now be disabled.

@m-sasha m-sasha force-pushed the m-sasha/disabled-text-context-menu-items branch from d1cd25d to e4fefcf Compare December 3, 2025 14:09
@m-sasha m-sasha requested review from MatkovIvan and igordmn December 3, 2025 15:14
Comment on lines -188 to -196
private fun SemanticsNodeInteraction.assertMenuItemDoesNotExistOrIsDisabled() {
// New context menus disabled items; old context menus don't show disabled items.
if (ComposeFoundationFlags.isNewContextMenuEnabled) {
assertIsNotEnabled()
} else {
assertDoesNotExist()
}
}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you'll still need this if you add tests for the "hide disabled items" menu, which seem to be missing

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contextMenuArea disables or removes copy action for SelectionContainer with empty selection tests this once, but since the default behavior is now to disable the menu item, there's no need for this function (and also the behavior no longer depends on isNewContextMenuEnabled, but on LocalTextContextMenu).

@m-sasha m-sasha requested a review from igordmn December 4, 2025 12:11
Copy link
Collaborator

@igordmn igordmn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs a second approval

@m-sasha
Copy link
Author

m-sasha commented Dec 4, 2025

Needs a second approval

@MatkovIvan

Copy link
Member

@terrakok terrakok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems OK for me, but I'm far from Desktop target API and specifics

@m-sasha m-sasha merged commit c3a7e38 into jb-main Dec 8, 2025
25 of 26 checks passed
@m-sasha m-sasha deleted the m-sasha/disabled-text-context-menu-items branch December 8, 2025 10:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants