Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added SetItemHeight to the Tree widget, copying from the List widget #5149

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions widget/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Tree struct {
branchMinSize fyne.Size
currentFocus TreeNodeID
focused bool
itemHeights map[TreeNodeID]float32
leafMinSize fyne.Size
offset fyne.Position
open map[TreeNodeID]bool
Expand Down Expand Up @@ -263,6 +264,22 @@ func (t *Tree) ScrollToBottom() {
t.Refresh()
}

// SetItemHeight supports changing the height of the specified list item. Items normally take the height of the template
// returned from the CreateItem callback. The height parameter uses the same units as a fyne.Size type and refers
// to the internal content height not including the divider size.
//
// Since: 2.x
func (t *Tree) SetItemHeight(id TreeNodeID, height float32) {
t.propertyLock.Lock()

if t.itemHeights == nil {
t.itemHeights = make(map[TreeNodeID]float32)
}

t.itemHeights[id] = height
t.propertyLock.Unlock()
}

// ScrollTo scrolls to the node with the given id.
//
// Since 2.1
Expand Down Expand Up @@ -447,6 +464,9 @@ func (t *Tree) findBottom() (y float32, size fyne.Size) {
if branch {
size = t.branchMinSize
}
if n, ok := t.itemHeights[id]; ok {
size.Height = n
}

// Root node is not rendered unless it has been customized
if t.Root == "" && id == "" {
Expand Down Expand Up @@ -475,6 +495,9 @@ func (t *Tree) offsetAndSize(uid TreeNodeID) (y float32, size fyne.Size, found b
if branch {
m = t.branchMinSize
}
if n, ok := t.itemHeights[id]; ok {
m.Height = n
}
if id == uid {
found = true
size = m
Expand Down Expand Up @@ -659,6 +682,9 @@ func (r *treeContentRenderer) Layout(size fyne.Size) {
if isBranch {
m = r.treeContent.tree.branchMinSize
}
if n, ok := r.treeContent.tree.itemHeights[uid]; ok {
m.Height = n
}
if y+m.Height < offsetY {
// Node is above viewport and not visible
} else if y > offsetY+viewport.Height {
Expand Down Expand Up @@ -766,6 +792,9 @@ func (r *treeContentRenderer) MinSize() (min fyne.Size) {
if isBranch {
m = r.treeContent.tree.branchMinSize
}
if n, ok := r.treeContent.tree.itemHeights[uid]; ok {
m.Height = n
}
m.Width += float32(depth) * (iconSize + pad)
min.Width = fyne.Max(min.Width, m.Width)
min.Height += m.Height
Expand Down