Humans are not constraint solvers
amigueloferreira.pt
amigueloferreira.pt
The article criticizes what it calls "source-order independence" where "you can have two unrelated components be involved in a constraint." We called this "cross-cutting constraints," and discouraged their use for exactly the reason the author gives. Most constraints attached to you should be to yourself, a sibling, or a parent. Cross-cutting constraints are not a primary benefit of constraint-based layout.
So what do constraint-based layouts give us? A key one is that constraints regularize layout under a uniform language. You are given a handful of properties (width, height, two centers, four edges, and baseline) and constants, and can relate them using equalities, inequalities, and priorities. From those primitive elements, you can build a lot of stuff that other frameworks handle in an ad-hoc manner.
Want to set a fixed width? Equate the width to a constant. Make it a minimum width? Change the relation to >=. Right align? Equate the two right edges. Centering? Equate the two centers. Two containers ought to have equal widths? Relate their widths to each other. Aspect ratio? Relate the width to the height. All this stuff just falls out naturally, requiring no special support.
Compare to CSS, which supports many of these features, but in a completely arbitrary, ad-hoc way. Right-aligning requires setting a position, but center-aligning requires setting margin-left and margin-right, and vertical centering requires a completely different set of hackish techniques. Or compare to Android, which has lots of ad-hoc methods to achieve specific layout tasks (setMeasureWithLargestChildEnabled, setBaselineAligned, etc).
Autolayout has its share of problems: it's verbose, has a steep learning curve, works poorly with grid-type layouts, struggles with nonlinear layouts like text reflowing, and more. But its approach of a uniform layout language is powerful and elegant, compared to ad-hoc "bag of properties" layout mechanisms.
We do all our iOS layout in code, and all our Android layout in XML (or all that we can, save the 1% that necessitates munging in the Java). This probably affects my opinion, since Java layout code for Android is just as obtuse as the objective-c autolayout, if not more so.
Everything else works well with absolute positioning. Need something vertically aligned?
position: absolute;
top: 50%;
margin-top: (1/2 height of element)Your intention is to align both centers. Why do we need to be dealing with 'top' and 'margin-top' to achieve this? It is a poor way to describe your intention.
It's much easier to say:
box1.center == box2.center
The cassowary solver allows you to specify exactly your intentions in relation to the edges and centers of boxes. It's easier to reason about and doesn't require the bag of weird tricks that are needed in CSS (eg. margin: 0 auto; to center??? wtf?)As of just a few months ago, it is. Look into the now-almost-standardised (last call working draft) version of flexbox, which is implemented in recent versions of Firefox and Chrome, and also IE11.
An example of what you say is impossible, in just a few lines of CSS: http://philipwalton.github.io/solved-by-flexbox/demos/sticky...
http://philipwalton.github.io/solved-by-flexbox/demos/holy-g...
and now it is:
http://philipwalton.github.io/solved-by-flexbox/demos/sticky...
The latter is not what I described, and I can't get the former to work even in the latest Firefox.
I'm using Firefox 32.0.3, and both examples work for me perfectly. I'm not sure what the issue is.
According to: http://css-tricks.com/snippets/css/a-guide-to-flexbox/ Firefox should work from v22 on.
A constraint solver interface is a good match to a GUI. It's not a good match to a textual interface. Web designers should be using a layout tool like Dreamweaver, not hand-coding constraint expressions.
For an example of a constraint-based tool for a much harder problem, see Autodesk Inventor, which is a 3D CAD system. It has 2D sketches, and those are controlled by constraints. You can draw rectangles, and if they have coincident points or edges when drawn, those are constrained to stay together. You can explicitly dimension something, and it becomes that length. If you try to apply inconsistent constraints, the system rejects them. You can make all constraints visible as icons, then delete any you don't want. Web designers would probably love the power of Inventor sketch mode, because it's not restricted to horizontal and vertical lines, or even to straight lines. That might be overkill for web design, although being able to wrap text around a curved image would make some people happy. Inventor demonstrates that a much harder constraint problem than web design has been successfully solved with a good GUI.
Asking visual designers to write constraint expressions by hand is pounding a screw with a hammer. Use the right tool for the job.
If you don't have a constraint solver in your layout engine, you had better be a good one yourself, because somebody has to do it.
All of this has been solved long time ago at Java Swing. Checkout Mig Layout.