Hacker News new | past | comments | ask | show | jobs | submit | patchoulol's favorites login

(Original author of css-layout here)

I've spend a few months playing with Cassowary before working on css-layout and had long chats with its creator Greg Badros and the people behind https://gridstylesheets.org/.

Cassowary underlying abstraction is a linear inequation solver. So you give it a bunch of constraints of the form ax + by + cz + d < 0 and it's going to give you values for x, y and z.

## There are two innovations in Cassowary:

1) There's a concept of strength (required, strong, medium, weak) which allows you to define which rules should be dropped in case of conflicts. This is a very different way to think about building UI and was a really fun exercise.

That said, I had to build a dedicated tool to show me which rules where overridden or why otherwise it was very hard to understand what happened when the layout didn't work as I expected.

2) Cassowary is an iterative solver, this means that it's not going to recompute the entire simplex from scratch on every tiny update.

## The biggest issue with Cassowary is that you cannot express everything with a linear system of equations:

The most annoying instance is line breaking. You cannot encode how lines are going to break as a set of linear inequations. In practice, what happens is that you're going to "fake it" by iterating outside of the simplex by first giving it a rough size and then do another pass with the real one. It usually works but you are not guaranteed for it to converge nor to have the optimal layout.

## In practice

It's extremely painful to write all the inequations yourself. If you want to have a container with an item of height 100px with 5px of padding it looks like.

    obj.left = parent.left + 5
    obj.top = parent.top + 5
    obj.right = parent.right - 5
    obj.height = 100
    parent.height = obj + 5
For every single object you need to define 4 inequations to "define": top, left, width and height which is very verbose.

So I started writing abstractions for containers, padding... At some point, I realized that I was just reimplementing flexbox. But it was a worst implementation of flexbox because it didn't properly work with text and was way slower.

The other difficulty is that you need references to all those elements. In order to tell where an element is, you need a reference to your parent. This model doesn't work well with React components where you only shouldn't know about your parent.

## Where it shines

The places where constraint solver shines is when you have a wysiwyg element builder. You have a reference to all the elements as they are visible on-screen so it's very easy to link them and you can show 4 inputs for each constraint.

This is very useful for xcode interface builder and would be a good fit for designer tools like photoshop.

Hopefully this gives some light around why we didn't proceed with cassowary :)


Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: