6 ms·
Why not just embrace Cassowary (https://en.wikipedia.org/wiki/Cassowary_(software) https://en.wikipedia.org/wiki/Cassowary_(software)) aka the algorithm behind
by adamnemecek 10y ago
Why not just embrace Cassowary (https://en.wikipedia.org/wiki/Cassowary_(software) https://en.wikipedia.org/wiki/Cassowary_(software)) aka the algorithm behind Cocoa's Auto Layout? Auto Layout isn't perfect but I think that it's fundamentally the API/UI that you are working with, the concepts are definitely at the right level of abstraction. Some sort of DSL could really solve this.
Flexbox could be implemented as a framework on top of this.
- drcode 10y agoPete Hunt, one of the original developers of React at Facebook, began work on a Cassowary-like layout engine at one point and came to the conclusion (whether rightly or wrongly) that arbitrary constraint-based layout is too user hostile to pursue further. I too like constraint-based layout, but I have to admit flexbox is nice in that it has very predictable behavior. The main hangup I have with flexbox is that it doesn't allow for layout based on aspect ratios, but Yoga has extensions that resolve this issue.
- adamnemecek 10y ago> Pete Hunt, one of the original developers of React at Facebook, began work on a Cassowary-like layout engine at one point and came to the conclusion (whether rightly or wrongly) that arbitrary constraint-based layout is too user hostile to pursue further. I think that pg's point #2 from this post applies http://paulgraham.com/javacover.html http://paulgraham.com/javacover.html
- OldSchoolJohnny 10y agoOff topic but I read #2 and I can't for the life of me think of why he would consider Cobol a bad language, perhaps he has no personal experience with it.
- adamnemecek 10y agoThere are some mentions here http://paulgraham.com/popular.html http://paulgraham.com/popular.html "It is a mistake to try to baby the user with long-winded expressions that are meant to resemble English. Cobol is notorious for this flaw. A hacker would consider being asked to write."
- iLoch 10y agoSpeaking as someone coming from the web, auto-layout is really awful. You're way better off making UI easy to reason about for web developers transitioning to mobile, because that's where most of the talent comes from. By DSL, do you mean something like HTML/CSS? React Native basically has this with JSX and style properties which use Yoga under the hood.
- stefano 10y agoI have a web background myself, but I always found auto layout a pleasure to work with. I use it with http://snapkit.io/docs/ http://snapkit.io/docs/ (a DSL built on top of the iOS APIs) and with programmatically written UIs. I find it so much easier to reason about than CSS.
- krschultz 10y agoAutoLayout basically directly exposes that algorithm and all of its complexity to the developer. That makes it the most powerful, but also the hardest to learn. Flexbox is a nice middle ground where you can express most things in a sane manner, but it's not the single layout engine to rule them all.
- on_and_off 10y agoAndroid has started embracing it with ConstraintLayout. It implements a Cassowary solver in order to resolve the constraint imputed by the dev. I have been using it for some time and it is an huge step in the right direction. A downside of Yoga seems to be its tooling, ConstraintLayout is very well integrated with Android Studio. Actually they created it hand in hand with a rewrite of its layout editor.
- andrewflnr 10y agoI think you're thinking of RelativeLayout? I don't have ConstraintLayout in my IDE right now, but RelativeLayout sounds like what you're talking about.
- on_and_off 10y agoNope, I am refering to ConstraintLayout. RelativeLayout does not use Cassowary (even though for an API consumer, this is just an implementation detail). Actually, CL one of the reasons behind the creation of RL is to improve on RL : - remove the need to nest for complex layouts. Nesting has some intrinsic measure/layout overhead. CL solves this by allowing way more types of relative constraint. The only remaining reasons for manual ViewGroups are performances (still faster than any generalistic approach) or really deep customization. - solve some of RL qwirks. There are a couple of behaviors which are surprising. CL streamline these corner cases. - deeper layouting possibilities - integrated with a better layout editor. By better, I mean something you might choose to use, unlike the previous layout editor. CL is in beta right now, you can add it as an external lib (that way the android team can update it anytime they want). The layout itself is already useable in production, the layout editor is where there are still some potential bugs.
- sandGorgon 10y agoThis is incredible! is this going to be paired with a wysiwyg tool... because i have a feeling that writing the constraints is going to get frustrating very quickly.
- vjeux 10y ago(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/ 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 :)