[GH-ISSUE #564] TreeView.SetChangedFunc passes previous instead of current node #412

Closed
opened 2026-03-04 01:04:46 +03:00 by kerem · 6 comments
Owner

Originally created by @rkoster on GitHub (Feb 10, 2021).
Original GitHub issue: https://github.com/rivo/tview/issues/564

TreeView.SetChangedFunc passes previous instead of current node. In addition running TreeView.GetCurrentNode inside the call back also return the previous node.

I'm suspecting this issue was introduce in: https://github.com/rivo/tview/issues/489 since reverting to a8048787f0 (commit before said PR was merged) "fixes" the issue.

Originally created by @rkoster on GitHub (Feb 10, 2021). Original GitHub issue: https://github.com/rivo/tview/issues/564 [TreeView.SetChangedFunc](https://pkg.go.dev/github.com/rivo/tview#TreeView.SetChangedFunc ) passes previous instead of current node. In addition running [TreeView.GetCurrentNode](https://pkg.go.dev/github.com/rivo/tview#TreeView.GetCurrentNode) inside the call back also return the previous node. I'm suspecting this issue was introduce in: https://github.com/rivo/tview/issues/489 since reverting to a8048787f0c8 (commit before said PR was merged) "fixes" the issue.
kerem closed this issue 2026-03-04 01:04:46 +03:00
Author
Owner

@rivo commented on GitHub (Feb 17, 2021):

Could you please post some code that illustrates this? I'm having trouble reproducing this.

<!-- gh-comment-id:780563032 --> @rivo commented on GitHub (Feb 17, 2021): Could you please post some code that illustrates this? I'm having trouble reproducing this.
Author
Owner

@rkoster commented on GitHub (Feb 17, 2021):

// Demo code for the TreeView primitive.
package main

import (
	"io/ioutil"
	"path/filepath"

	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

// Show a navigable tree view of the current directory.
func main() {
	rootDir := "."
	root := tview.NewTreeNode(rootDir).
		SetColor(tcell.ColorRed)
	tree := tview.NewTreeView().
		SetRoot(root).
		SetCurrentNode(root)

	details := tview.NewBox().SetBorder(true)

	// A helper function which adds the files and directories of the given path
	// to the given target node.
	add := func(target *tview.TreeNode, path string) {
		files, err := ioutil.ReadDir(path)
		if err != nil {
			panic(err)
		}
		for _, file := range files {
			node := tview.NewTreeNode(file.Name()).
				SetReference(filepath.Join(path, file.Name())).
				SetSelectable(file.IsDir())
			if file.IsDir() {
				node.SetColor(tcell.ColorGreen)
			}
			target.AddChild(node)
		}
	}

	// Add the current directory to the root node.
	add(root, rootDir)

	// If a directory was selected, open it.
	tree.SetSelectedFunc(func(node *tview.TreeNode) {
		reference := node.GetReference()
		if reference == nil {
			return // Selecting the root node does nothing.
		}
		children := node.GetChildren()
		if len(children) == 0 {
			// Load and show files in this directory.
			path := reference.(string)
			add(node, path)
		} else {
			// Collapse if visible, expand if collapsed.
			node.SetExpanded(!node.IsExpanded())
		}
	})

	tree.SetChangedFunc(func(node *tview.TreeNode) {
		details.SetTitle(node.GetReference().(string))
	})

	if err := tview.NewApplication().SetRoot(
		tview.NewFlex().
			AddItem(tree, 0, 1, true).
			AddItem(details, 0, 1, true), true).
		SetFocus(tree).
		EnableMouse(true).Run(); err != nil {
		panic(err)
	}
}

image

<!-- gh-comment-id:780586548 --> @rkoster commented on GitHub (Feb 17, 2021): ``` // Demo code for the TreeView primitive. package main import ( "io/ioutil" "path/filepath" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) // Show a navigable tree view of the current directory. func main() { rootDir := "." root := tview.NewTreeNode(rootDir). SetColor(tcell.ColorRed) tree := tview.NewTreeView(). SetRoot(root). SetCurrentNode(root) details := tview.NewBox().SetBorder(true) // A helper function which adds the files and directories of the given path // to the given target node. add := func(target *tview.TreeNode, path string) { files, err := ioutil.ReadDir(path) if err != nil { panic(err) } for _, file := range files { node := tview.NewTreeNode(file.Name()). SetReference(filepath.Join(path, file.Name())). SetSelectable(file.IsDir()) if file.IsDir() { node.SetColor(tcell.ColorGreen) } target.AddChild(node) } } // Add the current directory to the root node. add(root, rootDir) // If a directory was selected, open it. tree.SetSelectedFunc(func(node *tview.TreeNode) { reference := node.GetReference() if reference == nil { return // Selecting the root node does nothing. } children := node.GetChildren() if len(children) == 0 { // Load and show files in this directory. path := reference.(string) add(node, path) } else { // Collapse if visible, expand if collapsed. node.SetExpanded(!node.IsExpanded()) } }) tree.SetChangedFunc(func(node *tview.TreeNode) { details.SetTitle(node.GetReference().(string)) }) if err := tview.NewApplication().SetRoot( tview.NewFlex(). AddItem(tree, 0, 1, true). AddItem(details, 0, 1, true), true). SetFocus(tree). EnableMouse(true).Run(); err != nil { panic(err) } } ``` ![image](https://user-images.githubusercontent.com/380697/108216913-46cc5880-7133-11eb-8a1b-d9e7fb586950.png)
Author
Owner

@rkoster commented on GitHub (Mar 22, 2021):

@rivo where you able to reproduce the issue with the example above?

<!-- gh-comment-id:803887318 --> @rkoster commented on GitHub (Mar 22, 2021): @rivo where you able to reproduce the issue with the example above?
Author
Owner

@rivo commented on GitHub (Apr 27, 2021):

Unfortunately not. This is what it looks like for me with your code:

image

Are you on the latest version of tview? What OS and terminal are you on?

<!-- gh-comment-id:827570071 --> @rivo commented on GitHub (Apr 27, 2021): Unfortunately not. This is what it looks like for me with your code: ![image](https://user-images.githubusercontent.com/480930/116241483-3a80ff00-a765-11eb-95d5-fe86b39bb8f3.png) Are you on the latest version of `tview`? What OS and terminal are you on?
Author
Owner

@rkoster commented on GitHub (Apr 27, 2021):

@rivo have just confirmed that using the latest version of tview seems to work.
I looked back at my commits and found the I could reproduce the issue with the code posted above in combination with:

go get github.com/rivo/tview@dbc1f32bb1d02db2f8c497700b7db237362d4ff8
<!-- gh-comment-id:827579747 --> @rkoster commented on GitHub (Apr 27, 2021): @rivo have just confirmed that using the latest version of tview seems to work. I looked back at my commits and found the I could reproduce the issue with the code posted above in combination with: ``` go get github.com/rivo/tview@dbc1f32bb1d02db2f8c497700b7db237362d4ff8 ```
Author
Owner

@rivo commented on GitHub (Apr 27, 2021):

Ok, well, if it works now, that's good. Thanks for getting back to me.

<!-- gh-comment-id:827599069 --> @rivo commented on GitHub (Apr 27, 2021): Ok, well, if it works now, that's good. Thanks for getting back to me.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#412
No description provided.