forked from desromech/sysmel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I am starting to add a basic table view to Morphic.
- Loading branch information
Showing
18 changed files
with
461 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
public abstract class DataSource definition: { | ||
public abstract method numberOfElements => Size | ||
:= self subclassResponsibility. | ||
|
||
public abstract method elementAtRow: (rowIndex: Size) ::=> AnyValue | ||
:= self subclassResponsibility. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
public class ListViewMorph superclass: TableViewMorph; definition: { | ||
protected field listDataSource => SimpleListDataSource. | ||
|
||
public override method initialize => Void := { | ||
super initialize. | ||
listDataSource := SimpleListDataSource new. | ||
self dataSource: listDataSource. | ||
}. | ||
|
||
public method removeAll => Void := { | ||
listDataSource removeAll. | ||
self changed. | ||
}. | ||
|
||
public method addItem: item ::=> Size := { | ||
let index := listDataSource addItem: item. | ||
self changed. | ||
index | ||
}. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
public class SimpleListDataSource superclass: DataSource; definition: { | ||
public field items => OrderedCollection. | ||
|
||
public override method initialize => Void := { | ||
super initialize. | ||
items := OrderedCollection new. | ||
}. | ||
|
||
public override method numberOfElements => Size | ||
:= items size. | ||
|
||
public override method elementAtRow: (rowIndex: Size) ::=> AnyValue | ||
:= rowIndex < items size | ||
ifTrue: (items at: rowIndex) | ||
ifFalse: nil. | ||
|
||
public method removeAll => Void := { | ||
items removeAll | ||
}. | ||
|
||
public method addItem: item ::=> Size := { | ||
let index := items size. | ||
items add: item. | ||
index | ||
}. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class StringTableViewColumn superclass: TableViewColumn; definition: { | ||
let DisplayBlockType := (AnyValue) => String. | ||
|
||
public field display => DisplayBlockType. | ||
|
||
public override method initialize => Void := { | ||
super initialize. | ||
display := {:element :: String | element asString }. | ||
}. | ||
|
||
public override method createCellMorphForElement: element ::=> Morph | ||
:= display(element) asMorph. | ||
|
||
public override method updateCellMorph: (morph: Morph) withElement: element ::=> Void := { | ||
(morph downCastTo: StringMorph) contents: display(element) | ||
}. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
public class TableRowSelection definition: { | ||
public field selectedIndices => Array. | ||
|
||
__Meta__ definition: { | ||
public method with: (index: Size) ::=> self | ||
:= self new selectedIndices: (Array with: index); yourself | ||
}. | ||
|
||
public override method initialize => Void := { | ||
super initialize. | ||
selectedIndices := #(). | ||
}. | ||
|
||
public method includesRowIndex: (index: Size) ::=> Boolean | ||
:= selectedIndices includes: index. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
public abstract class TableViewColumn definition: { | ||
public field name => String. | ||
public field width => Float32. | ||
|
||
public virtual method minimalCellExtent ::=> Float32x2 | ||
:= 0f32 @ FontFace default height. | ||
|
||
public virtual method createCellMorphForElement: element ::=> Morph | ||
:= Morph new. | ||
|
||
public virtual method updateCellMorph: (morph: Morph) withElement: element ::=> Void := { | ||
## By default do nothing. | ||
}. | ||
}. |
122 changes: 122 additions & 0 deletions
122
package-sources/Morphic.Core/TableViewContentMorph.sysmel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
TableViewContentMorph definition: { | ||
protected field visibleRows => Array. | ||
|
||
public override method initialize => Void := { | ||
super initialize. | ||
visibleRows := #(). | ||
}. | ||
|
||
public method tableViewMorph => TableViewMorph | ||
:= owner downCastTo: TableViewMorph. | ||
|
||
public method dataSource => DataSource | ||
:= self tableViewMorph dataSource. | ||
|
||
public method columns => OrderedCollection | ||
:= self tableViewMorph columns. | ||
|
||
public method rowHeight => Float32 | ||
:= self tableViewMorph rowHeight. | ||
|
||
public override method defaultColor => Color | ||
:= Color transparent. | ||
|
||
public override method validSubmorphs => Array := { | ||
self ensureThatVisibleRowsExists. | ||
super validSubmorphs | ||
}. | ||
|
||
public method updateContent => Void := { | ||
self ensureThatVisibleRowsExists. | ||
visibleRows do: {:(TableViewRowMorph)eachRow :: Void | | ||
eachRow updateContent | ||
} | ||
}. | ||
|
||
public override method updateLayout => Void := { | ||
let rowHeight := self rowHeight. | ||
let rowExtent := self extent x @ rowHeight. | ||
|
||
visibleRows do: {:(TableViewRowMorph)eachRow :: Void | | ||
let rowPosition := 0f32 @ (eachRow rowIndex asFloat32 * rowHeight). | ||
eachRow bounds: (rowPosition extent: rowExtent) | ||
}. | ||
}. | ||
|
||
public method computeVisibleRowRangeFrom: (firstVisibleRow: Size ref) to: (lastVisibleRow: Size ref) ::=> Void := { | ||
let visibleBounds := self localBounds. | ||
let rowHeight := self rowHeight. | ||
let dataSource := self dataSource. | ||
let elementCount := dataSource ifNil: 0sz ifNotNil: dataSource numberOfElements. | ||
let maxRowIndex := elementCount > 0sz ifTrue: elementCount - 1sz ifFalse: 0sz. | ||
|
||
firstVisibleRow := ((visibleBounds min y / rowHeight) floor max: 0f32) asSize min: maxRowIndex. | ||
lastVisibleRow := ((visibleBounds max y / rowHeight) ceiling max: 0f32) asSize min: maxRowIndex. | ||
}. | ||
|
||
public method ensureThatVisibleRowsExists => Void := { | ||
let dataSource := self dataSource. | ||
dataSource isNil || (dataSource numberOfElements = 0sz) ifTrue: { | ||
self removeAllMorphs. | ||
visibleRows := #(). | ||
return: void | ||
}. | ||
|
||
let firstVisibleRowIndex mutable := 0sz. | ||
let lastVisibleRowIndex mutable := 0sz. | ||
self computeVisibleRowRangeFrom: firstVisibleRowIndex to: lastVisibleRowIndex. | ||
|
||
visibleRows isNotEmpty && | ||
(firstVisibleRowIndex = visibleRows first rowIndex) && | ||
(lastVisibleRowIndex = visibleRows last rowIndex) ifTrue: { | ||
return: void | ||
}. | ||
|
||
let overlappingRows := visibleRows select: {:(TableViewRowMorph)eachRowMorph :: Boolean | | ||
(firstVisibleRowIndex <= eachRowMorph rowIndex) && (eachRowMorph rowIndex <= lastVisibleRowIndex) | ||
}. | ||
|
||
let previousEndIndex := overlappingRows isEmpty | ||
ifTrue: (firstVisibleRowIndex) | ||
ifFalse: (overlappingRows first rowIndex). | ||
let nextStartIndex := overlappingRows isEmpty | ||
ifTrue: (firstVisibleRowIndex) | ||
ifFalse: (overlappingRows last rowIndex + 1sz). | ||
|
||
let newVisibleRows := OrderedCollection new. | ||
|
||
{ | ||
let i mutable := firstVisibleRowIndex. | ||
while: (i < previousEndIndex) do: { | ||
let rowMorph := TableViewRowMorph new. | ||
self addMorph: rowMorph. | ||
|
||
newVisibleRows add: (rowMorph | ||
rowIndex: i; | ||
createCells; | ||
updateContent; | ||
yourself) | ||
} continueWith: (i := i + 1sz). | ||
}. | ||
|
||
newVisibleRows addAll: overlappingRows. | ||
|
||
{ | ||
let i mutable := nextStartIndex. | ||
while: (i <= lastVisibleRowIndex) do: { | ||
let rowMorph := TableViewRowMorph new. | ||
self addMorph: rowMorph. | ||
|
||
newVisibleRows add: (rowMorph | ||
rowIndex: i; | ||
createCells; | ||
updateContent; | ||
yourself) | ||
} continueWith: (i := i + 1sz). | ||
}. | ||
|
||
visibleRows := newVisibleRows asArray. | ||
self replaceAllMorphsWith: visibleRows. | ||
self updateLayout. | ||
}. | ||
}. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
TableViewHeaderMorph definition: { | ||
|
||
}. |
Oops, something went wrong.