tree: Add context menu support
Fixes #1643 and partially #1489
Adds .context_menu for ListItem
After making this I'm not sure if it's needed, or the docs could be updated to add context_menu on the list item child instead?
tree(&tree_state, |ix, entry, selected, window, cx| {
ListItem::new(ix)
.selected(selected)
.child(
div()
.child(entry.item().label.clone())
.context_menu(|menu, _, _| {
menu.menu("Rename", Box::new(Rename))
.menu("Delete", Box::new(Delete))
.separator()
.menu("Copy Path", Box::new(CopyPath))
})
)
})
There is a bug with this approach, you have to right click twice, because it seems like the window.on_mouse_event in ContextMenu fires before the on_mouse_down event of the table (or tree in this case). You can see it in the table_story. Probably because the uniform list is further down in the element tree than the context menu on the parent/root element?
It also means that it is showing the wrong context menu, since the right clicked index is the previous one :disappointed: Perhaps context menu needs to be something "global" like dialog/sheet? So we can force it open in the correct event listener?
@huacnlee Updated it to use the same pattern as table with a delegate. It suffers from the same issue as table though, see #1651
Should we keep tree()?
No, we can not change the tree API.
You're doing this effected this PR more complex now.
Small improve must be a small changes.
I don't know how to do this better right now, but I am sure we can't to change the Tree API. I will consider this when have a rest time.
No, we can not change the
treeAPI.You're doing this effected this PR more complex now.
Small improve must be a small changes.
I don't know how to do this better right now, but I am sure we can't to change the
TreeAPI. I will consider this when have a rest time.
OK! I will close this and open a PR with the docs fix. We could do tree(...).context_menu({}) without breaking the API, but that would require storing the context menu handler in the TreeState.