<commit_before>"
I am a special layout that uses measured positions of children to determine parent's extent.

I should be considered to be a root of graph or visualisation layouts. For that purpose override measurePositions: with:
"
Class {
	#name : #BlFitLayout,
	#superclass : #BlLayout,
	#category : 'Bloc-Layout-Fit'
}

{ #category : #measure }
BlFitLayout >> boundingBox: anElement [
	<return: #Rectangle>
	| aBoundingBox |

	anElement children isEmpty
		ifTrue: [ ^ BlBounds new asRectangle ].

	aBoundingBox := nil.
	self
		layeredChildren: anElement
		do: [ :aChild |
			aBoundingBox
				ifNil: [ aBoundingBox := BlBounds fromRectangle: aChild measuredBounds bounds ]
				ifNotNil: [ aBoundingBox merge: aChild measuredBounds bounds ] ].

	^ aBoundingBox expanded asRectangle
]

{ #category : #layout }
BlFitLayout >> fit: anElement with: anExtentSpec [
	| boundingBox width height |

	boundingBox := self boundingBox: anElement.

	width := boundingBox width + anElement padding width.
	height := boundingBox height + anElement padding height.

	anElement measuredExtent: (width @ height)
]

{ #category : #layout }
BlFitLayout >> layout: anElement in: aRectangle [

	self normalize: anElement in: aRectangle.

	self layoutIgnored: anElement.
	self
		layeredChildren: anElement
		do: [ :aChild | aChild applyLayoutIn: aChild measuredBounds asRectangle ]
]

{ #category : #measure }
BlFitLayout >> measure: anElement with: anExtentSpec [
	
	self measuredChildren: anElement do: [ :aChild |
		self
			measureChild: aChild
			parentSpec: anExtentSpec ].

	self measureIgnored: anElement with: anExtentSpec.
	self measurePositions: anElement with: anExtentSpec.
	
	self fit: anElement with: anExtentSpec
]

{ #category : #measure }
BlFitLayout >> measurePositions: anElement with: anExtentSpec [
	"Override me to implement a custom child position measurement algorithm.
	Graph or visualisation layouts should implement me"
]

{ #category : #measure }
BlFitLayout >> normalize: anElement in: aRectangle [
	| boundingBox delta |

	boundingBox := self boundingBox: anElement.
	delta := ((aRectangle extent / 2.0) - boundingBox center).
	"bounding box depends on element positions, while position themselves depend on bounding box.
	That is why we have to implement a cycle breaking mechanism. In our case it makes sense to
	to not relocate children if delta is less then 1 point"
	delta abs < (1@1)
		ifTrue: [ ^ self ].

	self<commit_msg>measure ignored after measuring the parent in a fit layout<commit_after>"
I am a special layout that uses measured positions of children to determine parent's extent.

I should be considered to be a root of graph or visualisation layouts. For that purpose override measurePositions: with:
"
Class {
	#name : #BlFitLayout,
	#superclass : #BlLayout,
	#category : 'Bloc-Layout-Fit'
}

{ #category : #measure }
BlFitLayout >> boundingBox: anElement [
	<return: #Rectangle>
	| aBoundingBox |

	anElement children isEmpty
		ifTrue: [ ^ BlBounds new asRectangle ].

	aBoundingBox := nil.
	self
		layeredChildren: anElement
		do: [ :aChild |
			aBoundingBox
				ifNil: [ aBoundingBox := BlBounds fromRectangle: aChild measuredBounds bounds ]
				ifNotNil: [ aBoundingBox merge: aChild measuredBounds bounds ] ].

	^ aBoundingBox expanded asRectangle
]

{ #category : #layout }
BlFitLayout >> fit: anElement with: anExtentSpec [
	| boundingBox width height |

	boundingBox := self boundingBox: anElement.

	width := boundingBox width + anElement padding width.
	height := boundingBox height + anElement padding height.

	anElement measuredExtent: (width @ height)
]

{ #category : #layout }
BlFitLayout >> layout: anElement in: aRectangle [

	self normalize: anElement in: aRectangle.

	self layoutIgnored: anElement.
	self
		layeredChildren: anElement
		do: [ :aChild | aChild applyLayoutIn: aChild measuredBounds asRectangle ]
]

{ #category : #measure }
BlFitLayout >> measure: anElement with: anExtentSpec [
	
	self measuredChildren: anElement do: [ :aChild |
		self
			measureChild: aChild
			parentSpec: anExtentSpec ].

	self measurePositions: anElement with: anExtentSpec.
	
	self fit: anElement with: anExtentSpec.
	
	self measureIgnored: anElement with: anExtentSpec
]

{ #category : #measure }
BlFitLayout >> measurePositions: anElement with: anExtentSpec [
	"Override me to implement a custom child position measurement algorithm.
	Graph or visualisation layouts should implement me"
]

{ #category : #measure }
BlFitLayout >> normalize: anElement in: aRectangle [
	| boundingBox delta |

	boundingBox := self boundingBox: anElement.
	delta := ((aRectangle extent / 2.0) - boundingBox center).
	"bounding box depends on element positions, while position themselves depend on bounding box.
	That is why we have to implement a cycle breaking mechanism. In our case it makes sense to
	to not relocate children if delta is less then 1 point"
	delta abs < (1@1)
		ifTrue: [ ^ self ].

	self
{'fraction_non_alphanumeric': 0.08196388719821465, 'fraction_numerical': 0.002028809089064719, 'mean_word_length': 4.45353982300885, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1671168', 'n_tokens_mistral': 1562, 'n_tokens_neox': 1443, 'n_words': 526}