Bloc icon indicating copy to clipboard operation
Bloc copied to clipboard

Method for center of a BlElement

Open Enzo-Demeulenaere opened this issue 1 year ago • 4 comments

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)

Enzo-Demeulenaere avatar May 11 '23 11:05 Enzo-Demeulenaere

afaik there is no such method.

plantec avatar May 13 '23 06:05 plantec

@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.
Screenshot 2023-08-03 at 17 56 45

tinchodias avatar Aug 03 '23 21:08 tinchodias

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.
Screenshot 2023-08-03 at 18 08 22

After I resized the parent, the position is untouched:

Screenshot 2023-08-03 at 18 08 42

tinchodias avatar Aug 03 '23 22:08 tinchodias

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```

plantec avatar Aug 11 '23 13:08 plantec