JetBrains / compose-multiplatform

Compose Multiplatform, a modern UI framework for Kotlin that makes building performant and beautiful user interfaces easy and enjoyable.
https://jetbrains.com/lp/compose-multiplatform
Apache License 2.0
16.21k stars 1.17k forks source link

DropdownMenu performs onDismissRequest twice #4080

Closed tangshimin closed 9 months ago

tangshimin commented 10 months ago

Describe the bug Sometimes DropdownMenu executes onDismissRequest twice. CursorDropdownMenu has the same issue.

Affected platforms

Versions

To Reproduce

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Settings
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Window
import androidx.compose.ui.window.application

fun main() = application {
    Window(onCloseRequest = ::exitApplication) {
        Box(Modifier.fillMaxSize()){
            var settingsExpanded by remember { mutableStateOf(false) }
            var counter by remember { mutableStateOf(0) }
            Row(
                horizontalArrangement = Arrangement.SpaceBetween,
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier
                    .align(Alignment.TopCenter)
                    .clickable { settingsExpanded = true }
                    .width(48.dp)
                    .height(48.dp)
            ) {
                Icon(
                    imageVector = Icons.Filled.Settings,
                    contentDescription = "",
                    tint = MaterialTheme.colors.onBackground,
                    modifier = Modifier.size(48.dp, 48.dp).padding(top = 12.dp, bottom = 12.dp)
                )
                DropdownMenu(
                    expanded = settingsExpanded,
                    onDismissRequest = {
                        settingsExpanded = false
                        counter++
                        println("DismissRequest $counter")
                    },
                ) {
                    Surface {
                        Column(Modifier.width(80.dp).height(180.dp).padding(start = 16.dp, end = 16.dp)) {
                            Text("Item 1")
                            Text("Item 2")
                            Text("Item 3")
                            Text("Item 4")
                            Text("Item 5")
                        }
                    }
                }

            }
        }
    }

}

https://github.com/JetBrains/compose-multiplatform/assets/16540656/1f57cbb6-b3e9-4af9-861e-5d74f12ff644

okushnikov commented 2 months ago

Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks.