Skip to content

About

4D Component to align form objects

Topics

Resources

Stars

1 star

Watchers

3 watching

Forks

Repository files navigation

AJUI_Alignment

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.

Requirements

  • 4D v21, project mode.
  • The AJUI_flex component (≥ 0.2.0).

Installation

The repository contains the component in the AJUI_Alignment/ folder (Project/, Resources/).

  1. Put the component folder AJUI_Alignment and the AJUI_flex repository folder AJUI_flex next to your host project folder:
    MyApp/
      Project/
    AJUI_Alignment/
      Project/
    AJUI_flex/
      Project/
    
  2. In the host Project/Sources/dependencies.json:
    { "dependencies": { "AJUI_Alignment": {}, "AJUI_flex": {} } }
    (The component's own dependencies.json also declares AJUI_flex; declaring it in the host as well is the safe choice with local components.)
  3. 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).

Usage

Historical method (unchanged signature)

// 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.

Class API (new)

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)

Definition format

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 to maxWidth) and shrinks (down to minWidth) by the same amount as the others, not proportionally to its size (distribution: "equal"). distribution: "css" uses CSS proportional shrinking instead.
  • In wrap mode, 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).

Tests

git clone https://github.com/minzirillo/AJUI_flex ../AJUI_flex     # or set AJUI_FLEX_DIR
python3 tests/run_tests.py

tests/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).

Compatibility

  • Target: 4D v21, project mode (compatibilityVersion 2000, as AJUI_flex; 4D may rewrite it when the project is opened). The original component was a 4D 18.x project using C_OBJECT and numbered commands.
  • Component class store namespace: alignment (cs.alignment.Alignment).
  • Relies on cs.flex from AJUI_flex (a component loaded by the host). Whether cs.flex is visible from a component that is itself loaded as a secondary dependency has not been checked in 4D.

License

MIT © 2020 AJAR SA

About

4D Component to align form objects

Topics

Resources

Stars

1 star

Watchers

3 watching

Forks

Releases

Packages

Contributors

Languages