Skip to content

Commit

Permalink
fix inconsisitent data view caused by renaming file entry
Browse files Browse the repository at this point in the history
  • Loading branch information
oligo committed Dec 1, 2024
1 parent 4c2c32b commit 9f8b7e3
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
4 changes: 2 additions & 2 deletions example/basic/navi.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ func (item simpleNavItem) ContextMenuOptions(gtx C) ([][]menu.MenuOption, bool)
return nil, false
}

func (item simpleNavItem) Children() []navi.NavItem {
return nil
func (item simpleNavItem) Children() ([]navi.NavItem, bool) {
return nil, false
}

func (ss simpleItemSection) Title() string {
Expand Down
4 changes: 4 additions & 0 deletions explorer/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ func (n *EntryNode) UpdateName(newName string) error {
n.Path = filepath.Clean(newPath)
st, _ := os.Stat(n.Path)
n.FileInfo = st

if len(n.children) > 0 {
n.Refresh(hiddenFileFilter)
}
}()

return os.Rename(n.Path, newPath)
Expand Down
24 changes: 16 additions & 8 deletions explorer/tree_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type EntryNavItem struct {
menuOptionFunc MenuOptionFunc
onSelectFunc OnSelectFunc

parent navi.NavItem
parent *EntryNavItem
children []navi.NavItem
label *gv.Editable
expaned bool
Expand Down Expand Up @@ -135,6 +135,8 @@ func (eitem *EntryNavItem) layout(gtx layout.Context, th *theme.Theme, textColor
err := eitem.state.UpdateName(text)
if err != nil {
log.Println("err: ", err)
} else {
eitem.needSync = true
}
})
}
Expand Down Expand Up @@ -177,21 +179,23 @@ func (eitem *EntryNavItem) ContextMenuOptions(gtx C) ([][]menu.MenuOption, bool)
return nil, false
}

func (eitem *EntryNavItem) Children() []navi.NavItem {
func (eitem *EntryNavItem) Children() ([]navi.NavItem, bool) {
if eitem.state.Kind() == FileNode {
return nil
return nil, false
}

if !eitem.expaned {
return nil
return nil, false
}

changed := false
if eitem.children == nil || eitem.needSync {
eitem.buildChildren(true)
eitem.needSync = false
changed = true
}

return eitem.children
return eitem.children, changed
}

func (eitem *EntryNavItem) buildChildren(sync bool) {
Expand Down Expand Up @@ -280,7 +284,7 @@ func (eitem *EntryNavItem) Remove() error {
return err
}

(eitem.parent).(*EntryNavItem).needSync = true
eitem.parent.needSync = true
return nil
}

Expand All @@ -289,6 +293,10 @@ func (eitem *EntryNavItem) Name() string {
return eitem.state.Name()
}

func (eitem *EntryNavItem) Parent() *EntryNavItem {
return eitem.parent
}

// File or folder path of this node
func (eitem *EntryNavItem) Path() string {
return eitem.state.Path
Expand All @@ -304,7 +312,7 @@ func (eitem *EntryNavItem) OnPaste(data string, removeOld bool, src *EntryNavIte
// when paste destination is a normal file node, use its parent dir to ease the CUT/COPY operations.
dest := eitem
if !eitem.IsDir() && eitem.parent != nil {
dest = eitem.parent.(*EntryNavItem)
dest = eitem.parent
}

pathes := strings.Split(string(data), "\n")
Expand All @@ -316,7 +324,7 @@ func (eitem *EntryNavItem) OnPaste(data string, removeOld bool, src *EntryNavIte
}

if src != nil && src.parent != nil {
parent := src.parent.(*EntryNavItem)
parent := src.parent
parent.children = slices.DeleteFunc(parent.children, func(chd navi.NavItem) bool {
entry := chd.(*EntryNavItem)
return entry.Path() == p
Expand Down
15 changes: 8 additions & 7 deletions navi/navitem.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ type NavItem interface {
// The returned boolean value suggest the position of the popup menu should be at
// fixed position or not. NavTree should place a clickable icon to guide user interactions.
ContextMenuOptions(gtx layout.Context) ([][]menu.MenuOption, bool)
Children() []NavItem
// Children returns children of the item, and a boolean value indicating wether children have changed.
Children() ([]NavItem, bool)
}

type NavTree struct {
Expand Down Expand Up @@ -134,18 +135,18 @@ func (n *NavTree) Layout(gtx C, th *theme.Theme) D {

n.Update(gtx)

itemChildren := n.item.Children()
if len(n.item.Children()) <= 0 {
return n.layoutRoot(gtx, th)
}

if len(n.children) != len(itemChildren) {
itemChildren, changed := n.item.Children()
if changed || len(n.children) != len(itemChildren) {
n.children = n.children[:0]
for _, child := range itemChildren {
n.children = append(n.children, NewNavItem(child, n.OnClicked))
}
}

if len(n.children) <= 0 {
return n.layoutRoot(gtx, th)
}

n.childList.Axis = layout.Vertical

return layout.Flex{
Expand Down

0 comments on commit 9f8b7e3

Please sign in to comment.