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

TreeView.SetChangedFunc passes previous instead of current node #564

Closed
rkoster opened this issue Feb 10, 2021 · 6 comments
Closed

TreeView.SetChangedFunc passes previous instead of current node #564

rkoster opened this issue Feb 10, 2021 · 6 comments

Comments

@rkoster
Copy link

rkoster commented Feb 10, 2021

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: #489 since reverting to a804878 (commit before said PR was merged) "fixes" the issue.

@rivo
Copy link
Owner

rivo commented Feb 17, 2021

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

@rkoster
Copy link
Author

rkoster commented 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

@rkoster
Copy link
Author

rkoster commented Mar 22, 2021

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

@rivo
Copy link
Owner

rivo commented 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?

@rkoster
Copy link
Author

rkoster commented 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

@rkoster rkoster closed this as completed Apr 27, 2021
@rivo
Copy link
Owner

rivo commented Apr 27, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants