Skip to content

Commit 62af7bd

Browse files
authored
Merge pull request #33 from Nyan11/patch-5
Update layout.md - Linear layout
2 parents 7cb6b19 + d080263 commit 62af7bd

File tree

1 file changed

+99
-24
lines changed

1 file changed

+99
-24
lines changed

Chapters/bloc/layout.md

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -367,49 +367,124 @@ Stef should continue down here.
367367

368368
### Linear layout - BlLinearLayout
369369

370-
Children can use dynamic size with constraints. The number of element will then fit
371-
its parents available space. If you specify their size, and the total is
372-
over its parents, they will be hidden. need to specify their size. If they use
373-
constraint, the last one will hide previous one. They will fit available space +
374-
move to next line if necessary
370+
A linear layout is used when you need to display a multiple children elements next to each others.
371+
A linear layout can diplay the children elements horizontaly or verticaly.
372+
The size of a child element depends of the child's constraints.
373+
The constraints are:
375374

376-
#### Parent definition
375+
- `BlLayoutCommonConstraintsAxis exact:` to define an exact size for the child.
376+
- `BlLayoutCommonConstraintsAxis fitContent` to define a size that will match the content of the child.
377+
- `BlLinearLayoutConstraints weight:` to define a proportional size of the child compare to its siblings' weigths (only available if matchParent is set as the BlLayoutCommonConstraintsAxis). By default the weight equals 1.
377378

378-
- horizontal
379-
- vertical
379+
#### Example
380+
381+
In this example the childA has a weight of 3, the childB has a default weight of 1 and the childC has a weight of 200.
382+
But the weight of the childC will not be considered because its horizontal size is defined has an exact value.
383+
384+
- The childA will take `3 / (3 + 1) = 3 / 4` of the available space.
385+
- The childB will take `1 / (3 + 1) = 1 / 4` of the available space.
386+
387+
```smalltalk
388+
root := BlElement new
389+
border: (BlBorder paint: Color red width: 1);
390+
background: (Color red alpha: 0.2);
391+
layout: BlLinearLayout horizontal;
392+
constraintsDo: [ :c |
393+
c horizontal matchParent.
394+
c vertical fitContent ];
395+
yourself.
396+
397+
childA := BlElement new
398+
border: (BlBorder paint: Color blue width: 1);
399+
background: (Color blue alpha: 0.2);
400+
margin: (BlInsets all: 10);
401+
constraintsDo: [ :c |
402+
c horizontal matchParent.
403+
c vertical exact: 50.
404+
c linear weight: 3 ];
405+
yourself.
406+
407+
childB := BlElement new
408+
border: (BlBorder paint: Color blue width: 1);
409+
background: (Color blue alpha: 0.2);
410+
margin: (BlInsets all: 10);
411+
constraintsDo: [ :c |
412+
c horizontal matchParent.
413+
c vertical exact: 50 ];
414+
yourself.
415+
416+
childC := BlElement new
417+
border: (BlBorder paint: Color blue width: 1);
418+
background: (Color blue alpha: 0.2);
419+
margin: (BlInsets all: 10);
420+
constraintsDo: [ :c |
421+
c horizontal exact: 50.
422+
c vertical exact: 50.
423+
c linear weight: 20 ];
424+
yourself.
425+
426+
root addChildren: { childA . childB . childC }.
427+
root openInNewSpace.
428+
```
380429

381430
#### Child constraints
382431

383-
- horizontal
432+
Constraints can be added on the children relative position.
433+
434+
If the layout is vertical:
384435
- alignCenter
385436
- alignLeft
386437
- alignRight
387-
- vertical
438+
439+
If the layout is horizontal:
388440
- alignBottom
389441
- alignCenter
390442
- alignTop
391443

392-
#### Example
393-
394-
```smalltalk
444+
```st
395445
root := BlElement new
396446
border: (BlBorder paint: Color red width: 1);
397447
background: (Color red alpha: 0.2);
398-
layout: BlLinearLayout horizontal ;
448+
layout: BlLinearLayout horizontal;
399449
constraintsDo: [ :c |
400-
c horizontal fitContent.
401-
c vertical fitContent ].
402-
403-
50 timesRepeat: [ |elt| elt := BlElement new border: (BlBorder paint: Color blue width: 1);
404-
size: 40@80;
405-
background: (Color blue alpha: 0.2);
406-
margin: (BlInsets all: 5);
407-
padding: (BlInsets all: 5).
450+
c horizontal matchParent.
451+
c vertical exact: 80 ];
452+
yourself.
453+
454+
childA := BlElement new
455+
border: (BlBorder paint: Color blue width: 1);
456+
background: (Color blue alpha: 0.2);
457+
margin: (BlInsets all: 10);
458+
constraintsDo: [ :c |
459+
c horizontal matchParent.
460+
c vertical exact: 20.
461+
c linear vertical alignBottom ];
462+
yourself.
463+
464+
childB := BlElement new
465+
border: (BlBorder paint: Color blue width: 1);
466+
background: (Color blue alpha: 0.2);
467+
margin: (BlInsets all: 10);
468+
constraintsDo: [ :c |
469+
c horizontal matchParent.
470+
c vertical exact: 20.
471+
c linear vertical alignCenter ];
472+
yourself.
473+
474+
childC := BlElement new
475+
border: (BlBorder paint: Color blue width: 1);
476+
background: (Color blue alpha: 0.2);
477+
margin: (BlInsets all: 10);
478+
constraintsDo: [ :c |
479+
c horizontal matchParent.
480+
c vertical exact: 20.
481+
c linear vertical alignTop ];
482+
yourself.
408483
409-
root addChild: elt. ].
484+
root addChildren: { childA . childB . childC }.
485+
root openInNewSpace.
410486
```
411487

412-
![Linear layout.](figures/linearlayout.png width=70)
413488

414489
### Flow layout - BlFlowLayout
415490

0 commit comments

Comments
 (0)