Skip to content

Commit

Permalink
Merge pull request #1475 from pharo-spec/fix-column-alignment
Browse files Browse the repository at this point in the history
Fix column alignment
  • Loading branch information
estebanlm authored Nov 24, 2023
2 parents 42f0c0a + ad1a55a commit 128deff
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 @@ -22,12 +22,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 @@ -146,6 +165,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 @@ -268,7 +298,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 @@ -277,9 +306,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 128deff

Please sign in to comment.