Skip to content

Commit

Permalink
added SetItemHeight to the Tree widget, copying from the List widget
Browse files Browse the repository at this point in the history
  • Loading branch information
kothawoc committed Sep 17, 2024
1 parent c2cc495 commit fa99e5a
Showing 1 changed file with 29 additions and 0 deletions.
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

0 comments on commit fa99e5a

Please sign in to comment.