Merge branch 'master' of https://github.com/funkygao/termui into refactoring

Conflicts:
	grid.go
This commit is contained in:
gizak 2015-04-16 19:08:08 -04:00
commit 0a29dad7e7

52
grid.go
View File

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