English · Français
A 4D component that aligns the objects of a 4D form the way CSS flexbox does. You describe a container
and its items in an object (display, direction, wrap, justifyContent, padding, margin,
width, minWidth, maxWidth, nested items…); AJUI_Alignment moves and resizes the form objects,
typically on On Resize.
Since 1.0.0 the layout itself is computed by AJUI_flex
(an implementation of the W3C CSS Flexible Box algorithm in plain 4D). AJUI_Alignment keeps the
original definition format and the original public method AJUI_Flexbox_apply, and adds a small
class API.
Status. The translation layer is tested outside 4D (see Tests). It has not been run in 4D yet, and neither has AJUI_flex 0.2.0, on which it relies. See Compatibility.
- 4D v21, project mode.
- The AJUI_flex component (≥ 0.2.0).
The repository contains the component in the AJUI_Alignment/ folder (Project/, Resources/).
- Put the component folder
AJUI_Alignmentand the AJUI_flex repository folderAJUI_flexnext to your host project folder:MyApp/ Project/ AJUI_Alignment/ Project/ AJUI_flex/ Project/ - In the host
Project/Sources/dependencies.json:(The component's own{ "dependencies": { "AJUI_Alignment": {}, "AJUI_flex": {} } }dependencies.jsonalso declaresAJUI_flex; declaring it in the host as well is the safe choice with local components.) - Restart the host project.
Once AJUI_flex publishes a GitHub release, it can be declared as
"AJUI_flex": { "github": "minzirillo/AJUI_flex", "version": "^0.2.0" }
(see the 4D documentation, Components → Components stored on GitHub).
// Form method
Case of
: (Form event code=On Load)
Form.flexbox:=JSON Parse(Document to text(Get 4D folder(Current resources folder; *)+"test1.json"; "UTF-8"))
AJUI_Flexbox_apply(Form.flexbox)
: (Form event code=On Resize)
AJUI_Flexbox_apply(Form.flexbox)
End case The first call measures the objects and stores the computed layout in Form.flexbox._alignment.
Later calls only re-read the container size. To force a new measurement (a label changed), set
Form.flexbox.itemsReady:=False, as before.
Form.align:=cs.alignment.Alignment.new(Form.flexbox) // measures the objects
Form.align.apply() // returns the flat list of rects
Form.align.item("item_3").grow:=1 // tweak one item (AJUI_flex descriptor)
Form.align.refresh() // measure again (drops item() tweaks)See 4D_Flex_Alignment_PROTO.json and AJUI_Alignment/Resources/test1.json. display must be "flex";
any other value ("absolute" included) leaves the objects untouched, as before.
| Container | Values | Default |
|---|---|---|
id |
name of the container object | (whole window) |
display |
flex |
none: nothing happens |
direction |
row, row-reverse, column, column-reverse |
row |
wrap |
nowrap, wrap, wrap-reverse |
nowrap |
justifyContent |
flex-start, flex-end, center, space-between, space-around, space-evenly |
flex-start |
alignItems (new) |
flex-start, flex-end, center, stretch |
flex-start |
alignContent (new) |
flex-start, flex-end, center, stretch, space-between, space-around, space-evenly |
flex-start |
padding, paddingLeft/Top/Right/Bottom |
pixels | 0 |
gap, rowGap, columnGap (new) |
pixels | 0 |
distribution (new) |
equal (historical), css |
equal |
items |
collection of items (recursive) |
| Item | Meaning | Default |
|---|---|---|
id |
object name | required |
width, height |
suggested size | minWidth/minHeight |
minWidth, minHeight |
best size of the object (OBJECT GET BEST SIZE), else its current size |
|
maxWidth, maxHeight |
the suggested size (the object does not grow) | |
margin, marginLeft/Top/Right/Bottom |
pixels | 0 |
order |
historical rule: order*10-1 against (position+1)*10 |
position |
display, direction, wrap, … + items |
nested container | |
grow, shrink, basis, alignSelf (new) |
forwarded to AJUI_flex | computed (see below) |
How the historical behaviour is kept on top of CSS flexbox:
- In
nowrap, an object grows (up tomaxWidth) and shrinks (down tominWidth) by the same amount as the others, not proportionally to its size (distribution: "equal").distribution: "css"uses CSS proportional shrinking instead. - In
wrapmode, objects neither grow nor shrink: lines are cut on the suggested sizes only. - The cross axis is not stretched or centred by default (
alignItems: "flex-start"), unlike CSS (stretch). - Objects without a best size (rectangles…) use their current size.
See Migration for what changed.
To reduce the size of the built component, see Reducing the footprint (guide only, nothing implemented).
git clone https://github.com/minzirillo/AJUI_flex ../AJUI_flex # or set AJUI_FLEX_DIR
python3 tests/run_tests.pytests/run_tests.py runs the real AlignmentModel.4dm and Alignment.4dm files, on top of AJUI_flex's
FlexEngine.4dm / FlexLayout.4dm, with mini4d (the small 4D interpreter of AJUI_flex) and a simulated
form. It compares the result with tests/legacy_ref.py, a hand transcription of the original engine
(default values, ordering, nowrap grow/shrink in row and column, wrap in row): 400 random cases each.
mini4d is a safety net, not 4D, and legacy_ref.py is a transcription, not the original code.
In 4D: run the shared method AlignmentTests_run (pure checks of AlignmentModel, returns
{total; failed; failures}). It has not been run in 4D yet. For a visual check, run
launch_flexboxFormTest and resize the window.
FlexDemo: an interactive demo form for AJUI_flex (sliders, menus, axis / gap / padding annotations). Run the shared method FlexDemo; tests: python3 tests/run_flexdemo_tests.py. Not run in 4D yet. See FlexDemo (French).
- Target: 4D v21, project mode (
compatibilityVersion2000, as AJUI_flex; 4D may rewrite it when the project is opened). The original component was a 4D 18.x project usingC_OBJECTand numbered commands. - Component class store namespace:
alignment(cs.alignment.Alignment). - Relies on
cs.flexfrom AJUI_flex (a component loaded by the host). Whethercs.flexis visible from a component that is itself loaded as a secondary dependency has not been checked in 4D.
MIT © 2020 AJAR SA