Bloc
Bloc copied to clipboard
Method for center of a BlElement
I couldn't find a method to position an element at the center of its parent, for now I'm using parent width/2 @ (parent height/2)
afaik there is no such method.
@Enzo-Demeulenaere Do you want to request or tu push a new method ?
Proportional layout is not what you want but may be in some case you find it useful:
space := BlSpace new.
childA := BlElement new
id: #childA;
constraintsDo: [ :c |
c proportional horizontal
left: 0.1;
right: 0.9.
c proportional vertical
top: 0.1;
bottom: 0.9 ];
background: Color blue;
yourself.
space root
layout: BlProportionalLayout new;
addChild: childA;
yourself.
space title: 'BlProportionalLayout'.
space extent: 400 asPoint.
space show.
The problem is that when you add a child element, the parent may not have size yet. So in this example I apply it within a whenLayoutedDoOnce:
:
space := BlSpace new.
childA := BlElement new
size: 300 @ 200;
background: Color blue;
yourself.
space root whenLayoutedDoOnce: [
childA position: (childA parent size - childA size) / 2. ].
space root addChild: childA.
space title: 'positioned at center of parent'.
space extent: 400 asPoint.
space show.
After I resized the parent, the position is untouched:
possible with BlFrameLayout:
| root centered |
root := ToElement new layout: BlFrameLayout new.
root matchParent.
centered := ToElement new constraintsDo: [ :c | c frame horizontal alignCenter. c frame vertical alignCenter ].
centered background: Color blue.
centered size: 50@50.
root addChild: centered.
root openInSpace```