lume / autolayout

Apple's Auto Layout and Visual Format Language for JavaScript (using cassowary constraints)
MIT License
305 stars 10 forks source link

[Question] Dynamic constraints/Removing constraints #3

Open theSherwood opened 6 months ago

theSherwood commented 6 months ago

Looking at the API, there don't seem to be any options for removing constraints or subviews. I didn't see anything in the docs or examples about how to handle the case of subviews getting removed or their relationships to other subviews changing. Is there any way to do that with this library?

trusktr commented 6 months ago

Currently we just make a new auto layout view with the new constraints like so:

https://github.com/lume/lume/blob/deb0d9a90db442dd4793e6e0920a252c31c5cce6/src/layouts/Autolayout.ts#L149-L152

trusktr commented 6 months ago

For now that's the only official way, as the View only has addConstraint() and no reciprocal removeConstraint():

The underlying kiwi.Solver does have both add and remove methods though:

To optimize this so that we don't have to recreate the whole View to change constraints, we need to hook up a new removeConstraints method to kiwi.Solver's.

theSherwood commented 6 months ago

Hey, thanks for the quick reply.

The underlying kiwi.Solver does have both add and remove methods though

I noticed that about the kiwi solver. Is adding removeConstraint on the roadmap or are you saying that you'd be open to the right PR?

I guess another wrinkle might be making sure that the VFL/EVFL parsers return the constraints they create so that they can be removed. It would also be nice to be able to remove a SubView and have it automatically remove all associated constraints.

theSherwood commented 6 months ago

I guess another wrinkle might be making sure that the VFL/EVFL parsers return the constraints they create so that they can be removed.

Looks like this is already the case.

I notice that there are also some constraints created implicitly in SubView. Those may or may not be the kinds of constraints that a user might want to remove. I don't know.

trusktr commented 6 months ago

Yeah, that would be robust. Add or remove things and it updates as we'd expect.

I don't have the time at the moment to get to this, but would be happy to merge a PR!

theSherwood commented 6 months ago

Looking more at this, I think it might make some sense to make some changes to Kiwi first. Are you open to PRs? There's no public API in Kiwi to return the constraints, for example.

trusktr commented 6 months ago

Yeah, totally open to PRs. Would be great to add that.