resolve pr comments

This commit is contained in:
Igor German 2019-05-02 15:50:48 +03:00
parent be3cc6d20d
commit 7357ed4649
3 changed files with 27 additions and 19 deletions

View File

@ -70,7 +70,7 @@ func main() {
l := widgets.NewTree() l := widgets.NewTree()
l.TextStyle = ui.NewStyle(ui.ColorYellow) l.TextStyle = ui.NewStyle(ui.ColorYellow)
l.WrapText = false l.WrapText = false
l.Nodes = nodes l.SetNodes(nodes)
x, y := ui.TerminalDimensions() x, y := ui.TerminalDimensions()

View File

@ -69,8 +69,8 @@ type ListTheme struct {
type TreeTheme struct { type TreeTheme struct {
Text Style Text Style
Collapse rune Collapsed rune
Expand rune Expanded rune
} }
type ParagraphTheme struct { type ParagraphTheme struct {
@ -131,8 +131,8 @@ var Theme = RootTheme{
Tree: TreeTheme{ Tree: TreeTheme{
Text: NewStyle(ColorWhite), Text: NewStyle(ColorWhite),
Collapse: COLLAPSED, Collapsed: COLLAPSED,
Expand: EXPANDED, Expanded: EXPANDED,
}, },
StackedBarChart: StackedBarChartTheme{ StackedBarChart: StackedBarChartTheme{

View File

@ -17,7 +17,7 @@ type TreeNode struct {
Expanded bool Expanded bool
Nodes []*TreeNode Nodes []*TreeNode
//level stores the node level in the tree. // level stores the node level in the tree.
level int level int
} }
@ -32,9 +32,9 @@ func (self *TreeNode) parseStyles(style Style) []Cell {
} else { } else {
sb.WriteString(strings.Repeat(treeIndent, self.level)) sb.WriteString(strings.Repeat(treeIndent, self.level))
if self.Expanded { if self.Expanded {
sb.WriteRune(Theme.Tree.Expand) sb.WriteRune(Theme.Tree.Expanded)
} else { } else {
sb.WriteRune(Theme.Tree.Collapse) sb.WriteRune(Theme.Tree.Collapsed)
} }
sb.WriteByte(' ') sb.WriteByte(' ')
} }
@ -45,15 +45,15 @@ func (self *TreeNode) parseStyles(style Style) []Cell {
// Tree is a tree widget. // Tree is a tree widget.
type Tree struct { type Tree struct {
Block Block
Nodes []*TreeNode
TextStyle Style TextStyle Style
SelectedRowStyle Style SelectedRowStyle Style
WrapText bool WrapText bool
SelectedRow int SelectedRow int
topRow int
//rows is flatten nodes for rendering. nodes []*TreeNode
// rows is flatten nodes for rendering.
rows []*TreeNode rows []*TreeNode
topRow int
} }
// NewTree creates a new Tree widget. // NewTree creates a new Tree widget.
@ -66,9 +66,14 @@ func NewTree() *Tree {
} }
} }
func (self *Tree) SetNodes(nodes []*TreeNode) {
self.nodes = nodes
self.prepareNodes()
}
func (self *Tree) prepareNodes() { func (self *Tree) prepareNodes() {
self.rows = self.rows[:0] self.rows = make([]*TreeNode, 0)
for _, node := range self.Nodes { for _, node := range self.nodes {
self.prepareNode(node, 0) self.prepareNode(node, 0)
} }
} }
@ -85,7 +90,7 @@ func (self *Tree) prepareNode(node *TreeNode, level int) {
} }
func (self *Tree) Walk(fn TreeWalkFn) { func (self *Tree) Walk(fn TreeWalkFn) {
for _, n := range self.Nodes { for _, n := range self.nodes {
if !self.walk(n, fn) { if !self.walk(n, fn) {
break break
} }
@ -108,8 +113,6 @@ func (self *Tree) walk(n *TreeNode, fn TreeWalkFn) bool {
func (self *Tree) Draw(buf *Buffer) { func (self *Tree) Draw(buf *Buffer) {
self.Block.Draw(buf) self.Block.Draw(buf)
self.prepareNodes()
point := self.Inner.Min point := self.Inner.Min
// adjusts view into widget // adjusts view into widget
@ -209,6 +212,7 @@ func (self *Tree) ScrollBottom() {
func (self *Tree) Collapse() { func (self *Tree) Collapse() {
self.rows[self.SelectedRow].Expanded = false self.rows[self.SelectedRow].Expanded = false
self.prepareNodes()
} }
func (self *Tree) Expand() { func (self *Tree) Expand() {
@ -216,6 +220,7 @@ func (self *Tree) Expand() {
if len(node.Nodes) > 0 { if len(node.Nodes) > 0 {
self.rows[self.SelectedRow].Expanded = true self.rows[self.SelectedRow].Expanded = true
} }
self.prepareNodes()
} }
func (self *Tree) ToggleExpand() { func (self *Tree) ToggleExpand() {
@ -223,6 +228,7 @@ func (self *Tree) ToggleExpand() {
if len(node.Nodes) > 0 { if len(node.Nodes) > 0 {
node.Expanded = !node.Expanded node.Expanded = !node.Expanded
} }
self.prepareNodes()
} }
func (self *Tree) ExpandAll() { func (self *Tree) ExpandAll() {
@ -232,6 +238,7 @@ func (self *Tree) ExpandAll() {
} }
return true return true
}) })
self.prepareNodes()
} }
func (self *Tree) CollapseAll() { func (self *Tree) CollapseAll() {
@ -239,4 +246,5 @@ func (self *Tree) CollapseAll() {
n.Expanded = false n.Expanded = false
return true return true
}) })
self.prepareNodes()
} }