Convert layout code to use logical directions (rebase)

This is a rebase/squash of #2797. See PR comments there.

r? @pcwalton