GUI elements whose positions depend on one another

Started by
11 comments, last by Zondartul 8 years, 5 months ago

I worked for a while on a layout library, called HBox. Here is an excerpt from the linked page:

Some GUI libraries come with their own layout facilities. Other libraries require the user to take care of all GUI layout functions: positioning and sizing elements, responding to size change, enforcing minimum/maximum constraints, hit-testing... etc. Whether it's for an existing GUI library, or as a back component of your own library, HBox aims to take care of all your layout needs.

HBox is a small C++ library with a simple API. The API is composed of a single HBox class, which represents a box. Naturally the boxes are hierarchically related. Each box will layout its children when changes are made to its size and other properties. The final layout depends on the "solver" of the parent, as well as the properties of the children.

Each box can have margins and padding, similar to the CSS box model. HBox however does not attempt to recreate the same behavior and rules of CSS, so it is only conceptually similar to the CSS box model, and HBox attributes do not necessarily mean the same as their CSS counterparts. Also, HBox currently has no support for thick borders or other border properties.

hbox-ss.png

HBox aims to be versatile and simple at the same time. Complex layouts can be accomplished by using additional boxes that don't correspond directly to GUI elements. Using such boxes with the right "solver" types allows this versatility to create all sorts of layouts. More about that later.

HBox supports 3 types of property/distance units: absolute (pixels if you'd like), percentage, and flex units. Flex units are like springs, with minimum and maximum constraints. Again, more on that later.

It's not finished yet unfortunately, so it's not very useful to you at this stage. It implements the mentioned two-pass approach to resolving sizes and positions of boxes. I hope this has been helpful somehow. If you'd like the source code, let me know.

Advertisement


Why don't you have a look at the code for QT or WxWidgets?

There are simpler options to read (insert shameless plug for simplui in that respect).

I'm not sure I've ever encountered a GUI framework that *doesn't* do 2-pass measure and layout. It should allow you to solve most non-recursive constraints with relative ease (as long as you avoid pathological edge-cases like Android's RelativeLayout).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Thank you all for your advice! I deliberated between using the method I posted and the two-pass layout as seen in most GUI frameworks.

The method I was planning to do has some issues, so I'll look into how Qt / simplui / WxWidgets do the layout. I'll tell you if I figure anything out.

Turns out, the method in my second post, the one that involves a "constraint solver" that simply constructs a graph and then "fixes" constraints one-by-one in the order of dependency, in the general case suffers from a multiple-writer problem as constraints don't know about each other. It would probably be possible to have all constraints satisfied simultaneously if they were posed as a set of linear equations, and then the whole system could be solved using matrices or some such. Buuut... that's kind of too difficult for me to handle. And it might, possibly, make the constraints too unintuitive for the user. So I'm sticking with the more common method for now.

This topic is closed to new replies.

Advertisement