gizak 2015-05-05 11:08:45 -04:00
parent f23ed68e30
commit 672baf23ee
1 changed files with 26 additions and 26 deletions

52
grid.go
View File

@ -13,9 +13,9 @@ type GridBufferer interface {
SetY(int)
}
// row builds a layout tree
type row struct {
Cols []*row //children
// Row builds a layout tree
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) {
r.SetWidth(w)
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,
// 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()
@ -96,7 +96,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() {
@ -112,7 +112,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() {
@ -130,12 +130,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)
@ -143,7 +143,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)
@ -151,7 +151,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)
@ -160,7 +160,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() {
@ -204,7 +204,7 @@ func (r *row) Buffer() []Point {
ui.Render(ui.Body)
*/
type Grid struct {
Rows []*row
Rows []*Row
Width int
X int
Y int
@ -212,29 +212,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 {
@ -243,11 +243,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
}