Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SelectionContainer not work with any clickable component #1450

Closed
HuixingWong opened this issue Nov 24, 2021 · 9 comments · Fixed by JetBrains/compose-multiplatform-core#1106
Assignees
Labels
bug Something isn't working desktop

Comments

@HuixingWong
Copy link

HuixingWong commented Nov 24, 2021

SelectionContainer {
       ClickableText(
           text = AnnotatedString("Click Me"),
           onClick = { offset ->
               println("ClickableText $offset -th character is clicked.")
           }
       )
   }

This code works fine on Android but does not respond to desktop clicks

@akurasov akurasov self-assigned this Dec 8, 2021
@akurasov akurasov added desktop bug Something isn't working labels Dec 8, 2021
@akurasov akurasov added this to the 1.1? milestone Jan 11, 2022
@akurasov akurasov added the AK label Jan 11, 2022
@akurasov
Copy link
Contributor

Reproduced on 1.0.0

@akurasov akurasov assigned igordmn and unassigned akurasov Jan 11, 2022
@akurasov akurasov removed the AK label Jan 11, 2022
@theapache64
Copy link
Contributor

Any update on this guys?

@wgryglas
Copy link
Contributor

wgryglas commented Apr 10, 2022

I can confirm it does not work still (compose 1.1.1, kotlin 1.6.10)

I was trying to wrap the Text with a Box as an InlineTextContent instead of using ClickableText in order to capture clicks - I thought ClickableText was the only one affected.
It turns out that the "hover" event propagates correctly because the interaction (background highlight) works.
However, the "click" event is not.

It seems SelectionContainer somehow prevents propagating the click event deeper to the remaining composables

@HuixingWong
Copy link
Author

@akurasov 😂 ?

@fnfunfunc
Copy link

Is this problem solved?

@gandrewstone
Copy link

gandrewstone commented May 25, 2023

Is this problem solved?

no @ compose 1.4.0, but you might solve your problem by wrapping the part you want to be clickable in DisableSelection.
something like:

SelectionContainer {

Text("selectable text")

DisableSelection {
Button(onClick = {
println("clicked!!") }) { Text("click me") }
}
}

}

@ialokim
Copy link

ialokim commented Aug 31, 2023

Here is another workaround which leaves the whole text selectable:

    var lastLayoutResult: TextLayoutResult? by remember { mutableStateOf(null) }

    BasicText(
        text = formattedString,
        modifier = modifier
            // workaround for https://github.com/JetBrains/compose-multiplatform/issues/1450
            // todo: when fixed, change to ClickableText and move logic to onClick parameter
            .onPointerEvent(PointerEventType.Release) {
                val offset = lastLayoutResult?.getOffsetForPosition(it.changes.first().position) ?: 0
                formattedString
                    .getStringAnnotations(tag = "link", start = offset, end = offset)
                    .firstOrNull()
                    ?.let { annotation ->
                        handleLink(annotation.item)
                    }
            },
        onTextLayout = { layoutResult ->
            lastLayoutResult = layoutResult
        }
    )

@sunny-chung
Copy link

sunny-chung commented Feb 13, 2024

The workaround using DisableSelection is not working in Compose Multiplatform 1.6.0-beta02. After clicking a selectable text, and then click the image button, the clickable handler is not triggered. It is only triggered on the consecutive second click.

var count by remember { mutableStateOf(0) }

SelectionContainer {
        Row {
            DisableSelection {
                Image(
                    painter = painterResource("copy-to-clipboard.svg"),
                    colorFilter = ColorFilter.tint(Color(0f, 0f, 1f)),
                    contentDescription = null,
                    modifier = Modifier.size(40.dp)
                        .clickable(onClick = { ++count })
                )
            }
            Text(text = "Count: $count", fontSize = 40.sp)
        }
}

MatkovIvan added a commit to JetBrains/compose-multiplatform-core that referenced this issue Feb 15, 2024
## Proposed Changes

- Do not recreate modifier nodes on focus change - Buttons inside
`DisableSelection` now clickable. Before it receives only second click
- Do not consume UP events if there were no drag before - Buttons inside
`SelectionContainer` now clickable, but text inside them is still
selectable
- `SelectionManager.hasFocus` now `true` if any of it's children is
focusable
- `onClearSelectionRequested` now triggers on consumed click on
focusable, non-selectable child

## Testing

Test: `Selection` page in mpp

## Issues Fixed

Fixes JetBrains/compose-multiplatform#1450
@okushnikov
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working desktop
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants