Skip to content

Commit

Permalink
Merge pull request #4016 from andydotxyz/fix/treeslow
Browse files Browse the repository at this point in the history
  • Loading branch information
andydotxyz authored Jul 3, 2023
2 parents bd682d1 + bdc6920 commit 1d044fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
13 changes: 10 additions & 3 deletions cmd/fyne_demo/tutorials/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func makeTableTab(_ fyne.Window) fyne.CanvasObject {
func makeTreeTab(_ fyne.Window) fyne.CanvasObject {
data := map[string][]string{
"": {"A"},
"A": {"B", "D", "H", "J", "L", "O", "P", "S", "V"},
"A": {"B", "D", "H", "J", "L", "O", "P", "S", "V", "Z"},
"B": {"C"},
"C": {"abc"},
"D": {"E"},
Expand All @@ -103,10 +103,17 @@ func makeTreeTab(_ fyne.Window) fyne.CanvasObject {
"V": {"W"},
"W": {"X"},
"X": {"Y"},
"Y": {"Z"},
"Z": {"avwxyz"},
"Y": {"zzz"},
"Z": {},
}

childlen := 10000
z := make([]string, childlen)
for i := 0; i < childlen; i++ {
z[i] = strconv.Itoa(i)
}
data["Z"] = z

tree := widget.NewTreeWithStrings(data)
tree.OnSelected = func(id string) {
fmt.Println("Tree node selected:", id)
Expand Down
36 changes: 20 additions & 16 deletions widget/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,13 +479,14 @@ func (r *treeContentRenderer) Layout(size fyne.Size) {
branches := make(map[string]*branch)
leaves := make(map[string]*leaf)

pad := theme.Padding()
offsetY := r.treeContent.tree.offset.Y
viewport := r.treeContent.viewport
width := fyne.Max(size.Width, viewport.Width)
separatorCount := 0
separatorThickness := theme.SeparatorThicknessSize()
separatorSize := fyne.NewSize(width, separatorThickness)
separatorOff := (theme.Padding() - separatorThickness) / 2
separatorOff := (pad + separatorThickness) / 2
y := float32(0)
// walkAll open branches and obtain nodes to render in scroller's viewport
r.treeContent.tree.walkAll(func(uid string, isBranch bool, depth int) {
Expand All @@ -499,19 +500,9 @@ func (r *treeContentRenderer) Layout(size fyne.Size) {
}

// If this is not the first item, add a separator
if y > 0 {
var separator fyne.CanvasObject
if separatorCount < len(r.separators) {
separator = r.separators[separatorCount]
} else {
separator = NewSeparator()
r.separators = append(r.separators, separator)
}
separator.Move(fyne.NewPos(0, y+separatorOff))
separator.Resize(separatorSize)
separator.Show()
r.objects = append(r.objects, separator)
y += theme.Padding()
addSeparator := y > 0
if addSeparator {
y += pad
separatorCount++
}

Expand All @@ -525,6 +516,21 @@ func (r *treeContentRenderer) Layout(size fyne.Size) {
// Node is below viewport and not visible
} else {
// Node is in viewport

if addSeparator {
var separator fyne.CanvasObject
if separatorCount < len(r.separators) {
separator = r.separators[separatorCount]
} else {
separator = NewSeparator()
r.separators = append(r.separators, separator)
}
separator.Move(fyne.NewPos(0, y-separatorOff))
separator.Resize(separatorSize)
r.objects = append(r.objects, separator)
separatorCount++
}

var n fyne.CanvasObject
if isBranch {
b, ok := r.branches[uid]
Expand Down Expand Up @@ -567,13 +573,11 @@ func (r *treeContentRenderer) Layout(size fyne.Size) {
// Release any nodes that haven't been reused
for uid, b := range r.branches {
if _, ok := branches[uid]; !ok {
b.Hide()
r.branchPool.Release(b)
}
}
for uid, l := range r.leaves {
if _, ok := leaves[uid]; !ok {
l.Hide()
r.leafPool.Release(l)
}
}
Expand Down

0 comments on commit 1d044fe

Please sign in to comment.