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

How to make a TreeView node expandable without it actually having any children? #29

Closed
sungit opened this issue Oct 30, 2012 · 8 comments

Comments

@sungit
Copy link

sungit commented Oct 30, 2012

In the old treeview implementation, i found it's easy to make a expandable treeview item,

But now how can i do that?

@lxn
Copy link
Owner

lxn commented Oct 30, 2012

The new TreeView checks your implementation of the TreeItem.ChildCount method to determine if the item is expandable. Depending on how you populate your model, it may be necessary to notify the TreeView about new items by calling TreeModelBase.PublishItemsReset (if you embed TreeModelBase in your model), or publish the event yourself and pass the parent item (or nil if roots are to be reset) as parameter.

TreeModel has a LazyPopulation method, that controls how a TreeView populates its internal items. So far I have only tested the lazy case. Maybe the eager case (default with TreeModelBase) isn't working yet.

Anyway, see the filebrowser example for how it can be done.

@lxn
Copy link
Owner

lxn commented Oct 30, 2012

Whoops, just noticed you asked how it can be done without having children. I guess that isn't supported with the new TreeView. If your motivation for doing this is lazy initialization, my first reply should help.

@sungit
Copy link
Author

sungit commented Oct 31, 2012

Well, I was using the dummy child to handle this problem.

Your comment are very useful, thank you!

@sungit
Copy link
Author

sungit commented Oct 31, 2012

TreeModelBase.PublishItemsReset failed with none nil parent when i want to dynamically populate the treeitem child contents.

After item.children appended new contents, I've tried TreeModelBase.PublishItemsReset(item) and TreeModelBase.PublishItemsReset(item.Parent()) in the handler attached to treeview.ItemExpanded(), but both failed (crash my program with the output below).

I found only TreeModelBase.PublishItemsReset(nil) works as i've tried.

    C:/Go/src/pkg/runtime/proc.c:1443
    C:/Go/src/pkg/runtime/runtime.c:128
    C:/Go/src/pkg/runtime/thread_windows.c:276
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/treeview.go:388
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/container.go:258
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/widget.go:1369
    C:/Go/src/pkg/runtime/cgocall.c:210
    C:/Go/src/pkg/runtime/asm_386.s:473
    C:/Go/src/pkg/runtime/asm_386.s:377
    C:/Go/src/pkg/runtime/cgocall.c:135
    C:/Go/src/pkg/runtime/zsyscall_windows_386.c:97
    e:/repos/libs/gopkgs/src/github.com/lxn/go-winapi/user32.go:1487
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/widget.go:1467
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/treeview.go:432
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/widget.go:1369
    C:/Go/src/pkg/runtime/cgocall.c:210
    C:/Go/src/pkg/runtime/asm_386.s:473
    C:/Go/src/pkg/runtime/asm_386.s:377
    C:/Go/src/pkg/runtime/cgocall.c:135
    C:/Go/src/pkg/runtime/zsyscall_windows_386.c:97
    e:/repos/libs/gopkgs/src/github.com/lxn/go-winapi/user32.go:1727
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/toplevelwindow.go:119
    E:/repos/libs/gopkgs/src/github.com/lxn/walk/dialog.go:219

@lxn
Copy link
Owner

lxn commented Oct 31, 2012

Ok, so you just want lazy initialization.

You should do the same as the filebrowser example:

  1. Return true from your implementation of TreeModel.LazyPopulation.
  2. In your implementation(s) of TreeItem.ChildCount check if the children of that item have been initialized.
  3. If they haven't been initialized, initialize them inside ChildCount and return the number of children.

The TreeView then should take care of everything else and only ever ask for one level of items if you expand an item.

Of course the crash problem should be fixed anyway.

Thanks for your feedback!

@sungit
Copy link
Author

sungit commented Oct 31, 2012

Yes, I've tried lazy initialization following the filebrowser example, Just as Step 2 and 3 required.

But it's obviously an synchronous process when creating of the child items and returning the item count in the TreeItem.ChildCount function.

My situation demanding an asynchronous population of the child items, which regards the item expanding click as a trigger to fetch all the content from a remote server. But my communication with the server is asynchronous so i
can only be sure the child items would be fetched, but surely after the TreeItem.ChildCount's being called.

Well, i have't found a solution yet. Any suggestion?

@lxn
Copy link
Owner

lxn commented Nov 1, 2012

Please try again with the latest changes.

@sungit
Copy link
Author

sungit commented Nov 2, 2012

Well, thank you!

Your works are really appreciated!

@sungit sungit closed this as completed Nov 2, 2012
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