| 1 |
.. _uses-enaml: |
| 2 |
|
| 3 |
Enaml |
| 4 |
===== |
| 5 |
|
| 6 |
.. include:: ../substitutions.sub |
| 7 |
|
| 8 |
Enaml is a programming language and framework for creating professional-quality |
| 9 |
user interfaces with minimal effort. It relies on Kiwi to layout widgets. |
| 10 |
|
| 11 |
To implement its layout, Enaml uses a nestable model. Containers widgets |
| 12 |
handle the constraints generation used to layout their children. Furthermore, |
| 13 |
they pass to their their children a representation of the bounding box in |
| 14 |
which they should live which allows the widgets to position themselves inside |
| 15 |
their parent. Since each leaf component has a "preferred size", the system can |
| 16 |
be solved from the bottom-up and set the size the parents based on the required |
| 17 |
space of the children. If at a later time the parent is resized, this new input |
| 18 |
can be used to solve the layout problem. |
| 19 |
|
| 20 |
The following sections will describe in more details how the constraints are |
| 21 |
generated and the preferred size estimated. |
| 22 |
|
| 23 |
Widget variables |
| 24 |
---------------- |
| 25 |
|
| 26 |
In Enaml, each widget that can be constrained defines a bunch of Kiwi |
| 27 |
|Variable|: ``left``, ``top``, ``width`` and ``height``. In addition, |
| 28 |
it provides easy access to combination of those: ``right``, ``bottom``, |
| 29 |
``h_center``, ``v_center``. Those variables will be used to define the layout. |
| 30 |
|
| 31 |
In addition, because each widget has a preferred size, it defines a set of |
| 32 |
constraints related to that preferred size on which the user can act upon by |
| 33 |
modifying their strength: |
| 34 |
- hug_width: equivalent to (width == hint) | hug_width |
| 35 |
- hug_height: equivalent to (height == hint) | hug_height |
| 36 |
- resist_width: equivalent to (width >= hint) | resist_width |
| 37 |
- resist_height: equivalent to (height >= hint) | resist_height |
| 38 |
- limit_width: equivalent to (width <= hint) | limit_width |
| 39 |
- limit_height: equivalent to (height <= hint) | limit_height |
| 40 |
|
| 41 |
Finally, widget that can contain other widgets define a set of variables that |
| 42 |
they expose to their children to allow to place themselves relative to their |
| 43 |
parents. Those are: ``contents_left``, ``contents_top``, ``contents_width``, |
| 44 |
``contents_height``, ``contents_right``, ``contents_bottom``, |
| 45 |
``contents_h_center``, ``contents_v_center``. Those are usually not equivalent |
| 46 |
to the non-prefixed variables (even-though they are related) because of the |
| 47 |
container margins. |
| 48 |
|
| 49 |
The base classes used a mixin to implement those behaviors are defined in: |
| 50 |
https://github.com/nucleic/enaml/blob/main/enaml/layout/constrainable.py |
| 51 |
|
| 52 |
Constraints definition |
| 53 |
---------------------- |
| 54 |
|
| 55 |
Using the above variable, one can express any constraints. However, even for |
| 56 |
simple vertical or horizontal boxes, the constraints to define, in |
| 57 |
particular if one needs to introduce some spacing around the objects, become |
| 58 |
quite painful to write by hand. |
| 59 |
|
| 60 |
To make constraints definition easier, Enaml relies on helpers function and |
| 61 |
classes. In the following, we will focus on how horizontal and vertical boxes |
| 62 |
constraints are handled, by studying the following example in details: |
| 63 |
|
| 64 |
.. image:: enaml_hbox.svg |
| 65 |
|
| 66 |
Here we consider a container widget with three child widgets. The outer black |
| 67 |
frame represents the limit of the container. The dashed frame represents the |
| 68 |
contents visible to children when defining their constraint. The container uses |
| 69 |
the margin definition to relate the outer left, top, width and height to their |
| 70 |
'contents' equivalent. |
| 71 |
|
| 72 |
The three widgets are arranged according to a horizontal box for which the |
| 73 |
constraints are created using the `hbox` helper function which simply accepts |
| 74 |
a list of widgets and spacers . To define the constraints from that list, Enaml |
| 75 |
relies on the spacers represented here in orange. Each spacer has a |
| 76 |
given size and a policy regarding that size (is it a minimum value, maximum, |
| 77 |
how strongly to enforce that size). For each orientation, `hbox` add spacers so |
| 78 |
that there is a spacer between each widget and between the widgets and the |
| 79 |
parent boundaries. Some spacers can have a zero size, simply meaning that |
| 80 |
widgets should be in contact. |
| 81 |
|
| 82 |
When generating the constraints, `hbox` will be passed the container and use |
| 83 |
the spacers to generate the constraints by simply glueing the anchors of |
| 84 |
surrounding widgets. Each spacer can generate multiple constraints which gives |
| 85 |
this process a lot of flexibility. Furthermore, those helpers define the same |
| 86 |
variable as the widgets allowing for to position groups with respect to one |
| 87 |
another. |
| 88 |
|
| 89 |
.. note:: |
| 90 |
|
| 91 |
In practice, `hbox` itself relies on some helpers but the above gives you |
| 92 |
the general idea. |
| 93 |
|
| 94 |
For further details you can have a look at the source of the helpers described |
| 95 |
in this section which can be found in the Enaml source: |
| 96 |
|
| 97 |
- spacers: https://github.com/nucleic/enaml/blob/main/enaml/layout/spacers.py |
| 98 |
- helpers: |
| 99 |
|
| 100 |
- https://github.com/nucleic/enaml/blob/main/enaml/layout/layout_helpers.py |
| 101 |
- https://github.com/nucleic/enaml/blob/main/enaml/layout/linear_box_helper.py |
| 102 |
- https://github.com/nucleic/enaml/blob/main/enaml/layout/sequence_helper.py |
| 103 |
|
| 104 |
|
| 105 |
Setting up the solver |
| 106 |
--------------------- |
| 107 |
|
| 108 |
So far we have only defined the constraints that represent the layout, we will |
| 109 |
now turn to how Enaml pass those to the solver and how it handle updates and |
| 110 |
solver resets. |
| 111 |
|
| 112 |
By default, each container manages its own solver independently. This has |
| 113 |
the advantage of keeping the system relatively smalls and hence allow for |
| 114 |
faster updates. When setting up the solver, the container will add for each |
| 115 |
widget a set of constraints reflecting the preference of the widget regarding |
| 116 |
its size as reported by the widget, and add to those the constraints defining |
| 117 |
the layout. It will also add two edit variable representing the width and |
| 118 |
height of the container. |
| 119 |
|
| 120 |
Once the solver has been set up it can be used to compute different values, |
| 121 |
such as the best size for the container (requesting a size of 0 with a 0.1*weak |
| 122 |
strength), its min size (0 size, medium strength) and max size (max size, |
| 123 |
medium strength). |
| 124 |
|
| 125 |
When the parent is resized, the solver is invoked again with the new width and |
| 126 |
height as suggestion. On the other hand, if the constraints change either |
| 127 |
because widgets have been added or removed or because the users modified them, |
| 128 |
the solver is reset and the constraints are rebuilt-from scratch. This means |
| 129 |
that we never keep the solver around long enough to have to worry about memory |
| 130 |
consumption due to unused variables in the solver. |
| 131 |
|
| 132 |
In a complex hierarchy, the top parent will request the sizes of the nested |
| 133 |
containers which will trigger the solving of their constraints. At some point |
| 134 |
in the nested structure, we will only find widgets which provides a size hint |
| 135 |
without requiring to solve constraints (ex: a button). This will allow to solve |
| 136 |
the system and then propagate back upward. |
| 137 |
|
| 138 |
|
| 139 |
Hopefully this brief introduction will have clarified how Enaml make use of |
| 140 |
kiwi to layout its widgets. Some fine mechanics have been simplified for the |
| 141 |
sake of this description but you can check Enaml sources for a more in depth |
| 142 |
description. |