Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix column alignment #1475

Merged
merged 3 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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