core icon indicating copy to clipboard operation
core copied to clipboard

filetree.OpenPath retains info

Open mattkasun opened this issue 7 months ago • 1 comments

Describe the bug

if filetree.OpenPath is called a second time and the new directory has dirs/files that existed in the original directory, those nodes will not be updated and still point to dirs/files in old dir

How to reproduce

run example code below where current directory has file with same name as a file in parent directory. Select open parent button. Select file that exists in both directories. File selected will be from sub-directory not parent directory

Example code

package main

import (
	"log"
	"os"
	"path/filepath"

	"cogentcore.org/core/core"
	"cogentcore.org/core/events"
	"cogentcore.org/core/filetree"
)

func main() {
	var selected []string
	log.SetFlags(log.Lshortfile)
	current, _ := os.Getwd()
	b := core.NewBody("Cogent Files")
	core.NewButton(b).SetText("Hello")

	fp := filetree.NewTree(b).OpenPath(current).SetDirsOnTop(true)
	fp.OnSelect(func(e events.Event) {
		fp.SelectedFunc(func(n *filetree.Node) {
			selected = []string{(string(n.Filepath))}
			log.Println(selected)
		})
	})
	b.AddBottomBar(func(bar *core.Frame) {
		b.AddCancel(bar)
		core.NewButton(bar).SetText("open parent").OnClick(func(e events.Event) {
			current = filepath.Dir(current)
			fp.OpenPath(current)
			b.Update()
			log.Println(current)
		})
		b.AddOK(bar).OnClick(func(event events.Event) {
			log.Println(selected)
		})
	})
	b.RunMainWindow()
}

Relevant output


Platform

Linux

mattkasun avatar May 11 '25 23:05 mattkasun

Thank you for reporting this. I can reproduce this and we will work on fixing it when we have the time.

In the meantime, as a workaround, you can add this to the start of your open parent button OnClick event handler:

fp.DeleteChildren()

kkoreilly avatar May 12 '25 06:05 kkoreilly