Hey guys, I'm using the `drawBehind` modifier, whi...
# compose-desktop
a
Hey guys, I'm using the
drawBehind
modifier, which doesn't provide a Composable scope. However I need a composable scope in order to use
rememberTextMeasurer
in
drawText
. How can I do that?
l
remember
it in a composable, then use it in
drawBehind
. If you don't have one, wrap it with
Modifier.composed
.
a
@Loney Chou Ah, I see thank you. It's annoying to have to pass text measurer instance into all my drawing functions, though 😕
e
That is what
Modifier.composed
is for
e
or use
drawWithCache
e.g. this will effectively "remember" the
TextMeasurer
across draw calls:
Copy code
val fontFamilyResolver = LocalFontFamilyResolver.current
Modifier
    .drawWithCache {
        val textMeasurer = TextMeasurer(fontFamilyResolver, this, layoutDirection)
        onDrawBehind {
            drawText(textMeasurer, "Hello, world!")
        }
    }
I suppose it doesn't really have a benefit over
rememberTextMeasurer()
outside, though
z
@Halil Ozercan Maybe we need a sibling to
rememberTextMeasurer
that can be used from
Modifier.Node
?
h
This was discussed the first time around we were designing the TextMeasurer. The best approach is to be able to instantiate a TextMeasurer solely from drawWithCache scope. Now that we have the ability to read locals (LocalFontFamilyResolver) in Modifier.Node, there should be nothing stopping us from doing that.
z
What if it’s also needed from a layout modifier, like for the BTF2 use case?
e
@Halil Ozercan reading Locals from node is not implemented in any released version right? Is there an open PR or something I can look at to see more details
e
Amazing! Is this making it into 1.4? (pls say yes black 😅)
h
@Andrew Bailey This was merged right after API freeze was lifted so I'm gonna assume it's part of 1.5 track?
a
Yes. It's going in as part of 1.5