Is it possible in compose to add different click h...
# compose
i
Is it possible in compose to add different click handlers on different logical layers. For example: library code provide the modifier, that provide some long click action. Client code use this modifier and also would like to add
onClick
handler. I tried few ways, but those click actions interfere between each other. Actually last one overrides first one. May I somehow combine library-long-click and client code click?
What have i tried: Construct different combintions of pointerInput and clickable, or with two different pointerInputs with different keys, but it was unsuccessfully
a
Client code use this modifier and also would like to add
onClick
handler.
how does that look like from an API pov?
i
It is tooltip api. When I added it first time, i didn't presume, that i'll use tooltip for clickable views. So at start modifier
tooltip
just provided own "onClick" behavior. For now I decided to add tooltip for clickable views, and the best way (in my head) was to add into api
boolean
flag
clickable
. Under the hood depends on this flag modifier can provide
onClick
or
onLongClick
action to show tooltip
a
im slow. can u show me code?
i
For now i am weirdo adapt it, but ok, let me to show )
This is tooltip modifier listing
Copy code
fun Modifier.tooltip(
    config: TooltipConfig = TooltipConfig(),
    onAnchorClick: (() -> Unit)? = null,
    content: @Composable () -> Unit,
) = composed {
    var coordinates by remember { mutableStateOf<LayoutCoordinates?>(null) }
    fun showTooltip() {
        tooltipBundle = coordinates?.let {
            TooltipBundle(content, config, it)
        }
    }
    onGloballyPositioned { coordinates = it }
        .then(if (onAnchorClick != null) {
            Modifier.combinedClickable(
                onLongClick = ::showTooltip,
                onClick = onAnchorClick
            )
        } else {
                Modifier.pointerInput("Modifier.tooltip.key") {
                    detectTapGestures(onPress = {
                        showTooltip()
                    })
                }
            })
}
onAnchorClick
is duct tape to combine long and short clicks
But i don't like api, that i made
In root of hierarchy there is
TooltipOverlay
, that show tooltip by
TooltipBundle
and coordinates, passed from anchor
And if view is not clickable tooltip just showed by click
Behavior that I want to reach is set
onClick
in client code, and somehow tell about that for that modifier, for example by flag
clickable
. And if
clickable
is
true
i want show tooltip with long click, and if it is
false
then i would like to show tooltip by shortClick
a
i'm confused towards what the problem is here. What is stopping you from achieving the api you want?
ie, what is stopping you from having a
clickable
parameter on the modifier parameter and the use it to use the respentive Modifier in your
if else
block? you are already doing it
i
I'd like to combine client code click handlers and api click handlers. And same time I want to place client code click handler and tooltip click handlers in client code and tooltip code respectively Problem is that
Modifier.tooltip
click handlers override client code click handlers if tooltip modifier applied after
clickable
modifier of client code. nd in opposite case if tooltip applied before that client code click handlers override tooltip click handlers For now code in listing works, but it is a bit of cringe, to pass client code click hndler into tooltip. Tooltip should know nothing about client code clicks, maximum - clickable view or not