Skip to content

Comments

Fix JPopupContextMenuRepresentation.Representation reacting to changes in arguments#2702

Merged
m-sasha merged 1 commit intojb-mainfrom
m-sasha/fix-JPopupContextMenuRepresentation-remembering
Jan 20, 2026
Merged

Fix JPopupContextMenuRepresentation.Representation reacting to changes in arguments#2702
m-sasha merged 1 commit intojb-mainfrom
m-sasha/fix-JPopupContextMenuRepresentation-remembering

Conversation

@m-sasha
Copy link

@m-sasha m-sasha commented Jan 20, 2026

Fix JPopupContextMenuRepresentation.Representation reacting to changes in arguments

Fixes JPopupContextMenuRepresentation should key its remember {}

Testing

Unfortunately unit tests are not possible because

  • runComposeUiTest doesn't work because JPopupTextMenu needs an actual AWT window.
  • runApplicationTest is internal in ui.

Tested with

fun main() = singleWindowApplication {
    var background by remember { mutableStateOf(Color.WHITE) }
    val coroutineScope = rememberCoroutineScope()
    JPopupContextMenu(
        background = background,
        onContextMenuCreated = {
            coroutineScope.launch {
                delay(2000)
                background = Color(Random.nextInt())
            }
        }
    ) {
        Box(Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
            TextField(rememberTextFieldState("abc"), lineLimits = TextFieldLineLimits.SingleLine)
        }
    }
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun WindowScope.JPopupContextMenu(
    background: java.awt.Color,
    onContextMenuCreated: () -> Unit,
    content: @Composable () -> Unit
) {
    CompositionLocalProvider(
        LocalTextContextMenu provides JPopupTextMenu(window) { textManager, items ->
            onContextMenuCreated()

            fun swingMenuItem(text: String, action: TextContextMenu.Action) = JMenuItem(text).also {
                it.background = background
                it.addActionListener { action.execute() }
            }
            JPopupMenu().apply {
                textManager.cut?.also {
                    add(swingMenuItem("Cut", it))
                }
                textManager.copy?.also {
                    add(swingMenuItem("Copy", it))
                }
                textManager.paste?.also {
                    add(swingMenuItem("Paste", it))
                }
                textManager.selectAll?.also {
                    add(swingMenuItem("Select All", it))
                }
            }
        },
        content = content
    )
}

Open a context menu in the text field and observe the background change.

This should be tested by QA

Release Notes

Fixes - Desktop

  • Fixed JPopupContextMenuRepresentation to correctly recreate the menu when the createMenu argument changes.

@m-sasha m-sasha requested a review from igordmn January 20, 2026 13:25
@m-sasha m-sasha merged commit 15cc7b5 into jb-main Jan 20, 2026
17 of 18 checks passed
@m-sasha m-sasha deleted the m-sasha/fix-JPopupContextMenuRepresentation-remembering branch January 20, 2026 15:48
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.

2 participants