export row so that dynamic making rows is possible

This commit is contained in:
funkygao 2015-04-15 21:50:09 +08:00
parent d30d5901e7
commit 31039974ca
1 changed files with 25 additions and 25 deletions

50
grid.go
View File

@ -14,8 +14,8 @@ type GridBufferer interface {
}
// row builds a layout tree
type row struct {
Cols []*row //children
type Row struct {
Cols []*Row //children
Widget GridBufferer // root
X int
Y int
@ -26,7 +26,7 @@ type row struct {
}
// calculate and set the underlying layout tree's x, y, height and width.
func (r *row) calcLayout() {
func (r *Row) calcLayout() {
r.assignWidth(r.Width)
r.Height = r.solveHeight()
r.assignX(r.X)
@ -34,16 +34,16 @@ func (r *row) calcLayout() {
}
// tell if the node is leaf in the tree.
func (r *row) isLeaf() bool {
func (r *Row) isLeaf() bool {
return r.Cols == nil || len(r.Cols) == 0
}
func (r *row) isRenderableLeaf() bool {
func (r *Row) isRenderableLeaf() bool {
return r.isLeaf() && r.Widget != nil
}
// assign widgets' (and their parent rows') width recursively.
func (r *row) assignWidth(w int) {
func (r *Row) assignWidth(w int) {
cw := int(float64(w*r.Span) / 12)
r.SetWidth(cw)
@ -54,7 +54,7 @@ func (r *row) assignWidth(w int) {
// bottom up calc and set rows' (and their widgets') height,
// return r's total height.
func (r *row) solveHeight() int {
func (r *Row) solveHeight() int {
if r.isRenderableLeaf() {
r.Height = r.Widget.GetHeight()
return r.Widget.GetHeight()
@ -79,7 +79,7 @@ func (r *row) solveHeight() int {
}
// recursively assign x position for r tree.
func (r *row) assignX(x int) {
func (r *Row) assignX(x int) {
r.SetX(x)
if !r.isLeaf() {
@ -95,7 +95,7 @@ func (r *row) assignX(x int) {
}
// recursively assign y position to r.
func (r *row) assignY(y int) {
func (r *Row) assignY(y int) {
r.SetY(y)
if r.isLeaf() {
@ -113,12 +113,12 @@ func (r *row) assignY(y int) {
}
// GetHeight implements GridBufferer interface.
func (r row) GetHeight() int {
func (r Row) GetHeight() int {
return r.Height
}
// SetX implements GridBufferer interface.
func (r *row) SetX(x int) {
func (r *Row) SetX(x int) {
r.X = x
if r.Widget != nil {
r.Widget.SetX(x)
@ -126,7 +126,7 @@ func (r *row) SetX(x int) {
}
// SetY implements GridBufferer interface.
func (r *row) SetY(y int) {
func (r *Row) SetY(y int) {
r.Y = y
if r.Widget != nil {
r.Widget.SetY(y)
@ -134,7 +134,7 @@ func (r *row) SetY(y int) {
}
// SetWidth implements GridBufferer interface.
func (r *row) SetWidth(w int) {
func (r *Row) SetWidth(w int) {
r.Width = w
if r.Widget != nil {
r.Widget.SetWidth(w)
@ -143,7 +143,7 @@ func (r *row) SetWidth(w int) {
// Buffer implements Bufferer interface,
// recursively merge all widgets buffer
func (r *row) Buffer() []Point {
func (r *Row) Buffer() []Point {
merged := []Point{}
if r.isRenderableLeaf() {
@ -187,7 +187,7 @@ func (r *row) Buffer() []Point {
ui.Render(ui.Body)
*/
type Grid struct {
Rows []*row
Rows []*Row
Width int
X int
Y int
@ -195,29 +195,29 @@ type Grid struct {
}
// NewGrid returns *Grid with given rows.
func NewGrid(rows ...*row) *Grid {
func NewGrid(rows ...*Row) *Grid {
return &Grid{Rows: rows}
}
// AddRows appends given rows to Grid.
func (g *Grid) AddRows(rs ...*row) {
func (g *Grid) AddRows(rs ...*Row) {
g.Rows = append(g.Rows, rs...)
}
// NewRow creates a new row out of given columns.
func NewRow(cols ...*row) *row {
rs := &row{Span: 12, Cols: cols}
func NewRow(cols ...*Row) *Row {
rs := &Row{Span: 12, Cols: cols}
return rs
}
// NewCol accepts: widgets are LayoutBufferer or widgets is A NewRow.
// Note that if multiple widgets are provided, they will stack up in the col.
func NewCol(span, offset int, widgets ...GridBufferer) *row {
r := &row{Span: span, Offset: offset}
func NewCol(span, offset int, widgets ...GridBufferer) *Row {
r := &Row{Span: span, Offset: offset}
if widgets != nil && len(widgets) == 1 {
wgt := widgets[0]
nw, isRow := wgt.(*row)
nw, isRow := wgt.(*Row)
if isRow {
r.Cols = nw.Cols
} else {
@ -226,11 +226,11 @@ func NewCol(span, offset int, widgets ...GridBufferer) *row {
return r
}
r.Cols = []*row{}
r.Cols = []*Row{}
ir := r
for _, w := range widgets {
nr := &row{Span: 12, Widget: w}
ir.Cols = []*row{nr}
nr := &Row{Span: 12, Widget: w}
ir.Cols = []*Row{nr}
ir = nr
}