Skip to content

Commit

Permalink
Merge pull request #1476 from pharo-spec/fix-column-alignment-p11
Browse files Browse the repository at this point in the history
 fixes pharo-project/pharo#14868 (for Pharo11)
  • Loading branch information
estebanlm authored Nov 27, 2023
2 parents cadcb2b + 4821c92 commit c1c41bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/Spec2-Adapters-Morphic/SpMorphicTableCellBuilder.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,31 @@ SpMorphicTableCellBuilder class >> on: aDataSource [

{ #category : #private }
SpMorphicTableCellBuilder >> addAlignmentColumn: aTableColumn item: item to: content [
| block containerMorph alignment |

aTableColumn displayAlignment ifNotNil: [ :block |
^ content asText addAttribute: (block cull: item) asTextAlignment ].

^ content
(block := aTableColumn displayAlignment) ifNil: [ ^ content ].

alignment := (block cull: item) asTextAlignment.
containerMorph := Morph new
color: Color transparent;
layoutPolicy: TableLayout new;
hResizing: #spaceFill;
vResizing: #spaceFill;
borderWidth: 0;
yourself.

alignment = TextAlignment rightFlush ifTrue: [
containerMorph listDirection: #rightToLeft ].
alignment = TextAlignment leftFlush ifTrue: [
containerMorph listDirection: #leftToRight ].

alignment = TextAlignment centered
ifTrue: [ containerMorph addMorphBack: self newFillerMorph ].
containerMorph addMorphBack: content asMorph asReadOnlyMorph.
alignment = TextAlignment centered
ifTrue: [ containerMorph addMorphBack: self newFillerMorph ].

^ containerMorph
]

{ #category : #private }
Expand Down Expand Up @@ -144,6 +163,17 @@ SpMorphicTableCellBuilder >> item [
^ self dataSource elementAt: self rowIndex
]

{ #category : #private }
SpMorphicTableCellBuilder >> newFillerMorph [
"This is used as a helper to center text when applying alignment=centered"

^ Morph new
color: Color transparent;
hResizing: #spaceFill;
vResizing: #spaceFill;
yourself
]

{ #category : #accessing }
SpMorphicTableCellBuilder >> rowIndex [
^ rowIndex
Expand Down Expand Up @@ -266,7 +296,6 @@ SpMorphicTableCellBuilder >> visitStringColumn: aTableColumn [

content := aTableColumn readObject: item.
"add properties"
content := self addAlignmentColumn: aTableColumn item: item to: content.
content := self addColorColumn: aTableColumn item: item to: content.
content := self addItalicColumn: aTableColumn item: item to: content.
content := self addBoldColumn: aTableColumn item: item to: content.
Expand All @@ -275,9 +304,12 @@ SpMorphicTableCellBuilder >> visitStringColumn: aTableColumn [
aTableColumn isEditable
ifTrue: [ self visitStringColumnEditable: aTableColumn on: content ]
ifFalse: [ "add cell"
"I need to calculate here alignement because I will wrap the content
into a container morph, so it needs to be the last one before applying"
content := self addAlignmentColumn: aTableColumn item: item to: content.
self addCell: content column: aTableColumn.
"add background (this is a special case of properties,
since background needs to be applied to the cell and not to the text)"
since background needs to be applied to the cell and not to the text)"
self addBackgroundColorColumn: aTableColumn item: item toMorph: cell ]
]

Expand Down
17 changes: 17 additions & 0 deletions src/Spec2-Examples/SpTablePresenter.extension.st
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ SpTablePresenter class >> exampleSorting [
open
]

{ #category : #'*Spec2-Examples' }
SpTablePresenter class >> exampleWithColumnAlignment [
"Shows how we can align columns"
| column |

column := SpStringTableColumn new
title: 'Alignments';
evaluated: [ :object | object ];
displayAlignment: [ :object | SpColumnAlignment perform: object ];
yourself.

SpTablePresenter new
items: { #right. #center. #left };
addColumn: column;
open
]

{ #category : #'*Spec2-Examples' }
SpTablePresenter class >> exampleWithColumnHeaders [
<sampleInstance>
Expand Down

0 comments on commit c1c41bf

Please sign in to comment.